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