update Husky

This commit is contained in:
wyy 2013-11-23 06:26:43 -08:00
parent f2cc6c07e3
commit f48445403e
6 changed files with 64 additions and 49 deletions

View File

@ -1,2 +1,2 @@
#!/bin/sh #!/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 &

View File

@ -3,9 +3,9 @@
namespace Husky namespace Husky
{ {
IRequestHandler * Daemon::m_pHandler; IWorkHandler * Daemon::m_pHandler;
ServerFrame Daemon::m_ServerFrame;
int Daemon::m_nChildPid = 0; int Daemon::m_nChildPid = 0;
const char* Daemon::m_pidFile = NULL;
bool Daemon::isAbnormalExit(int pid, int status) bool Daemon::isAbnormalExit(int pid, int status)
{ {
@ -40,9 +40,9 @@ namespace Husky
return bRestart; 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()); int masterPid = atoi(masterPidStr.c_str());
if(masterPid) if(masterPid)
{ {
@ -57,7 +57,7 @@ namespace Husky
char buf[64]; char buf[64];
sprintf(buf, "%d", getpid()); sprintf(buf, "%d", getpid());
if (!WriteStr2File(MASTER_PID_FILE,buf ,"w")) if (!WriteStr2File(m_pidFile,buf ,"w"))
{ {
LogFatal("Write master pid fail!"); LogFatal("Write master pid fail!");
} }
@ -80,31 +80,26 @@ namespace Husky
LogFatal("m_pHandler init failed!"); LogFatal("m_pHandler init failed!");
return false; 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 #ifdef DEBUG
LogDebug("Worker init ok pid = %d",(int)getpid()); LogDebug("Worker init ok pid = %d",(int)getpid());
#endif #endif
if (!m_ServerFrame.RunServer()) if (!m_pHandler->run())
{ {
LogError("m_ServerFrame.RunServer finish -fail!"); LogError("m_pHandler run finish with failure!");
return false; return false;
} }
#ifdef DEBUG #ifdef DEBUG
LogDebug("run finish -ok!"); LogDebug("run finish -ok!");
#endif #endif
if(!m_pHandler->dispose()) //if(!m_pHandler->dispose())
{ //{
LogError("m_pHandler.dispose -fail!"); // LogError("m_pHandler dispose with failure!");
return false; // return false;
} //}
#ifdef DEBUG #ifdef DEBUG
LogDebug("Worker dispose -ok!"); //LogDebug("Worker dispose -ok!");
#endif #endif
exit(0); 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()); int masterPid = atoi(masterPidStr.c_str());
if(masterPid) if(masterPid)
{ {
@ -185,7 +180,7 @@ namespace Husky
{ {
if (sig == SIGUSR1) if (sig == SIGUSR1)
{ {
m_ServerFrame.CloseServer(); m_pHandler->dispose();
LogDebug("master = %d signal accept current pid =%d!",getppid(),getpid()); LogDebug("master = %d signal accept current pid =%d!",getppid(),getpid());
} }

View File

@ -9,32 +9,43 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <signal.h> #include <signal.h>
#include "../Limonp/logger.hpp" #include "../Limonp/logger.hpp"
#include "ServerFrame.h"
namespace Husky namespace Husky
{ {
using namespace Limonp; using namespace Limonp;
class IWorkHandler
{
public:
virtual ~IWorkHandler(){}
virtual bool init() = 0;
virtual bool dispose() = 0;
virtual bool run() = 0;
};
class Daemon class Daemon
{ {
public: public:
Daemon(IRequestHandler * pHandler) Daemon(IWorkHandler * workHandler, const char* pidFile)
{ {
m_pHandler = pHandler; m_pHandler = workHandler;
m_pidFile = pidFile;
} }
~Daemon(){}; ~Daemon(){};
public: public:
bool Start(unsigned int port, unsigned int threadNum); bool start();
bool Stop(); bool stop();
public: public:
static void initAsDaemon(); static void initAsDaemon();
static void sigMasterHandler(int sig); static void sigMasterHandler(int sig);
static void sigChildHandler(int sig); static void sigChildHandler(int sig);
static bool isAbnormalExit(int pid, int status); static bool isAbnormalExit(int pid, int status);
private: private:
static IRequestHandler* m_pHandler; //static IRequestHandler* m_pHandler;
static ServerFrame m_ServerFrame; //static ServerFrame m_ServerFrame;
static int m_nChildPid; static int m_nChildPid;
static IWorkHandler * m_pHandler;
static const char* m_pidFile;
}; };
} }
#endif #endif

View File

@ -6,7 +6,7 @@ namespace Husky
pthread_mutex_t ServerFrame::m_pmAccept; pthread_mutex_t ServerFrame::m_pmAccept;
bool ServerFrame::m_bShutdown = false; bool ServerFrame::m_bShutdown = false;
bool ServerFrame::CloseServer() bool ServerFrame::dispose()
{ {
m_bShutdown=true; m_bShutdown=true;
if (SOCKET_ERROR==closesocket(m_lsnSock)) if (SOCKET_ERROR==closesocket(m_lsnSock))
@ -39,12 +39,14 @@ namespace Husky
LogError("error [%s]", strerror(errno)); LogError("error [%s]", strerror(errno));
} }
close(sockfd); close(sockfd);
LogInfo("CloseServer ok."); if(!m_pHandler->dispose())
{
LogFatal("m_pHandler dispose failed.");
}
return true; return true;
} }
bool ServerFrame::RunServer() bool ServerFrame::run()
{ {
if(SOCKET_ERROR==listen(m_lsnSock,LISEN_QUEUR_LEN)) 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)) 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; return false;
} }
pthread_mutex_init(&m_pmAccept,NULL);
LogInfo("CreatServer ok {port:%d, threadNum:%d}", nPort, nThreadCount);
return true; return true;
} }
@ -201,7 +206,6 @@ namespace Husky
return false; return false;
} }
/* 使地址马上可以重用 */
int nRet = 1; int nRet = 1;
if(SOCKET_ERROR==setsockopt(m_lsnSock, SOL_SOCKET, SO_REUSEADDR, (char*)&nRet, sizeof(nRet))) if(SOCKET_ERROR==setsockopt(m_lsnSock, SOL_SOCKET, SO_REUSEADDR, (char*)&nRet, sizeof(nRet)))
{ {

View File

@ -16,6 +16,7 @@
#include "globals.h" #include "globals.h"
#include "ThreadManager.hpp" #include "ThreadManager.hpp"
#include "HttpReqInfo.hpp" #include "HttpReqInfo.hpp"
#include "Daemon.h"
#define INVALID_SOCKET -1 #define INVALID_SOCKET -1
#define SOCKET_ERROR -1 #define SOCKET_ERROR -1
@ -47,14 +48,20 @@ namespace Husky
IRequestHandler * pHandler; IRequestHandler * pHandler;
}; };
class ServerFrame class ServerFrame: public IWorkHandler
{ {
public: public:
ServerFrame(){}; ServerFrame(unsigned nPort, unsigned nThreadCount, IRequestHandler* pHandler)
~ServerFrame(){pthread_mutex_destroy(&m_pmAccept);}; {
bool CreateServer(u_short nPort,u_short nThreadCount,IRequestHandler *pHandler); m_nLsnPort = nPort;
bool CloseServer(); m_nThreadCount = nThreadCount;
bool RunServer(); 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: protected:

View File

@ -10,8 +10,6 @@
namespace Husky namespace Husky
{ {
const char* const MASTER_PID_FILE= "masterDaemon.pid";
const char* const RESPONSE_CHARSET_UTF8 = "UTF-8"; const char* const RESPONSE_CHARSET_UTF8 = "UTF-8";
const char* const RESPONSE_CHARSET_GB2312 = "GB2312"; const char* const RESPONSE_CHARSET_GB2312 = "GB2312";
const char* const CLIENT_IP_K = "CLIENT_IP"; const char* const CLIENT_IP_K = "CLIENT_IP";