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

Last change on this file since 4952 was 4952, checked in by mervart, 11 years ago
File size: 2.6 KB
RevLine 
[4942]1#include <iostream>
2#include <string>
3
4#include <thrift/protocol/TBinaryProtocol.h>
[4943]5#include <thrift/server/TThreadedServer.h>
[4942]6#include <thrift/transport/TServerSocket.h>
7#include <thrift/transport/TBufferTransports.h>
[4946]8#include <thrift/concurrency/Thread.h>
[4948]9#include <thrift/concurrency/PosixThreadFactory.h>
[4942]10
11#include "gen-cpp/myService.h"
12
13using namespace std;
14using namespace boost;
[4948]15using namespace apache::thrift;
16using namespace apache::thrift::protocol;
17using namespace apache::thrift::transport;
18using namespace apache::thrift::server;
19using namespace apache::thrift::concurrency;
[4942]20
[4945]21class t_connection {
[4942]22 public:
[4950]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;
[4945]28};
29
[4947]30shared_ptr<t_connection> CONNECTION;
31
[4945]32class myProcessorFactory : public TProcessorFactory {
33 public:
[4943]34 myProcessorFactory() {};
[4944]35 shared_ptr<TProcessor> getProcessor(const TConnectionInfo& info) {
[4950]36 shared_ptr<myServiceClient> client(new myServiceClient(info.output));
[4952]37 shared_ptr<TProcessor> processor(new myServiceProcessor(client));
[4947]38
[4952]39 cout << "new connection " << endl;
40
[4948]41 CONNECTION.reset(new t_connection);
[4950]42 CONNECTION->_client = client;
[4948]43 CONNECTION->_processor = processor;
44 CONNECTION->_protocolInp = info.input;
45 CONNECTION->_protocolOut = info.output;
46 CONNECTION->_transport = info.transport;
[4945]47
[4943]48 return processor;
[4942]49 }
50};
51
[4948]52class t_serverThread : public apache::thrift::concurrency::Runnable {
[4946]53 public:
54 t_serverThread() {}
[4948]55 ~t_serverThread() {}
[4946]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();
[4948]66 }
[4946]67};
68
[4942]69int main(int argc, char **argv) {
70
[4948]71 shared_ptr<PosixThreadFactory> threadFactory(new PosixThreadFactory);
[4942]72
[4948]73 shared_ptr<t_serverThread> serverThread(new t_serverThread);
74
75 shared_ptr<Thread> thread = threadFactory->newThread(serverThread);
76 thread->start();
77
[4952]78 try {
79 while (true) {
80 if (CONNECTION) {
81 CONNECTION->_client->answer("How are you?");
82 }
83 sleep(1);
[4949]84 }
[4952]85 } catch (TException& exc) {
86 cout << "Exception: " << exc.what() << endl;
[4946]87 }
[4942]88
89 return 0;
90}
91
Note: See TracBrowser for help on using the repository browser.