version 0.3.0
[fms.git] / src / fmsapp.cpp
1 #include "../include/fmsapp.h"\r
2 #include "../include/global.h"\r
3 #include "../include/dbsetup.h"\r
4 #include "../include/optionssetup.h"\r
5 #include "../include/option.h"\r
6 #include "../include/stringfunctions.h"\r
7 #include "../include/http/httpthread.h"\r
8 #include "../include/nntp/nntplistener.h"\r
9 #include "../include/dbmaintenancethread.h"\r
10 #include "../include/freenet/freenetmasterthread.h"\r
11 #include "../include/threadwrapper/threadedexecutor.h"\r
12 \r
13 #include <Poco/Util/HelpFormatter.h>\r
14 #include <Poco/FileChannel.h>\r
15 #include <Poco/FormattingChannel.h>\r
16 #include <Poco/PatternFormatter.h>\r
17 #include <iostream>\r
18 \r
19 #ifdef _WIN32\r
20         #include <direct.h>\r
21 #else\r
22         #include <unistd.h>\r
23 #endif\r
24 \r
25 //debug\r
26 #include <Poco/ScopedLock.h>\r
27 #include <Poco/Runnable.h>\r
28 #include <Poco/Thread.h>\r
29 #include <Poco/ThreadPool.h>\r
30 #include "../include/threadwrapper/cancelablerunnable.h"\r
31 #include "../include/threadwrapper/cancelablethread.h"\r
32 \r
33 Poco::FastMutex m1;\r
34 class TestRunner:public CancelableRunnable\r
35 {\r
36 public:\r
37         void run()\r
38         {\r
39                 do\r
40                 {\r
41 \r
42                 }while(!IsCancelled());\r
43         }\r
44 };\r
45 \r
46 FMSApp::FMSApp():m_displayhelp(false)\r
47 {\r
48 \r
49 }\r
50 \r
51 void FMSApp::defineOptions(Poco::Util::OptionSet &options)\r
52 {\r
53         ServerApplication::defineOptions(options);\r
54 \r
55         // add command line options here\r
56         options.addOption(Poco::Util::Option("help","?","display help for command line arguments",false).repeatable(false).callback(Poco::Util::OptionCallback<FMSApp>(this,&FMSApp::handleHelp)));\r
57 }\r
58 \r
59 void FMSApp::displayHelp()\r
60 {\r
61         Poco::Util::HelpFormatter helpFormatter(options());\r
62         helpFormatter.setCommand(commandName());\r
63         helpFormatter.setUsage("OPTIONS");\r
64         helpFormatter.setHeader("The Freenet Message System.");\r
65         helpFormatter.format(std::cout);\r
66 }\r
67 \r
68 void FMSApp::handleHelp(const std::string &name, const std::string &value)\r
69 {\r
70         m_displayhelp=true;\r
71         displayHelp();\r
72         stopOptionsProcessing();\r
73 }\r
74 \r
75 void FMSApp::initialize(Poco::Util::Application &self)\r
76 {\r
77         ServerApplication::initialize(self);\r
78 \r
79         // set working directory to program directory\r
80         int rval=chdir(config().getString("application.dir").c_str());\r
81 \r
82         SetupDB();\r
83         SetupDefaultOptions();\r
84         initializeLogger();\r
85         config().setString("application.logger","logfile");\r
86 }\r
87 \r
88 void FMSApp::initializeLogger()\r
89 {\r
90         int initiallevel=Poco::Message::PRIO_TRACE;\r
91 \r
92         std::string tempval="";\r
93         if(Option::Instance()->Get("LogLevel",tempval))\r
94         {\r
95                 StringFunctions::Convert(tempval,initiallevel);\r
96         }\r
97 \r
98         Poco::AutoPtr<Poco::FormattingChannel> formatter=new Poco::FormattingChannel(new Poco::PatternFormatter("%Y-%m-%d %H:%M:%S | %p | %t"));\r
99         Poco::AutoPtr<Poco::FileChannel> fc=new Poco::FileChannel("fms.log");\r
100         fc->setProperty("rotation","daily");    // rotate log file daily\r
101         fc->setProperty("times","utc");                 // utc date/times for log entries\r
102         fc->setProperty("archive","timestamp"); // add timestamp to old logs\r
103         fc->setProperty("purgeCount","30");             // purge old logs after 30 logs have accumulated\r
104         fc->setProperty("compress","true");             // gz compress old log files\r
105         formatter->setChannel(fc);\r
106         \r
107         setLogger(Poco::Logger::create("logfile",formatter,initiallevel));\r
108 }\r
109 \r
110 int FMSApp::main(const std::vector<std::string> &args)\r
111 {\r
112 \r
113         if(m_displayhelp==false)\r
114         {\r
115                 logger().information("FMS startup v"FMS_VERSION);\r
116 \r
117                 StartThreads();\r
118 \r
119                 waitForTerminationRequest();\r
120 \r
121                 m_threads.Cancel();\r
122                 m_threads.Join();\r
123 \r
124                 logger().information("FMS shutdown");\r
125         }\r
126 \r
127         return FMSApp::EXIT_OK;\r
128 }\r
129 \r
130 void FMSApp::StartThreads()\r
131 {\r
132         std::string tempval="";\r
133 \r
134         // always start the DB maintenance thread\r
135         m_threads.Start(new DBMaintenanceThread());\r
136 \r
137         Option::Instance()->Get("StartHTTP",tempval);\r
138         if(tempval=="true")\r
139         {\r
140                 m_threads.Start(new HTTPThread());\r
141         }\r
142 \r
143         tempval="";\r
144         Option::Instance()->Get("StartNNTP",tempval);\r
145         if(tempval=="true")\r
146         {\r
147                 m_threads.Start(new NNTPListener());\r
148         }\r
149 \r
150         tempval="";\r
151         Option::Instance()->Get("StartFreenetUpdater",tempval);\r
152         if(tempval=="true")\r
153         {\r
154                 m_threads.Start(new FreenetMasterThread());\r
155         }\r
156 \r
157 }\r