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