[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() {}
|
---|
[4951] | 24 | void answer(const std::string& question) {
|
---|
[4942] | 25 | // implemented on the client-side only
|
---|
| 26 | }
|
---|
| 27 | };
|
---|
| 28 |
|
---|
[4945] | 29 | class t_connection {
|
---|
[4942] | 30 | public:
|
---|
[4950] | 31 | shared_ptr<myService> _service;
|
---|
| 32 | shared_ptr<myServiceClient> _client;
|
---|
| 33 | shared_ptr<TProcessor> _processor;
|
---|
| 34 | shared_ptr<TProtocol> _protocolInp;
|
---|
| 35 | shared_ptr<TProtocol> _protocolOut;
|
---|
| 36 | shared_ptr<TTransport> _transport;
|
---|
[4945] | 37 | };
|
---|
| 38 |
|
---|
[4947] | 39 | shared_ptr<t_connection> CONNECTION;
|
---|
| 40 |
|
---|
[4945] | 41 | class myProcessorFactory : public TProcessorFactory {
|
---|
| 42 | public:
|
---|
[4943] | 43 | myProcessorFactory() {};
|
---|
[4944] | 44 | shared_ptr<TProcessor> getProcessor(const TConnectionInfo& info) {
|
---|
[4950] | 45 | shared_ptr<myServiceClient> client(new myServiceClient(info.output));
|
---|
| 46 | shared_ptr<myService> service(new myService());
|
---|
| 47 | shared_ptr<TProcessor> processor(new myServiceProcessor(service));
|
---|
[4944] | 48 | cout << "connection " << endl;
|
---|
[4947] | 49 |
|
---|
[4948] | 50 | CONNECTION.reset(new t_connection);
|
---|
| 51 | CONNECTION->_service = service;
|
---|
[4950] | 52 | CONNECTION->_client = client;
|
---|
[4948] | 53 | CONNECTION->_processor = processor;
|
---|
| 54 | CONNECTION->_protocolInp = info.input;
|
---|
| 55 | CONNECTION->_protocolOut = info.output;
|
---|
| 56 | CONNECTION->_transport = info.transport;
|
---|
[4945] | 57 |
|
---|
[4943] | 58 | return processor;
|
---|
[4942] | 59 | }
|
---|
| 60 | };
|
---|
| 61 |
|
---|
[4948] | 62 | class t_serverThread : public apache::thrift::concurrency::Runnable {
|
---|
[4946] | 63 | public:
|
---|
| 64 | t_serverThread() {}
|
---|
[4948] | 65 | ~t_serverThread() {}
|
---|
[4946] | 66 | void run() {
|
---|
| 67 | int port = 9090;
|
---|
| 68 | shared_ptr<TServerSocket> serverTransport(new TServerSocket(port));
|
---|
| 69 | shared_ptr<myProcessorFactory> processorFactory(new myProcessorFactory());
|
---|
| 70 | shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
|
---|
| 71 | shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
|
---|
| 72 |
|
---|
| 73 | TThreadedServer server(processorFactory, serverTransport,
|
---|
| 74 | transportFactory, protocolFactory);
|
---|
| 75 | server.serve();
|
---|
[4948] | 76 | }
|
---|
[4946] | 77 | };
|
---|
| 78 |
|
---|
[4942] | 79 | int main(int argc, char **argv) {
|
---|
| 80 |
|
---|
[4948] | 81 | shared_ptr<PosixThreadFactory> threadFactory(new PosixThreadFactory);
|
---|
[4942] | 82 |
|
---|
[4948] | 83 | shared_ptr<t_serverThread> serverThread(new t_serverThread);
|
---|
| 84 |
|
---|
| 85 | shared_ptr<Thread> thread = threadFactory->newThread(serverThread);
|
---|
| 86 | thread->start();
|
---|
| 87 |
|
---|
| 88 | cout << "server thread started" << endl;
|
---|
| 89 |
|
---|
[4946] | 90 | while (true) {
|
---|
[4948] | 91 | cout << "sleep ..." << endl;
|
---|
[4949] | 92 | if (CONNECTION) {
|
---|
| 93 | cout << "CONNECTION " << endl;
|
---|
[4951] | 94 | CONNECTION->_client->answer("How are you?");
|
---|
[4949] | 95 | }
|
---|
[4946] | 96 | sleep(1);
|
---|
| 97 | }
|
---|
[4942] | 98 |
|
---|
| 99 | return 0;
|
---|
| 100 | }
|
---|
| 101 |
|
---|