Advertisement
Guest User

server

a guest
Feb 10th, 2012
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.80 KB | None | 0 0
  1. #include "Poco/Net/SocketReactor.h"
  2. #include "Poco/Net/SocketAcceptor.h"
  3. #include "Poco/Net/SocketNotification.h"
  4. #include "Poco/Net/StreamSocket.h"
  5. #include "Poco/Net/ServerSocket.h"
  6. #include "Poco/NObserver.h"
  7. #include "Poco/Exception.h"
  8. #include "Poco/Thread.h"
  9. #include "Poco/Util/ServerApplication.h"
  10. #include "Poco/Util/Option.h"
  11. #include "Poco/Util/OptionSet.h"
  12. #include "Poco/Util/HelpFormatter.h"
  13. #include "Poco/Util/RegExpValidator.h"
  14. #include "Poco/Util/IntValidator.h"
  15. #include <cstdio>
  16. #include <iostream>
  17. #include <string>
  18. #include <cmath>
  19. #include <boost/lexical_cast.hpp>
  20. using namespace Poco::Net;
  21. using namespace Poco;
  22. using namespace Poco::Util;
  23. using namespace std;
  24. using namespace boost;
  25. class EchoServiceHandler {
  26. public:
  27.     EchoServiceHandler(StreamSocket& socket, SocketReactor& reactor) :
  28.             _socket(socket), _reactor(reactor), _pBuffer(new char[1024]) {
  29.         _reactor.addEventHandler(
  30.                 _socket,
  31.                 NObserver<EchoServiceHandler, ReadableNotification>(*this,
  32.                         &EchoServiceHandler::onReadable));
  33.         _reactor.addEventHandler(_socket, NObserver<EchoServiceHandler, ShutdownNotification>(*this,
  34.                         &EchoServiceHandler::onShutdown));
  35.     }
  36.     ~EchoServiceHandler() {
  37.         _reactor.removeEventHandler(_socket, NObserver<EchoServiceHandler, ReadableNotification>(*this,
  38.                         &EchoServiceHandler::onReadable));
  39.         _reactor.removeEventHandler(_socket, NObserver<EchoServiceHandler, ShutdownNotification>(*this,
  40.                         &EchoServiceHandler::onShutdown));
  41.         delete[] _pBuffer;
  42.     }
  43.     void onReadable(const AutoPtr<ReadableNotification>& pNf) {
  44.         int n = _socket.receiveBytes(_pBuffer, 1023);
  45.         if (n > 0) {
  46.             _pBuffer[n] = 0;
  47.             int t;sscanf(_pBuffer, "%d", &t);
  48.             string r = "[";bool fst = true;
  49.             for (int i = 2; i <= sqrt(t); i++)
  50.                 while (t % i == 0) {
  51.                     t /= i;
  52.                     r += (fst ? (fst = false, "") : ", ") + lexical_cast<string>(i);
  53.                 }
  54.             if(t > 1)r += (fst ? (fst = false, "") : ", ") + lexical_cast<string>(t);
  55.             _socket.sendBytes((r + "]").c_str(), r.size() + 1);
  56.         } else
  57.             delete this;
  58.     }
  59.     void onShutdown(const AutoPtr<ShutdownNotification>& pNf) {
  60.         delete this;
  61.     }
  62. private:
  63.     StreamSocket _socket;
  64.     SocketReactor& _reactor;
  65.     char* _pBuffer;
  66. };
  67. class EchoServer: public Poco::Util::ServerApplication
  68. {
  69. public:
  70.     EchoServer() :  _helpRequested(false), _isServer(false), _port(8562), _host("0.0.0.0") {}
  71.     ~EchoServer() {}
  72. protected:
  73.     void initialize(Application& self) {
  74.         loadConfiguration(); // load default configuration files, if present
  75.         ServerApplication::initialize(self);
  76.     }
  77.     void uninitialize() {
  78.         ServerApplication::uninitialize();
  79.     }
  80.     void defineOptions(OptionSet& options) {
  81.         ServerApplication::defineOptions(options);
  82.         options.addOption(Option("help", "h", "display help information on command line arguments")
  83.                 .required(false).repeatable(false));
  84.         options.addOption(Option("mode", "m", "operation mode, server or client, default - client")
  85.                 .required(false).repeatable(false).argument("MODE")
  86.                 .validator(new RegExpValidator("(client|server)")));
  87.         options.addOption(Option("port", "p", "port, default is 8562").argument("PORT").required(false)
  88.                 .repeatable(false).validator(new IntValidator(1, 65535)));
  89.         options.addOption(Option("host", "c", "host to connect or to bind").required(false)
  90.                 .repeatable(false).argument("HOST"));
  91.     }
  92.     void handleOption(const std::string& name, const std::string& value) {
  93.         ServerApplication::handleOption(name, value);
  94.         if (name == "help")_helpRequested = true;
  95.         if (name == "mode" && value == "server")_isServer = true;
  96.         if (name == "port")_port = lexical_cast<int>(value);
  97.         if (name == "host"){_host = value;_hostSetted = true;}
  98.     }
  99.     void displayHelp() {
  100.         HelpFormatter helpFormatter(options());
  101.         helpFormatter.setCommand(commandName());
  102.         helpFormatter.setUsage("OPTIONS");
  103.         helpFormatter.setHeader("An echo server implemented using the Reactor and Acceptor patterns.");
  104.         helpFormatter.format(std::cout);
  105.     }
  106.     int main(const std::vector<std::string>& args) {
  107.         if (_helpRequested || (!_isServer && !_hostSetted))displayHelp();
  108.         else if(_isServer) {
  109.             ServerSocket svs(SocketAddress(_host, _port));
  110.             SocketReactor reactor;
  111.             SocketAcceptor<EchoServiceHandler> acceptor(svs, reactor);
  112.             Thread thread;
  113.             thread.start(reactor);
  114.             waitForTerminationRequest();
  115.             reactor.stop();
  116.             thread.join();
  117.         }
  118.         else
  119.         {
  120.             string num; cin >> num;
  121.             StreamSocket svs(SocketAddress(_host, _port));
  122.             svs.sendBytes(num.c_str(), num.length());
  123.             char buf[4097];
  124.             int sz = svs.receiveBytes(buf, 4096);
  125.             buf[sz] = 0;
  126.             cout << "Result: " << buf << '\n';
  127.         }
  128.         return Application::EXIT_OK;
  129.     }
  130. private:
  131.     bool _helpRequested;
  132.     bool _isServer;
  133.     int _port;
  134.     string _host;
  135.     bool _hostSetted;
  136. };
  137. int main(int argc, char** argv) {
  138.     EchoServer app;
  139.     return app.run(argc, argv);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement