Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

serviceProgram.cpp 5.3KB

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