|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- // \file serviceProgram.cpp
- //
- // 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.
- //
- // 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 <unistd.h>
-
- #include <cstdlib>
- #include <fstream>
-
- #include "CodeDweller/service.hpp"
-
- /// Callback functor for Pause message.
- class PauseCallback : public CodeDweller::Service::Callback {
-
- public:
-
- PauseCallback() : pauseFlag(false) {}
-
- bool pauseFlag;
-
- void operator()() {
- pauseFlag = true;
- }
-
- };
-
- PauseCallback pauseCbck;
- PauseCallback pauseCbck1;
-
- /// Callback functor for Resume message.
- class ResumeCallback : public CodeDweller::Service::Callback {
-
- public:
-
- ResumeCallback() : resumeFlag(false) {}
-
- bool resumeFlag;
-
- void operator()() {
- resumeFlag = true;
- }
-
- };
-
- ResumeCallback resumeCbck;
- ResumeCallback resumeCbck1;
-
- /// Callback functor for Restart message.
- class RestartCallback : public CodeDweller::Service::Callback {
-
- public:
-
- RestartCallback() : restartFlag(false) {}
-
- bool restartFlag;
-
- void operator()() {
- restartFlag = true;
- }
-
- };
-
- RestartCallback restartCbck;
- RestartCallback restartCbck1;
-
- /// Callback functor for Stop message.
- class StopCallback : public CodeDweller::Service::Callback {
-
- public:
-
- StopCallback() : stopFlag(false) {}
-
- bool stopFlag;
-
- void operator()() {
- stopFlag = true;
- }
-
- };
-
- StopCallback stopCbck;
- StopCallback stopCbck1;
- StopCallback notStopCbck;
-
- int CodeDweller::Service::run() {
-
- // Get the singleton.
- CodeDweller::Service &svc = CodeDweller::Service::getInstance();
-
- // Get the log file name.
- auto arguments = svc.arguments();
-
- if (arguments.size() != 2) {
- return(EXIT_FAILURE);
- }
-
- // Get log file.
- std::ofstream logStream(arguments[1]);
-
- // Register the callbacks.
- svc.onPauseCall(pauseCbck);
- svc.onPauseCall(pauseCbck1);
-
- svc.onResumeCall(resumeCbck);
- svc.onResumeCall(resumeCbck1);
-
- svc.onRestartCall(restartCbck);
- svc.onRestartCall(restartCbck1);
-
- svc.onStopCall(stopCbck);
- svc.onStopCall(stopCbck1);
-
- while (!stopCbck.stopFlag) {
-
- logStream << "Sleeping 2 s" << std::endl;
- sleep(2);
-
- logStream << "receivedPause(): " << svc.receivedPause() << std::endl;
- logStream << "receivedResume(): " << svc.receivedResume() << std::endl;
- logStream << "receivedRestart(): " << svc.receivedRestart() << std::endl;
- logStream << "receivedStop(): " << svc.receivedStop() << std::endl;
-
- logStream << "Clearing all flags." << std::endl;
- svc.clearReceivedPause();
- svc.clearReceivedResume();
- svc.clearReceivedRestart();
- svc.clearReceivedStop();
-
- logStream << "receivedPause(): " << svc.receivedPause() << std::endl;
- logStream << "receivedResume(): " << svc.receivedResume() << std::endl;
- logStream << "receivedRestart(): " << svc.receivedRestart() << std::endl;
- logStream << "receivedStop(): " << svc.receivedStop() << std::endl;
-
- logStream << "pauseCbck.pauseFlag: " << pauseCbck.pauseFlag << std::endl;
- logStream << "pauseCbck1.pauseFlag: " << pauseCbck1.pauseFlag << std::endl;
- logStream << "resumeCbck.resumeFlag: " << resumeCbck.resumeFlag
- << std::endl;
- logStream << "resumeCbck1.resumeFlag: " << resumeCbck1.resumeFlag
- << std::endl;
- logStream << "restartCbck.restartFlag: " << restartCbck.restartFlag
- << std::endl;
- logStream << "restartCbck1.restartFlag: " << restartCbck1.restartFlag
- << std::endl;
- logStream << "stopCbck.stopFlag: " << stopCbck.stopFlag << std::endl;
- logStream << "stopCbck1.stopFlag: " << stopCbck1.stopFlag << std::endl;
- logStream << "notStopCbck.stopFlag: " << notStopCbck.stopFlag << std::endl;
-
- pauseCbck.pauseFlag = false;
- pauseCbck1.pauseFlag = false;
- resumeCbck.resumeFlag = false;
- resumeCbck1.resumeFlag = false;
- restartCbck.restartFlag = false;
- restartCbck1.restartFlag = false;
-
- logStream << std::endl;
-
- }
-
- logStream << "Exiting." << std::endl;
-
- logStream.close();
-
- return(EXIT_SUCCESS);
- }
|