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

Annotation of /innwebd/controld.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (hide annotations)
Sat Feb 26 16:10:25 2005 UTC (21 years ago) by sysadm
Branch: MAIN
Changes since 1.11: +1 -1 lines
Content type: text/x-c++src
Copyright update

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

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