Exemplo n.º 1
0
int CAuxHandle::Run()
{
	m_bHandleRun = true;
	while(1)
	{
		if (unlikely(m_iRunStatus == rt_stopped))
			break;

		// 1ms
		if((m_Main2MePipe.GetCodeLength() == 0) && (m_pSvr2MePipe->GetCodeLength() == 0))
			m_stEPollFlowUp.Wait(1);
		else
			m_stEPollFlowUp.Wait(0);

		//每次循环取一次
		gettimeofday(&m_tNow,NULL);

		//读取客户端包
		CheckClientMessage();

		//读取主线程包
		CheckMainMessage();

		//读取服务器端包
		CheckSvrMessage();

		//检测通讯超时
		CheckTimeOut();
	}
	m_bHandleRun = false;
	return 0;
}
Exemplo n.º 2
0
void CDNSChildWorker::TimeOutWork()
{
	//检测和DCS的链接状态
	if(m_DcsNodeFd.moNetStat == TCP_CLOSED)
	{
		TRACE(1, "CDNSChildWorker::TimeOutWork 和DCS链接断开了。");
	}
	uint64 time_now = CTimeBase::get_current_time();
	//发送存活
	if(time_now > m_i64LastKeepLive + KEEP_LIVE_TIME_OUT * 1000)
	{
		KeepLive();
		m_i64LastKeepLive = time_now;
	}

	//dump
	if(time_now > m_i64LastDumpTime + m_pDnsConfig->dump_info_time * 1000)
	{
		Dump();
		m_i64LastDumpTime = time_now;
	}

	//log
	if(time_now > m_i64LastLogTime + m_pDnsConfig->log_file_update_time*1000)
	{
		unsigned int process_id = CCommon::GetProcessId();

		char lszLogFileName[255];
		memset(lszLogFileName, 0, 255);
		CFileStream::GetAppPath(lszLogFileName,255);

		//设置日志文件信息
		SET_TRACE_LEVEL(5);
		unsigned liOptions = (CDebugTrace::Timestamp | CDebugTrace::LogLevel
			& ~CDebugTrace::FileAndLine | CDebugTrace::AppendToFile);
		SET_TRACE_OPTIONS(GET_TRACE_OPTIONS() | liOptions);

		strcpy(strrchr(lszLogFileName,'/'),"//DNS_log");
		CFileStream::CreatePath(lszLogFileName);
		char lszFileDate[50];
		memset(lszFileDate, 0, 50);
		time_t loSystemTime;
		time(&loSystemTime);
		struct tm* lptm = localtime(&loSystemTime);
		sprintf(lszFileDate, "//DNS-%u-%4d%02d%02d%02d%02d.log", process_id,
			1900+lptm->tm_year,1+lptm->tm_mon,lptm->tm_mday, lptm->tm_hour, lptm->tm_min);
		strcat(lszLogFileName,lszFileDate);
		SET_LOG_FILENAME(lszLogFileName);
		m_i64LastLogTime = time_now;
	}

	if(time_now > m_i64LastCheckTimeOut + CRS_DNS_TIMEOUT * 1000)
	{
		CheckTimeOut();
		//test();
		m_i64LastCheckTimeOut = time_now;
	}
}
Exemplo n.º 3
0
	void TimerThread::Run()
	{
		unsigned long long lastCheckTime = GetCurrentTimeMillis();
		unsigned long long currentCheckTime = lastCheckTime;

		while (!m_closed)
		{
			currentCheckTime = GetCurrentTimeMillis();
			unsigned int elapse = (unsigned int)(currentCheckTime - lastCheckTime);

			std::list<TimerInfo> timeList;

			CheckTimeOut(elapse,timeList);

			if (!timeList.empty())
			{
				std::list<TimerInfo>::iterator it = timeList.begin();
				for (; it != timeList.end(); it++)
				{
					it->pTimerHandler->OnTimeOut(it->id);
				}
			}

			unsigned long long checkEndTime = GetCurrentTimeMillis();
			int sleepTime = m_checkInterval - (int)(checkEndTime -currentCheckTime);
			if (sleepTime < 0)
			{
				sleepTime = 0;
			}

			lastCheckTime = currentCheckTime;

			try
			{
				kpr::ScopedLock<kpr::Monitor> lock(*this);
				Wait(sleepTime);
			}
			catch (...)
			{
			}
		}
	}
