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 myService : virtual public myServiceIf {
|
---|
22 | public:
|
---|
23 | myService() {}
|
---|
24 | void answer(std::string& answ, const std::string& question) {
|
---|
25 | // implemented on the client-side only
|
---|
26 | }
|
---|
27 | };
|
---|
28 |
|
---|
29 | class t_connection {
|
---|
30 | public:
|
---|
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 |
|
---|
38 | shared_ptr<t_connection> CONNECTION;
|
---|
39 |
|
---|
40 | class myProcessorFactory : public TProcessorFactory {
|
---|
41 | public:
|
---|
42 | myProcessorFactory() {};
|
---|
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;
|
---|
47 |
|
---|
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;
|
---|
54 |
|
---|
55 | return processor;
|
---|
56 | }
|
---|
57 | };
|
---|
58 |
|
---|
59 | class t_serverThread : public apache::thrift::concurrency::Runnable {
|
---|
60 | public:
|
---|
61 | t_serverThread() {}
|
---|
62 | ~t_serverThread() {}
|
---|
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();
|
---|
73 | }
|
---|
74 | };
|
---|
75 |
|
---|
76 | int main(int argc, char **argv) {
|
---|
77 |
|
---|
78 | shared_ptr<PosixThreadFactory> threadFactory(new PosixThreadFactory);
|
---|
79 |
|
---|
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 |
|
---|
87 | while (true) {
|
---|
88 | cout << "sleep ..." << endl;
|
---|
89 | if (CONNECTION) {
|
---|
90 | string answ;
|
---|
91 | cout << "CONNECTION " << endl;
|
---|
92 | CONNECTION->_service->answer(answ, "How are you?");
|
---|
93 | }
|
---|
94 | sleep(1);
|
---|
95 | }
|
---|
96 |
|
---|
97 | return 0;
|
---|
98 | }
|
---|
99 |
|
---|