void CHarmonyHub::Do_Work() { _log.Log(LOG_STATUS,"Harmony Hub: Worker thread started..."); //start with getting the activities unsigned short checkAct=HARMONY_POLL_FETCH_ACTIVITY_SECONDS / HARMONY_POLL_INTERVAL_SECONDS; while (!m_stoprequested) { sleep_seconds(HARMONY_POLL_INTERVAL_SECONDS); checkAct++; //check the active activity unsigned short checkLogin=HARMONY_RETRY_LOGIN_SECONDS / HARMONY_POLL_INTERVAL_SECONDS; while(m_bDoLogin && !m_stoprequested) { if (HARMONY_RETRY_LOGIN_SECONDS <= checkLogin) { checkLogin=0; if(Login() && SetupCommandSocket()) { m_bDoLogin=false; break; } } else { mytime(&m_LastHeartbeat); sleep_seconds(1); checkLogin++; } } //extra check after sleep if we must not quit right away if(m_stoprequested) break; //check/update the active activity if(!UpdateCurrentActivity()) m_usCommandsMissed++; else m_usCommandsMissed=0; //check for activities change. Update our heartbeat too. if (HARMONY_POLL_FETCH_ACTIVITY_SECONDS / HARMONY_POLL_INTERVAL_SECONDS <= checkAct) { if(!UpdateActivities()) m_usCommandsMissed++; else m_usCommandsMissed=0; checkAct=0; } //and set the heartbeat while we're at it mytime(&m_LastHeartbeat); if(m_usCommandsMissed>=MAX_MISS_COMMANDS) { Logout(); _log.Log(LOG_STATUS,"Harmony Hub: Too many commands missed. Resetting connection."); //m_bDoLogin=true; //re login } } _log.Log(LOG_STATUS,"Harmony Hub: Worker stopped..."); }
void CHarmonyHub::Do_Work() { _log.Log(LOG_STATUS,"Harmony Hub: Worker thread started..."); //start with getting the activities int scounter = 0; int mcounter = 0; bool bFirstTime = true; while (!m_stoprequested) { sleep_milliseconds(500); if (m_stoprequested) break; mcounter++; if (mcounter<2) continue; mcounter = 0; scounter++; if (scounter % 12 == 0) { m_LastHeartbeat=mytime(NULL); } if (m_bDoLogin) { if ((scounter%HARMONY_RETRY_LOGIN_SECONDS == 0) || (bFirstTime)) { bFirstTime = false; if(Login() && SetupCommandSocket()) { m_bDoLogin=false; if (!UpdateCurrentActivity()) { Logout(); m_bDoLogin = true; } else { if (!UpdateActivities()) { Logout(); m_bDoLogin = true; } } } } continue; } if (scounter % HARMONY_PING_INTERVAL_SECONDS == 0) { //Ping the server if (!SendPing()) { _log.Log(LOG_STATUS, "Harmony Hub: Error pinging server.. Resetting connection."); Logout(); } continue; } bool bIsDataReadable = true; m_commandcsocket->canRead(&bIsDataReadable, 0.5f); if (bIsDataReadable) { boost::lock_guard<boost::mutex> lock(m_mutex); std::string strData; while (bIsDataReadable) { if (memset(m_databuffer, 0, BUFFER_SIZE) > 0) { m_commandcsocket->read(m_databuffer, BUFFER_SIZE, false); std::string szNewData = std::string(m_databuffer); if (!szNewData.empty()) { strData.append(m_databuffer); m_commandcsocket->canRead(&bIsDataReadable, 0.4f); } else bIsDataReadable = false; } else bIsDataReadable = false; } if (!strData.empty()) CheckIfChanging(strData); } } _log.Log(LOG_STATUS,"Harmony Hub: Worker stopped..."); }