|
|
|
|
|
|
|
|
// |
|
|
// |
|
|
// Usage: |
|
|
// Usage: |
|
|
// |
|
|
// |
|
|
// serviceProgram <logFileName> |
|
|
|
|
|
|
|
|
// serviceProgram <logFileName> <message> |
|
|
// |
|
|
// |
|
|
// where <logFileName> is the name of a file to write to. |
|
|
|
|
|
|
|
|
// where <logFileName> is the name of a file to write to, and |
|
|
|
|
|
// <message> is the message callback timeout to test. <message> can |
|
|
|
|
|
// be Pause, Resume, or Stop. If <message> is anything else, then |
|
|
|
|
|
// <message> is ignored. |
|
|
// |
|
|
// |
|
|
// This program: |
|
|
// This program: |
|
|
// |
|
|
// |
|
|
// 1) Registers callbacks for various messages. |
|
|
|
|
|
|
|
|
// 1) Sets the callback timeout to 500 ms. |
|
|
// |
|
|
// |
|
|
// 2) While the stop flag is false, outputs the status of all flags |
|
|
|
|
|
|
|
|
// 2) Registers callbacks for various messages. Each callback |
|
|
|
|
|
// normaly sleeps for 400 ms, which is less than the timeout. |
|
|
|
|
|
// However, if <message> is present, the callback for the specified |
|
|
|
|
|
// message sleeps for 600 ms. This should cause the service to |
|
|
|
|
|
// exit. |
|
|
|
|
|
// |
|
|
|
|
|
// 3) While the stop flag is false, outputs the status of all flags |
|
|
// to the log file every 2 seconds, and clears all flags. |
|
|
// to the log file every 2 seconds, and clears all flags. |
|
|
// |
|
|
// |
|
|
// 3) After Stop is received, output the status of all flags, and |
|
|
|
|
|
|
|
|
// 4) After Stop is received, output the status of all flags, and |
|
|
// exit. |
|
|
// exit. |
|
|
// |
|
|
// |
|
|
// Copyright (C) 2014 MicroNeil Research Corporation. |
|
|
// Copyright (C) 2014 MicroNeil Research Corporation. |
|
|
|
|
|
|
|
|
// Place, Suite 330, Boston, MA 02111-1307 USA |
|
|
// Place, Suite 330, Boston, MA 02111-1307 USA |
|
|
//============================================================================== |
|
|
//============================================================================== |
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////// |
|
|
|
|
|
// Configuration //////////////////////////////////////////////////////////// |
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////// |
|
|
|
|
|
|
|
|
|
|
|
// Callback timeout time. |
|
|
|
|
|
const int timeoutTime_ms = 500; |
|
|
|
|
|
|
|
|
|
|
|
// How long the callback takes to not exceed the timeout. |
|
|
|
|
|
const int shortSleepTime_ms = 400; |
|
|
|
|
|
|
|
|
|
|
|
// How long the callback takes to exceed the timeout. |
|
|
|
|
|
const int longSleepTime_ms = 600; |
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////// |
|
|
|
|
|
// End of configuration ///////////////////////////////////////////////////// |
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////// |
|
|
|
|
|
|
|
|
#include <cstdlib> |
|
|
#include <cstdlib> |
|
|
#include <fstream> |
|
|
#include <fstream> |
|
|
#include <thread> |
|
|
#include <thread> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PauseCallback() : pauseFlag(false) {} |
|
|
PauseCallback() : pauseFlag(false) {} |
|
|
|
|
|
|
|
|
|
|
|
// Total sleep time for all Pause callbacks should be less than 1000 |
|
|
|
|
|
// ms. Reason: The while loop below sleeps for two seconds, and the |
|
|
|
|
|
// buildAndRun script sends the message one second into the sleep. |
|
|
|
|
|
// The callbacks must be completed before the main loop checks the |
|
|
|
|
|
// pauseFlag value. |
|
|
|
|
|
// |
|
|
|
|
|
// This applies to ResumeCallback and StopCallback. |
|
|
|
|
|
int sleepTime_ms = shortSleepTime_ms; |
|
|
|
|
|
|
|
|
bool pauseFlag; |
|
|
bool pauseFlag; |
|
|
|
|
|
|
|
|
void operator()() { |
|
|
void operator()() { |
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime_ms)); |
|
|
pauseFlag = true; |
|
|
pauseFlag = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ResumeCallback() : resumeFlag(false) {} |
|
|
ResumeCallback() : resumeFlag(false) {} |
|
|
|
|
|
|
|
|
|
|
|
int sleepTime_ms = shortSleepTime_ms; |
|
|
|
|
|
|
|
|
bool resumeFlag; |
|
|
bool resumeFlag; |
|
|
|
|
|
|
|
|
void operator()() { |
|
|
void operator()() { |
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime_ms)); |
|
|
resumeFlag = true; |
|
|
resumeFlag = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StopCallback() : stopFlag(false) {} |
|
|
StopCallback() : stopFlag(false) {} |
|
|
|
|
|
|
|
|
|
|
|
int sleepTime_ms = shortSleepTime_ms; |
|
|
|
|
|
|
|
|
bool stopFlag; |
|
|
bool stopFlag; |
|
|
|
|
|
|
|
|
void operator()() { |
|
|
void operator()() { |
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime_ms)); |
|
|
stopFlag = true; |
|
|
stopFlag = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int CodeDweller::Service::run() { |
|
|
int CodeDweller::Service::run() { |
|
|
|
|
|
|
|
|
|
|
|
// Set the callback timeout to the default. |
|
|
|
|
|
CodeDweller::Service::setCallbackTimeout_ms(timeoutTime_ms); |
|
|
|
|
|
|
|
|
// Get the log file name. |
|
|
// Get the log file name. |
|
|
auto arguments = CodeDweller::Service::arguments(); |
|
|
auto arguments = CodeDweller::Service::arguments(); |
|
|
|
|
|
|
|
|
if (arguments.size() != 2) { |
|
|
|
|
|
|
|
|
if (arguments.size() == 3) { |
|
|
|
|
|
// Increase the time it takes for a callback to execute. |
|
|
|
|
|
if (arguments[2] == "Pause") { |
|
|
|
|
|
pauseCbck.sleepTime_ms = longSleepTime_ms; |
|
|
|
|
|
pauseCbck1.sleepTime_ms = longSleepTime_ms; |
|
|
|
|
|
} else if (arguments[2] == "Resume") { |
|
|
|
|
|
resumeCbck.sleepTime_ms = longSleepTime_ms; |
|
|
|
|
|
resumeCbck1.sleepTime_ms = longSleepTime_ms; |
|
|
|
|
|
} else if (arguments[2] == "Stop") { |
|
|
|
|
|
stopCbck.sleepTime_ms = longSleepTime_ms; |
|
|
|
|
|
stopCbck1.sleepTime_ms = longSleepTime_ms; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
if ( (arguments.size() != 2) && (arguments.size() != 3) ) { |
|
|
return(EXIT_FAILURE); |
|
|
return(EXIT_FAILURE); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Get log file. |
|
|
// Get log file. |
|
|
std::ofstream logStream(arguments[1]); |
|
|
|
|
|
|
|
|
std::ofstream logStream(arguments[1], std::fstream::app); |
|
|
|
|
|
|
|
|
// Register the callbacks. |
|
|
// Register the callbacks. |
|
|
CodeDweller::Service::onPauseCall(pauseCbck); |
|
|
CodeDweller::Service::onPauseCall(pauseCbck); |