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

Last change on this file since 8136 was 4953, checked in by mervart, 12 years ago
File size: 2.7 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#include <thrift/concurrency/Mutex.h>
11
12#include "gen-cpp/myService.h"
13
14using namespace std;
15using namespace boost;
16using namespace apache::thrift;
17using namespace apache::thrift::protocol;
18using namespace apache::thrift::transport;
19using namespace apache::thrift::server;
20using namespace apache::thrift::concurrency;
21
22class t_connection {
23 public:
24 shared_ptr<myServiceClient> _client;
25 shared_ptr<TProcessor> _processor;
26 shared_ptr<TProtocol> _protocolInp;
27 shared_ptr<TProtocol> _protocolOut;
28 shared_ptr<TTransport> _transport;
29};
30
31Mutex MUTEX;
32shared_ptr<t_connection> CONNECTION;
33
34class myProcessorFactory : public TProcessorFactory {
35 public:
36 myProcessorFactory() {};
37 shared_ptr<TProcessor> getProcessor(const TConnectionInfo& info) {
38
39 Guard m(MUTEX);
40
41 shared_ptr<myServiceClient> client(new myServiceClient(info.output));
42 shared_ptr<TProcessor> processor(new myServiceProcessor(client));
43
44 cout << "new connection " << endl;
45
46 CONNECTION.reset(new t_connection);
47 CONNECTION->_client = client;
48 CONNECTION->_processor = processor;
49 CONNECTION->_protocolInp = info.input;
50 CONNECTION->_protocolOut = info.output;
51 CONNECTION->_transport = info.transport;
52
53 return processor;
54 }
55};
56
57class t_serverThread : public apache::thrift::concurrency::Runnable {
58 public:
59 t_serverThread() {}
60 ~t_serverThread() {}
61 void run() {
62 int port = 9090;
63 shared_ptr<TServerSocket> serverTransport(new TServerSocket(port));
64 shared_ptr<myProcessorFactory> processorFactory(new myProcessorFactory());
65 shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
66 shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
67
68 TThreadedServer server(processorFactory, serverTransport,
69 transportFactory, protocolFactory);
70 server.serve();
71 }
72};
73
74int main(int argc, char **argv) {
75
76 shared_ptr<PosixThreadFactory> threadFactory(new PosixThreadFactory);
77
78 shared_ptr<t_serverThread> serverThread(new t_serverThread);
79
80 shared_ptr<Thread> thread = threadFactory->newThread(serverThread);
81 thread->start();
82
83 try {
84 while (true) {
85 {
86 Guard m(MUTEX);
87 if (CONNECTION) {
88 CONNECTION->_client->answer("How are you?");
89 }
90 }
91 sleep(1);
92 }
93 } catch (TException& exc) {
94 cout << "Exception: " << exc.what() << endl;
95 }
96
97 return 0;
98}
99
Note: See TracBrowser for help on using the repository browser.