Merge branch 'master' of github.com:aszxqw/cppjieba

This commit is contained in:
wyy 2014-05-20 19:32:44 +08:00
commit 5a7f8fea95
4 changed files with 73 additions and 79 deletions

View File

@ -17,6 +17,7 @@
#include <sys/epoll.h> #include <sys/epoll.h>
#include <fcntl.h> #include <fcntl.h>
#include "HttpReqInfo.hpp" #include "HttpReqInfo.hpp"
#include "Limonp/InitOnOff.hpp"
@ -40,7 +41,7 @@ namespace Husky
virtual bool do_POST(const HttpReqInfo& httpReq, string& res) const = 0; virtual bool do_POST(const HttpReqInfo& httpReq, string& res) const = 0;
}; };
class EpollServer class EpollServer: public InitOnOff
{ {
private: private:
static const size_t LISTEN_QUEUE_LEN = 1024; static const size_t LISTEN_QUEUE_LEN = 1024;
@ -48,38 +49,26 @@ namespace Husky
static const int MAXEPOLLSIZE = 512; static const int MAXEPOLLSIZE = 512;
private: private:
const IRequestHandler* _reqHandler; const IRequestHandler & _reqHandler;
int _host_socket; int _host_socket;
int _epoll_fd; int _epoll_fd;
bool _isShutDown;
int _epollSize; int _epollSize;
unordered_map<int, string> _sockIpMap; unordered_map<int, string> _sockIpMap;
private:
bool _isInited;
bool _getInitFlag() const {return _isInited;}
bool _setInitFlag(bool flag) {return _isInited = flag;}
public: public:
explicit EpollServer(uint port, const IRequestHandler* pHandler): _reqHandler(pHandler), _host_socket(-1), _isShutDown(false), _epollSize(0) explicit EpollServer(size_t port, const IRequestHandler & handler): _reqHandler(handler), _host_socket(-1), _epollSize(0)
{ {
assert(_reqHandler);
_setInitFlag(_init_epoll(port)); _setInitFlag(_init_epoll(port));
}; };
~EpollServer(){};// unfinished; ~EpollServer(){};
public:
operator bool() const
{
return _getInitFlag();
}
public: public:
bool start() bool start()
{ {
//int clientSock;
sockaddr_in clientaddr; sockaddr_in clientaddr;
socklen_t nSize = sizeof(clientaddr); socklen_t nSize = sizeof(clientaddr);
struct epoll_event events[MAXEPOLLSIZE]; struct epoll_event events[MAXEPOLLSIZE];
int nfds, clientSock; int nfds, clientSock;
while(!_isShutDown) while(true)
{ {
if(-1 == (nfds = epoll_wait(_epoll_fd, events, _epollSize, -1))) if(-1 == (nfds = epoll_wait(_epoll_fd, events, _epollSize, -1)))
{ {
@ -105,8 +94,6 @@ namespace Husky
continue; continue;
} }
//LogInfo("connecting from: %d:%d client socket: %d\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port), clientSock);
/* inet_ntoa is not thread safety at some version */ /* inet_ntoa is not thread safety at some version */
//_sockIpMap[clientSock] = inet_ntoa(clientaddr.sin_addr); //_sockIpMap[clientSock] = inet_ntoa(clientaddr.sin_addr);
@ -123,35 +110,6 @@ namespace Husky
} }
return true; return true;
} }
void stop()
{
_isShutDown = true;
if(-1 == close(_host_socket))
{
LogError(strerror(errno));
return;
}
int sockfd;
struct sockaddr_in dest;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
LogError(strerror(errno));
return;
}
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(_host_socket);
if(0 == inet_aton("127.0.0.1", (struct in_addr *) &dest.sin_addr.s_addr))
{
LogError(strerror(errno));
return;
}
if(connect(sockfd, (struct sockaddr *) &dest, sizeof(dest)) < 0)
{
LogError(strerror(errno));
}
_closesocket(sockfd);
}
private: private:
bool _epoll_add(int sockfd, uint32_t events) bool _epoll_add(int sockfd, uint32_t events)
{ {
@ -171,7 +129,8 @@ namespace Husky
_epollSize ++; _epollSize ++;
return true; return true;
} }
bool _response(int sockfd) const
bool _setsockopt(int sockfd) const
{ {
if(-1 == setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&LNG, sizeof(LNG))) if(-1 == setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&LNG, sizeof(LNG)))
{ {
@ -188,8 +147,11 @@ namespace Husky
LogError(strerror(errno)); LogError(strerror(errno));
return false; return false;
} }
return true;
}
string strRec, strSnd, strRetByHandler; bool _receive(int sockfd, string& strRec) const
{
char recvBuf[RECV_BUFFER_SIZE]; char recvBuf[RECV_BUFFER_SIZE];
int nRetCode = -1; int nRetCode = -1;
while(true) while(true)
@ -198,13 +160,13 @@ namespace Husky
nRetCode = recv(sockfd, recvBuf, sizeof(recvBuf) - 1, 0); nRetCode = recv(sockfd, recvBuf, sizeof(recvBuf) - 1, 0);
if(-1 == nRetCode) if(-1 == nRetCode)
{ {
LogDebug(strerror(errno)); LogError(strerror(errno));
return false; return false;
} }
if(0 == nRetCode) if(0 == nRetCode)
{ {
LogDebug("client socket orderly shut down"); LogDebug("client socket orderly shut down");
return false; return true;
} }
strRec += recvBuf; strRec += recvBuf;
if(nRetCode != sizeof(recvBuf) - 1) if(nRetCode != sizeof(recvBuf) - 1)
@ -212,29 +174,52 @@ namespace Husky
break; break;
} }
} }
return true;
}
bool _send(int sockfd, const string& strSnd) const
{
if(-1 == send(sockfd, strSnd.c_str(), strSnd.length(), 0))
{
LogError(strerror(errno));
return false;
}
return true;
}
bool _response(int sockfd) const
{
if(!_setsockopt(sockfd))
{
return false;
}
string strRec, strSnd, strRetByHandler;
if(!_receive(sockfd, strRec))
{
return false;
}
HttpReqInfo httpReq(strRec); HttpReqInfo httpReq(strRec);
if("GET" == httpReq.getMethod() && !_reqHandler->do_GET(httpReq, strRetByHandler)) if("GET" == httpReq.getMethod() && !_reqHandler.do_GET(httpReq, strRetByHandler))
{ {
LogError("do_GET failed."); LogError("do_GET failed.");
return false; return false;
} }
if("POST" == httpReq.getMethod() && !_reqHandler->do_POST(httpReq, strRetByHandler)) if("POST" == httpReq.getMethod() && !_reqHandler.do_POST(httpReq, strRetByHandler))
{ {
LogError("do_POST failed."); LogError("do_POST failed.");
return false; return false;
} }
string_format(strSnd, HTTP_FORMAT, CHARSET_UTF8, strRetByHandler.length(), strRetByHandler.c_str()); string_format(strSnd, HTTP_FORMAT, CHARSET_UTF8, strRetByHandler.length(), strRetByHandler.c_str());
if(-1 == send(sockfd, strSnd.c_str(), strSnd.length(), 0)) if(!_send(sockfd, strSnd))
{ {
LogError(strerror(errno));
return false; return false;
} }
LogInfo("{response:%s, epollsize:%d}", strRetByHandler.c_str(), _epollSize);
LogInfo("response:%s", strRetByHandler.c_str());
return true; return true;
} }
bool _init_epoll(uint port) bool _init_epoll(size_t port)
{ {
_host_socket = socket(AF_INET, SOCK_STREAM, 0); _host_socket = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == _host_socket) if(-1 == _host_socket)
@ -276,7 +261,7 @@ namespace Husky
LogError("_epoll_add(%d, EPOLLIN) failed.", _host_socket); LogError("_epoll_add(%d, EPOLLIN) failed.", _host_socket);
return false; return false;
} }
LogInfo("create socket listening port[%u], epoll{size:%d} init ok", port, _epollSize); LogInfo("create socket listening port[%u], epoll{size:%d} init ok", port, MAXEPOLLSIZE);
return true; return true;
} }
void _closesocket(int sockfd) void _closesocket(int sockfd)

