You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

serviceProgram.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // \file serviceProgram.cpp
  2. //
  3. // Service program for testing CodeDweller::Service.
  4. //
  5. // Usage:
  6. //
  7. // serviceProgram <logFileName>
  8. //
  9. // where <logFileName> is the name of a file to write to.
  10. //
  11. // This program:
  12. //
  13. // 1) Registers callbacks for various messages.
  14. //
  15. // 2) While the stop flag is false, outputs the status of all flags
  16. // to the log file every 2 seconds, and clears all flags.
  17. //
  18. // 3) After Stop is received, output the status of all flags, and
  19. // exit.
  20. //
  21. // Copyright (C) 2014 MicroNeil Research Corporation.
  22. //
  23. // This program is part of the MicroNeil Research Open Library Project. For
  24. // more information go to http://www.microneil.com/OpenLibrary/index.html
  25. //
  26. // This program is free software; you can redistribute it and/or modify it
  27. // under the terms of the GNU General Public License as published by the
  28. // Free Software Foundation; either version 2 of the License, or (at your
  29. // option) any later version.
  30. //
  31. // This program is distributed in the hope that it will be useful, but WITHOUT
  32. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  33. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  34. // more details.
  35. //
  36. // You should have received a copy of the GNU General Public License along with
  37. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  38. // Place, Suite 330, Boston, MA 02111-1307 USA
  39. //==============================================================================
  40. #include <cstdlib>
  41. #include <fstream>
  42. #include <thread>
  43. #include "CodeDweller/service.hpp"
  44. /// Callback functor for Pause message.
  45. class PauseCallback : public CodeDweller::Service::Callback {
  46. public:
  47. PauseCallback() : pauseFlag(false) {}
  48. bool pauseFlag;
  49. void operator()() {
  50. pauseFlag = true;
  51. }
  52. };
  53. PauseCallback pauseCbck;
  54. PauseCallback pauseCbck1;
  55. /// Callback functor for Resume message.
  56. class ResumeCallback : public CodeDweller::Service::Callback {
  57. public:
  58. ResumeCallback() : resumeFlag(false) {}
  59. bool resumeFlag;
  60. void operator()() {
  61. resumeFlag = true;
  62. }
  63. };
  64. ResumeCallback resumeCbck;
  65. ResumeCallback resumeCbck1;
  66. /// Callback functor for Stop message.
  67. class StopCallback : public CodeDweller::Service::Callback {
  68. public:
  69. StopCallback() : stopFlag(false) {}
  70. bool stopFlag;
  71. void operator()() {
  72. stopFlag = true;
  73. }
  74. };
  75. StopCallback stopCbck;
  76. StopCallback stopCbck1;
  77. StopCallback notStopCbck;
  78. int CodeDweller::Service::run() {
  79. // Get the singleton.
  80. CodeDweller::Service &svc = CodeDweller::Service::getInstance();
  81. // Get the log file name.
  82. auto arguments = svc.arguments();
  83. if (arguments.size() != 2) {
  84. return(EXIT_FAILURE);
  85. }
  86. // Get log file.
  87. std::ofstream logStream(arguments[1]);
  88. // Register the callbacks.
  89. svc.onPauseCall(pauseCbck);
  90. svc.onPauseCall(pauseCbck1);
  91. svc.onResumeCall(resumeCbck);
  92. svc.onResumeCall(resumeCbck1);
  93. svc.onStopCall(stopCbck);
  94. svc.onStopCall(stopCbck1);
  95. while (!stopCbck.stopFlag) {
  96. logStream << "Sleeping 2 s...";
  97. logStream.flush();
  98. std::this_thread::sleep_for(std::chrono::milliseconds(2000));
  99. logStream << "done." << std::endl;
  100. logStream << "receivedPause(): " << svc.receivedPause() << std::endl;
  101. logStream << "receivedResume(): " << svc.receivedResume() << std::endl;
  102. logStream << "receivedStop(): " << svc.receivedStop() << std::endl;
  103. logStream << "Clearing all flags." << std::endl;
  104. svc.clearReceivedPause();
  105. svc.clearReceivedResume();
  106. svc.clearReceivedStop();
  107. logStream << "receivedPause(): " << svc.receivedPause() << std::endl;
  108. logStream << "receivedResume(): " << svc.receivedResume() << std::endl;
  109. logStream << "receivedStop(): " << svc.receivedStop() << std::endl;
  110. logStream << "pauseCbck.pauseFlag: " << pauseCbck.pauseFlag << std::endl;
  111. logStream << "pauseCbck1.pauseFlag: " << pauseCbck1.pauseFlag << std::endl;
  112. logStream << "resumeCbck.resumeFlag: " << resumeCbck.resumeFlag
  113. << std::endl;
  114. logStream << "resumeCbck1.resumeFlag: " << resumeCbck1.resumeFlag
  115. << std::endl;
  116. logStream << "stopCbck.stopFlag: " << stopCbck.stopFlag << std::endl;
  117. logStream << "stopCbck1.stopFlag: " << stopCbck1.stopFlag << std::endl;
  118. logStream << "notStopCbck.stopFlag: " << notStopCbck.stopFlag << std::endl;
  119. pauseCbck.pauseFlag = false;
  120. pauseCbck1.pauseFlag = false;
  121. resumeCbck.resumeFlag = false;
  122. resumeCbck1.resumeFlag = false;
  123. logStream << std::endl;
  124. }
  125. logStream << "Exiting." << std::endl;
  126. logStream.close();
  127. return(EXIT_SUCCESS);
  128. }