]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/console_thread.cpp
3b84c8bd0772c8b36ba4d28627d5b804d4857361
[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       #ifdef HAVE_WIN32
104          Exit();
105       #endif
106       return NULL;
107    }
108
109    csprint("Connected\n");
110
111    jcr.dir_bsock = UA_sock;
112    if (!authenticate_director(&jcr, dir, NULL)) {
113       csprint("ERR=");
114       csprint(UA_sock->msg);
115       csprint(NULL, CS_END);
116       csprint(NULL, CS_DISCONNECTED);
117       csprint(NULL, CS_TERMINATED);
118       #ifdef HAVE_WIN32
119          Exit();
120       #endif
121       return NULL;
122    }
123    
124    csprint(NULL, CS_CONNECTED);
125    
126    Write("messages\n");
127
128    int stat;
129
130    /* main loop */
131    while(!TestDestroy()) {   /* Tests if thread has been ended */
132       if ((stat = bnet_recv(UA_sock)) >= 0) {
133          csprint(UA_sock->msg);
134       }
135       else if (stat == BNET_SIGNAL) {
136          if (UA_sock->msglen == BNET_PROMPT) {
137             csprint(NULL, CS_PROMPT);
138          }
139          else if (UA_sock->msglen == BNET_EOD) {
140             csprint(NULL, CS_END);
141          }
142          else if (UA_sock->msglen == BNET_HEARTBEAT) {
143             bnet_sig(UA_sock, BNET_HB_RESPONSE);
144             csprint("<< Heartbeat signal received, answered. >>\n", CS_DEBUG);
145          }
146          else {
147             csprint("<< Unexpected signal received : ", CS_DEBUG);
148             csprint(bnet_sig_to_ascii(UA_sock), CS_DEBUG);
149             csprint(">>\n", CS_DEBUG);
150          }
151       }
152       else { /* BNET_HARDEOF || BNET_ERROR */
153          csprint(NULL, CS_END);
154          break;
155       }
156            
157       if (is_bnet_stop(UA_sock)) {
158          csprint(NULL, CS_END);
159          break;            /* error or term */
160       }
161    }
162    
163    csprint(NULL, CS_DISCONNECTED);
164
165    csprint("Connection terminated\n");
166    
167    UA_sock = NULL;
168
169    csprint(NULL, CS_TERMINATED);
170
171    #ifdef HAVE_WIN32
172       Exit();
173    #endif
174    
175    return NULL;
176 }
177
178 void console_thread::Write(const char* str) {
179    if (UA_sock) {
180        UA_sock->msglen = strlen(str);
181        pm_strcpy(&UA_sock->msg, str);
182        bnet_send(UA_sock);
183    }
184 }
185
186 void console_thread::Delete() {
187    Write("quit\n");
188    if (UA_sock) {
189       bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
190       bnet_close(UA_sock);
191       UA_sock = NULL;
192    }
193 }
194