Ejemplo n.º 1
0
///配置运行环境信息
int CwxEchoApp::initRunEnv(){
    ///设置系统的时钟间隔,最小刻度为1ms,此为1s。
    this->setClick(1000);//1s
    ///设置工作目录
    this->setWorkDir(this->m_config.m_strWorkDir.c_str());
    ///设置循环运行日志的数量
    this->setLogFileNum(LOG_FILE_NUM);
    ///设置每个日志文件的大小
    this->setLogFileSize(LOG_FILE_SIZE*1024*1024);
    ///调用架构的initRunEnv,使以上设置的参数生效
    if (CwxAppFramework::initRunEnv() == -1 ) return -1;
    blockSignal(SIGPIPE);
    //set version
    this->setAppVersion(ECHO_APP_VERSION);
    //set last modify date
    this->setLastModifyDatetime(ECHO_MODIFY_DATE);
    //set compile date
    this->setLastCompileDatetime(CWX_COMPILE_DATE(_BUILD_DATE));

    ///将加载的配置文件信息输出到日志文件中,以供查看检查
    string strConfOut;
    m_config.outputConfig(strConfOut);
    CWX_INFO((strConfOut.c_str()));

    ///注册echo请求的处理handle,echo请求的svr-id为SVR_TYPE_ECHO
    m_eventHandler = new CwxEchoEventHandler(this);         
    this->getCommander().regHandle(SVR_TYPE_ECHO, m_eventHandler);

    ///监听TCP连接,其建立的连接的svr-id都为SVR_TYPE_ECHO,接收的消息的svr-id都为SVR_TYPE_ECHO。
    ///全部由m_eventHandler对象来处理
    if (0 > this->noticeTcpListen(SVR_TYPE_ECHO, 
        this->m_config.m_listen.getHostName().c_str(),
        this->m_config.m_listen.getPort(),
        false,
        CWX_APP_MSG_MODE,
        CwxEchoApp::setSockAttr,
        this))
    {
        CWX_ERROR(("Can't register the echo acceptor port: addr=%s, port=%d",
            this->m_config.m_listen.getHostName().c_str(),
            this->m_config.m_listen.getPort()));
        return -1;
    }
    ///监听UNIX DOMAIN连接,其建立的连接的svr-id都为SVR_TYPE_ECHO,接收的消息的svr-id都为SVR_TYPE_ECHO。
    ///全部由m_eventHandler对象来处理
    if (0 > this->noticeLsockListen(SVR_TYPE_ECHO, 
        this->m_config.m_strUnixPathFile.c_str()))
    {
        CWX_ERROR(("Can't register the echo unix acceptor port: path=%s",
            m_config.m_strUnixPathFile.c_str()));
        return -1;
    }
    ///创建线程池对象,此线程池中线程的group-id为2,线程池的线程数量为m_config.m_unThreadNum。
    m_threadPool = new CwxThreadPool(m_config.m_unThreadNum,
        &getCommander());
    ///启动线程,线程池中线程的线程栈大小为1M。
    if ( 0 != m_threadPool->start(NULL)){
        CWX_ERROR(("Failure to start thread pool"));
        return -1;
    }
    return 0;

}
Ejemplo n.º 2
0
///配置运行环境信息
int CwxProProxyApp::initRunEnv() {
  ///设置系统的时间间隔,最小时刻为1ms,此时为0.1s
  this->setClick(1000); ///1s
  ///设置工作目录
  this->setWorkDir(m_config.getCmmon().m_strWorkDir.c_str());
  ///设置循环运行的日志数量
  this->setLogFileNum(LOG_FILE_NUM);
  ///设置每个日志文件的大小
  this->setLogFileSize(LOG_FILE_SIZE * 1024 * 1024);
  ///调用架构的initRunEnv,是以上参数生效
  if (CwxAppFramework::initRunEnv() == -1) return -1;
  ///将加载的配置文件信息输出到日志文件
  m_config.outputConfig();
  ///block各种signal
  this->blockSignal(SIGTERM);
  this->blockSignal(SIGUSR1);
  this->blockSignal(SIGUSR2);
  this->blockSignal(SIGCHLD);
  this->blockSignal(SIGCLD);
  this->blockSignal(SIGHUP);
  this->blockSignal(SIGPIPE);
  this->blockSignal(SIGALRM);
  this->blockSignal(SIGCONT);
  this->blockSignal(SIGSTOP);
  this->blockSignal(SIGTSTP);
  this->blockSignal(SIGTTOU);

  //set version
  this->setAppVersion(CWX_PRODUCT_PROXY_VERSION);
  //set last modify date
  this->setLastModifyDatetime(CWX_PRODUCT_PROXY_MODIFY);
  //set compile date
  this->setLastCompileDatetime(CWX_COMPILE_DATE(_BUILD_DATE));
  ///设置启动时间
  CwxDate::getDateY4MDHMS2(time(NULL), m_strStartTime);

  ///创建mq的消息的处理handle
  m_mqHandler = new CwxProProxyMqHandler(this);
  ///注册handele
  getCommander().regHandle(SVR_PRO_TYPE_MQ, m_mqHandler);
  ///创建代理消息的处理handle
  m_recvHandler = new CwxProProxyRecvHandler(this);
  getCommander().regHandle(SVR_PRO_TYPE_RECV, m_recvHandler);
  ///创建消息处理线程池
  m_mqThreadPool = new CwxThreadPool(1,
      &getCommander());
  ///创建线程的tss对象
  CwxTss** pTss = new CwxTss*[1];
  pTss[0] = new CwxMqTss();
  ((CwxMqTss*)pTss[0])->init();
  ///启动线程
  if (0 != m_mqThreadPool->start(pTss)) {
    CWX_ERROR(("Failure to start mq thread pool."));
    return -1;
  }

  ///启动zk线程池
  m_zkThreadPool = new CwxThreadPool(1,
      &getCommander(),
      CwxProProxyApp::zkThreadMain,
      this);
  pTss = new CwxTss*[1];
  pTss[0] = new CwxMqTss();
  ((CwxMqTss*)pTss[0])->init();
  if (0 != m_zkThreadPool->start(pTss)) {
    CWX_ERROR(("Failure to start zookeeper thread pool."));
    return -1;
  }
  ///创建zk的handler
  m_zkHandler = new CwxProProxyZkHandler(this,
      m_config.getZk().m_strRootPath,
      m_config.getZk().m_strZkServer
      );
  if (0 != m_zkHandler->init()) {
    CWX_ERROR(("Failure to init zk handler."));
    return -1;
  }
  ///打开监听端口
  if(0 > this->noticeTcpListen(SVR_PRO_TYPE_RECV,
      m_config.getCmmon().m_listen.getHostName().c_str(),
      m_config.getCmmon().m_listen.getPort(),
      false,
      CWX_APP_MSG_MODE,
      CwxProProxyApp::setRecvSockAttr,
      this)) {
    CWX_ERROR(("Failure to open listen host:%s port:%d",
        m_config.getCmmon().m_listen.getHostName().c_str(),
        m_config.getCmmon().m_listen.getPort()));
    return -1;
  }
  return 0;
}
Ejemplo n.º 3
0
///配置运行环境信息
int CwxMqApp::initRunEnv() {
  ///设置系统的时钟间隔,最小刻度为1ms,此为0.1s。
  this->setClick(100); //0.1s
  ///设置工作目录
  this->setWorkDir(m_config.getCommon().m_strWorkDir.c_str());
  ///设置循环运行日志的数量
  this->setLogFileNum(LOG_FILE_NUM);
  ///设置每个日志文件的大小
  this->setLogFileSize(LOG_FILE_SIZE * 1024 * 1024);
  ///调用架构的initRunEnv,使以上设置的参数生效
  if (CwxAppFramework::initRunEnv() == -1) return -1;
  ///将加载的配置文件信息输出到日志文件中,以供查看检查
  m_config.outputConfig();
  ///block各种signal
  this->blockSignal(SIGTERM);
  this->blockSignal(SIGUSR1);
  this->blockSignal(SIGUSR2);
  this->blockSignal(SIGCHLD);
  this->blockSignal(SIGCLD);
  this->blockSignal(SIGHUP);
  this->blockSignal(SIGPIPE);
  this->blockSignal(SIGALRM);
  this->blockSignal(SIGCONT);
  this->blockSignal(SIGSTOP);
  this->blockSignal(SIGTSTP);
  this->blockSignal(SIGTTOU);

  //set version
  this->setAppVersion(CWX_MQ_VERSION);
  //set last modify date
  this->setLastModifyDatetime(CWX_MQ_MODIFY_DATE);
  //set compile date
  this->setLastCompileDatetime(CWX_COMPILE_DATE(_BUILD_DATE));
  ///设置启动时间
  CwxDate::getDateY4MDHMS2(time(NULL), m_strStartTime);

  //启动binlog管理器
  if (0 != startBinLogMgr()) return -1;
  if (m_config.getCommon().m_bMaster) {
    ///注册数据接收handler
    if (m_config.getRecv().m_recv.getHostName().length()) {
      m_recvHandler = new CwxMqRecvHandler(this);
      getCommander().regHandle(SVR_TYPE_RECV, m_recvHandler);
    }
  } else {
    ///注册slave的master数据接收handler
    m_masterHandler = new CwxMqMasterHandler(this);
    getCommander().regHandle(SVR_TYPE_MASTER, m_masterHandler);
  }

  ///启动网络连接与监听
  if (0 != startNetwork()) return -1;
  ///创建recv线程池对象,此线程池中线程的group-id为THREAD_GROUP_USER_START,
  ///线程池的线程数量为1。
  m_recvThreadPool = new CwxThreadPool(1, &getCommander());
  ///创建线程的tss对象
  CwxTss** pTss = new CwxTss*[1];
  pTss[0] = new CwxMqTss();
  ((CwxMqTss*) pTss[0])->init();
  ///启动线程
  if (0 != m_recvThreadPool->start(pTss)) {
    CWX_ERROR(("Failure to start recv thread pool"));
    return -1;
  }
  //创建分发线程池
  if (m_config.getDispatch().m_async.getHostName().length()) {
    m_dispChannel = new CwxAppChannel();
    m_dispThreadPool = new CwxThreadPool(1,
      &getCommander(),
      CwxMqApp::dispatchThreadMain,
      this);
    ///启动线程
    pTss = new CwxTss*[1];
    pTss[0] = new CwxMqTss();
    ((CwxMqTss*) pTss[0])->init();
    if (0 != m_dispThreadPool->start(pTss)) {
      CWX_ERROR(("Failure to start dispatch thread pool"));
      return -1;
    }
  }
  //创建mq线程池
  if (m_config.getMq().m_mq.getHostName().length()
    || m_config.getMq().m_mq.getUnixDomain().length())
  {
    m_mqChannel = new CwxAppChannel();
    m_mqThreadPool = new CwxThreadPool(1,
      &getCommander(),
      CwxMqApp::mqFetchThreadMain,
      this);
    ///启动线程
    pTss = new CwxTss*[1];
    pTss[0] = new CwxMqTss();
    ((CwxMqTss*) pTss[0])->init();
    if (0 != m_mqThreadPool->start(pTss)) {
      CWX_ERROR(("Failure to start mq thread pool"));
      return -1;
    }
  }
  return 0;
}