View File

@ -37,10 +37,6 @@ namespace Husky
{ {
buf[0] = sIn[ix]; buf[0] = sIn[ix];
} }
//else if ( isspace( (BYTE)sIn[ix] ) ) //貌似把空格编码成%20或者+都可以
//{
// buf[0] = '+';
//}
else else
{ {
buf[0] = '%'; buf[0] = '%';
@ -151,24 +147,26 @@ namespace Husky
{ {
return _find(_methodGetMap, argKey, res); return _find(_methodGetMap, argKey, res);
} }
//bool POST(const string& argKey, string& res)const
//{
// return _find(_methodPostMap, argKey, res);
//}
const string& getMethod() const const string& getMethod() const
{ {
return _headerMap.find(KEY_METHOD)->second; return _headerMap.find(KEY_METHOD)->second;
} }
const unordered_map<string, string> & getMethodGetMap() const
{
return _methodGetMap;
}
const unordered_map<string, string> & getHeaders() const
{
return _headerMap;
}
const string& getBody() const const string& getBody() const
{ {
return _body; return _body;
} }
private: private:
std::unordered_map<string, string> _headerMap; unordered_map<string, string> _headerMap;
std::unordered_map<string, string> _methodGetMap; unordered_map<string, string> _methodGetMap;
//std::unordered_map<string, string> _methodPostMap;
string _body; string _body;
//public:
friend ostream& operator<<(ostream& os, const HttpReqInfo& obj); friend ostream& operator<<(ostream& os, const HttpReqInfo& obj);
private: private:
bool _find(const std::unordered_map<string, string>& mp, const string& key, string& res)const bool _find(const std::unordered_map<string, string>& mp, const string& key, string& res)const
@ -189,14 +187,14 @@ namespace Husky
return false; return false;
} }
uint pos = url.find('?'); size_t pos = url.find('?');
if(string::npos == pos) if(string::npos == pos)
{ {
return false; return false;
} }
uint kleft = 0, kright = 0; size_t kleft = 0, kright = 0;
uint vleft = 0, vright = 0; size_t vleft = 0, vright = 0;
for(uint i = pos + 1; i < url.size();) for(size_t i = pos + 1; i < url.size();)
{ {
kleft = i; kleft = i;
while(i < url.size() && url[i] != '=') while(i < url.size() && url[i] != '=')

View File

@ -56,7 +56,7 @@ bool run(int argc, char** argv)
{ {
return false; return false;
} }
unsigned int port = 0; size_t port = 0;
string dictPath; string dictPath;
string modelPath; string modelPath;
string val; string val;
@ -79,7 +79,7 @@ bool run(int argc, char** argv)
} }
ReqHandler reqHandler(dictPath, modelPath); ReqHandler reqHandler(dictPath, modelPath);
EpollServer sf(port, &reqHandler); EpollServer sf(port, reqHandler);
return sf.start(); return sf.start();
} }

11
test/testdata/server.conf vendored Normal file
View File

@ -0,0 +1,11 @@
# config
#socket listen port
port=11200
#dict path
dict_path=../dict/jieba.dict.utf8
#model path
model_path=../dict/hmm_model.utf8