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

Annotation of /innwebd/controld.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (hide 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 sysadm 1.1 /*******************************************************/
2     /* */
3 sysadm 1.6 /* LeafOK Innbbsd */
4 sysadm 1.12 /* Copyright (C) LeafOK.com, 2003-2005 */
5 sysadm 1.1 /* */
6     /* Programmed by Leaf */
7 sysadm 1.6 /* E-mail:leaflet@leafok.com QQ:6049044 */
8 sysadm 1.1 /* */
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 sysadm 1.3 #include ".\base_active.h"
18 sysadm 1.1 #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 sysadm 1.6 this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WAITING);
46 sysadm 1.4
47 sysadm 1.1 if (this->s_receive(out) == 0) //Connection closed
48     break;
49     out.Trim();
50    
51 sysadm 1.6 this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING);
52 sysadm 1.4
53 sysadm 1.1 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 sysadm 1.4 result = ((controld*)(this->GetParentThread()))->bbsd.begin();
81 sysadm 1.1 }
82     if (cmd == "INND")
83     {
84 sysadm 1.4 result = ((controld*)(this->GetParentThread()))->innd.begin();
85 sysadm 1.1 }
86     if (cmd == "OUTD")
87     {
88 sysadm 1.4 result = ((controld*)(this->GetParentThread()))->outd.begin();
89 sysadm 1.1 }
90    
91     this->s_send(result == 0 ? "OK" : "Failed");
92 sysadm 1.13
93     syslog << logfile::log_head << "Begin " << cmd << " ... " << (result == 0 ? "OK" : "Failed") << endl;
94 sysadm 1.1 }
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 sysadm 1.4 result = ((controld*)(this->GetParentThread()))->bbsd.end();
125 sysadm 1.1 }
126     if (cmd == "INND")
127     {
128 sysadm 1.4 result = ((controld*)(this->GetParentThread()))->innd.end();
129 sysadm 1.1 }
130     if (cmd == "OUTD")
131     {
132 sysadm 1.4 result = ((controld*)(this->GetParentThread()))->outd.end();
133 sysadm 1.1 }
134    
135     this->s_send(result == 0 ? "OK" : "Failed");
136 sysadm 1.13
137     syslog << logfile::log_head << "End " << cmd << " ... " << (result == 0 ? "OK" : "Failed") << endl;
138 sysadm 1.1 }
139     else
140     {
141     this->s_send("400 Forbidden");
142     }
143     }
144     else
145     {
146 sysadm 1.9 this->s_send("501 service");
147 sysadm 1.1 }
148 sysadm 1.8 continue;
149 sysadm 1.1 }
150    
151     if (cmd == "SHUTDOWN")
152     {
153     if (this->access.control)
154     {
155 sysadm 1.7 this->s_send("Shutting down ... ",false);
156 sysadm 1.1
157 sysadm 1.4 ((controld*)(this->GetParentThread()))->ShutdownChild();
158 sysadm 1.1
159     this->s_send("OK");
160    
161 sysadm 1.4 ((controld*)(this->GetParentThread()))->Shutdown();
162 sysadm 1.1 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 sysadm 1.4 ((controld*)(this->GetParentThread()))->unload_priv();
176     if (((controld*)(this->GetParentThread()))->load_priv() == 0)
177 sysadm 1.1 this->s_send("OK");
178     else
179     this->s_send("Failed");
180    
181     continue;
182     }
183    
184     if (cmd == "ACTIVE")
185     {
186 sysadm 1.6 if (sscanf(out,"%*s %20s",temp) == 1)
187 sysadm 1.2 cmd = strupr(temp);
188 sysadm 1.1 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 sysadm 1.3 base_active nntpClient;
198 sysadm 1.1 nntpClient.configure(this->innd_id,this->innd_name,this->innd_server,this->innd_uid,"",0,this->w_conn_str);
199 sysadm 1.6
200     this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WAITING);
201    
202 sysadm 1.1 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 sysadm 1.6
211     this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING);
212    
213 sysadm 1.1 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 sysadm 1.3 base_active nntpClient;
259 sysadm 1.1
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 sysadm 1.11 syslog << logfile::log_head << "Startup delay for " <<
434     this->startup_delay << " seconds"<< endl;
435    
436 sysadm 1.1 Sleep(this->startup_delay * 1000);
437     }
438    
439     void controld::ShutdownChild(void)
440     {
441 sysadm 1.10 if (this->bbsd.IsRunning())
442 sysadm 1.1 {
443 sysadm 1.10 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 sysadm 1.1 }
476     }
477 sysadm 1.5
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 sysadm 1.11
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