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