3 * Interaction thread between director and the GUI
5 * Nicolas Boichat, April 2004
10 Copyright (C) 2004-2005 Kern Sibbald
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 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;
53 wxString console_thread::working_dir = ".";
55 void console_thread::SetWorkingDirectory(wxString w_dir) {
56 if ((w_dir.Last() == '/') || (w_dir.Last() == '\\')) {
57 console_thread::working_dir = w_dir.Mid(0, w_dir.Length()-1);
60 console_thread::working_dir = w_dir;
64 void console_thread::InitLib() {
65 if (WSA_Init() != 0) {
66 csprint("Error while initializing windows sockets...\n");
72 my_name_is(0, NULL, "wx-console");
73 //textdomain("bacula-console");
74 working_directory = console_thread::working_dir;
79 void console_thread::FreeLib() {
81 if (WSACleanup() != 0) {
82 csprint("Error while cleaning up windows sockets...\n");
90 * Format a scanner error message
92 static void scan_err(const char *file, int line, LEX *lc, const char *msg, ...)
99 va_start(arg_ptr, msg);
100 bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
103 if (lc->line_no > lc->begin_line_no) {
104 bsnprintf(more, sizeof(more),
105 _("Problem probably begins at line %d.\n"), lc->begin_line_no);
109 bsnprintf(err, sizeof(err), _("Config error: %s\n"
110 " : line %d, col %d of file %s\n%s\n%s"),
111 buf, lc->line_no, lc->col_no, lc->fname, lc->line, more);
115 wxString console_thread::LoadConfig(wxString configfile) {
119 return "Error while initializing library.";
122 free_config_resources();
124 MSGS* msgs = (MSGS *)malloc(sizeof(MSGS));
125 memset(msgs, 0, sizeof(MSGS));
126 for (int i=1; i<=M_MAX; i++) {
128 add_msg_dest(msgs, MD_STDOUT, i, NULL, NULL);
130 add_msg_dest(msgs, MD_SYSLOG, i, NULL, NULL);
131 add_msg_dest(msgs, MD_CONSOLE, i, NULL, NULL);
134 init_msg(NULL, msgs);
135 init_console_msg(console_thread::working_dir);
138 if (!parse_config(configfile.c_str(), &scan_err)) {
139 configloaded = false;
145 wxRemoveFile(console_thread::working_dir + "/wx-console.conmsg");
146 init_msg(NULL, NULL);
154 console_thread::console_thread() {
156 choosingdirector = false;
160 console_thread::~console_thread() {
162 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
171 void* console_thread::Entry() {
173 csprint("Error : Library not initialized\n");
174 csprint(NULL, CS_END);
175 csprint(NULL, CS_DISCONNECTED);
176 csprint(NULL, CS_TERMINATED);
184 csprint("Error : No configuration file loaded\n");
185 csprint(NULL, CS_END);
186 csprint(NULL, CS_DISCONNECTED);
187 csprint(NULL, CS_TERMINATED);
194 csprint("Connecting...\n");
197 DIRRES* res[16]; /* Maximum 16 directors */
201 foreach_res(dir, R_DIRECTOR) {
211 csprint("Error : No director defined in config file.\n");
212 csprint(NULL, CS_END);
213 csprint(NULL, CS_DISCONNECTED);
214 csprint(NULL, CS_TERMINATED);
220 else if (count == 1) {
225 csprint("Multiple directors found in your config file.\n");
226 for (int i = 0; i < count; i++) {
228 csprint(wxString(" ") << (i+1) << ": " << res[i]->hdr.name << "\n");
231 csprint(wxString(" ") << (i+1) << ": " << res[i]->hdr.name << "\n");
234 csprint(wxString("Please choose a director (1-") << count << ") : ");
235 csprint(NULL, CS_PROMPT);
236 choosingdirector = true;
237 directorchoosen = -1;
238 while(directorchoosen == -1) {
239 bmicrosleep(0, 2000);
242 choosingdirector = false;
243 if (directorchoosen != 0) {
249 memset(&jcr, 0, sizeof(jcr));
251 jcr.dequeuing = 1; /* TODO: catch messages */
253 UA_sock = bnet_connect(&jcr, 3, 3, "Director daemon",
254 res[directorchoosen-1]->address, NULL, res[directorchoosen-1]->DIRport, 0);
256 if (UA_sock == NULL) {
257 csprint("Failed to connect to the director\n");
258 csprint(NULL, CS_END);
259 csprint(NULL, CS_DISCONNECTED);
260 csprint(NULL, CS_TERMINATED);
267 csprint("Connected\n");
269 jcr.dir_bsock = UA_sock;
271 /* If cons==NULL, default console will be used */
272 CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
274 if (!authenticate_director(&jcr, res[directorchoosen-1], cons)) {
276 csprint(UA_sock->msg);
277 csprint(NULL, CS_END);
278 csprint(NULL, CS_DISCONNECTED);
279 csprint(NULL, CS_TERMINATED);
286 csprint(NULL, CS_CONNECTED);
288 Write(".messages\n");
293 while(!TestDestroy()) { /* Tests if thread has been ended */
294 if ((stat = bnet_recv(UA_sock)) >= 0) {
295 csprint(UA_sock->msg);
297 else if (stat == BNET_SIGNAL) {
298 if (UA_sock->msglen == BNET_PROMPT) {
299 csprint(NULL, CS_PROMPT);
301 else if (UA_sock->msglen == BNET_EOD) {
302 csprint(NULL, CS_END);
304 else if (UA_sock->msglen == BNET_HEARTBEAT) {
305 bnet_sig(UA_sock, BNET_HB_RESPONSE);
306 csprint("<< Heartbeat signal received, answered. >>\n", CS_DEBUG);
309 csprint("<< Unexpected signal received : ", CS_DEBUG);
310 csprint(bnet_sig_to_ascii(UA_sock), CS_DEBUG);
311 csprint(">>\n", CS_DEBUG);
314 else { /* BNET_HARDEOF || BNET_ERROR */
315 csprint(NULL, CS_END);
319 if (is_bnet_stop(UA_sock)) {
320 csprint(NULL, CS_END);
321 break; /* error or term */
325 csprint(NULL, CS_DISCONNECTED);
327 csprint("Connection terminated\n");
331 csprint(NULL, CS_TERMINATED);
340 void console_thread::Write(const char* str) {
342 UA_sock->msglen = strlen(str);
343 pm_strcpy(&UA_sock->msg, str);
346 else if (choosingdirector) {
347 wxString number = str;
348 number.RemoveLast(); /* Removes \n */
350 if (number.ToLong(&val)) {
351 directorchoosen = (int)val;
359 void console_thread::Delete() {
362 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
365 /*csprint(NULL, CS_END);
366 csprint(NULL, CS_DISCONNECTED);
367 csprint(NULL, CS_TERMINATED);*/