Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Poco/Net/SocketReactor.h"
- #include "Poco/Net/SocketAcceptor.h"
- #include "Poco/Net/SocketNotification.h"
- #include "Poco/Net/StreamSocket.h"
- #include "Poco/Net/ServerSocket.h"
- #include "Poco/NObserver.h"
- #include "Poco/Exception.h"
- #include "Poco/Thread.h"
- #include "Poco/Util/ServerApplication.h"
- #include "Poco/Util/Option.h"
- #include "Poco/Util/OptionSet.h"
- #include "Poco/Util/HelpFormatter.h"
- #include "Poco/Util/RegExpValidator.h"
- #include "Poco/Util/IntValidator.h"
- #include <cstdio>
- #include <iostream>
- #include <string>
- #include <cmath>
- #include <boost/lexical_cast.hpp>
- using namespace Poco::Net;
- using namespace Poco;
- using namespace Poco::Util;
- using namespace std;
- using namespace boost;
- class EchoServiceHandler {
- public:
- EchoServiceHandler(StreamSocket& socket, SocketReactor& reactor) :
- _socket(socket), _reactor(reactor), _pBuffer(new char[1024]) {
- _reactor.addEventHandler(
- _socket,
- NObserver<EchoServiceHandler, ReadableNotification>(*this,
- &EchoServiceHandler::onReadable));
- _reactor.addEventHandler(_socket, NObserver<EchoServiceHandler, ShutdownNotification>(*this,
- &EchoServiceHandler::onShutdown));
- }
- ~EchoServiceHandler() {
- _reactor.removeEventHandler(_socket, NObserver<EchoServiceHandler, ReadableNotification>(*this,
- &EchoServiceHandler::onReadable));
- _reactor.removeEventHandler(_socket, NObserver<EchoServiceHandler, ShutdownNotification>(*this,
- &EchoServiceHandler::onShutdown));
- delete[] _pBuffer;
- }
- void onReadable(const AutoPtr<ReadableNotification>& pNf) {
- int n = _socket.receiveBytes(_pBuffer, 1023);
- if (n > 0) {
- _pBuffer[n] = 0;
- int t;sscanf(_pBuffer, "%d", &t);
- string r = "[";bool fst = true;
- for (int i = 2; i <= sqrt(t); i++)
- while (t % i == 0) {
- t /= i;
- r += (fst ? (fst = false, "") : ", ") + lexical_cast<string>(i);
- }
- if(t > 1)r += (fst ? (fst = false, "") : ", ") + lexical_cast<string>(t);
- _socket.sendBytes((r + "]").c_str(), r.size() + 1);
- } else
- delete this;
- }
- void onShutdown(const AutoPtr<ShutdownNotification>& pNf) {
- delete this;
- }
- private:
- StreamSocket _socket;
- SocketReactor& _reactor;
- char* _pBuffer;
- };
- class EchoServer: public Poco::Util::ServerApplication
- {
- public:
- EchoServer() : _helpRequested(false), _isServer(false), _port(8562), _host("0.0.0.0") {}
- ~EchoServer() {}
- protected:
- void initialize(Application& self) {
- loadConfiguration(); // load default configuration files, if present
- ServerApplication::initialize(self);
- }
- void uninitialize() {
- ServerApplication::uninitialize();
- }
- void defineOptions(OptionSet& options) {
- ServerApplication::defineOptions(options);
- options.addOption(Option("help", "h", "display help information on command line arguments")
- .required(false).repeatable(false));
- options.addOption(Option("mode", "m", "operation mode, server or client, default - client")
- .required(false).repeatable(false).argument("MODE")
- .validator(new RegExpValidator("(client|server)")));
- options.addOption(Option("port", "p", "port, default is 8562").argument("PORT").required(false)
- .repeatable(false).validator(new IntValidator(1, 65535)));
- options.addOption(Option("host", "c", "host to connect or to bind").required(false)
- .repeatable(false).argument("HOST"));
- }
- void handleOption(const std::string& name, const std::string& value) {
- ServerApplication::handleOption(name, value);
- if (name == "help")_helpRequested = true;
- if (name == "mode" && value == "server")_isServer = true;
- if (name == "port")_port = lexical_cast<int>(value);
- if (name == "host"){_host = value;_hostSetted = true;}
- }
- void displayHelp() {
- HelpFormatter helpFormatter(options());
- helpFormatter.setCommand(commandName());
- helpFormatter.setUsage("OPTIONS");
- helpFormatter.setHeader("An echo server implemented using the Reactor and Acceptor patterns.");
- helpFormatter.format(std::cout);
- }
- int main(const std::vector<std::string>& args) {
- if (_helpRequested || (!_isServer && !_hostSetted))displayHelp();
- else if(_isServer) {
- ServerSocket svs(SocketAddress(_host, _port));
- SocketReactor reactor;
- SocketAcceptor<EchoServiceHandler> acceptor(svs, reactor);
- Thread thread;
- thread.start(reactor);
- waitForTerminationRequest();
- reactor.stop();
- thread.join();
- }
- else
- {
- string num; cin >> num;
- StreamSocket svs(SocketAddress(_host, _port));
- svs.sendBytes(num.c_str(), num.length());
- char buf[4097];
- int sz = svs.receiveBytes(buf, 4096);
- buf[sz] = 0;
- cout << "Result: " << buf << '\n';
- }
- return Application::EXIT_OK;
- }
- private:
- bool _helpRequested;
- bool _isServer;
- int _port;
- string _host;
- bool _hostSetted;
- };
- int main(int argc, char** argv) {
- EchoServer app;
- return app.run(argc, argv);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement