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 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 Stop message.
  50. class StopCallback : public CodeDweller::Service::Callback {
  51. public:
  52. StopCallback() : stopFlag(false) {}
  53. bool stopFlag;
  54. void operator()() {
  55. stopFlag = true;
  56. }
  57. };
  58. StopCallback stopCallback;
  59. int CodeDweller::Service::run() {
  60. // Get the singleton.
  61. CodeDweller::Service &service = CodeDweller::Service::getInstance();
  62. // Get the log file name.
  63. auto arguments = service.arguments();
  64. if (arguments.size() != 2) {
  65. return(EXIT_FAILURE);
  66. }
  67. // Get log file.
  68. std::ofstream logStream(arguments[1]);
  69. // Register the Stop callback.
  70. service.onStopCall(&stopCallback);
  71. while (!stopCallback.stopFlag) {
  72. logStream << "Sleeping 2 s" << std::endl;
  73. sleep(2);
  74. }
  75. logStream << "receivedStop(): " << service.receivedStop() << std::endl;
  76. service.clearReceivedStop();
  77. logStream << "receivedStop(): " << service.receivedStop() << std::endl;
  78. logStream.close();
  79. return(EXIT_SUCCESS);
  80. }