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;
54 wxString console_thread::working_dir = ".";
56 void console_thread::SetWorkingDirectory(wxString w_dir) {
57 if ((w_dir.Last() == '/') || (w_dir.Last() == '\\')) {
58 console_thread::working_dir = w_dir.Mid(0, w_dir.Length()-1);
61 console_thread::working_dir = w_dir;
65 void console_thread::InitLib() {
66 if (WSA_Init() != 0) {
67 csprint("Error while initializing windows sockets...\n");
73 my_name_is(0, NULL, "wx-console");
74 //textdomain("bacula-console");
75 working_directory = console_thread::working_dir;
80 void console_thread::FreeLib() {
82 if (WSACleanup() != 0) {
83 csprint("Error while cleaning up windows sockets...\n");
88 wxString console_thread::LoadConfig(wxString configfile) {
92 return "Error while initializing library.";
95 free_config_resources();
97 MSGS* msgs = (MSGS *)malloc(sizeof(MSGS));
98 memset(msgs, 0, sizeof(MSGS));
99 for (int i=1; i<=M_MAX; i++) {
101 add_msg_dest(msgs, MD_STDOUT, i, NULL, NULL);
103 add_msg_dest(msgs, MD_SYSLOG, i, NULL, NULL);
104 add_msg_dest(msgs, MD_CONSOLE, i, NULL, NULL);
107 init_msg(NULL, msgs);
108 init_console_msg(console_thread::working_dir);
110 if (!parse_config(configfile.c_str(), 0)) {
111 configloaded = false;
112 wxFile file(console_thread::working_dir + "/wx-console.conmsg");
113 if (!file.IsOpened())
114 return "Unable to retrieve error message.";
118 while ((len = file.Read(buffer, 512)) > -1) {
119 buffer[len] = (wxChar)0;
126 wxRemoveFile(console_thread::working_dir + "/wx-console.conmsg");
131 wxRemoveFile(console_thread::working_dir + "/wx-console.conmsg");
132 init_msg(NULL, NULL);
140 console_thread::console_thread() {
142 choosingdirector = false;
146 console_thread::~console_thread() {
148 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
157 void* console_thread::Entry() {
159 csprint("Error : Library not initialized\n");
160 csprint(NULL, CS_END);
161 csprint(NULL, CS_DISCONNECTED);
162 csprint(NULL, CS_TERMINATED);
170 csprint("Error : No configuration file loaded\n");
171 csprint(NULL, CS_END);
172 csprint(NULL, CS_DISCONNECTED);
173 csprint(NULL, CS_TERMINATED);
180 csprint("Connecting...\n");
183 DIRRES* res[16]; /* Maximum 16 directors */
187 foreach_res(dir, R_DIRECTOR) {
197 csprint("Error : No director defined in config file.\n");
198 csprint(NULL, CS_END);
199 csprint(NULL, CS_DISCONNECTED);
200 csprint(NULL, CS_TERMINATED);
206 else if (count == 1) {
211 csprint("Multiple directors found in your config file.\n");
212 for (int i = 0; i < count; i++) {
214 csprint(wxString(" ") << (i+1) << ": " << res[i]->hdr.name << "\n");
217 csprint(wxString(" ") << (i+1) << ": " << res[i]->hdr.name << "\n");
220 csprint(wxString("Please choose a director (1-") << count << ") : ");
221 csprint(NULL, CS_PROMPT);
222 choosingdirector = true;
223 directorchoosen = -1;
224 while(directorchoosen == -1) {
225 bmicrosleep(0, 2000);
228 choosingdirector = false;
229 if (directorchoosen != 0) {
235 memset(&jcr, 0, sizeof(jcr));
237 jcr.dequeuing = 1; /* TODO: catch messages */
239 UA_sock = bnet_connect(&jcr, 3, 3, "Director daemon",
240 res[directorchoosen-1]->address, NULL, res[directorchoosen-1]->DIRport, 0);
242 if (UA_sock == NULL) {
243 csprint("Failed to connect to the director\n");
244 csprint(NULL, CS_END);
245 csprint(NULL, CS_DISCONNECTED);
246 csprint(NULL, CS_TERMINATED);
253 csprint("Connected\n");
255 jcr.dir_bsock = UA_sock;
257 /* If cons==NULL, default console will be used */
258 CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
260 if (!authenticate_director(&jcr, res[directorchoosen-1], cons)) {
262 csprint(UA_sock->msg);
263 csprint(NULL, CS_END);
264 csprint(NULL, CS_DISCONNECTED);
265 csprint(NULL, CS_TERMINATED);
272 csprint(NULL, CS_CONNECTED);
274 Write(".messages\n");
279 while(!TestDestroy()) { /* Tests if thread has been ended */
280 if ((stat = bnet_recv(UA_sock)) >= 0) {
281 csprint(UA_sock->msg);
283 else if (stat == BNET_SIGNAL) {
284 if (UA_sock->msglen == BNET_PROMPT) {
285 csprint(NULL, CS_PROMPT);
287 else if (UA_sock->msglen == BNET_EOD) {
288 csprint(NULL, CS_END);
290 else if (UA_sock->msglen == BNET_HEARTBEAT) {
291 bnet_sig(UA_sock, BNET_HB_RESPONSE);
292 csprint("<< Heartbeat signal received, answered. >>\n", CS_DEBUG);
295 csprint("<< Unexpected signal received : ", CS_DEBUG);
296 csprint(bnet_sig_to_ascii(UA_sock), CS_DEBUG);
297 csprint(">>\n", CS_DEBUG);
300 else { /* BNET_HARDEOF || BNET_ERROR */
301 csprint(NULL, CS_END);
305 if (is_bnet_stop(UA_sock)) {
306 csprint(NULL, CS_END);
307 break; /* error or term */
311 csprint(NULL, CS_DISCONNECTED);
313 csprint("Connection terminated\n");
317 csprint(NULL, CS_TERMINATED);
326 void console_thread::Write(const char* str) {
328 UA_sock->msglen = strlen(str);
329 pm_strcpy(&UA_sock->msg, str);
332 else if (choosingdirector) {
333 wxString number = str;
334 number.RemoveLast(); /* Removes \n */
336 if (number.ToLong(&val)) {
337 directorchoosen = (int)val;
345 void console_thread::Delete() {
348 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
351 /*csprint(NULL, CS_END);
352 csprint(NULL, CS_DISCONNECTED);
353 csprint(NULL, CS_TERMINATED);*/