diff --git a/pandora_agents/win32/ChangeLog b/pandora_agents/win32/ChangeLog index 945daf3636..79bcdea210 100644 --- a/pandora_agents/win32/ChangeLog +++ b/pandora_agents/win32/ChangeLog @@ -1,3 +1,10 @@ +2013-04-04 Koichiro KIKUCHI + + * windows_service.cc, + pandora_windows_service.cc, + windows_service.h: Set iteration base ticks after sleeping startup_delay + to exclude time taken by startup_delay from sleep time subtraction. + 2013-04-03 Koichiro KIKUCHI * windows_service.cc: Fixed typo of my previous commit. diff --git a/pandora_agents/win32/pandora_windows_service.cc b/pandora_agents/win32/pandora_windows_service.cc index e2b1813305..239b0fc7cc 100644 --- a/pandora_agents/win32/pandora_windows_service.cc +++ b/pandora_agents/win32/pandora_windows_service.cc @@ -1629,8 +1629,6 @@ void Pandora_Windows_Service::pandora_run_broker (string config) { Pandora_Agent_Conf *conf = NULL; string server_addr; - int startup_delay = 0; - static unsigned char delayed = 0; unsigned char data_flag = 0; unsigned char intensive_match; @@ -1725,7 +1723,7 @@ Pandora_Windows_Service::pandora_run () { string server_addr, conf_file, *all_conf; int startup_delay = 0; int i, num; - static unsigned char delayed = 0; + static bool startup = true; unsigned char data_flag = 0; unsigned char intensive_match; @@ -1733,13 +1731,17 @@ Pandora_Windows_Service::pandora_run () { conf = this->getConf (); + /* process only once at startup */ + if (startup) { + startup = false; /* Sleep if a startup delay was specified */ startup_delay = atoi (conf->getValue ("startup_delay").c_str ()) * 1000; - if (startup_delay > 0 && delayed == 0) { - delayed = 1; + if (startup_delay > 0) { pandoraLog ("Delaying startup %d miliseconds", startup_delay); Sleep (startup_delay); } + setIterationBaseTicks(GetTickCount()); + } /* Set the run time */ this->run_time = time (NULL); diff --git a/pandora_agents/win32/windows_service.cc b/pandora_agents/win32/windows_service.cc index 7fad95033b..a7f500f0f1 100644 --- a/pandora_agents/win32/windows_service.cc +++ b/pandora_agents/win32/windows_service.cc @@ -112,10 +112,11 @@ Windows_Service::setInitFunction (void (Windows_Service::*f) ()) { void Windows_Service::execRunFunction () { int sleep_time_remain; - DWORD tickbase, ticknow; + DWORD ticknow; if (run_function != NULL) { - tickbase = GetTickCount(); + // init here + setIterationBaseTicks(GetTickCount()); do { (this->*run_function) (); @@ -127,14 +128,14 @@ Windows_Service::execRunFunction () { // Subtract time taken by run_funtion() from sleep_time // to *start* each iteration with the same interval - sleep_time_remain = sleep_time - (ticknow - tickbase); + sleep_time_remain = sleep_time - (ticknow - iter_base_ticks); if (0 <= sleep_time_remain && sleep_time_remain <= sleep_time) { - tickbase += sleep_time; + setIterationBaseTicks(iter_base_ticks + sleep_time); } else { // run_function() took more than "sleep_time", or something goes wrong. sleep_time_remain = 0; // don't sleep - tickbase = ticknow; // use current ticks as base ticks + setIterationBaseTicks(ticknow); // use current ticks as base ticks } } while (WaitForSingleObject (stop_event, sleep_time_remain) != WAIT_OBJECT_0); } @@ -175,6 +176,19 @@ Windows_Service::setSleepTime (unsigned int s) { current_service->sleep_time = sleep_time; } +/** + * Set the base tick count. + * + * This ticks used to start each iteration with the same + * interval (this->sleep_time) + * @param ticks base tick count.. + */ +void +Windows_Service::setIterationBaseTicks(DWORD ticks) { + iter_base_ticks = ticks; + current_service->iter_base_ticks = ticks; +} + /** * Install the service in the Windows services system. * diff --git a/pandora_agents/win32/windows_service.h b/pandora_agents/win32/windows_service.h index 38c1a77a7b..f918017b53 100644 --- a/pandora_agents/win32/windows_service.h +++ b/pandora_agents/win32/windows_service.h @@ -42,6 +42,7 @@ protected: private: HANDLE stop_event; int sleep_time; + DWORD iter_base_ticks; SC_HANDLE sc_service; void (Windows_Service::*run_function) (); @@ -62,6 +63,7 @@ public: void setInitFunction (void (Windows_Service::*f) ()); LPSTR getServiceName (); void setSleepTime (unsigned int s); + void setIterationBaseTicks(DWORD ticks); void execRunFunction (); void execInitFunction ();