[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 |
|
---|
| 13 | using namespace std;
|
---|
| 14 | using namespace boost;
|
---|
[4948] | 15 | using namespace apache::thrift;
|
---|
| 16 | using namespace apache::thrift::protocol;
|
---|
| 17 | using namespace apache::thrift::transport;
|
---|
| 18 | using namespace apache::thrift::server;
|
---|
| 19 | using namespace apache::thrift::concurrency;
|
---|
[4942] | 20 |
|
---|
[4945] | 21 | class 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] | 30 | shared_ptr<t_connection> CONNECTION;
|
---|
| 31 |
|
---|
[4945] | 32 | class 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] | 52 | class 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] | 69 | int 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 |
|
---|