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

Annotation of /innwebd/controld.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Sat Jul 3 13:56:04 2004 UTC (21 years, 8 months ago) by sysadm
Branch: MAIN
Changes since 1.3: +33 -10 lines
Content type: text/x-c++src
Add thread_pool

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

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