[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>
|
---|
[4942] | 9 |
|
---|
| 10 | #include "gen-cpp/myService.h"
|
---|
| 11 |
|
---|
| 12 | using namespace std;
|
---|
| 13 | using namespace boost;
|
---|
| 14 | using namespace ::apache::thrift;
|
---|
| 15 | using namespace ::apache::thrift::protocol;
|
---|
| 16 | using namespace ::apache::thrift::transport;
|
---|
| 17 | using namespace ::apache::thrift::server;
|
---|
[4946] | 18 | using namespace ::apache::thrift::concurrency;
|
---|
[4942] | 19 |
|
---|
[4944] | 20 | class myService : virtual public myServiceIf {
|
---|
[4942] | 21 | public:
|
---|
[4944] | 22 | myService() {}
|
---|
[4943] | 23 | void answer(std::string& answ, const std::string& question) {
|
---|
[4942] | 24 | // implemented on the client-side only
|
---|
| 25 | }
|
---|
| 26 | };
|
---|
| 27 |
|
---|
[4945] | 28 | class t_connection {
|
---|
[4942] | 29 | public:
|
---|
[4945] | 30 | shared_ptr<myService> _service;
|
---|
| 31 | shared_ptr<TProcessor> _processor;
|
---|
| 32 | shared_ptr<TProtocol> _protocolInp;
|
---|
| 33 | shared_ptr<TProtocol> _protocolOut;
|
---|
| 34 | shared_ptr<TTransport> _transport;
|
---|
| 35 | };
|
---|
| 36 |
|
---|
[4947] | 37 | shared_ptr<t_connection> CONNECTION;
|
---|
| 38 |
|
---|
[4945] | 39 | class myProcessorFactory : public TProcessorFactory {
|
---|
| 40 | public:
|
---|
[4943] | 41 | myProcessorFactory() {};
|
---|
[4944] | 42 | shared_ptr<TProcessor> getProcessor(const TConnectionInfo& info) {
|
---|
| 43 | shared_ptr<myService> service(new myService());
|
---|
| 44 | shared_ptr<TProcessor> processor(new myServiceProcessor(service));
|
---|
| 45 | cout << "connection " << endl;
|
---|
[4947] | 46 |
|
---|
| 47 | shared_ptr<t_connection> connection(new t_connection);
|
---|
| 48 | connection->_service = service;
|
---|
| 49 | connection->_processor = processor;
|
---|
| 50 | connection->_protocolInp = info.input;
|
---|
| 51 | connection->_protocolOut = info.output;
|
---|
| 52 | connection->_transport = info.transport;
|
---|
[4945] | 53 |
|
---|
[4943] | 54 | return processor;
|
---|
[4942] | 55 | }
|
---|
| 56 | };
|
---|
| 57 |
|
---|
[4946] | 58 | class t_serverThread : public Runnable {
|
---|
| 59 | public:
|
---|
| 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 |
|
---|
[4942] | 74 | int main(int argc, char **argv) {
|
---|
| 75 |
|
---|
[4946] | 76 | t_serverThread serverThread;
|
---|
| 77 | serverThread.run();
|
---|
[4942] | 78 |
|
---|
[4946] | 79 | while (true) {
|
---|
| 80 | sleep(1);
|
---|
| 81 | }
|
---|
[4942] | 82 |
|
---|
| 83 | return 0;
|
---|
| 84 | }
|
---|
| 85 |
|
---|