[4942] | 1 | #include <iostream>
|
---|
| 2 | #include <string>
|
---|
| 3 |
|
---|
| 4 | #include <thrift/protocol/TBinaryProtocol.h>
|
---|
[4943] | 5 | #include <thrift/server/TThreadedServer.h>
|
---|
[4942] | 6 | #include <thrift/transport/TServerSocket.h>
|
---|
| 7 | #include <thrift/transport/TBufferTransports.h>
|
---|
[4946] | 8 | #include <thrift/concurrency/Thread.h>
|
---|
[4948] | 9 | #include <thrift/concurrency/PosixThreadFactory.h>
|
---|
[4942] | 10 |
|
---|
| 11 | #include "gen-cpp/myService.h"
|
---|
| 12 |
|
---|
| 13 | using namespace std;
|
---|
| 14 | using namespace boost;
|
---|
[4948] | 15 | using namespace apache::thrift;
|
---|
| 16 | using namespace apache::thrift::protocol;
|
---|
| 17 | using namespace apache::thrift::transport;
|
---|
| 18 | using namespace apache::thrift::server;
|
---|
| 19 | using namespace apache::thrift::concurrency;
|
---|
[4942] | 20 |
|
---|
[4944] | 21 | class myService : virtual public myServiceIf {
|
---|
[4942] | 22 | public:
|
---|
[4944] | 23 | myService() {}
|
---|
[4943] | 24 | void answer(std::string& answ, const std::string& question) {
|
---|
[4942] | 25 | // implemented on the client-side only
|
---|
| 26 | }
|
---|
| 27 | };
|
---|
| 28 |
|
---|
[4945] | 29 | class t_connection {
|
---|
[4942] | 30 | public:
|
---|
[4945] | 31 | shared_ptr<myService> _service;
|
---|
| 32 | shared_ptr<TProcessor> _processor;
|
---|
| 33 | shared_ptr<TProtocol> _protocolInp;
|
---|
| 34 | shared_ptr<TProtocol> _protocolOut;
|
---|
| 35 | shared_ptr<TTransport> _transport;
|
---|
| 36 | };
|
---|
| 37 |
|
---|
[4947] | 38 | shared_ptr<t_connection> CONNECTION;
|
---|
| 39 |
|
---|
[4945] | 40 | class myProcessorFactory : public TProcessorFactory {
|
---|
| 41 | public:
|
---|
[4943] | 42 | myProcessorFactory() {};
|
---|
[4944] | 43 | shared_ptr<TProcessor> getProcessor(const TConnectionInfo& info) {
|
---|
| 44 | shared_ptr<myService> service(new myService());
|
---|
| 45 | shared_ptr<TProcessor> processor(new myServiceProcessor(service));
|
---|
| 46 | cout << "connection " << endl;
|
---|
[4947] | 47 |
|
---|
[4948] | 48 | CONNECTION.reset(new t_connection);
|
---|
| 49 | CONNECTION->_service = service;
|
---|
| 50 | CONNECTION->_processor = processor;
|
---|
| 51 | CONNECTION->_protocolInp = info.input;
|
---|
| 52 | CONNECTION->_protocolOut = info.output;
|
---|
| 53 | CONNECTION->_transport = info.transport;
|
---|
[4945] | 54 |
|
---|
[4943] | 55 | return processor;
|
---|
[4942] | 56 | }
|
---|
| 57 | };
|
---|
| 58 |
|
---|
[4948] | 59 | class t_serverThread : public apache::thrift::concurrency::Runnable {
|
---|
[4946] | 60 | public:
|
---|
| 61 | t_serverThread() {}
|
---|
[4948] | 62 | ~t_serverThread() {}
|
---|
[4946] | 63 | void run() {
|
---|
| 64 | int port = 9090;
|
---|
| 65 | shared_ptr<TServerSocket> serverTransport(new TServerSocket(port));
|
---|
| 66 | shared_ptr<myProcessorFactory> processorFactory(new myProcessorFactory());
|
---|
| 67 | shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
|
---|
| 68 | shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
|
---|
| 69 |
|
---|
| 70 | TThreadedServer server(processorFactory, serverTransport,
|
---|
| 71 | transportFactory, protocolFactory);
|
---|
| 72 | server.serve();
|
---|
[4948] | 73 | }
|
---|
[4946] | 74 | };
|
---|
| 75 |
|
---|
[4942] | 76 | int main(int argc, char **argv) {
|
---|
| 77 |
|
---|
[4948] | 78 | shared_ptr<PosixThreadFactory> threadFactory(new PosixThreadFactory);
|
---|
[4942] | 79 |
|
---|
[4948] | 80 | shared_ptr<t_serverThread> serverThread(new t_serverThread);
|
---|
| 81 |
|
---|
| 82 | shared_ptr<Thread> thread = threadFactory->newThread(serverThread);
|
---|
| 83 | thread->start();
|
---|
| 84 |
|
---|
| 85 | cout << "server thread started" << endl;
|
---|
| 86 |
|
---|
[4946] | 87 | while (true) {
|
---|
[4948] | 88 | cout << "sleep ..." << endl;
|
---|
[4946] | 89 | sleep(1);
|
---|
| 90 | }
|
---|
[4942] | 91 |
|
---|
| 92 | return 0;
|
---|
| 93 | }
|
---|
| 94 |
|
---|