diff --git a/conf/cppjieba_server.conf b/conf/cppjieba_server.conf new file mode 100644 index 0000000..8cff412 --- /dev/null +++ b/conf/cppjieba_server.conf @@ -0,0 +1,16 @@ +# config + +#socket listen port +port=11200 + +#number of thread +thread_num=4 + +#demon pid filepath +pid_file=/var/run/cppjieba_server.pid + +#dict path +dict_path=/usr/share/CppJieba/jieba.dict.utf8 + +#model path +model_path=/usr/share/CppJieba/hmm_model.utf8 diff --git a/src/Limonp/ArgvContext.hpp b/src/Limonp/ArgvContext.hpp index 8be15f1..4838a81 100644 --- a/src/Limonp/ArgvContext.hpp +++ b/src/Limonp/ArgvContext.hpp @@ -43,7 +43,7 @@ namespace Limonp ~ArgvContext(){}; public: friend ostream& operator << (ostream& os, const ArgvContext& args); - string operator [](uint i) + string operator [](uint i) const { if(i < _args.size()) { @@ -51,7 +51,7 @@ namespace Limonp } return ""; } - string operator [](const string& key) + string operator [](const string& key) const { map::const_iterator it = _mpss.find(key); if(it != _mpss.end()) @@ -61,7 +61,7 @@ namespace Limonp return ""; } public: - bool hasKey(const string& key) + bool hasKey(const string& key) const { if(_mpss.find(key) != _mpss.end() || _sset.find(key) != _sset.end()) { diff --git a/src/Limonp/CMakeLists.txt b/src/Limonp/CMakeLists.txt deleted file mode 100644 index e01bc74..0000000 --- a/src/Limonp/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -FILE(GLOB HEAD_HPP_LIST "*.hpp") -INSTALL(FILES ${HEAD_HPP_LIST} DESTINATION include/CppJieba/Limonp) diff --git a/src/Limonp/Config.hpp b/src/Limonp/Config.hpp new file mode 100644 index 0000000..f107b9e --- /dev/null +++ b/src/Limonp/Config.hpp @@ -0,0 +1,82 @@ +/************************************ + * file enc : utf8 + * author : wuyanyi09@gmail.com + ************************************/ +#ifndef LIMONP_CONFIG_H +#define LIMONP_CONFIG_H + + +#include +#include +#include +#include "logger.hpp" +#include "str_functs.hpp" + +namespace Limonp +{ + using namespace std; + class Config + { + public: + bool loadFile(const char * const filePath) + { + ifstream ifs(filePath); + if(!ifs) + { + LogFatal("open file[%s] failed.", filePath); + return false; + } + string line; + vector vecBuf; + uint lineno = 0; + while(getline(ifs, line)) + { + lineno ++; + trim(line); + if(line.empty() || strStartsWith(line, "#")) + { + continue; + } + vecBuf.clear(); + if(!splitStr(line, vecBuf, "=") || 2 != vecBuf.size()) + { + LogFatal("line[%d:%s] is illegal.", lineno, line.c_str()); + return false; + } + string& key = vecBuf[0]; + string& value = vecBuf[1]; + trim(key); + trim(value); + if(_map.end() != _map.find(key)) + { + LogFatal("key[%s] already exists.", key.c_str()); + return false; + } + _map[key] = value; + } + ifs.close(); + return true; + } + bool get(const string& key, string& value) const + { + map::const_iterator it = _map.find(key); + if(_map.end() != it) + { + value = it->second; + return true; + } + return false; + } + private: + map _map; + private: + friend ostream& operator << (ostream& os, const Config& config); + }; + + ostream& operator << (ostream& os, const Config& config) + { + return os << config._map; + } +} + +#endif diff --git a/src/Limonp/MysqlClient.hpp b/src/Limonp/MysqlClient.hpp index 8e9d620..3bd482c 100644 --- a/src/Limonp/MysqlClient.hpp +++ b/src/Limonp/MysqlClient.hpp @@ -1,7 +1,7 @@ #ifndef LIMONP_MYSQLCLIENT_H #define LIMONP_MYSQLCLIENT_H -#include +#include #include #include #include diff --git a/src/Limonp/config.hpp b/src/Limonp/config.hpp deleted file mode 100644 index 3e45e6d..0000000 --- a/src/Limonp/config.hpp +++ /dev/null @@ -1,107 +0,0 @@ -/************************************ - * file enc : utf8 - * author : wuyanyi09@gmail.com - ************************************/ -#ifndef LIMONP_CONFIG_H -#define LIMONP_CONFIG_H - - -#include -#include -#include -#include "logger.hpp" -#include "str_functs.hpp" - -namespace Limonp -{ - using std::map; - using std::string; - using std::cout; - using std::endl; - using std::ifstream; - class Config - { - public: - Config(){_isInit = false;}; - ~Config(){}; - bool init(const string& configFile) - { - if(_isInit) - { - LogFatal("already have been initialized. "); - return false; - } - ifstream ifile(configFile.c_str()); - if(!ifile) - { - LogFatal("open configFile[%s] failed.", configFile.c_str()); - return false; - } - string line, key, value; - vector vecBuf; - while(getline(ifile, line)) - { - //line = _stripComment(line); - if(line.empty()) - { - continue; - } - vecBuf.clear(); - if(!splitStr(line, vecBuf, "=") || 2 != vecBuf.size()) - { - LogFatal("line[%s] is illegal.", line.c_str()); - return false; - } - key = vecBuf[0]; - value = vecBuf[1]; - if(_map.end() != _map.find(key)) - { - LogFatal("key[%s] already exists.", key.c_str()); - return false; - } - _map[key] = value; - } - ifile.close(); - _isInit = true; - return true; - } - void display() - { - for(map::iterator it = _map.begin(); it != _map.end(); it++) - { - cout<<"("<first<<","<second<<")"< _map; - bool _isInit; - - }; -} - -namespace Limonp -{ - extern Config gConfig; -} - -#endif diff --git a/src/server.cpp b/src/server.cpp index c8cd571..b3c39fb 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -4,6 +4,7 @@ #include #include #include "Limonp/ArgvContext.hpp" +#include "Limonp/Config.hpp" #include "Husky/Daemon.h" #include "Husky/ServerFrame.h" #include "MPSegment.h" @@ -13,17 +14,17 @@ using namespace Husky; using namespace CppJieba; -const char * const DEFAULT_DICTPATH = "../dicts/jieba.dict.utf8"; -const char * const DEFAULT_MODELPATH = "../dicts/hmm_model.utf8"; - -class ServerDemo: public IRequestHandler +class ReqHandler: public IRequestHandler { - public: - ServerDemo(){}; - virtual ~ServerDemo(){}; - virtual bool init(){return _segment.init(DEFAULT_DICTPATH, DEFAULT_MODELPATH);}; - virtual bool dispose(){return _segment.dispose();}; - public: + private: + string _dictPath; + string _modelPath; + public: + ReqHandler(const string& dictPath, const string& modelPath): _dictPath(dictPath), _modelPath(modelPath){}; + virtual ~ReqHandler(){}; + virtual bool init(){return _segment.init(_dictPath.c_str(), _modelPath.c_str());}; + virtual bool dispose(){return _segment.dispose();}; + public: virtual bool do_GET(const HttpReqInfo& httpReq, string& strSnd) { string sentence, tmp; @@ -38,26 +39,78 @@ class ServerDemo: public IRequestHandler MixSegment _segment; }; -int main(int argc,char* argv[]) +bool run(int argc, char** argv) { - if(argc != 7) - { - printf("usage: %s -n THREAD_NUMBER -p LISTEN_PORT -k start|stop\n",argv[0]); - return -1; - } + if(argc < 2) + { + return false; + } ArgvContext arg(argc, argv); - unsigned int port = atoi(arg["-p"].c_str()); - unsigned int threadNum = atoi(arg["-n"].c_str()); + if(arg["-c"].empty()) + { + return false; + } + Config conf; + if(!conf.loadFile(arg["-c"].c_str())) + { + return false; + } + unsigned int port = 0; + unsigned int threadNum = 0; + string pidFile; + string dictPath; + string modelPath; + string val; + if(!conf.get("port", val)) + { + LogFatal("conf get port failed."); + return false; + } + port = atoi(val.c_str()); + if(!conf.get("thread_num", val)) + { + LogFatal("conf get thread_num failed."); + return false; + } + threadNum = atoi(val.c_str()); + if(!conf.get("pid_file", pidFile)) + { + LogFatal("conf get pid_file failed."); + return false; + } - ServerDemo s; - Daemon daemon(&s); + if(!conf.get("dict_path", dictPath)) + { + LogFatal("conf get dict_path failed."); + return false; + } + if(!conf.get("model_path", modelPath)) + { + LogFatal("conf get model_path failed."); + return false; + } + + ReqHandler reqHandler(dictPath, modelPath); + ServerFrame sf(port, threadNum, &reqHandler); + Daemon daemon(&sf, pidFile.c_str()); if(arg["-k"] == "start") { - return !daemon.Start(port, threadNum); + return daemon.start(); } - else + else if(arg["-k"] == "stop") { - return !daemon.Stop(); + return daemon.stop(); } + return false; +} + +int main(int argc, char* argv[]) +{ + if(!run(argc, argv)) + { + printf("usage: %s -c -k \n", argv[0]); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; }