X-Git-Url: https://git.pterodactylus.net/?p=fms.git;a=blobdiff_plain;f=src%2Ffmsapp.cpp;h=94443c4cd8440e298bda8018b8b986b4bbea85d4;hp=04562fc3afa06b798b95256108e71b0d38c59b8a;hb=026dc6b2bc548c945359c4e166eff514f2c47c6a;hpb=3751f10f7127ae0905aa0b52dc6de1c782a38c99 diff --git a/src/fmsapp.cpp b/src/fmsapp.cpp index 04562fc..94443c4 100644 --- a/src/fmsapp.cpp +++ b/src/fmsapp.cpp @@ -9,6 +9,7 @@ #include "../include/dbmaintenancethread.h" #include "../include/freenet/freenetmasterthread.h" #include "../include/threadwrapper/threadedexecutor.h" +#include "../include/db/sqlite3db.h" #include #include @@ -23,30 +24,17 @@ #include #endif -//debug -#include -#include -#include -#include -#include "../include/threadwrapper/cancelablerunnable.h" -#include "../include/threadwrapper/cancelablethread.h" - -Poco::FastMutex m1; -class TestRunner:public CancelableRunnable +FMSApp::FMSApp():m_displayhelp(false),m_showoptions(false),m_setoption(false),m_logtype("file"),m_workingdirectory("") { -public: - void run() + // get current working dir so we can go to it later + char wd[1024]; + char *wdptr=NULL; + memset(wd,0,1024); + wdptr=getcwd(wd,1023); + if(wdptr) { - do - { - - }while(!IsCancelled()); + m_workingdirectory=wdptr; } -}; - -FMSApp::FMSApp():m_displayhelp(false),m_logtype("file") -{ - } void FMSApp::defineOptions(Poco::Util::OptionSet &options) @@ -54,8 +42,10 @@ void FMSApp::defineOptions(Poco::Util::OptionSet &options) ServerApplication::defineOptions(options); // add command line options here - options.addOption(Poco::Util::Option("help","?","display help for command line arguments",false).repeatable(false).callback(Poco::Util::OptionCallback(this,&FMSApp::handleHelp))); - options.addOption(Poco::Util::Option("log","l","select type of log output (file|stdout|stderr)",false,"type",true).repeatable(false).callback(Poco::Util::OptionCallback(this,&FMSApp::handleLogOption))); + options.addOption(Poco::Util::Option("help","?","Display help for command line arguments.",false).repeatable(false).callback(Poco::Util::OptionCallback(this,&FMSApp::handleHelp))); + options.addOption(Poco::Util::Option("log","l","Select type of log output (file|stdout|stderr).",false,"type",true).repeatable(false).callback(Poco::Util::OptionCallback(this,&FMSApp::handleLogOption))); + options.addOption(Poco::Util::Option("showoptions","","Show all options that can be set and their current values.",false).repeatable(false).callback(Poco::Util::OptionCallback(this,&FMSApp::handleShowOptions))); + options.addOption(Poco::Util::Option("setoption","","Set an option. Values are not validated, so be sure to set them correctly.",false,"option=value",true).repeatable(true).callback(Poco::Util::OptionCallback(this,&FMSApp::handleSetOption))); } void FMSApp::displayHelp() @@ -82,12 +72,38 @@ void FMSApp::handleLogOption(const std::string &name, const std::string &value) } } +void FMSApp::handleSetOption(const std::string &name, const std::string &value) +{ + std::vector valueparts; + StringFunctions::Split(value,"=",valueparts); + + if(valueparts.size()==2) + { + m_setoptions[valueparts[0]]=valueparts[1]; + } + else + { + std::cout << "Expected option=value but found " << value << std::endl; + } + + m_setoption=true; +} + +void FMSApp::handleShowOptions(const std::string &name, const std::string &value) +{ + m_showoptions=true; +} + void FMSApp::initialize(Poco::Util::Application &self) { ServerApplication::initialize(self); - // set working directory to program directory - int rval=chdir(config().getString("application.dir").c_str()); + // set working directory - fall back on application.dir if working directory isn't set + if(m_workingdirectory=="") + { + m_workingdirectory=config().getString("application.dir"); + } + int rval=chdir(m_workingdirectory.c_str()); SetupDB(); SetupDefaultOptions(); @@ -131,7 +147,9 @@ void FMSApp::initializeLogger() } } - setLogger(Poco::Logger::create("logfile",formatter,initiallevel)); + setLogger(Poco::Logger::create("logfile",formatter,Poco::Message::PRIO_INFORMATION)); + Poco::Logger::get("logfile").information("LogLevel set to "+tempval); + Poco::Logger::get("logfile").setLevel(initiallevel); } int FMSApp::main(const std::vector &args) @@ -139,10 +157,20 @@ int FMSApp::main(const std::vector &args) // running as a daemon would reset the working directory to / before calling main // so we need to set the working directory again - // set working directory to program directory - int rval=chdir(config().getString("application.dir").c_str()); + int rval=chdir(m_workingdirectory.c_str()); - if(m_displayhelp==false) + if(m_displayhelp) + { + } + else if(m_showoptions) + { + showOptions(); + } + else if(m_setoption) + { + setOptions(); + } + else { logger().information("FMS startup v"FMS_VERSION); @@ -155,7 +183,14 @@ int FMSApp::main(const std::vector &args) waitForTerminationRequest(); + if(isInteractive()==true) + { + std::cout << "Please wait while FMS shuts down." << std::endl << std::endl; + } + + logger().trace("FMSApp::main cancelling threads"); m_threads.Cancel(); + logger().trace("FMSApp::main joining threads"); m_threads.Join(); logger().information("FMS shutdown"); @@ -164,31 +199,81 @@ int FMSApp::main(const std::vector &args) return FMSApp::EXIT_OK; } +void FMSApp::setOptions() +{ + for(std::map::iterator i=m_setoptions.begin(); i!=m_setoptions.end(); i++) + { + std::string tempval=""; + if(Option::Instance()->Get((*i).first,tempval)) + { + Option::Instance()->Set((*i).first,(*i).second); + std::cout << "Option " << (*i).first << " set to " << (*i).second << std::endl; + } + else + { + std::cout << "Unknown option " << (*i).first << std::endl; + } + } +} + +void FMSApp::showOptions() +{ + SQLite3DB::Statement st=SQLite3DB::DB::Instance()->Prepare("SELECT Option, OptionValue FROM tblOption;"); + st.Step(); + while(st.RowReturned()) + { + std::string option=""; + std::string optionvalue=""; + st.ResultText(0,option); + st.ResultText(1,optionvalue); + + std::cout << option << " = " << optionvalue << std::endl; + + st.Step(); + } +} + void FMSApp::StartThreads() { std::string tempval=""; // always start the DB maintenance thread + logger().trace("FMSApp::StartThreads starting DBMaintenanceThread"); m_threads.Start(new DBMaintenanceThread()); Option::Instance()->Get("StartHTTP",tempval); if(tempval=="true") { + logger().trace("FMSApp::StartThreads starting HTTPThread"); m_threads.Start(new HTTPThread()); } + else + { + logger().trace("FMSApp::StartThreads not starting HTTPThread"); + } tempval=""; Option::Instance()->Get("StartNNTP",tempval); if(tempval=="true") { + logger().trace("FMSApp::StartThreads starting NNTPListener"); m_threads.Start(new NNTPListener()); } + else + { + logger().trace("FMSApp::StartThreads not starting NNTPListener"); + } tempval=""; Option::Instance()->Get("StartFreenetUpdater",tempval); if(tempval=="true") { + logger().trace("FMSApp::StartThreads starting FreenetMasterThread"); m_threads.Start(new FreenetMasterThread()); } + else + { + logger().trace("FMSApp::StartThreads not starting FreenetMasterThread"); + } }