]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/console_thread.cpp
7bf327a705b7f566ef3b716f871d8e390ea310b8
[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 }
61
62 /*
63  * Thread entry point
64  */
65 void* console_thread::Entry() {
66 #ifdef HAVE_WIN32
67    if (WSA_Init() == 0) {
68       csprint("Windows sockets initialized successfully...\n");
69    }
70    else {
71       csprint("Error while initializing windows sockets...\n");
72       return 0;
73    }
74 #endif
75    
76    csprint("Connecting...\n");
77
78    init_stack_dump();
79    my_name_is(0, NULL, "wx-console");
80    textdomain("bacula-console");
81    init_msg(NULL, NULL);
82
83    /* TODO (#4#): Allow the user to choose his config file. */
84    parse_config("./wx-console.conf");
85
86    LockRes();
87    DIRRES *dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
88    UnlockRes();
89
90    memset(&jcr, 0, sizeof(jcr));
91
92    UA_sock = bnet_connect(&jcr, 3, 3, "Director daemon", dir->address, NULL, dir->DIRport, 0);
93    if (UA_sock == NULL) {
94       csprint("Failed to connect to the director\n");
95       return NULL;
96    }
97
98    csprint("Connected\n");
99
100    jcr.dir_bsock = UA_sock;
101    if (!authenticate_director(&jcr, dir, NULL)) {
102       csprint("ERR=");
103       csprint(UA_sock->msg);
104       return NULL;
105    }
106    
107    csprint(NULL, CS_CONNECTED);
108    
109    Write("messages\n");
110
111    int stat;
112
113    /* main loop */
114    while(!TestDestroy()) {   /* Tests if thread has been ended */
115       if ((stat = bnet_recv(UA_sock)) >= 0) {
116          csprint(UA_sock->msg);
117       }
118       else {
119          csprint(NULL, CS_END);
120       }
121
122       if (is_bnet_stop(UA_sock)) {
123          csprint(NULL, CS_END);
124          break;            /* error or term */
125       }
126    }
127    
128    csprint(NULL, CS_DISCONNECTED);
129
130    csprint("Connection terminated\n");
131
132    return 0;
133 }
134
135 void console_thread::Write(const char* str) {
136    if (UA_sock) {
137        UA_sock->msglen = strlen(str);
138        pm_strcpy(&UA_sock->msg, str);
139        bnet_send(UA_sock);
140    }
141 }
142
143 void console_thread::Delete() {
144    Write("quit\n");
145    if (UA_sock) {
146       bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
147       bnet_close(UA_sock);
148       UA_sock = NULL;
149    }
150 }
151