3 * Interaction thread between director and the GUI
5 * Nicolas Boichat, April 2004
10 Copyright (C) 2004 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 as published by the Free Software Foundation; either version 2
15 of the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 // http://66.102.9.104/search?q=cache:Djc1mPF3hRoJ:cvs.sourceforge.net/viewcvs.py/audacity/audacity-src/src/AudioIO.cpp%3Frev%3D1.102+macos+x+wxthread&hl=fr
29 #include "console_thread.h" // class's header file
31 #include <wx/wxprec.h>
33 #include <wx/thread.h>
38 #include "console_conf.h"
44 DWORD g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
45 char OK_msg[] = "2000 OK\n";
46 char TERM_msg[] = "2999 Terminate\n";
49 /* Imported functions */
50 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons);
52 bool console_thread::inited = false;
53 bool console_thread::configloaded = false;
55 void console_thread::InitLib() {
56 if (WSA_Init() != 0) {
57 csprint("Error while initializing windows sockets...\n");
63 my_name_is(0, NULL, "wx-console");
64 //textdomain("bacula-console");
69 void console_thread::FreeLib() {
71 if (WSACleanup() != 0) {
72 csprint("Error while cleaning up windows sockets...\n");
77 wxString console_thread::LoadConfig(wxString configfile) {
81 return "Error while initializing library.";
84 free_config_resources();
86 MSGS* msgs = (MSGS *)malloc(sizeof(MSGS));
87 memset(msgs, 0, sizeof(MSGS));
88 for (int i=1; i<=M_MAX; i++) {
90 add_msg_dest(msgs, MD_STDOUT, i, NULL, NULL);
92 add_msg_dest(msgs, MD_SYSLOG, i, NULL, NULL);
93 add_msg_dest(msgs, MD_CONSOLE, i, NULL, NULL);
97 init_console_msg(".");
99 /* TODO (#4#): Allow the user to choose his config file. */
100 if (!parse_config(configfile.c_str(), 0)) {
101 configloaded = false;
102 wxFile file("./wx-console.conmsg");
103 if (!file.IsOpened())
104 return "Unable to retrieve error message.";
108 while ((len = file.Read(buffer, 512)) > -1) {
109 buffer[len] = (wxChar)0;
116 wxRemoveFile("./wx-console.conmsg");
121 wxRemoveFile("./wx-console.conmsg");
122 init_msg(NULL, NULL);
130 console_thread::console_thread() {
132 choosingdirector = false;
136 console_thread::~console_thread() {
138 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
147 void* console_thread::Entry() {
149 csprint("Error : Library not initialized\n");
150 csprint(NULL, CS_END);
151 csprint(NULL, CS_DISCONNECTED);
152 csprint(NULL, CS_TERMINATED);
160 csprint("Error : No configuration file loaded\n");
161 csprint(NULL, CS_END);
162 csprint(NULL, CS_DISCONNECTED);
163 csprint(NULL, CS_TERMINATED);
170 csprint("Connecting...\n");
173 DIRRES* res[16]; /* Maximum 16 directors */
177 foreach_res(dir, R_DIRECTOR) {
187 csprint("Error : No director defined in config file.\n");
188 csprint(NULL, CS_END);
189 csprint(NULL, CS_DISCONNECTED);
190 csprint(NULL, CS_TERMINATED);
196 else if (count == 1) {
201 csprint("Multiple directors found in your config file.\n");
202 for (int i = 0; i < count; i++) {
204 csprint(wxString(" ") << (i+1) << ": " << res[i]->hdr.name << "\n");
207 csprint(wxString(" ") << (i+1) << ": " << res[i]->hdr.name << "\n");
210 csprint(wxString("Please choose a director (1-") << count << ") : ");
211 csprint(NULL, CS_PROMPT);
212 choosingdirector = true;
213 directorchoosen = -1;
214 while(directorchoosen == -1) {
215 bmicrosleep(0, 2000);
218 choosingdirector = false;
219 if (directorchoosen != 0) {
225 memset(&jcr, 0, sizeof(jcr));
227 jcr.dequeuing = 1; /* TODO: catch messages */
229 UA_sock = bnet_connect(&jcr, 3, 3, "Director daemon",
230 res[directorchoosen-1]->address, NULL, res[directorchoosen-1]->DIRport, 0);
232 if (UA_sock == NULL) {
233 csprint("Failed to connect to the director\n");
234 csprint(NULL, CS_END);
235 csprint(NULL, CS_DISCONNECTED);
236 csprint(NULL, CS_TERMINATED);
243 csprint("Connected\n");
245 jcr.dir_bsock = UA_sock;
247 /* If cons==NULL, default console will be used */
248 CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
250 if (!authenticate_director(&jcr, res[directorchoosen-1], cons)) {
252 csprint(UA_sock->msg);
253 csprint(NULL, CS_END);
254 csprint(NULL, CS_DISCONNECTED);
255 csprint(NULL, CS_TERMINATED);
262 csprint(NULL, CS_CONNECTED);
269 while(!TestDestroy()) { /* Tests if thread has been ended */
270 if ((stat = bnet_recv(UA_sock)) >= 0) {
271 csprint(UA_sock->msg);
273 else if (stat == BNET_SIGNAL) {
274 if (UA_sock->msglen == BNET_PROMPT) {
275 csprint(NULL, CS_PROMPT);
277 else if (UA_sock->msglen == BNET_EOD) {
278 csprint(NULL, CS_END);
280 else if (UA_sock->msglen == BNET_HEARTBEAT) {
281 bnet_sig(UA_sock, BNET_HB_RESPONSE);
282 csprint("<< Heartbeat signal received, answered. >>\n", CS_DEBUG);
285 csprint("<< Unexpected signal received : ", CS_DEBUG);
286 csprint(bnet_sig_to_ascii(UA_sock), CS_DEBUG);
287 csprint(">>\n", CS_DEBUG);
290 else { /* BNET_HARDEOF || BNET_ERROR */
291 csprint(NULL, CS_END);
295 if (is_bnet_stop(UA_sock)) {
296 csprint(NULL, CS_END);
297 break; /* error or term */
301 csprint(NULL, CS_DISCONNECTED);
303 csprint("Connection terminated\n");
307 csprint(NULL, CS_TERMINATED);
316 void console_thread::Write(const char* str) {
318 UA_sock->msglen = strlen(str);
319 pm_strcpy(&UA_sock->msg, str);
322 else if (choosingdirector) {
323 wxString number = str;
324 number.RemoveLast(); /* Removes \n */
326 if (number.ToLong(&val)) {
327 directorchoosen = (int)val;
335 void console_thread::Delete() {
338 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
341 /*csprint(NULL, CS_END);
342 csprint(NULL, CS_DISCONNECTED);
343 csprint(NULL, CS_TERMINATED);*/