Exemplo n.º 4
0
int CMainCtrl::Run()
{
	while(1)
	{
		if(Flag_Exit == m_iRunFlag)
		{
			TLib_Log_LogMsg("Server Exit!\n");
			return 0;
		}
		
		if(Flag_ReloadCfg == m_iRunFlag)
		{
			ReadCfgFile(m_stConfig.m_szCfgFileName);
			TLib_Log_LogMsg("reload config file ok!\n");
			m_iRunFlag = 0;
		}

		// 1ms
		if(m_Svr2MePipe.GetCodeLength() == 0)
			m_stEPollFlow.Wait(1);
		else
			m_stEPollFlow.Wait(0);		

		//每次循环取一次
		gettimeofday(&m_tNow,NULL);
		
		//读取客户端包
		CheckClientMessage();    
		
		//读取服务器端包
		CheckSvrMessage(); 

		//检测通讯超时
		CheckTimeOut();    

		//写统计
		WriteStat();
	}
	
	return 0;
}
/*******************************************************************************
Purpose:
  This routine manage system initialization state by checking on the LAN link
  status between RTU and server; and LAN link between two RTUs

Input:
  None

Return:
  None

History
    Name           Date          Remark
    ----           ----          ------
  Yu, Wei        12-Aug-2004   Initial revision

*******************************************************************************/
void GoState3_Initialization(void)
{

  if(g_bRMMServerLinkCheck == true)  //Check server link.
  {
    clock_gettime(CLOCK_REALTIME, &g_pRTUStatusTable->m_tStartTime);
    if(g_tRTUStatus.unRTUServerLinkStatus == LINK_FAULTY)
    {
      #if ((defined CFG_PRN_WARN) && CFG_PRN_WARN_RMM_STATE)
      printf("WARN [RMM] GoState3_Initialization, server link faulty\n");
      #endif // ((defined CFG_PRN_WARN) && CFG_PRN_WARN_RMMLINK)

      if(CheckTimeOut(g_tRTUConfig.nInitLANCheckTimeout) == true)
      {
        //Timeout. Confirm the link down.
        g_bRMMServerLinkCheck = false;
        //Check link status between RTU.
        g_bRMMOtherRTULinkCheck = true;
        ActivateTimeOut();
      }
    }else{
      #if ((defined CFG_DEBUG_MSG) && CFG_DEBUG_RMM_STATE)
      printf("[RMM] GoState3_Initialization, server link healthy, change state "
             "to STATEFLAG_STANDBY\n");
      #endif // ((defined CFG_PRN_WARN) && (CFG_DEBUG_RMM))
      //Server link is OK, goto standby state.
      g_bRTUInitLinkCheck = false;
      ChangeStateTo(STATEFLAG_STANDBY);
    }
  }

  if(g_bRMMOtherRTULinkCheck == true)
  {
    if((CheckTimeOut(SOCKET_CONNECT_TIMEOUT *2 -
                     g_tRTUConfig.nInitLANCheckTimeout) == true) ||
       (g_tRTUConfig.nInitLANCheckTimeout >= SOCKET_CONNECT_TIMEOUT*2) ||
       (g_tRTUStatus.abOtherLinkStatus[0] == true) ||
       (g_tRTUStatus.abOtherLinkStatus[1] == true))
    {
      //Check LAN 2 link
      if(g_tRTUStatus.abOtherLinkStatus[0] == false)
      {
        //Check LAN 1 link
        if(g_tRTUStatus.abOtherLinkStatus[1] == false)
        {
          #if ((defined CFG_PRN_WARN) && CFG_PRN_WARN_RMM_STATE)
          printf("WARN [RMM] GoState3_Initialization, both RTU-RTU LAN links "
                 "down, change to STATEFLAG_STANDBY\n");
          #endif // ((defined CFG_PRN_WARN) && CFG_PRN_WARN_RMMLINK)
          //Two link are down. Set to standby state.
          g_bRTUInitLinkCheck = false;
          ChangeStateTo(STATEFLAG_STANDBY);
        }else{
          //Link2 between RTU is OK
          //Stop check RTU link.
          g_bRMMOtherRTULinkCheck = false;
        }
      }
      else
      {
        #if ((defined CFG_PRN_WARN) && CFG_PRN_WARN_RMM_STATE)
        printf("WARN [RMM] GoState3_Initialization, at least one RTU-RTU LAN "
               "link is healthy other is down\n");
        #endif // ((defined CFG_PRN_WARN) && (CFG_DEBUG_RMM))
        //Link1 between RTU is OK
        //Stop check RTU link.
        g_bRMMOtherRTULinkCheck = false;
      }
    }// //Stop check RTU link.

    if(g_bRMMOtherRTULinkCheck == false)
    {
      // check the role of other RTU. If other RTU is on standby, set RTU to
      // primary; otherwise, set RTU to standby
      if(g_pRTUStatusTable->m_nOtherRTUStateFlag == STATEFLAG_PRIMARY)
      {
        #if ((defined CFG_DEBUG_MSG) && CFG_DEBUG_RMM_STATE)
        printf("[RMM] GoState3_Initialization, other RTU is on Primary "
               "set local RTU to standby\n");
        #endif // ((defined CFG_DEBUG_MSG) && CFG_DEBUG_RMM_STATE)
        g_bRTUInitLinkCheck = false;
        ChangeStateTo(STATEFLAG_STANDBY);
      }else if(g_pRTUStatusTable->m_nOtherRTUStateFlag == STATEFLAG_STANDBY){
        #if ((defined CFG_DEBUG_MSG) && CFG_DEBUG_RMM_STATE)
        printf("[RMM] GoState3_Initialization, other RTU is on Standby "
               "set local RTU to Primary\n");
        #endif // ((defined CFG_DEBUG_MSG) && CFG_DEBUG_RMM_STATE)
        g_pRTUStatusTable->m_bSWCSwitchCompletedFlag = true ;//2008
        ChangeStateTo(STATEFLAG_PRIMARY);
      }else{
        #if ((defined CFG_PRN_WARN) && (CFG_PRN_WARN_RMM_STATE))
        printf("WARN [RMM] GoState3_Initialization, cannot determine other RTU "
               "role set local RTU to Standby\n");
        #endif // ((defined CFG_PRN_WARN) && (CFG_DEBUG_RMM))
        g_bRTUInitLinkCheck = false;
        ChangeStateTo(STATEFLAG_STANDBY);
      }
    }// if(g_bRMMOtherRTULinkCheck == false)
  }// if(g_bRMMOtherRTULinkCheck == true)
}// GoState3_Initialization
/*******************************************************************************
Purpose:
  Primary state.

History

    Name          Date          Remark
    ----          ----          ------
  Bryan Chong   28-Jun-2010   Update to include timeout for server
                              which is correspondent to LAN1_TIME_OUT

*******************************************************************************/
void GoState1_Primary(void)
{
  //If link between RTU is not down, auto switching will implement.
  if((g_tRTUStatus.abOtherLinkStatus[0] == true) ||
     (g_tRTUStatus.abOtherLinkStatus[1] == true))
  {
    //SWC link down processing completed or not in SWC link down status.
    //When primary RTU link down, m_bSWCLinkDownProcessFlag  will set to true,
    //When standby RTU completed link check, the flag will set to false.
    if(g_pRTUStatusTable->m_bSWCLinkDownProcessFlag == false)
    {

      if(g_tRTUStatus.unRTUServerLinkStatus !=
         g_pRTUStatusTable->m_unOtherRTUServerLinkStatus)
      {
        //RTU1 and RTU2 server link is same. (All are Link OK or Link down)
        if(g_tRTUStatus.unRTUServerLinkStatus == LINK_FAULTY)
        {
          #if ((defined CFG_DEBUG_MSG) && \
               (CFG_DEBUG_RMM || CFG_DEBUG_RMMLINKCHK))
          printf("[RMM] GoState1_Primary, PtoS, due to server link faulty\n");
          #endif // ((defined CFG_DEBUG_MSG) && (CFG_DEBUG_RMMLINK)
          ChangeStateTo(STATEFLAG_SWITCHING_PTOS);  //040322 Yu Wei
        }
      }
      else if(g_bRTUInitLinkCheck == false)
      {
        if(g_pRTUStatusTable->m_unSWCWeight <
           g_pRTUStatusTable->m_unOtherSWCWeight)
        {
          #if ((defined CFG_DEBUG_MSG) && \
               (CFG_DEBUG_RMM || CFG_DEBUG_RMMLINKCHK))
          printf("[RMM] GoState1_Primary, PtoS,\n"
                 "  local RTU notation, %d, > other RTU, %d\n",
                 g_pRTUStatusTable->m_unSWCWeight,
                 g_pRTUStatusTable->m_unOtherSWCWeight);
          #endif // ((defined CFG_DEBUG_MSG) && (CFG_DEBUG_RMMLINK)
          ChangeStateTo(STATEFLAG_SWITCHING_PTOS);  //040322 Yu Wei
          g_nWeightDoubleCheck = 0;
        }
        else
        {
          if (g_nWeightDoubleCheck != 0)
            g_nWeightDoubleCheck = 0;

          StandbyRTUImproveCheck();
        }
      }
    }
    else
    {
      //primary RTU link down , standby RTU has'n completed link check
      g_pRTUStatusTable->CheckSWCLinkStop();
    }

    g_pRTUStatusTable->m_unOtherRTUServerLinkStatusPrev =
      g_pRTUStatusTable->m_unOtherRTUServerLinkStatus;
    g_pRTUStatusTable->m_unOtherSWCWeightPrev =
      g_pRTUStatusTable->m_unOtherSWCWeight;

    ActivateTimeOut();
  }
  else if((g_tRTUStatus.unRTUServerLinkStatus == LINK_FAULTY) &&
          (CheckTimeOut(SOCKET_CONNECT_TIMEOUT*1.5) == true)) //kvmrt 20131028 Su
  {
    #if ((defined CFG_PRN_WARN) && CFG_PRN_WARN_RMMLINK)
    printf("[RMM] GoState1_Primary, PtoS, due to server link faulty and\n"
           "  SOCKET_CONNECT_TIMEOUT %d sec\n", SOCKET_CONNECT_TIMEOUT);
    #endif // ((defined CFG_PRN_WARN) && CFG_PRN_WARN_RMMLINK)
    ChangeStateTo(STATEFLAG_SWITCHING_PTOS);  //040322 Yu Wei
  }
  else if(g_pRTUStatusTable->m_bSWCLinkDownProcessFlag == true)
  {
    g_pRTUStatusTable->SetSWCLinkStart();
  }
}// GoState1_Primary