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

Last change on this file since 4952 was 4952, checked in by mervart, 9 years ago
File size: 2.6 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 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
30shared_ptr<t_connection> CONNECTION;
31
32class 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
52class 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
69int 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
Note: See TracBrowser for help on using the repository browser.