/[LeafOK_CVS]/innwebd/controld.cpp
ViewVC logotype

Contents of /innwebd/controld.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (show annotations)
Fri May 20 14:32:36 2005 UTC (20 years, 10 months ago) by sysadm
Branch: MAIN
CVS Tags: innwebd_1-4-16-0_MIL
Changes since 1.12: +4 -0 lines
Content type: text/x-c++src
Add log

1 /*******************************************************/
2 /* */
3 /* LeafOK Innbbsd */
4 /* Copyright (C) LeafOK.com, 2003-2005 */
5 /* */
6 /* Programmed by Leaf */
7 /* E-mail:leaflet@leafok.com QQ:6049044 */
8 /* */
9 /* http://bbs.leafok.com */
10 /* http://bbs.leafok.net */
11 /* http://bbs.fenglin.info */
12 /* */
13 /*******************************************************/
14
15 #include "StdAfx.h"
16 #include ".\controld.h"
17 #include ".\base_active.h"
18 #include ".\App_common.h"
19
20 using namespace std;
21
22 bool controld::thread_terminate = true;
23
24 controld::controld(void)
25 :bbsd_startup(false),innd_startup(false),outd_startup(false),startup_delay(0)
26 {
27 }
28
29 controld::~controld(void)
30 {
31 }
32
33 int controld::Accept(void)
34 {
35 CString out,cmd;
36 char temp[256];
37 int result;
38
39 try
40 {
41 out.Format("200 %s ready.",App_common::GetVersion());
42 this->s_send(out);
43 while(!this->thread_terminate)
44 {
45 this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WAITING);
46
47 if (this->s_receive(out) == 0) //Connection closed
48 break;
49 out.Trim();
50
51 this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING);
52
53 syslog << logfile::log_head << "Cmd: " << out << endl;
54
55 if (sscanf(out,"%20s",temp) ==1)
56 cmd = strupr(temp);
57 else
58 cmd = "";
59
60 if (cmd == "") //Blank command
61 continue;
62
63 if (cmd == "START")
64 {
65 if (sscanf(out,"%*s %20s",temp) ==1)
66 cmd = strupr(temp);
67 else
68 cmd = "";
69 cmd.Trim();
70
71 if (cmd == "BBSD" || cmd == "INND" || cmd == "OUTD")
72 {
73 if (this->access.control)
74 {
75 out.Format("Starting %s ... ",cmd);
76 this->s_send(out,false);
77
78 if (cmd == "BBSD")
79 {
80 result = ((controld*)(this->GetParentThread()))->bbsd.begin();
81 }
82 if (cmd == "INND")
83 {
84 result = ((controld*)(this->GetParentThread()))->innd.begin();
85 }
86 if (cmd == "OUTD")
87 {
88 result = ((controld*)(this->GetParentThread()))->outd.begin();
89 }
90
91 this->s_send(result == 0 ? "OK" : "Failed");
92
93 syslog << logfile::log_head << "Begin " << cmd << " ... " << (result == 0 ? "OK" : "Failed") << endl;
94 }
95 else
96 {
97 this->s_send("400 Forbidden");
98 }
99 }
100 else
101 {
102 this->s_send("501 service");
103 }
104 continue;
105 }
106
107 if (cmd == "STOP")
108 {
109 if (sscanf(out,"%*s %20s",temp) ==1)
110 cmd = strupr(temp);
111 else
112 cmd = "";
113 cmd.Trim();
114
115 if (cmd == "BBSD" || cmd == "INND" || cmd == "OUTD")
116 {
117 if (this->access.control)
118 {
119 out.Format("Stopping %s ... ",cmd);
120 this->s_send(out,false);
121
122 if (cmd == "BBSD")
123 {
124 result = ((controld*)(this->GetParentThread()))->bbsd.end();
125 }
126 if (cmd == "INND")
127 {
128 result = ((controld*)(this->GetParentThread()))->innd.end();
129 }
130 if (cmd == "OUTD")
131 {
132 result = ((controld*)(this->GetParentThread()))->outd.end();
133 }
134
135 this->s_send(result == 0 ? "OK" : "Failed");
136
137 syslog << logfile::log_head << "End " << cmd << " ... " << (result == 0 ? "OK" : "Failed") << endl;
138 }
139 else
140 {
141 this->s_send("400 Forbidden");
142 }
143 }
144 else
145 {
146 this->s_send("501 service");
147 }
148 continue;
149 }
150
151 if (cmd == "SHUTDOWN")
152 {
153 if (this->access.control)
154 {
155 this->s_send("Shutting down ... ",false);
156
157 ((controld*)(this->GetParentThread()))->ShutdownChild();
158
159 this->s_send("OK");
160
161 ((controld*)(this->GetParentThread()))->Shutdown();
162 break;
163 }
164 else
165 {
166 this->s_send("400 Forbidden");
167 }
168 continue;
169 }
170
171 if (cmd == "RELOAD_PRIV")
172 {
173 this->s_send("Reloading prev_list ... ",false);
174
175 ((controld*)(this->GetParentThread()))->unload_priv();
176 if (((controld*)(this->GetParentThread()))->load_priv() == 0)
177 this->s_send("OK");
178 else
179 this->s_send("Failed");
180
181 continue;
182 }
183
184 if (cmd == "ACTIVE")
185 {
186 if (sscanf(out,"%*s %20s",temp) == 1)
187 cmd = strupr(temp);
188 else
189 cmd = "";
190 cmd.Trim();
191
192 if (cmd == "POST" || cmd == "IHAVE")
193 {
194 if (this->access.control)
195 {
196 this->s_send("201 Entering native mode");
197 base_active nntpClient;
198 nntpClient.configure(this->innd_id,this->innd_name,this->innd_server,this->innd_uid,"",0,this->w_conn_str);
199
200 this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WAITING);
201
202 if (cmd == "POST") // Get & Post
203 {
204 nntpClient.work(true,false);
205 }
206 if (cmd == "IHAVE") // IHAVE only
207 {
208 nntpClient.work(false,true);
209 }
210
211 this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING);
212
213 this->s_send("Entering passive mode\r\n.");
214 }
215 else
216 {
217 this->s_send("400 Forbidden");
218 }
219 }
220 else
221 {
222 this->s_send("501 mode server port");
223 }
224 continue;
225 }
226
227 if (cmd == "QUIT")
228 {
229 this->s_send("205 .\r\n");
230 break;
231 }
232
233 this->s_send("500 What?");
234 }
235 }
236 catch(CException* e)
237 {
238 syslog << logfile::log_head << "Error in accept()" << endl;
239 e->Delete();
240 return -1;
241 }
242
243 return 0;
244 }
245
246 controld* controld::new_client(void)
247 {
248 return (new controld());
249 }
250
251 bool controld::IsShutdown(void)
252 {
253 return this->thread_terminate;
254 }
255
256 int controld::Shutdown(void)
257 {
258 base_active nntpClient;
259
260 if (this->thread_terminate)
261 return 1;
262
263 this->thread_terminate = true;
264 nntpClient.configure(this->innd_id,this->innd_name,this->innd_server,this->innd_uid,this->w_address,0,this->w_conn_str);
265 nntpClient.s_connect((strlen(this->hostaddr)>0 ? this->hostaddr : "127.0.0.1"),this->port);
266 nntpClient.s_close();
267
268 return 0;
269 }
270
271 int controld::Startup(void)
272 {
273 if (!this->thread_terminate)
274 return 1;
275
276 this->thread_terminate = false;
277
278 return 0;
279 }
280
281 int controld::db_env_init(void)
282 {
283 return 0;
284 }
285
286 int controld::load_conf(const char* conf_file)
287 {
288 // Innd settings
289 char innd_id[50] = "";
290 char innd_name[50] = "";
291 char innd_server[256] = "";
292 unsigned long innd_uid = 0;
293 char innd_address[50] = "";
294 unsigned int bbsd_port = 0;
295 unsigned int innd_port = 0;
296 unsigned int ctrl_port = 0;
297 char conn_str[256] = "";
298 unsigned int if_startup = 0;
299
300 // Load configuration
301 try
302 {
303 char c_name[256];
304 FILE * fin;
305
306 if ((fin = fopen(conf_file,"r")) == NULL)
307 {
308 syslog << logfile::log_head << "Open innd.conf failed" << endl;
309 return -1;
310 }
311
312 while(fscanf(fin,"%s",c_name) != EOF)
313 {
314 if (c_name[0] == '#')
315 continue;
316 fscanf(fin,"%*c");
317 strlwr(c_name);
318 if (strcmp(c_name,"innd_id")==0)
319 {
320 fscanf(fin,"%s",innd_id);
321 }
322 if (strcmp(c_name,"innd_name")==0)
323 {
324 fscanf(fin,"%s",innd_name);
325 }
326 if (strcmp(c_name,"innd_server")==0)
327 {
328 fscanf(fin,"%s",innd_server);
329 }
330 if (strcmp(c_name,"innd_uid")==0)
331 {
332 fscanf(fin,"%ul",&innd_uid);
333 }
334 if (strcmp(c_name,"innd_address")==0)
335 {
336 fscanf(fin,"%s",innd_address);
337 }
338 if (strcmp(c_name,"bbsd_port")==0)
339 {
340 fscanf(fin,"%ud",&bbsd_port);
341 }
342 if (strcmp(c_name,"innd_port")==0)
343 {
344 fscanf(fin,"%ud",&innd_port);
345 }
346 if (strcmp(c_name,"ctrl_port")==0)
347 {
348 fscanf(fin,"%ud",&ctrl_port);
349 }
350 if (strcmp(c_name,"bbsd_startup")==0)
351 {
352 fscanf(fin,"%ud",&if_startup);
353 this->bbsd_startup = (if_startup > 0);
354 }
355 if (strcmp(c_name,"innd_startup")==0)
356 {
357 fscanf(fin,"%ud",&if_startup);
358 this->innd_startup = (if_startup > 0);
359 }
360 if (strcmp(c_name,"outd_startup")==0)
361 {
362 fscanf(fin,"%ud",&if_startup);
363 this->outd_startup = (if_startup > 0);
364 }
365 if (strcmp(c_name,"startup_delay")==0)
366 {
367 fscanf(fin,"%ud",&(this->startup_delay));
368 }
369 if (strcmp(c_name,"conn_str")==0)
370 {
371 fgets(conn_str,255,fin);
372 }
373 }
374
375 fclose(fin);
376 }
377 catch(CException* e)
378 {
379 syslog << logfile::log_head << "Load configuration failed" << endl;
380 e->Delete();
381 return -1;
382 }
383
384 this->configure(innd_id,innd_name,innd_server,innd_uid,innd_address,ctrl_port,conn_str);
385 this->bbsd.configure(innd_id,innd_name,innd_server,innd_uid,innd_address,bbsd_port,conn_str);
386 this->innd.configure(innd_id,innd_name,innd_server,innd_uid,innd_address,innd_port,conn_str);
387 this->outd.configure(innd_id,innd_name,innd_server,innd_uid,innd_address,0,conn_str);
388
389 return 0;
390 }
391
392 void controld::StartupChild(void)
393 {
394 if (this->bbsd_startup)
395 {
396 if (this->bbsd.begin() == 0)
397 {
398 syslog << logfile::log_head << "Begin BBSD ... OK" << endl;
399 }
400 else
401 {
402 syslog << logfile::log_head << "Begin BBSD ... Failed" << endl;
403 }
404 }
405
406 if (this->innd_startup)
407 {
408 if (this->innd.begin() == 0)
409 {
410 syslog << logfile::log_head << "Begin INND ... OK" << endl;
411 }
412 else
413 {
414 syslog << logfile::log_head << "Begin INND ... Failed" << endl;
415 }
416 }
417
418 if (this->outd_startup)
419 {
420 if (this->outd.begin() == 0)
421 {
422 syslog << logfile::log_head << "Begin OUTD ... OK" << endl;
423 }
424 else
425 {
426 syslog << logfile::log_head << "Begin OUTD ... Failed" << endl;
427 }
428 }
429 }
430
431 void controld::StartupDelay(void)
432 {
433 syslog << logfile::log_head << "Startup delay for " <<
434 this->startup_delay << " seconds"<< endl;
435
436 Sleep(this->startup_delay * 1000);
437 }
438
439 void controld::ShutdownChild(void)
440 {
441 if (this->bbsd.IsRunning())
442 {
443 if (this->bbsd.end() == 0)
444 {
445 syslog << logfile::log_head << "End BBSD ... OK" << endl;
446 }
447 else
448 {
449 syslog << logfile::log_head << "End BBSD ... Failed" << endl;
450 }
451 }
452
453 if (this->innd.IsRunning())
454 {
455 if (this->innd.end() == 0)
456 {
457 syslog << logfile::log_head << "End INND ... OK" << endl;
458 }
459 else
460 {
461 syslog << logfile::log_head << "End INND ... Failed" << endl;
462 }
463 }
464
465 if (this->outd.IsRunning())
466 {
467 if (this->outd.end() == 0)
468 {
469 syslog << logfile::log_head << "End OUTD ... OK" << endl;
470 }
471 else
472 {
473 syslog << logfile::log_head << "End OUTD ... Failed" << endl;
474 }
475 }
476 }
477
478 int controld::InitThreadPool(void)
479 {
480 base* p_ParentThread;
481
482 p_ParentThread = new controld();
483
484 this->SetParentThread(p_ParentThread);
485 this->bbsd.SetParentThread(p_ParentThread);
486 this->innd.SetParentThread(p_ParentThread);
487 this->outd.SetParentThread(p_ParentThread);
488
489 p_ParentThread->SetThreadPool(new thread_pool(TS_MAX_THREAD));
490
491 if (p_ParentThread->GetThreadPool()->GetLastError() != 0)
492 {
493 syslog << logfile::log_head << "Create Thread Pool ... failed" << endl;
494 }
495
496 return 0;
497 }
498
499 int controld::CleanupThreadPool(void)
500 {
501 delete(this->GetParentThread());
502
503 return 0;
504 }
505
506 bool controld::check_priv(void)
507 {
508 return this->access.control;
509 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1