|
|
|
|
|
|
|
|
// |
|
|
// |
|
|
// Service program for testing CodeDweller::Service. |
|
|
// Service program for testing CodeDweller::Service. |
|
|
// |
|
|
// |
|
|
|
|
|
// Usage: |
|
|
|
|
|
// |
|
|
|
|
|
// serviceProgram <logFileName> |
|
|
|
|
|
// |
|
|
|
|
|
// where <logFileName> is the name of a file to write to. |
|
|
|
|
|
// |
|
|
|
|
|
// This program: |
|
|
|
|
|
// |
|
|
|
|
|
// 1) Registers a callback for the Stop message that sets a stop |
|
|
|
|
|
// flag. |
|
|
|
|
|
// |
|
|
|
|
|
// 2) While the stop flag is false, output a message to the log file |
|
|
|
|
|
// every 2 seconds. |
|
|
|
|
|
// |
|
|
|
|
|
// 3) After Stop is received, output the value returned by |
|
|
|
|
|
// Service::receivedStop(). |
|
|
|
|
|
// |
|
|
|
|
|
// 4) Call Service::clearReceivedStop(), and repeate step 3. |
|
|
|
|
|
// |
|
|
|
|
|
// 5) Exit. |
|
|
|
|
|
// |
|
|
// Copyright (C) 2014 MicroNeil Research Corporation. |
|
|
// Copyright (C) 2014 MicroNeil Research Corporation. |
|
|
// |
|
|
// |
|
|
// This program is part of the MicroNeil Research Open Library Project. For |
|
|
// This program is part of the MicroNeil Research Open Library Project. For |
|
|
|
|
|
|
|
|
#include <unistd.h> |
|
|
#include <unistd.h> |
|
|
|
|
|
|
|
|
#include <cstdlib> |
|
|
#include <cstdlib> |
|
|
|
|
|
#include <fstream> |
|
|
|
|
|
|
|
|
#include "CodeDweller/service.hpp" |
|
|
#include "CodeDweller/service.hpp" |
|
|
|
|
|
|
|
|
|
|
|
/// Callback functor for Stop message. |
|
|
|
|
|
class StopCallback : public CodeDweller::Service::Callback { |
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
|
|
|
|
StopCallback() : stopFlag(false) {} |
|
|
|
|
|
|
|
|
|
|
|
bool stopFlag; |
|
|
|
|
|
|
|
|
|
|
|
void operator()() { |
|
|
|
|
|
stopFlag = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
StopCallback stopCallback; |
|
|
|
|
|
|
|
|
int CodeDweller::Service::run() { |
|
|
int CodeDweller::Service::run() { |
|
|
sleep(5); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get the singleton. |
|
|
|
|
|
CodeDweller::Service &service = CodeDweller::Service::getInstance(); |
|
|
|
|
|
|
|
|
|
|
|
// Get the log file name. |
|
|
|
|
|
auto arguments = service.arguments(); |
|
|
|
|
|
|
|
|
|
|
|
if (arguments.size() != 2) { |
|
|
|
|
|
return(EXIT_FAILURE); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Get log file. |
|
|
|
|
|
std::ofstream logStream(arguments[1]); |
|
|
|
|
|
|
|
|
|
|
|
// Register the Stop callback. |
|
|
|
|
|
service.onStopCall(&stopCallback); |
|
|
|
|
|
|
|
|
|
|
|
while (!stopCallback.stopFlag) { |
|
|
|
|
|
|
|
|
|
|
|
logStream << "Sleeping 2 s" << std::endl; |
|
|
|
|
|
sleep(2); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
logStream << "receivedStop(): " << service.receivedStop() << std::endl; |
|
|
|
|
|
service.clearReceivedStop(); |
|
|
|
|
|
logStream << "receivedStop(): " << service.receivedStop() << std::endl; |
|
|
|
|
|
|
|
|
|
|
|
logStream.close(); |
|
|
|
|
|
|
|
|
return(EXIT_SUCCESS); |
|
|
return(EXIT_SUCCESS); |
|
|
} |
|
|
} |