]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/console_thread.cpp
ede22cd5656a44811604df9c2beb926041cd3cdb
[bacula/bacula] / bacula / src / wx-console / console_thread.cpp
1 /*
2  *
3  *    Interaction thread between director and the GUI
4  *
5  *    Nicolas Boichat, April 2004
6  *
7  */
8 /*
9    Copyright (C) 2004 Kern Sibbald and John Walker
10
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.
15
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.
20
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.
24  */
25
26 #include "console_thread.h" // class's header file
27
28 #include <wx/wxprec.h>
29
30 #include <wx/thread.h>
31 #include <bacula.h>
32 #include <jcr.h>
33
34 #include "console_conf.h"
35
36 #include "csprint.h"
37
38 #ifdef HAVE_WIN32
39 #include <windows.h>
40 DWORD  g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
41 char OK_msg[]   = "2000 OK\n";
42 char TERM_msg[] = "2999 Terminate\n";
43 #endif
44
45 /* Imported functions */
46 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons);
47
48 // class constructor
49 console_thread::console_thread() {
50    UA_sock = NULL;
51 }
52
53 // class destructor
54 console_thread::~console_thread() {
55    if (UA_sock) {
56       bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
57       bnet_close(UA_sock);
58       UA_sock = NULL;
59    }
60    if (WSACleanup() == 0) {
61       //csprint("Windows sockets cleaned up successfully...\n");
62    }
63    else {
64       csprint("Error while cleaning up windows sockets...\n");
65    }
66 }
67
68 /*
69  * Thread entry point
70  */
71 void* console_thread::Entry() {
72    if (WSA_Init() == 0) {
73       //csprint("Windows sockets initialized successfully...\n");
74    }
75    else {
76       csprint("Error while initializing windows sockets...\n");
77    }
78
79    csprint("Connecting...\n");
80
81    init_stack_dump();
82    my_name_is(0, NULL, "wx-console");
83    //textdomain("bacula-console");
84    init_msg(NULL, NULL);
85
86    /* TODO (#4#): Allow the user to choose his config file. */
87    parse_config("./wx-console.conf");
88
89    LockRes();
90    DIRRES *dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
91    UnlockRes();
92
93    memset(&jcr, 0, sizeof(jcr));
94    
95    jcr.dequeuing = 1; /* TODO: catch messages */
96
97    UA_sock = bnet_connect(&jcr, 3, 3, "Director daemon", dir->address, NULL, dir->DIRport, 0);
98    if (UA_sock == NULL) {
99       csprint("Failed to connect to the director\n");
100       csprint(NULL, CS_END);
101       csprint(NULL, CS_DISCONNECTED);
102       csprint(NULL, CS_TERMINATED);
103       Exit();
104       return NULL;
105    }
106
107    csprint("Connected\n");
108
109    jcr.dir_bsock = UA_sock;
110    if (!authenticate_director(&jcr, dir, NULL)) {
111       csprint("ERR=");
112       csprint(UA_sock->msg);
113       csprint(NULL, CS_END);
114       csprint(NULL, CS_DISCONNECTED);
115       csprint(NULL, CS_TERMINATED);
116       Exit();
117       return NULL;
118    }
119    
120    csprint(NULL, CS_CONNECTED);
121    
122    Write("messages\n");
123
124    int stat;
125
126    /* main loop */
127    while(!TestDestroy()) {   /* Tests if thread has been ended */
128       if ((stat = bnet_recv(UA_sock)) >= 0) {
129          csprint(UA_sock->msg);
130       }
131       else if (stat == BNET_SIGNAL) {
132          if (UA_sock->msglen == BNET_PROMPT) {
133             csprint(NULL, CS_PROMPT);
134          }
135          else if (UA_sock->msglen == BNET_EOD) {
136             csprint(NULL, CS_END);
137          }
138          else if (UA_sock->msglen == BNET_HEARTBEAT) {
139             bnet_sig(UA_sock, BNET_HB_RESPONSE);
140             csprint("<< Heartbeat signal received, answered. >>\n", CS_DEBUG);
141          }
142          else {
143             csprint("<< Unexpected signal received : ", CS_DEBUG);
144             csprint(bnet_sig_to_ascii(UA_sock), CS_DEBUG);
145             csprint(">>\n", CS_DEBUG);
146          }
147       }
148       else { /* BNET_HARDEOF || BNET_ERROR */
149          csprint(NULL, CS_END);
150          break;
151       }
152            
153       if (is_bnet_stop(UA_sock)) {
154          csprint(NULL, CS_END);
155          break;            /* error or term */
156       }
157    }
158    
159    csprint(NULL, CS_DISCONNECTED);
160
161    csprint("Connection terminated\n");
162    
163    UA_sock = NULL;
164
165    csprint(NULL, CS_TERMINATED);
166    
167    Exit();
168    
169    return NULL;
170 }
171
172 void console_thread::Write(const char* str) {
173    if (UA_sock) {
174        UA_sock->msglen = strlen(str);
175        pm_strcpy(&UA_sock->msg, str);
176        bnet_send(UA_sock);
177    }
178 }
179
180 void console_thread::Delete() {
181    Write("quit\n");
182    if (UA_sock) {
183       bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
184       bnet_close(UA_sock);
185       UA_sock = NULL;
186    }
187 }
188