// \file serviceProgram.cpp // // Service program for testing CodeDweller::Service. // // Usage: // // serviceProgram // // where 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. // // This program is part of the MicroNeil Research Open Library Project. For // more information go to http://www.microneil.com/OpenLibrary/index.html // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the // Free Software Foundation; either version 2 of the License, or (at your // option) any later version. // // This program is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for // more details. // // You should have received a copy of the GNU General Public License along with // this program; if not, write to the Free Software Foundation, Inc., 59 Temple // Place, Suite 330, Boston, MA 02111-1307 USA //============================================================================== #include #include #include #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() { // 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); }