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

Last change on this file since 4949 was 4949, checked in by mervart, 9 years ago
File size: 2.8 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(std::string& answ, 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<TProcessor> _processor;
33 shared_ptr<TProtocol> _protocolInp;
34 shared_ptr<TProtocol> _protocolOut;
35 shared_ptr<TTransport> _transport;
36};
37
38shared_ptr<t_connection> CONNECTION;
39
40class 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
59class 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
76int 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
Note: See TracBrowser for help on using the repository browser.