[4942] | 1 | #include <iostream>
|
---|
| 2 | #include <string>
|
---|
| 3 |
|
---|
| 4 | #include <thrift/protocol/TBinaryProtocol.h>
|
---|
| 5 | #include <thrift/server/TSimpleServer.h>
|
---|
| 6 | #include <thrift/transport/TServerSocket.h>
|
---|
| 7 | #include <thrift/transport/TBufferTransports.h>
|
---|
| 8 |
|
---|
| 9 | #include "gen-cpp/myService.h"
|
---|
| 10 |
|
---|
| 11 | using namespace std;
|
---|
| 12 | using namespace boost;
|
---|
| 13 | using namespace ::apache::thrift;
|
---|
| 14 | using namespace ::apache::thrift::protocol;
|
---|
| 15 | using namespace ::apache::thrift::transport;
|
---|
| 16 | using namespace ::apache::thrift::server;
|
---|
| 17 |
|
---|
| 18 | class myServiceHandler : virtual public myServiceIf {
|
---|
| 19 | public:
|
---|
| 20 | myServiceHandler() {}
|
---|
| 21 | void answer(std::string& /* answ */, const std::string& /* question */) {
|
---|
| 22 | // implemented on the client-side only
|
---|
| 23 | }
|
---|
| 24 | };
|
---|
| 25 |
|
---|
| 26 | class myServerEventHandler : virtual public TServerEventHandler {
|
---|
| 27 | public:
|
---|
| 28 | myServerEventHandler() {};
|
---|
| 29 | virtual void* createContext(shared_ptr<TProtocol> input,
|
---|
| 30 | shared_ptr<TProtocol> output) {
|
---|
| 31 | void* context = TServerEventHandler::createContext(input, output);
|
---|
| 32 | cout << "createContext " << context << endl;
|
---|
| 33 |
|
---|
| 34 | string* str = static_cast<string*>(context);
|
---|
| 35 | cout << "str = " << str << endl;
|
---|
| 36 |
|
---|
| 37 | return context;
|
---|
| 38 | }
|
---|
| 39 | };
|
---|
| 40 |
|
---|
| 41 | int main(int argc, char **argv) {
|
---|
| 42 | int port = 9090;
|
---|
| 43 | shared_ptr<myServiceHandler> handler(new myServiceHandler());
|
---|
| 44 | shared_ptr<TProcessor> processor(new myServiceProcessor(handler));
|
---|
| 45 | shared_ptr<TServerSocket> serverTransport(new TServerSocket(port));
|
---|
| 46 | shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
|
---|
| 47 | shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
|
---|
| 48 |
|
---|
| 49 | TSimpleServer server(processor, serverTransport,
|
---|
| 50 | transportFactory, protocolFactory);
|
---|
| 51 |
|
---|
| 52 | shared_ptr<TServerEventHandler> eventHandler(new myServerEventHandler());
|
---|
| 53 | server.setServerEventHandler(eventHandler);
|
---|
| 54 |
|
---|
| 55 | server.serve();
|
---|
| 56 | return 0;
|
---|
| 57 | }
|
---|
| 58 |
|
---|