source: ntrip/trunk/GnssCenter/thrift/test2/server.cpp@ 4951

Last change on this file since 4951 was 4951, checked in by mervart, 11 years ago
File size: 2.9 KB
Line 
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
13using namespace std;
14using namespace boost;
15using namespace apache::thrift;
16using namespace apache::thrift::protocol;
17using namespace apache::thrift::transport;
18using namespace apache::thrift::server;
19using namespace apache::thrift::concurrency;
20
21class myService : virtual public myServiceIf {
22 public:
23 myService() {}
24 void answer(const std::string& question) {
25 // implemented on the client-side only
26 }
27};
28
29class t_connection {
30 public:
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;
37};
38
39shared_ptr<t_connection> CONNECTION;
40
41class myProcessorFactory : public TProcessorFactory {
42 public:
43 myProcessorFactory() {};
44 shared_ptr<TProcessor> getProcessor(const TConnectionInfo& info) {
45 shared_ptr<myServiceClient> client(new myServiceClient(info.output));
46 shared_ptr<myService> service(new myService());
47 shared_ptr<TProcessor> processor(new myServiceProcessor(service));
48 cout << "connection " << endl;
49
50 CONNECTION.reset(new t_connection);
51 CONNECTION->_service = service;
52 CONNECTION->_client = client;
53 CONNECTION->_processor = processor;
54 CONNECTION->_protocolInp = info.input;
55 CONNECTION->_protocolOut = info.output;
56 CONNECTION->_transport = info.transport;
57
58 return processor;
59 }
60};
61
62class t_serverThread : public apache::thrift::concurrency::Runnable {
63 public:
64 t_serverThread() {}
65 ~t_serverThread() {}
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();
76 }
77};
78
79int main(int argc, char **argv) {
80
81 shared_ptr<PosixThreadFactory> threadFactory(new PosixThreadFactory);
82
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
90 while (true) {
91 cout << "sleep ..." << endl;
92 if (CONNECTION) {
93 cout << "CONNECTION " << endl;
94 CONNECTION->_client->answer("How are you?");
95 }
96 sleep(1);
97 }
98
99 return 0;
100}
101
Note: See TracBrowser for help on using the repository browser.