3 * Interaction thread between director and the GUI
5 * Nicolas Boichat, April 2004
9 Copyright (C) 2004 Kern Sibbald and John Walker
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.
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.
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.
26 #include "console_thread.h" // class's header file
28 #include <wx/wxprec.h>
30 #include <wx/thread.h>
34 #include "console_conf.h"
40 DWORD g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
41 char OK_msg[] = "2000 OK\n";
42 char TERM_msg[] = "2999 Terminate\n";
45 /* Imported functions */
46 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons);
49 console_thread::console_thread(wxString configfile) {
51 this->configfile = configfile;
55 console_thread::~console_thread() {
57 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
61 if (WSACleanup() == 0) {
62 //csprint("Windows sockets cleaned up successfully...\n");
65 csprint("Error while cleaning up windows sockets...\n");
72 void* console_thread::Entry() {
73 if (WSA_Init() == 0) {
74 //csprint("Windows sockets initialized successfully...\n");
77 csprint("Error while initializing windows sockets...\n");
80 csprint("Connecting...\n");
83 my_name_is(0, NULL, "wx-console");
84 //textdomain("bacula-console");
86 MSGS* msgs = (MSGS *)malloc(sizeof(MSGS));
87 memset(msgs, 0, sizeof(MSGS));
88 for (int i=1; i<=M_MAX; i++) {
90 add_msg_dest(msgs, MD_STDOUT, i, NULL, NULL);
92 add_msg_dest(msgs, MD_SYSLOG, i, NULL, NULL);
93 add_msg_dest(msgs, MD_CONSOLE, i, NULL, NULL);
97 init_console_msg(".");
99 /* TODO (#4#): Allow the user to choose his config file. */
100 if (!parse_config(configfile.c_str(), 0)) {
101 csprint("Unable to read configuration file.\n");
102 csprint(NULL, CS_END);
103 csprint(NULL, CS_DISCONNECTED);
104 csprint(NULL, CS_TERMINATED);
111 init_msg(NULL, NULL);
114 DIRRES *dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
117 memset(&jcr, 0, sizeof(jcr));
119 jcr.dequeuing = 1; /* TODO: catch messages */
121 UA_sock = bnet_connect(&jcr, 3, 3, "Director daemon", dir->address, NULL, dir->DIRport, 0);
122 if (UA_sock == NULL) {
123 csprint("Failed to connect to the director\n");
124 csprint(NULL, CS_END);
125 csprint(NULL, CS_DISCONNECTED);
126 csprint(NULL, CS_TERMINATED);
133 csprint("Connected\n");
135 jcr.dir_bsock = UA_sock;
137 /* If cons==NULL, default console will be used */
138 CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
140 if (!authenticate_director(&jcr, dir, cons)) {
142 csprint(UA_sock->msg);
143 csprint(NULL, CS_END);
144 csprint(NULL, CS_DISCONNECTED);
145 csprint(NULL, CS_TERMINATED);
152 csprint(NULL, CS_CONNECTED);
159 while(!TestDestroy()) { /* Tests if thread has been ended */
160 if ((stat = bnet_recv(UA_sock)) >= 0) {
161 csprint(UA_sock->msg);
163 else if (stat == BNET_SIGNAL) {
164 if (UA_sock->msglen == BNET_PROMPT) {
165 csprint(NULL, CS_PROMPT);
167 else if (UA_sock->msglen == BNET_EOD) {
168 csprint(NULL, CS_END);
170 else if (UA_sock->msglen == BNET_HEARTBEAT) {
171 bnet_sig(UA_sock, BNET_HB_RESPONSE);
172 csprint("<< Heartbeat signal received, answered. >>\n", CS_DEBUG);
175 csprint("<< Unexpected signal received : ", CS_DEBUG);
176 csprint(bnet_sig_to_ascii(UA_sock), CS_DEBUG);
177 csprint(">>\n", CS_DEBUG);
180 else { /* BNET_HARDEOF || BNET_ERROR */
181 csprint(NULL, CS_END);
185 if (is_bnet_stop(UA_sock)) {
186 csprint(NULL, CS_END);
187 break; /* error or term */
191 csprint(NULL, CS_DISCONNECTED);
193 csprint("Connection terminated\n");
197 csprint(NULL, CS_TERMINATED);
206 void console_thread::Write(const char* str) {
208 UA_sock->msglen = strlen(str);
209 pm_strcpy(&UA_sock->msg, str);
214 void console_thread::Delete() {
217 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
220 csprint(NULL, CS_END);
221 csprint(NULL, CS_DISCONNECTED);
222 csprint(NULL, CS_TERMINATED);