version 0.3.0
[fms.git] / src / fmsapp.cpp
diff --git a/src/fmsapp.cpp b/src/fmsapp.cpp
new file mode 100644 (file)
index 0000000..810bd5f
--- /dev/null
@@ -0,0 +1,157 @@
+#include "../include/fmsapp.h"\r
+#include "../include/global.h"\r
+#include "../include/dbsetup.h"\r
+#include "../include/optionssetup.h"\r
+#include "../include/option.h"\r
+#include "../include/stringfunctions.h"\r
+#include "../include/http/httpthread.h"\r
+#include "../include/nntp/nntplistener.h"\r
+#include "../include/dbmaintenancethread.h"\r
+#include "../include/freenet/freenetmasterthread.h"\r
+#include "../include/threadwrapper/threadedexecutor.h"\r
+\r
+#include <Poco/Util/HelpFormatter.h>\r
+#include <Poco/FileChannel.h>\r
+#include <Poco/FormattingChannel.h>\r
+#include <Poco/PatternFormatter.h>\r
+#include <iostream>\r
+\r
+#ifdef _WIN32\r
+       #include <direct.h>\r
+#else\r
+       #include <unistd.h>\r
+#endif\r
+\r
+//debug\r
+#include <Poco/ScopedLock.h>\r
+#include <Poco/Runnable.h>\r
+#include <Poco/Thread.h>\r
+#include <Poco/ThreadPool.h>\r
+#include "../include/threadwrapper/cancelablerunnable.h"\r
+#include "../include/threadwrapper/cancelablethread.h"\r
+\r
+Poco::FastMutex m1;\r
+class TestRunner:public CancelableRunnable\r
+{\r
+public:\r
+       void run()\r
+       {\r
+               do\r
+               {\r
+\r
+               }while(!IsCancelled());\r
+       }\r
+};\r
+\r
+FMSApp::FMSApp():m_displayhelp(false)\r
+{\r
+\r
+}\r
+\r
+void FMSApp::defineOptions(Poco::Util::OptionSet &options)\r
+{\r
+       ServerApplication::defineOptions(options);\r
+\r
+       // add command line options here\r
+       options.addOption(Poco::Util::Option("help","?","display help for command line arguments",false).repeatable(false).callback(Poco::Util::OptionCallback<FMSApp>(this,&FMSApp::handleHelp)));\r
+}\r
+\r
+void FMSApp::displayHelp()\r
+{\r
+       Poco::Util::HelpFormatter helpFormatter(options());\r
+       helpFormatter.setCommand(commandName());\r
+       helpFormatter.setUsage("OPTIONS");\r
+       helpFormatter.setHeader("The Freenet Message System.");\r
+       helpFormatter.format(std::cout);\r
+}\r
+\r
+void FMSApp::handleHelp(const std::string &name, const std::string &value)\r
+{\r
+       m_displayhelp=true;\r
+       displayHelp();\r
+       stopOptionsProcessing();\r
+}\r
+\r
+void FMSApp::initialize(Poco::Util::Application &self)\r
+{\r
+       ServerApplication::initialize(self);\r
+\r
+       // set working directory to program directory\r
+       int rval=chdir(config().getString("application.dir").c_str());\r
+\r
+       SetupDB();\r
+       SetupDefaultOptions();\r
+       initializeLogger();\r
+       config().setString("application.logger","logfile");\r
+}\r
+\r
+void FMSApp::initializeLogger()\r
+{\r
+       int initiallevel=Poco::Message::PRIO_TRACE;\r
+\r
+       std::string tempval="";\r
+       if(Option::Instance()->Get("LogLevel",tempval))\r
+       {\r
+               StringFunctions::Convert(tempval,initiallevel);\r
+       }\r
+\r
+       Poco::AutoPtr<Poco::FormattingChannel> formatter=new Poco::FormattingChannel(new Poco::PatternFormatter("%Y-%m-%d %H:%M:%S | %p | %t"));\r
+       Poco::AutoPtr<Poco::FileChannel> fc=new Poco::FileChannel("fms.log");\r
+       fc->setProperty("rotation","daily");    // rotate log file daily\r
+       fc->setProperty("times","utc");                 // utc date/times for log entries\r
+       fc->setProperty("archive","timestamp"); // add timestamp to old logs\r
+       fc->setProperty("purgeCount","30");             // purge old logs after 30 logs have accumulated\r
+       fc->setProperty("compress","true");             // gz compress old log files\r
+       formatter->setChannel(fc);\r
+       \r
+       setLogger(Poco::Logger::create("logfile",formatter,initiallevel));\r
+}\r
+\r
+int FMSApp::main(const std::vector<std::string> &args)\r
+{\r
+\r
+       if(m_displayhelp==false)\r
+       {\r
+               logger().information("FMS startup v"FMS_VERSION);\r
+\r
+               StartThreads();\r
+\r
+               waitForTerminationRequest();\r
+\r
+               m_threads.Cancel();\r
+               m_threads.Join();\r
+\r
+               logger().information("FMS shutdown");\r
+       }\r
+\r
+       return FMSApp::EXIT_OK;\r
+}\r
+\r
+void FMSApp::StartThreads()\r
+{\r
+       std::string tempval="";\r
+\r
+       // always start the DB maintenance thread\r
+       m_threads.Start(new DBMaintenanceThread());\r
+\r
+       Option::Instance()->Get("StartHTTP",tempval);\r
+       if(tempval=="true")\r
+       {\r
+               m_threads.Start(new HTTPThread());\r
+       }\r
+\r
+       tempval="";\r
+       Option::Instance()->Get("StartNNTP",tempval);\r
+       if(tempval=="true")\r
+       {\r
+               m_threads.Start(new NNTPListener());\r
+       }\r
+\r
+       tempval="";\r
+       Option::Instance()->Get("StartFreenetUpdater",tempval);\r
+       if(tempval=="true")\r
+       {\r
+               m_threads.Start(new FreenetMasterThread());\r
+       }\r
+\r
+}\r