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
./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
{
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());
}

View File

@ -9,32 +9,43 @@
#include <sys/stat.h>
#include <signal.h>
#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

View File

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

View File

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

View File

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