From f48445403ed7a97257f56de5f2bd89680d1ca30e Mon Sep 17 00:00:00 2001 From: wyy Date: Sat, 23 Nov 2013 06:26:43 -0800 Subject: [PATCH] update Husky --- scripts/start.sh | 2 +- src/Husky/Daemon.cpp | 37 ++++++++++++++++--------------------- src/Husky/Daemon.h | 27 +++++++++++++++++++-------- src/Husky/ServerFrame.cpp | 26 +++++++++++++++----------- src/Husky/ServerFrame.h | 19 +++++++++++++------ src/Husky/globals.h | 2 -- 6 files changed, 64 insertions(+), 49 deletions(-) diff --git a/scripts/start.sh b/scripts/start.sh index 5882c46..96f0598 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -1,2 +1,2 @@ #!/bin/sh -./server.demo -n 4 -p 11258 -k start >> run.log 2>&1 & +cjserver -n 4 -p 11258 -k start >> run.log 2>&1 & diff --git a/src/Husky/Daemon.cpp b/src/Husky/Daemon.cpp index d226a66..d8f76b8 100644 --- a/src/Husky/Daemon.cpp +++ b/src/Husky/Daemon.cpp @@ -3,9 +3,9 @@ namespace Husky { - IRequestHandler * Daemon::m_pHandler; - ServerFrame Daemon::m_ServerFrame; + IWorkHandler * Daemon::m_pHandler; int Daemon::m_nChildPid = 0; + const char* Daemon::m_pidFile = NULL; bool Daemon::isAbnormalExit(int pid, int status) { @@ -40,9 +40,9 @@ namespace Husky return bRestart; } - bool Daemon::Start(unsigned int port, unsigned int threadNum) + bool Daemon::start() { - string masterPidStr = loadFile2Str(MASTER_PID_FILE); + string masterPidStr = loadFile2Str(m_pidFile); int masterPid = atoi(masterPidStr.c_str()); if(masterPid) { @@ -57,7 +57,7 @@ namespace Husky char buf[64]; sprintf(buf, "%d", getpid()); - if (!WriteStr2File(MASTER_PID_FILE,buf ,"w")) + if (!WriteStr2File(m_pidFile,buf ,"w")) { LogFatal("Write master pid fail!"); } @@ -80,31 +80,26 @@ namespace Husky LogFatal("m_pHandler init failed!"); return false; } - if (!m_ServerFrame.CreateServer(port, threadNum, m_pHandler)) - { - LogFatal("m_ServerFrame CreateServer(%d, %d, m_pHandler) fail!", port, threadNum); - return false; - } #ifdef DEBUG LogDebug("Worker init ok pid = %d",(int)getpid()); #endif - if (!m_ServerFrame.RunServer()) + if (!m_pHandler->run()) { - LogError("m_ServerFrame.RunServer finish -fail!"); + LogError("m_pHandler run finish with failure!"); return false; } #ifdef DEBUG LogDebug("run finish -ok!"); #endif - if(!m_pHandler->dispose()) - { - LogError("m_pHandler.dispose -fail!"); - return false; - } + //if(!m_pHandler->dispose()) + //{ + // LogError("m_pHandler dispose with failure!"); + // return false; + //} #ifdef DEBUG - LogDebug("Worker dispose -ok!"); + //LogDebug("Worker dispose -ok!"); #endif exit(0); } @@ -122,9 +117,9 @@ namespace Husky } - bool Daemon::Stop() + bool Daemon::stop() { - string masterPidStr = loadFile2Str(MASTER_PID_FILE); + string masterPidStr = loadFile2Str(m_pidFile); int masterPid = atoi(masterPidStr.c_str()); if(masterPid) { @@ -185,7 +180,7 @@ namespace Husky { if (sig == SIGUSR1) { - m_ServerFrame.CloseServer(); + m_pHandler->dispose(); LogDebug("master = %d signal accept current pid =%d!",getppid(),getpid()); } diff --git a/src/Husky/Daemon.h b/src/Husky/Daemon.h index 61506a5..f810588 100644 --- a/src/Husky/Daemon.h +++ b/src/Husky/Daemon.h @@ -9,32 +9,43 @@ #include #include #include "../Limonp/logger.hpp" -#include "ServerFrame.h" namespace Husky { using namespace Limonp; + class IWorkHandler + { + public: + virtual ~IWorkHandler(){} + virtual bool init() = 0; + virtual bool dispose() = 0; + virtual bool run() = 0; + }; + class Daemon { public: - Daemon(IRequestHandler * pHandler) + Daemon(IWorkHandler * workHandler, const char* pidFile) { - m_pHandler = pHandler; + m_pHandler = workHandler; + m_pidFile = pidFile; } ~Daemon(){}; public: - bool Start(unsigned int port, unsigned int threadNum); - bool Stop(); + bool start(); + bool stop(); public: static void initAsDaemon(); static void sigMasterHandler(int sig); static void sigChildHandler(int sig); static bool isAbnormalExit(int pid, int status); private: - static IRequestHandler* m_pHandler; - static ServerFrame m_ServerFrame; - static int m_nChildPid; + //static IRequestHandler* m_pHandler; + //static ServerFrame m_ServerFrame; + static int m_nChildPid; + static IWorkHandler * m_pHandler; + static const char* m_pidFile; }; } #endif diff --git a/src/Husky/ServerFrame.cpp b/src/Husky/ServerFrame.cpp index 3bab9da..0039c7c 100644 --- a/src/Husky/ServerFrame.cpp +++ b/src/Husky/ServerFrame.cpp @@ -6,7 +6,7 @@ namespace Husky pthread_mutex_t ServerFrame::m_pmAccept; bool ServerFrame::m_bShutdown = false; - bool ServerFrame::CloseServer() + bool ServerFrame::dispose() { m_bShutdown=true; if (SOCKET_ERROR==closesocket(m_lsnSock)) @@ -39,12 +39,14 @@ namespace Husky LogError("error [%s]", strerror(errno)); } close(sockfd); - LogInfo("CloseServer ok."); + if(!m_pHandler->dispose()) + { + LogFatal("m_pHandler dispose failed."); + } return true; - } - bool ServerFrame::RunServer() + bool ServerFrame::run() { if(SOCKET_ERROR==listen(m_lsnSock,LISEN_QUEUR_LEN)) { @@ -177,18 +179,21 @@ namespace Husky } - bool ServerFrame::CreateServer(u_short nPort,u_short nThreadCount,IRequestHandler *pHandler) + bool ServerFrame::init() { - m_nLsnPort=nPort; - m_nThreadCount=nThreadCount; - m_pHandler=pHandler; if (!BindToLocalHost(m_lsnSock,m_nLsnPort)) { + LogFatal("BindToLocalHost failed."); + return false; + } + LogInfo("init ok {port:%d, threadNum:%d}", m_nLsnPort, m_nThreadCount); + + if(!m_pHandler->init()) + { + LogFatal("m_pHandler init failed."); return false; } - pthread_mutex_init(&m_pmAccept,NULL); - LogInfo("CreatServer ok {port:%d, threadNum:%d}", nPort, nThreadCount); return true; } @@ -201,7 +206,6 @@ namespace Husky return false; } - /* 使地址马上可以重用 */ int nRet = 1; if(SOCKET_ERROR==setsockopt(m_lsnSock, SOL_SOCKET, SO_REUSEADDR, (char*)&nRet, sizeof(nRet))) { diff --git a/src/Husky/ServerFrame.h b/src/Husky/ServerFrame.h index f467b86..1cb5c43 100644 --- a/src/Husky/ServerFrame.h +++ b/src/Husky/ServerFrame.h @@ -16,6 +16,7 @@ #include "globals.h" #include "ThreadManager.hpp" #include "HttpReqInfo.hpp" +#include "Daemon.h" #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 @@ -47,14 +48,20 @@ namespace Husky IRequestHandler * pHandler; }; - class ServerFrame + class ServerFrame: public IWorkHandler { public: - ServerFrame(){}; - ~ServerFrame(){pthread_mutex_destroy(&m_pmAccept);}; - bool CreateServer(u_short nPort,u_short nThreadCount,IRequestHandler *pHandler); - bool CloseServer(); - bool RunServer(); + ServerFrame(unsigned nPort, unsigned nThreadCount, IRequestHandler* pHandler) + { + m_nLsnPort = nPort; + m_nThreadCount = nThreadCount; + m_pHandler = pHandler; + pthread_mutex_init(&m_pmAccept,NULL); + }; + virtual ~ServerFrame(){pthread_mutex_destroy(&m_pmAccept);}; + virtual bool init(); + virtual bool dispose(); + virtual bool run(); protected: diff --git a/src/Husky/globals.h b/src/Husky/globals.h index 3cf2318..d58f6cc 100644 --- a/src/Husky/globals.h +++ b/src/Husky/globals.h @@ -10,8 +10,6 @@ namespace Husky { - const char* const MASTER_PID_FILE= "masterDaemon.pid"; - const char* const RESPONSE_CHARSET_UTF8 = "UTF-8"; const char* const RESPONSE_CHARSET_GB2312 = "GB2312"; const char* const CLIENT_IP_K = "CLIENT_IP";