Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // \file service.hpp
  2. //
  3. // Copyright (C) 2014 MicroNeil Research Corporation.
  4. //
  5. // This program is part of the MicroNeil Research Open Library Project. For
  6. // more information go to http://www.microneil.com/OpenLibrary/index.html
  7. //
  8. // This program is free software; you can redistribute it and/or modify it
  9. // under the terms of the GNU General Public License as published by the
  10. // Free Software Foundation; either version 2 of the License, or (at your
  11. // option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful, but WITHOUT
  14. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. // more details.
  17. //
  18. // You should have received a copy of the GNU General Public License along with
  19. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. // Place, Suite 330, Boston, MA 02111-1307 USA
  21. //==============================================================================
  22. /*
  23. \brief The service module provides a framework for implementing *nix
  24. daemons and Windows services.
  25. */
  26. #ifndef SERVICE_HPP
  27. #define SERVICE_HPP
  28. #include <string>
  29. #include <vector>
  30. namespace CodeDweller {
  31. /** Singleton class that implements a daemon (*nix) or service
  32. (Windows).
  33. This class implements a *nix daemon or a Windows service.
  34. To implement a daemon or service, implement the required methods
  35. of this class, and link with service.cpp. When compiled under
  36. *nix, the file service.cpp contains the definition of main for
  37. the *nix daemon. When compiled under Windows, the file
  38. service.cpp contains the entry points for the Windows service.
  39. Nomenclature:
  40. <ol>
  41. <li>Service is a *nix daemon or Windows service.</li>
  42. <li>Message is a posix signal or Windows message. This class
  43. supports the following messages:
  44. <ol>
  45. <li>Pause. This is the posix TSTP signal or Windows Pause
  46. message. This instructs the service to temporarily stop
  47. running.</li>
  48. <li>Resume. This is the posix CONT signal or Windows
  49. Resume message. This instructs the service to resume
  50. running after receiving a Pause message. If the service
  51. isn't temporarily stopped after receiving a Pause message,
  52. the Resume message has no effect.</li>
  53. <li>Restart. This is the posix HUP signal or Windows
  54. Restart message. This instructs the service to shut down
  55. and restart.</li>
  56. <li>Stop. This is the posix TERM signal or Windows Stop
  57. message. This instructs the service to shut down and
  58. exit.</li>
  59. </ol>
  60. </li>
  61. <li>Callback. This is a function that is executed when a
  62. message is received.</li>
  63. </ol>
  64. */
  65. class Service
  66. {
  67. public:
  68. /// Get the instance of the singleton.
  69. static Service& getInstance()
  70. {
  71. static Service service;
  72. return service;
  73. }
  74. /// Callback functor interface.
  75. class Callback {
  76. public:
  77. /// Callback method.
  78. virtual void operator()() = 0;
  79. };
  80. /// Main entry point.
  81. //
  82. // \param[in] argc is the number of arguments.
  83. //
  84. // \param[in] argv is an array of strings containing the
  85. // command-line arguments. The end of the array is indicated by a
  86. // null pointer.
  87. //
  88. // \returns exit code of the service.
  89. //
  90. int main(int argc, char *argv[]);
  91. /// Register a callback for receipt of Stop.
  92. //
  93. // \param[in] stopFunctor is the function object to invoke when
  94. // Stop is received.
  95. //
  96. void onStopCall(Callback *stopFunctor);
  97. /// Check whether Pause was received.
  98. //
  99. // \returns if the Pause message was received, false otherwise.
  100. //
  101. bool receivedPause();
  102. /// Check whether Resume was received.
  103. //
  104. // \returns if the Resume message was received, false otherwise.
  105. //
  106. bool receivedResume();
  107. /// Check whether Restart was received.
  108. //
  109. // \returns if the Restart message was received, false otherwise.
  110. //
  111. bool receivedRestart();
  112. /// Check whether the last message received was Stop.
  113. //
  114. // \returns true if Stop was the most recent message received,
  115. // false otherwise.
  116. //
  117. bool receivedStop();
  118. /// Clear receiving the Pause message.
  119. void clearReceivedPause();
  120. /// Clear receiving the Resume message.
  121. void clearReceivedResume();
  122. /// Clear receiving the Restart message.
  123. void clearReceivedRestart();
  124. /// Clear receiving the Stop message.
  125. void clearReceivedStop();
  126. /// Get a reference to the command-line arguments.
  127. //
  128. // \returns a reference to the vector of command-line arguments of
  129. // the application. Index i corresponds to command-line argument
  130. // i.
  131. //
  132. const std::vector<std::string> &arguments();
  133. private:
  134. /// Private constructor prevents instantiation.
  135. Service();
  136. /// Prevent copying.
  137. Service(Service const&) {}
  138. /// Prevent assignment.
  139. void operator=(Service const&) {}
  140. /// Load the command-line arguments.
  141. //
  142. // This method loads the object with the command-line parameters.
  143. //
  144. // \param[in] argc is the number of arguments.
  145. //
  146. // \param[in] argv is an array of strings containing the
  147. // command-line arguments. The end of the array is indicated by a
  148. // null pointer.
  149. //
  150. void loadArguments(int argc, char *argv[]);
  151. /// Initialize and run the application.
  152. //
  153. // \returns the exit status of the service.
  154. //
  155. int run();
  156. /// Thread start function to receive messages.
  157. void processMessages();
  158. /// Command-line arguments.
  159. std::vector<std::string> cmdLineArgs;
  160. /// Enumeration specifying the most recent message received.
  161. enum class Message {
  162. Pause,
  163. Resume,
  164. Restart,
  165. Stop,
  166. None
  167. };
  168. /// True if Pause message was received.
  169. bool pauseReceived;
  170. /// True if Resume message was received.
  171. bool resumeReceived;
  172. /// True if Restart message was received.
  173. bool restartReceived;
  174. /// True if Stop message was received.
  175. bool stopReceived;
  176. /// Functions to invoke when the Stop is received.
  177. std::vector<Callback *> stopCallbacks;
  178. /// Set of signals to wait for.
  179. sigset_t signalSet;
  180. };
  181. }
  182. #endif // SERVICE_HPP