1
0

poco_echo.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // EchoServer.cpp
  3. //
  4. // $Id: //poco/1.3/Net/samples/EchoServer/src/EchoServer.cpp#1 $
  5. //
  6. // This sample demonstrates the SocketReactor and SocketAcceptor classes.
  7. //
  8. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  9. // and Contributors.
  10. //
  11. // Permission is hereby granted, free of charge, to any person or organization
  12. // obtaining a copy of the software and accompanying documentation covered by
  13. // this license (the "Software") to use, reproduce, display, distribute,
  14. // execute, and transmit the Software, and to prepare derivative works of the
  15. // Software, and to permit third-parties to whom the Software is furnished to
  16. // do so, all subject to the following:
  17. //
  18. // The copyright notices in the Software and this entire statement, including
  19. // the above license grant, this restriction and the following disclaimer,
  20. // must be included in all copies of the Software, in whole or in part, and
  21. // all derivative works of the Software, unless such copies or derivative
  22. // works are solely in the form of machine-executable object code generated by
  23. // a source language processor.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  28. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  29. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  30. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  31. // DEALINGS IN THE SOFTWARE.
  32. //
  33. #include "Poco/Net/SocketReactor.h"
  34. #include "Poco/Net/SocketAcceptor.h"
  35. #include "Poco/Net/SocketNotification.h"
  36. #include "Poco/Net/StreamSocket.h"
  37. #include "Poco/Net/ServerSocket.h"
  38. #include "Poco/NObserver.h"
  39. #include "Poco/Exception.h"
  40. #include "Poco/Thread.h"
  41. #include "Poco/Util/ServerApplication.h"
  42. #include "Poco/Util/Option.h"
  43. #include "Poco/Util/OptionSet.h"
  44. #include "Poco/Util/HelpFormatter.h"
  45. #include <iostream>
  46. using Poco::Net::SocketReactor;
  47. using Poco::Net::SocketAcceptor;
  48. using Poco::Net::ReadableNotification;
  49. using Poco::Net::ShutdownNotification;
  50. using Poco::Net::ServerSocket;
  51. using Poco::Net::StreamSocket;
  52. using Poco::NObserver;
  53. using Poco::AutoPtr;
  54. using Poco::Thread;
  55. using Poco::Util::ServerApplication;
  56. using Poco::Util::Application;
  57. using Poco::Util::Option;
  58. using Poco::Util::OptionSet;
  59. using Poco::Util::HelpFormatter;
  60. class EchoServiceHandler
  61. {
  62. public:
  63. EchoServiceHandler(StreamSocket& socket, SocketReactor& reactor):
  64. _socket(socket),
  65. _reactor(reactor),
  66. _pBuffer(new char[BUFFER_SIZE])
  67. {
  68. // Application& app = Application::instance();
  69. // app.logger().information("Connection from " + socket.peerAddress().toString());
  70. _reactor.addEventHandler(_socket, NObserver<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onReadable));
  71. _reactor.addEventHandler(_socket, NObserver<EchoServiceHandler, ShutdownNotification>(*this, &EchoServiceHandler::onShutdown));
  72. }
  73. ~EchoServiceHandler()
  74. {
  75. // Application& app = Application::instance();
  76. // app.logger().information("Disconnecting " + _socket.peerAddress().toString());
  77. _reactor.removeEventHandler(_socket, NObserver<EchoServiceHandler, ReadableNotification>(*this, &EchoServiceHandler::onReadable));
  78. _reactor.removeEventHandler(_socket, NObserver<EchoServiceHandler, ShutdownNotification>(*this, &EchoServiceHandler::onShutdown));
  79. delete [] _pBuffer;
  80. }
  81. void onReadable(const AutoPtr<ReadableNotification>& pNf)
  82. {
  83. int n = _socket.receiveBytes(_pBuffer, BUFFER_SIZE);
  84. if (n > 0)
  85. _socket.sendBytes(_pBuffer, n);
  86. else
  87. delete this;
  88. }
  89. void onShutdown(const AutoPtr<ShutdownNotification>& pNf)
  90. {
  91. delete this;
  92. }
  93. private:
  94. enum
  95. {
  96. BUFFER_SIZE = 1024
  97. };
  98. StreamSocket _socket;
  99. SocketReactor& _reactor;
  100. char* _pBuffer;
  101. };
  102. class EchoServer: public Poco::Util::ServerApplication
  103. /// The main application class.
  104. ///
  105. /// This class handles command-line arguments and
  106. /// configuration files.
  107. /// Start the EchoServer executable with the help
  108. /// option (/help on Windows, --help on Unix) for
  109. /// the available command line options.
  110. ///
  111. /// To use the sample configuration file (EchoServer.properties),
  112. /// copy the file to the directory where the EchoServer executable
  113. /// resides. If you start the debug version of the EchoServer
  114. /// (EchoServerd[.exe]), you must also create a copy of the configuration
  115. /// file named EchoServerd.properties. In the configuration file, you
  116. /// can specify the port on which the server is listening (default
  117. /// 9977) and the format of the date/time string sent back to the client.
  118. ///
  119. /// To test the EchoServer you can use any telnet client (telnet localhost 9977).
  120. {
  121. public:
  122. EchoServer(): _helpRequested(false)
  123. {
  124. }
  125. ~EchoServer()
  126. {
  127. }
  128. protected:
  129. void initialize(Application& self)
  130. {
  131. loadConfiguration(); // load default configuration files, if present
  132. ServerApplication::initialize(self);
  133. }
  134. void uninitialize()
  135. {
  136. ServerApplication::uninitialize();
  137. }
  138. void defineOptions(OptionSet& options)
  139. {
  140. ServerApplication::defineOptions(options);
  141. options.addOption(
  142. Option("help", "h", "display help information on command line arguments")
  143. .required(false)
  144. .repeatable(false));
  145. }
  146. void handleOption(const std::string& name, const std::string& value)
  147. {
  148. ServerApplication::handleOption(name, value);
  149. if (name == "help")
  150. _helpRequested = true;
  151. }
  152. void displayHelp()
  153. {
  154. HelpFormatter helpFormatter(options());
  155. helpFormatter.setCommand(commandName());
  156. helpFormatter.setUsage("OPTIONS");
  157. helpFormatter.setHeader("An echo server implemented using the Reactor and Acceptor patterns.");
  158. helpFormatter.format(std::cout);
  159. }
  160. int main(const std::vector<std::string>& args)
  161. {
  162. if (_helpRequested)
  163. {
  164. displayHelp();
  165. }
  166. else
  167. {
  168. if (args.size() < 1) {
  169. printf("Usage: cmd port\n");
  170. return -10;
  171. }
  172. int port = atoi(args[0].c_str());
  173. // set-up a server socket
  174. ServerSocket svs(port);
  175. // set-up a SocketReactor...
  176. SocketReactor reactor;
  177. // ... and a SocketAcceptor
  178. SocketAcceptor<EchoServiceHandler> acceptor(svs, reactor);
  179. // run the reactor in its own thread so that we can wait for
  180. // a termination request
  181. Thread thread;
  182. thread.start(reactor);
  183. // wait for CTRL-C or kill
  184. waitForTerminationRequest();
  185. // Stop the SocketReactor
  186. reactor.stop();
  187. thread.join();
  188. }
  189. return Application::EXIT_OK;
  190. }
  191. private:
  192. bool _helpRequested;
  193. };
  194. int main(int argc, char** argv)
  195. {
  196. EchoServer app;
  197. return app.run(argc, argv);
  198. }