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 // http://66.102.9.104/search?q=cache:Djc1mPF3hRoJ:cvs.sourceforge.net/viewcvs.py/audacity/audacity-src/src/AudioIO.cpp%3Frev%3D1.102+macos+x+wxthread&hl=fr
28 #include "console_thread.h" // class's header file
30 #include <wx/wxprec.h>
32 #include <wx/thread.h>
37 #include "console_conf.h"
43 DWORD g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
44 char OK_msg[] = "2000 OK\n";
45 char TERM_msg[] = "2999 Terminate\n";
48 /* Imported functions */
49 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons);
51 bool console_thread::inited = false;
52 bool console_thread::configloaded = false;
54 void console_thread::InitLib() {
55 if (WSA_Init() != 0) {
56 csprint("Error while initializing windows sockets...\n");
62 my_name_is(0, NULL, "wx-console");
63 //textdomain("bacula-console");
68 void console_thread::FreeLib() {
70 if (WSACleanup() != 0) {
71 csprint("Error while cleaning up windows sockets...\n");
76 wxString console_thread::LoadConfig(wxString configfile) {
80 return "Error while initializing library.";
83 MSGS* msgs = (MSGS *)malloc(sizeof(MSGS));
84 memset(msgs, 0, sizeof(MSGS));
85 for (int i=1; i<=M_MAX; i++) {
87 add_msg_dest(msgs, MD_STDOUT, i, NULL, NULL);
89 add_msg_dest(msgs, MD_SYSLOG, i, NULL, NULL);
90 add_msg_dest(msgs, MD_CONSOLE, i, NULL, NULL);
94 init_console_msg(".");
96 /* TODO (#4#): Allow the user to choose his config file. */
97 if (!parse_config(configfile.c_str(), 0)) {
99 wxFile file("./wx-console.conmsg");
100 if (!file.IsOpened())
101 return "Unable to retrieve error message.";
105 while ((len = file.Read(buffer, 512)) > -1) {
106 buffer[len] = (wxChar)0;
113 wxRemoveFile("./wx-console.conmsg");
118 wxRemoveFile("./wx-console.conmsg");
119 init_msg(NULL, NULL);
127 console_thread::console_thread() {
132 console_thread::~console_thread() {
134 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
143 void* console_thread::Entry() {
145 csprint("Error : Library not initialized\n");
146 csprint(NULL, CS_END);
147 csprint(NULL, CS_DISCONNECTED);
148 csprint(NULL, CS_TERMINATED);
156 csprint("Error : No configuration file loaded\n");
157 csprint(NULL, CS_END);
158 csprint(NULL, CS_DISCONNECTED);
159 csprint(NULL, CS_TERMINATED);
166 csprint("Connecting...\n");
169 DIRRES *dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
172 memset(&jcr, 0, sizeof(jcr));
174 jcr.dequeuing = 1; /* TODO: catch messages */
176 UA_sock = bnet_connect(&jcr, 3, 3, "Director daemon", dir->address, NULL, dir->DIRport, 0);
177 if (UA_sock == NULL) {
178 csprint("Failed to connect to the director\n");
179 csprint(NULL, CS_END);
180 csprint(NULL, CS_DISCONNECTED);
181 csprint(NULL, CS_TERMINATED);
188 csprint("Connected\n");
190 jcr.dir_bsock = UA_sock;
192 /* If cons==NULL, default console will be used */
193 CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
195 if (!authenticate_director(&jcr, dir, cons)) {
197 csprint(UA_sock->msg);
198 csprint(NULL, CS_END);
199 csprint(NULL, CS_DISCONNECTED);
200 csprint(NULL, CS_TERMINATED);
207 csprint(NULL, CS_CONNECTED);
214 while(!TestDestroy()) { /* Tests if thread has been ended */
215 if ((stat = bnet_recv(UA_sock)) >= 0) {
216 csprint(UA_sock->msg);
218 else if (stat == BNET_SIGNAL) {
219 if (UA_sock->msglen == BNET_PROMPT) {
220 csprint(NULL, CS_PROMPT);
222 else if (UA_sock->msglen == BNET_EOD) {
223 csprint(NULL, CS_END);
225 else if (UA_sock->msglen == BNET_HEARTBEAT) {
226 bnet_sig(UA_sock, BNET_HB_RESPONSE);
227 csprint("<< Heartbeat signal received, answered. >>\n", CS_DEBUG);
230 csprint("<< Unexpected signal received : ", CS_DEBUG);
231 csprint(bnet_sig_to_ascii(UA_sock), CS_DEBUG);
232 csprint(">>\n", CS_DEBUG);
235 else { /* BNET_HARDEOF || BNET_ERROR */
236 csprint(NULL, CS_END);
240 if (is_bnet_stop(UA_sock)) {
241 csprint(NULL, CS_END);
242 break; /* error or term */
246 csprint(NULL, CS_DISCONNECTED);
248 csprint("Connection terminated\n");
252 csprint(NULL, CS_TERMINATED);
261 void console_thread::Write(const char* str) {
263 UA_sock->msglen = strlen(str);
264 pm_strcpy(&UA_sock->msg, str);
269 void console_thread::Delete() {
272 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
275 csprint(NULL, CS_END);
276 csprint(NULL, CS_DISCONNECTED);
277 csprint(NULL, CS_TERMINATED);