From c58aff265813ddb6633716ab407611c1fcd6d97f Mon Sep 17 00:00:00 2001 From: lotodore Date: Sun, 10 Feb 2008 12:38:12 +0000 Subject: [PATCH] Store server pid in file, location can be changed by. First try, needs some cleanup work. Might require additional link options on Linux (new dependency: boost::program_options). --- src/config/configfile.cpp | 146 +++++++++++++++++--------------------- src/config/configfile.h | 3 +- src/pokerth.cpp | 2 +- src/pokerth_server.cpp | 59 ++++++++++++++- 4 files changed, 125 insertions(+), 85 deletions(-) diff --git a/src/config/configfile.cpp b/src/config/configfile.cpp index efd2932f..2fe27827 100644 --- a/src/config/configfile.cpp +++ b/src/config/configfile.cpp @@ -39,111 +39,97 @@ using namespace std; -ConfigFile::ConfigFile(int argc, char **argv) : noWriteAccess(0) +ConfigFile::ConfigFile(char *argv0, bool readonly) : noWriteAccess(readonly) { - myArgv0 = argv[0]; - int i; + myArgv0 = argv0; myQtToolsInterface = CreateQtToolsWrapper(); - for (i=1; igetDataPathStdString(argv[0]))); + configList.push_back(ConfigInfo("AppDataDir", CONFIG_TYPE_STRING, myQtToolsInterface->getDataPathStdString(myArgv0))); configList.push_back(ConfigInfo("Language", CONFIG_TYPE_INT, myQtToolsInterface->getDefaultLanguage())); configList.push_back(ConfigInfo("InternetServerAddressIRCChannelUpdateDone", CONFIG_TYPE_INT, "1")); //HACK configList.push_back(ConfigInfo("ShowLeftToolBox", CONFIG_TYPE_INT, "1")); @@ -241,7 +227,7 @@ ConfigFile::ConfigFile(int argc, char **argv) : noWriteAccess(0) configList.push_back(ConfigInfo("LogInterval", CONFIG_TYPE_INT, "0")); configList.push_back(ConfigInfo("UserDataDir", CONFIG_TYPE_STRING, dataDir)); configList.push_back(ConfigInfo("CacheDir", CONFIG_TYPE_STRING, cacheDir)); - configList.push_back(ConfigInfo("CLA_NoWriteAccess", CONFIG_TYPE_INT, claNoWriteAccess)); + configList.push_back(ConfigInfo("CLA_NoWriteAccess", CONFIG_TYPE_INT, "0")); configList.push_back(ConfigInfo("DisableBackToLobbyWarning", CONFIG_TYPE_INT, "0")); //fill tempList firstTime @@ -272,7 +258,7 @@ ConfigFile::ConfigFile(int argc, char **argv) : noWriteAccess(0) if (tmpStr) tempAppDataPath = tmpStr; } - if (tempRevision < configRev || tempAppDataPath != myQtToolsInterface->getDataPathStdString(argv[0]) ) { /*löschen()*/ + if (tempRevision < configRev || tempAppDataPath != myQtToolsInterface->getDataPathStdString(myArgv0) ) { /*löschen()*/ myConfigState = OLD; updateConfig(myConfigState) ; } diff --git a/src/config/configfile.h b/src/config/configfile.h index 83380029..de9ba8c3 100644 --- a/src/config/configfile.h +++ b/src/config/configfile.h @@ -33,7 +33,7 @@ class QtToolsInterface; class ConfigFile{ public: - ConfigFile(int, char **); + ConfigFile(char *argv0, bool readonly); ~ConfigFile(); @@ -75,7 +75,6 @@ private: bool noWriteAccess; std::string logOnOffDefault; - std::string claNoWriteAccess; ConfigState myConfigState; QtToolsInterface *myQtToolsInterface; diff --git a/src/pokerth.cpp b/src/pokerth.cpp index c4528dc8..348de52b 100755 --- a/src/pokerth.cpp +++ b/src/pokerth.cpp @@ -80,7 +80,7 @@ int main( int argc, char **argv ) QApplication a( argc, argv ); //create defaultconfig - ConfigFile *myConfig = new ConfigFile(argc, argv); + ConfigFile *myConfig = new ConfigFile(argv[0], false); // set PlastiqueStyle even for mac-version to prevent artefacts on styled widgets a.setStyle(new QPlastiqueStyle); diff --git a/src/pokerth_server.cpp b/src/pokerth_server.cpp index 53b13904..0700eb7b 100644 --- a/src/pokerth_server.cpp +++ b/src/pokerth_server.cpp @@ -24,8 +24,11 @@ #include #include #include +#include #include #include +#include +#include #include #include @@ -49,6 +52,8 @@ #endif using namespace std; +namespace po = boost::program_options; +using namespace boost::filesystem; volatile int g_pokerthTerminate = 0; @@ -59,7 +64,9 @@ TerminateHandler(int /*signal*/) } // TODO: Hack -#ifndef _WIN32 +#ifdef _WIN32 + #include +#else #include #ifndef daemon int daemon(int, int); @@ -73,9 +80,42 @@ main(int argc, char *argv[]) // _CrtSetBreakAlloc(4772); + bool readonlyConfig = false; + string pidFile; + { + // Check command line options. + po::options_description desc("Allowed options"); + desc.add_options() + ("help,h", "produce help message") + ("version,v", "print version string") + ("pid-file,p", po::value(), "create pid-file in different location") + ("readonly-config", "treat config file as read-only") + ; + + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + if (vm.count("help")) + { + cout << desc << endl; + return 1; + } + if (vm.count("version")) + { + cout << "PokerTH server version " << POKERTH_BETA_RELEASE_STRING << endl + << "Network protocol version " << NET_VERSION_MAJOR << "." << NET_VERSION_MINOR << endl; + return 1; + } + if (vm.count("pid-file")) + pidFile = vm["pid-file"].as(); + if (vm.count("readonly-config")) + readonlyConfig = true; + } + auto_ptr myQtToolsInterface(CreateQtToolsWrapper()); //create defaultconfig - ConfigFile *myConfig = new ConfigFile(argc, argv); + ConfigFile *myConfig = new ConfigFile(argv[0], readonlyConfig); loghelper_init(myQtToolsInterface->stringFromUtf8(myConfig->readConfigString("LogDir"))); // TODO: Hack @@ -91,6 +131,21 @@ main(int argc, char *argv[]) LOG_MSG("Starting PokerTH dedicated server. Availability: IPv6 " << socket_has_ipv6() << ", SCTP " << socket_has_sctp() << ", Dual Stack " << socket_has_dual_stack() << "."); + // Store pid in file. + if (pidFile.empty()) + { + path tmpPidPath(myConfig->readConfigString("LogDir")); + tmpPidPath /= "pokerth.pid"; + pidFile = tmpPidPath.directory_string(); + } + { + ofstream pidStream(pidFile.c_str(), ios_base::out | ios_base::trunc); + if (!pidStream.fail()) + pidStream << getpid(); + else + LOG_ERROR("Could not create process id file \"" << pidFile << "\"!"); + } + // Create pseudo Gui Wrapper for the server. boost::shared_ptr myServerGuiInterface(new ServerGuiWrapper(myConfig, NULL, NULL, NULL)); boost::shared_ptr session(new Session(myServerGuiInterface.get(), myConfig));