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

Annotation of /innwebd/controld.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (hide annotations)
Fri Apr 11 17:14:49 2008 UTC (17 years, 11 months ago) by sysadm
Branch: MAIN
CVS Tags: HEAD
Changes since 1.13: +8 -8 lines
Content type: text/x-c++src
Update copyright
Add extra log for exception

1 sysadm 1.1 /*******************************************************/
2     /* */
3 sysadm 1.6 /* LeafOK Innbbsd */
4 sysadm 1.1 /* */
5 sysadm 1.14 /* Copyright (C) LeafOK.com, 2003-2008 */
6 sysadm 1.1 /* */
7 sysadm 1.14 /* http://www.leafok.com */
8 sysadm 1.1 /* */
9     /*******************************************************/
10    
11     #include "StdAfx.h"
12     #include ".\controld.h"
13 sysadm 1.3 #include ".\base_active.h"
14 sysadm 1.1 #include ".\App_common.h"
15    
16     using namespace std;
17    
18     bool controld::thread_terminate = true;
19    
20     controld::controld(void)
21     :bbsd_startup(false),innd_startup(false),outd_startup(false),startup_delay(0)
22     {
23     }
24    
25     controld::~controld(void)
26     {
27     }
28    
29     int controld::Accept(void)
30     {
31     CString out,cmd;
32     char temp[256];
33     int result;
34    
35     try
36     {
37     out.Format("200 %s ready.",App_common::GetVersion());
38     this->s_send(out);
39     while(!this->thread_terminate)
40     {
41 sysadm 1.6 this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WAITING);
42 sysadm 1.4
43 sysadm 1.1 if (this->s_receive(out) == 0) //Connection closed
44     break;
45     out.Trim();
46    
47 sysadm 1.6 this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING);
48 sysadm 1.4
49 sysadm 1.1 syslog << logfile::log_head << "Cmd: " << out << endl;
50    
51     if (sscanf(out,"%20s",temp) ==1)
52     cmd = strupr(temp);
53     else
54     cmd = "";
55    
56     if (cmd == "") //Blank command
57     continue;
58    
59     if (cmd == "START")
60     {
61     if (sscanf(out,"%*s %20s",temp) ==1)
62     cmd = strupr(temp);
63     else
64     cmd = "";
65     cmd.Trim();
66    
67     if (cmd == "BBSD" || cmd == "INND" || cmd == "OUTD")
68     {
69     if (this->access.control)
70     {
71     out.Format("Starting %s ... ",cmd);
72     this->s_send(out,false);
73    
74     if (cmd == "BBSD")
75     {
76 sysadm 1.4 result = ((controld*)(this->GetParentThread()))->bbsd.begin();
77 sysadm 1.1 }
78     if (cmd == "INND")
79     {
80 sysadm 1.4 result = ((controld*)(this->GetParentThread()))->innd.begin();
81 sysadm 1.1 }
82     if (cmd == "OUTD")
83     {
84 sysadm 1.4 result = ((controld*)(this->GetParentThread()))->outd.begin();
85 sysadm 1.1 }
86    
87     this->s_send(result == 0 ? "OK" : "Failed");
88 sysadm 1.13
89     syslog << logfile::log_head << "Begin " << cmd << " ... " << (result == 0 ? "OK" : "Failed") << endl;
90 sysadm 1.1 }
91     else
92     {
93     this->s_send("400 Forbidden");
94     }
95     }
96     else
97     {
98     this->s_send("501 service");
99     }
100     continue;
101     }
102    
103     if (cmd == "STOP")
104     {
105     if (sscanf(out,"%*s %20s",temp) ==1)
106     cmd = strupr(temp);
107     else
108     cmd = "";
109     cmd.Trim();
110    
111     if (cmd == "BBSD" || cmd == "INND" || cmd == "OUTD")
112     {
113     if (this->access.control)
114     {
115     out.Format("Stopping %s ... ",cmd);
116     this->s_send(out,false);
117    
118     if (cmd == "BBSD")
119     {
120 sysadm 1.4 result = ((controld*)(this->GetParentThread()))->bbsd.end();
121 sysadm 1.1 }
122     if (cmd == "INND")
123     {
124 sysadm 1.4 result = ((controld*)(this->GetParentThread()))->innd.end();
125 sysadm 1.1 }
126     if (cmd == "OUTD")
127     {
128 sysadm 1.4 result = ((controld*)(this->GetParentThread()))->outd.end();
129 sysadm 1.1 }
130    
131     this->s_send(result == 0 ? "OK" : "Failed");
132 sysadm 1.13
133     syslog << logfile::log_head << "End " << cmd << " ... " << (result == 0 ? "OK" : "Failed") << endl;
134 sysadm 1.1 }
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 sysadm 1.14 char strErrMsg[1024];
235     e->GetErrorMessage(strErrMsg,1024);
236     syslog << logfile::log_head << "Error in accept() [" << strErrMsg << "]" << endl;
237 sysadm 1.1 e->Delete();
238     return -1;
239     }
240    
241     return 0;
242     }
243    
244     controld* controld::new_client(void)
245     {
246     return (new controld());
247     }
248    
249     bool controld::IsShutdown(void)
250     {
251     return this->thread_terminate;
252     }
253    
254     int controld::Shutdown(void)
255     {
256 sysadm 1.3 base_active nntpClient;
257 sysadm 1.1
258     if (this->thread_terminate)
259     return 1;
260    
261     this->thread_terminate = true;
262     nntpClient.configure(this->innd_id,this->innd_name,this->innd_server,this->innd_uid,this->w_address,0,this->w_conn_str);
263     nntpClient.s_connect((strlen(this->hostaddr)>0 ? this->hostaddr : "127.0.0.1"),this->port);
264     nntpClient.s_close();
265    
266     return 0;
267     }
268    
269     int controld::Startup(void)
270     {
271     if (!this->thread_terminate)
272     return 1;
273    
274     this->thread_terminate = false;
275    
276     return 0;
277     }
278    
279     int controld::db_env_init(void)
280     {
281     return 0;
282     }
283    
284     int controld::load_conf(const char* conf_file)
285     {
286     // Innd settings
287     char innd_id[50] = "";
288     char innd_name[50] = "";
289     char innd_server[256] = "";
290     unsigned long innd_uid = 0;
291     char innd_address[50] = "";
292     unsigned int bbsd_port = 0;
293     unsigned int innd_port = 0;
294     unsigned int ctrl_port = 0;
295     char conn_str[256] = "";
296     unsigned int if_startup = 0;
297    
298     // Load configuration
299     try
300     {
301     char c_name[256];
302     FILE * fin;
303    
304     if ((fin = fopen(conf_file,"r")) == NULL)
305     {
306     syslog << logfile::log_head << "Open innd.conf failed" << endl;
307     return -1;
308     }
309    
310     while(fscanf(fin,"%s",c_name) != EOF)
311     {
312     if (c_name[0] == '#')
313     continue;
314     fscanf(fin,"%*c");
315     strlwr(c_name);
316     if (strcmp(c_name,"innd_id")==0)
317     {
318     fscanf(fin,"%s",innd_id);
319     }
320     if (strcmp(c_name,"innd_name")==0)
321     {
322     fscanf(fin,"%s",innd_name);
323     }
324     if (strcmp(c_name,"innd_server")==0)
325     {
326     fscanf(fin,"%s",innd_server);
327     }
328     if (strcmp(c_name,"innd_uid")==0)
329     {
330     fscanf(fin,"%ul",&innd_uid);
331     }
332     if (strcmp(c_name,"innd_address")==0)
333     {
334     fscanf(fin,"%s",innd_address);
335     }
336     if (strcmp(c_name,"bbsd_port")==0)
337     {
338     fscanf(fin,"%ud",&bbsd_port);
339     }
340     if (strcmp(c_name,"innd_port")==0)
341     {
342     fscanf(fin,"%ud",&innd_port);
343     }
344     if (strcmp(c_name,"ctrl_port")==0)
345     {
346     fscanf(fin,"%ud",&ctrl_port);
347     }
348     if (strcmp(c_name,"bbsd_startup")==0)
349     {
350     fscanf(fin,"%ud",&if_startup);
351     this->bbsd_startup = (if_startup > 0);
352     }
353     if (strcmp(c_name,"innd_startup")==0)
354     {
355     fscanf(fin,"%ud",&if_startup);
356     this->innd_startup = (if_startup > 0);
357     }
358     if (strcmp(c_name,"outd_startup")==0)
359     {
360     fscanf(fin,"%ud",&if_startup);
361     this->outd_startup = (if_startup > 0);
362     }
363     if (strcmp(c_name,"startup_delay")==0)
364     {
365     fscanf(fin,"%ud",&(this->startup_delay));
366     }
367     if (strcmp(c_name,"conn_str")==0)
368     {
369     fgets(conn_str,255,fin);
370     }
371     }
372    
373     fclose(fin);
374     }
375     catch(CException* e)
376     {
377 sysadm 1.14 char strErrMsg[1024];
378     e->GetErrorMessage(strErrMsg,1024);
379     syslog << logfile::log_head << "Load configuration failed [" << strErrMsg << "]" << endl;
380 sysadm 1.1 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