update limonp

This commit is contained in:
wyy 2013-11-23 06:59:08 -08:00
parent f48445403e
commit 80a4ac0a51
7 changed files with 178 additions and 136 deletions

16
conf/cppjieba_server.conf Normal file
View File

@ -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

View File

@ -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<string, string>::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())
{

View File

@ -1,2 +0,0 @@
FILE(GLOB HEAD_HPP_LIST "*.hpp")
INSTALL(FILES ${HEAD_HPP_LIST} DESTINATION include/CppJieba/Limonp)

82
src/Limonp/Config.hpp Normal file
View File

@ -0,0 +1,82 @@
/************************************
* file enc : utf8
* author : wuyanyi09@gmail.com
************************************/
#ifndef LIMONP_CONFIG_H
#define LIMONP_CONFIG_H
#include <map>
#include <fstream>
#include <iostream>
#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<string> 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<string, string>::const_iterator it = _map.find(key);
if(_map.end() != it)
{
value = it->second;
return true;
}
return false;
}
private:
map<string, string> _map;
private:
friend ostream& operator << (ostream& os, const Config& config);
};
ostream& operator << (ostream& os, const Config& config)
{
return os << config._map;
}
}
#endif

View File

@ -1,7 +1,7 @@
#ifndef LIMONP_MYSQLCLIENT_H
#define LIMONP_MYSQLCLIENT_H
#include <mysql/mysql.h>
#include <mysql.h>
#include <iostream>
#include <vector>
#include <string>

View File

@ -1,107 +0,0 @@
/************************************
* file enc : utf8
* author : wuyanyi09@gmail.com
************************************/
#ifndef LIMONP_CONFIG_H
#define LIMONP_CONFIG_H
#include <map>
#include <fstream>
#include <iostream>
#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<string> 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<string, string>::iterator it = _map.begin(); it != _map.end(); it++)
{
cout<<"("<<it->first<<","<<it->second<<")"<<endl;
}
}
bool get(const string& key, string& value)
{
if(_map.end() != _map.find(key))
{
value = _map[key];
return true;
}
return false;
}
private:
//bool _stripComment(const string& line, string& res)
//{
// res = line;
// string::size_type pos = res.find('#');
// if(string::npos != pos)
// {
// res = res.substr(0, pos);
// }
// trim(res);
//}
private:
map<string, string> _map;
bool _isInit;
};
}
namespace Limonp
{
extern Config gConfig;
}
#endif

View File

@ -4,6 +4,7 @@
#include <ctype.h>
#include <string.h>
#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 <config_file> -k <start|stop>\n", argv[0]);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}