123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- // \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 callbacks for various messages.
- //
- // 2) While the stop flag is false, outputs the status of 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
- // 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 <cstdlib>
- #include <fstream>
- #include <thread>
-
- #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 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.onStopCall(stopCbck);
- svc.onStopCall(stopCbck1);
-
- while (!stopCbck.stopFlag) {
-
- logStream << "Sleeping 2 s...";
- logStream.flush();
- std::this_thread::sleep_for(std::chrono::milliseconds(2000));
- logStream << "done." << std::endl;
-
- logStream << "receivedPause(): " << svc.receivedPause() << std::endl;
- logStream << "receivedResume(): " << svc.receivedResume() << std::endl;
- logStream << "receivedStop(): " << svc.receivedStop() << std::endl;
-
- logStream << "Clearing all flags." << std::endl;
- svc.clearReceivedPause();
- svc.clearReceivedResume();
- svc.clearReceivedStop();
-
- logStream << "receivedPause(): " << svc.receivedPause() << std::endl;
- logStream << "receivedResume(): " << svc.receivedResume() << 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 << "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;
-
- logStream << std::endl;
-
- }
-
- logStream << "Exiting." << std::endl;
-
- logStream.close();
-
- return(EXIT_SUCCESS);
- }
|