3 * Interaction thread between director and the GUI
5 * Nicolas Boichat, April 2004
9 Copyright (C) 2004 Kern Sibbald and John Walker
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 // 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
28 #include "console_thread.h" // class's header file
30 #include <wx/wxprec.h>
32 #include <wx/thread.h>
37 #include "console_conf.h"
43 DWORD g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
44 char OK_msg[] = "2000 OK\n";
45 char TERM_msg[] = "2999 Terminate\n";
48 /* Imported functions */
49 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons);
51 bool console_thread::inited = false;
52 bool console_thread::configloaded = false;
54 void console_thread::InitLib() {
55 if (WSA_Init() != 0) {
56 csprint("Error while initializing windows sockets...\n");
62 my_name_is(0, NULL, "wx-console");
63 //textdomain("bacula-console");
68 void console_thread::FreeLib() {
70 if (WSACleanup() != 0) {
71 csprint("Error while cleaning up windows sockets...\n");
76 wxString console_thread::LoadConfig(wxString configfile) {
80 return "Error while initializing library.";
83 free_config_resources();
85 MSGS* msgs = (MSGS *)malloc(sizeof(MSGS));
86 memset(msgs, 0, sizeof(MSGS));
87 for (int i=1; i<=M_MAX; i++) {
89 add_msg_dest(msgs, MD_STDOUT, i, NULL, NULL);
91 add_msg_dest(msgs, MD_SYSLOG, i, NULL, NULL);
92 add_msg_dest(msgs, MD_CONSOLE, i, NULL, NULL);
96 init_console_msg(".");
98 /* TODO (#4#): Allow the user to choose his config file. */
99 if (!parse_config(configfile.c_str(), 0)) {
100 configloaded = false;
101 wxFile file("./wx-console.conmsg");
102 if (!file.IsOpened())
103 return "Unable to retrieve error message.";
107 while ((len = file.Read(buffer, 512)) > -1) {
108 buffer[len] = (wxChar)0;
115 wxRemoveFile("./wx-console.conmsg");
120 wxRemoveFile("./wx-console.conmsg");
121 init_msg(NULL, NULL);
129 console_thread::console_thread() {
131 choosingdirector = false;
135 console_thread::~console_thread() {
137 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
146 void* console_thread::Entry() {
148 csprint("Error : Library not initialized\n");
149 csprint(NULL, CS_END);
150 csprint(NULL, CS_DISCONNECTED);
151 csprint(NULL, CS_TERMINATED);
159 csprint("Error : No configuration file loaded\n");
160 csprint(NULL, CS_END);
161 csprint(NULL, CS_DISCONNECTED);
162 csprint(NULL, CS_TERMINATED);
169 csprint("Connecting...\n");
172 DIRRES* res[16]; /* Maximum 16 directors */
176 foreach_res(dir, R_DIRECTOR) {
186 csprint("Error : No director defined in config file.\n");
187 csprint(NULL, CS_END);
188 csprint(NULL, CS_DISCONNECTED);
189 csprint(NULL, CS_TERMINATED);
195 else if (count == 1) {
200 csprint("Multiple directors found in your config file.\n");
201 for (int i = 0; i < count; i++) {
203 csprint(wxString(" ") << (i+1) << ": " << res[i]->hdr.name << "\n");
206 csprint(wxString(" ") << (i+1) << ": " << res[i]->hdr.name << "\n");
209 csprint(wxString("Please choose a director (1-") << count << ") : ");
210 csprint(NULL, CS_PROMPT);
211 choosingdirector = true;
212 directorchoosen = -1;
213 while(directorchoosen == -1) {
214 bmicrosleep(0, 2000);
217 choosingdirector = false;
218 if (directorchoosen != 0) {
224 memset(&jcr, 0, sizeof(jcr));
226 jcr.dequeuing = 1; /* TODO: catch messages */
228 UA_sock = bnet_connect(&jcr, 3, 3, "Director daemon",
229 res[directorchoosen-1]->address, NULL, res[directorchoosen-1]->DIRport, 0);
231 if (UA_sock == NULL) {
232 csprint("Failed to connect to the director\n");
233 csprint(NULL, CS_END);
234 csprint(NULL, CS_DISCONNECTED);
235 csprint(NULL, CS_TERMINATED);
242 csprint("Connected\n");
244 jcr.dir_bsock = UA_sock;
246 /* If cons==NULL, default console will be used */
247 CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
249 if (!authenticate_director(&jcr, res[directorchoosen-1], cons)) {
251 csprint(UA_sock->msg);
252 csprint(NULL, CS_END);
253 csprint(NULL, CS_DISCONNECTED);
254 csprint(NULL, CS_TERMINATED);
261 csprint(NULL, CS_CONNECTED);
268 while(!TestDestroy()) { /* Tests if thread has been ended */
269 if ((stat = bnet_recv(UA_sock)) >= 0) {
270 csprint(UA_sock->msg);
272 else if (stat == BNET_SIGNAL) {
273 if (UA_sock->msglen == BNET_PROMPT) {
274 csprint(NULL, CS_PROMPT);
276 else if (UA_sock->msglen == BNET_EOD) {
277 csprint(NULL, CS_END);
279 else if (UA_sock->msglen == BNET_HEARTBEAT) {
280 bnet_sig(UA_sock, BNET_HB_RESPONSE);
281 csprint("<< Heartbeat signal received, answered. >>\n", CS_DEBUG);
284 csprint("<< Unexpected signal received : ", CS_DEBUG);
285 csprint(bnet_sig_to_ascii(UA_sock), CS_DEBUG);
286 csprint(">>\n", CS_DEBUG);
289 else { /* BNET_HARDEOF || BNET_ERROR */
290 csprint(NULL, CS_END);
294 if (is_bnet_stop(UA_sock)) {
295 csprint(NULL, CS_END);
296 break; /* error or term */
300 csprint(NULL, CS_DISCONNECTED);
302 csprint("Connection terminated\n");
306 csprint(NULL, CS_TERMINATED);
315 void console_thread::Write(const char* str) {
317 UA_sock->msglen = strlen(str);
318 pm_strcpy(&UA_sock->msg, str);
321 else if (choosingdirector) {
322 wxString number = str;
323 number.RemoveLast(); /* Removes \n */
325 if (number.ToLong(&val)) {
326 directorchoosen = (int)val;
334 void console_thread::Delete() {
337 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
340 /*csprint(NULL, CS_END);
341 csprint(NULL, CS_DISCONNECTED);
342 csprint(NULL, CS_TERMINATED);*/