#include #include #include #include #include #include #include #include #include "gen-cpp/myService.h" using namespace std; using namespace boost; using namespace apache::thrift; using namespace apache::thrift::protocol; using namespace apache::thrift::transport; using namespace apache::thrift::server; using namespace apache::thrift::concurrency; class myService : virtual public myServiceIf { public: myService() {} void answer(std::string& answ, const std::string& question) { // implemented on the client-side only } }; class t_connection { public: shared_ptr _service; shared_ptr _processor; shared_ptr _protocolInp; shared_ptr _protocolOut; shared_ptr _transport; }; shared_ptr CONNECTION; class myProcessorFactory : public TProcessorFactory { public: myProcessorFactory() {}; shared_ptr getProcessor(const TConnectionInfo& info) { shared_ptr service(new myService()); shared_ptr processor(new myServiceProcessor(service)); cout << "connection " << endl; CONNECTION.reset(new t_connection); CONNECTION->_service = service; CONNECTION->_processor = processor; CONNECTION->_protocolInp = info.input; CONNECTION->_protocolOut = info.output; CONNECTION->_transport = info.transport; return processor; } }; class t_serverThread : public apache::thrift::concurrency::Runnable { public: t_serverThread() {} ~t_serverThread() {} void run() { int port = 9090; shared_ptr serverTransport(new TServerSocket(port)); shared_ptr processorFactory(new myProcessorFactory()); shared_ptr transportFactory(new TBufferedTransportFactory()); shared_ptr protocolFactory(new TBinaryProtocolFactory()); TThreadedServer server(processorFactory, serverTransport, transportFactory, protocolFactory); server.serve(); } }; int main(int argc, char **argv) { shared_ptr threadFactory(new PosixThreadFactory); shared_ptr serverThread(new t_serverThread); shared_ptr thread = threadFactory->newThread(serverThread); thread->start(); cout << "server thread started" << endl; while (true) { cout << "sleep ..." << endl; if (CONNECTION) { string answ; cout << "CONNECTION " << endl; CONNECTION->_service->answer(answ, "How are you?"); } sleep(1); } return 0; }