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

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