]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/contrib/transport-sample/ThriftCommon.h
1 // ThriftCommon.h : Common includes, namespaces and templates
2 // for sample Thrift client and server
4 // Add the following paths to the Project's properties:
6 // Configuration Properties -> C/C++ -> General-> Additional Include Directories --
7 // ../;../../../lib/cpp/src;../../../../boost;../../../../boost/boost/tr1;
9 // Configuration Properties -> Linker -> General -> Additional Library Directories --
10 // ../../../lib/cpp/$(Configuration);../../../../Boost/lib
12 // Configuration Properties -> Linker -> Input -> Additional Dependencies --
15 // ... adjust relative paths as necessary.
18 #ifdef _WIN32 //thrift is crashing when using boost threads on Mac OSX
20 # include <sys/socket.h>
21 # include <netinet/in.h>
24 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 //'server' side #includes
27 #include <thrift/concurrency/ThreadManager.h>
28 #include <thrift/concurrency/ThreadFactory.h>
29 #include <thrift/server/TThreadPoolServer.h>
30 #include <thrift/server/TSimpleServer.h>
31 //'client' side #includes
32 #include <thrift/transport/TPipeServer.h>
33 #include <thrift/transport/TPipe.h>
34 #include <thrift/transport/TBufferTransports.h>
35 #include <thrift/transport/TSocket.h>
36 #include <thrift/transport/TTransport.h>
38 #include <thrift/protocol/TBinaryProtocol.h>
41 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44 // Required Namespaces
45 //'server' side namespaces
46 using namespace apache::thrift::server
;
47 using namespace apache::thrift::concurrency
;
49 using namespace apache::thrift
;
50 using namespace apache::thrift::protocol
;
51 using namespace apache::thrift::transport
;
52 //using namespace boost; //using ns boost can introduce type conflicts
53 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55 namespace thriftcommon
57 //----------------------------------------------------------------------------
59 //Start the thrift 'server' (both server & client side run one for bidir event signaling)
60 // *** This function template will block ***
62 template <class MyHandler
, class MyProcessor
>
63 void RunThriftServer (boost::shared_ptr
<MyHandler
> hndlr
,
65 boost::shared_ptr
<TServerTransport
> transport
,
66 boost::shared_ptr
<TServer
> &server
)
70 throw std::exception("RunThriftServer() invalid handler");
72 throw std::exception("RunThriftServer() invalid transport");
74 if ( !hndlr
.get() || !transport
.get() )
75 throw std::exception();
78 boost::shared_ptr
<MyHandler
> handler(hndlr
);
79 boost::shared_ptr
<TProcessor
> processor(new MyProcessor(handler
));
80 boost::shared_ptr
<TTransportFactory
> tfactory(new TBufferedTransportFactory());
81 boost::shared_ptr
<TProtocolFactory
> pfactory(new TBinaryProtocolFactory());
84 { //Single-threaded server
85 server
.reset(new TSimpleServer(processor
, transport
, tfactory
, pfactory
));
88 { //Multi-threaded server
89 boost::shared_ptr
<ThreadManager
> threadManager
= ThreadManager::newSimpleThreadManager(NumThreads
);
90 boost::shared_ptr
<ThreadFactory
> threadFactory
= boost::shared_ptr
<ThreadFactory
>(new ThreadFactory());
91 threadManager
->threadFactory(threadFactory
);
92 threadManager
->start();
93 server
.reset(new TThreadPoolServer(processor
, transport
, tfactory
, pfactory
, threadManager
));
96 printf("Starting the 'server'...\n");
101 // Thrift server wrapper function that accepts a pipe name.
102 // A handler must be passed in to this version.
103 template <class MyHandler
, class MyProcessor
>
104 void RunThriftServer (boost::shared_ptr
<MyHandler
> hndlr
, int NumThreads
, std::string pipename
, boost::shared_ptr
<TServer
> &svr
)
106 #ifndef _WIN32 //Mac, *nix
107 unlink(pipename
.c_str());
109 boost::shared_ptr
<TServerTransport
> transport(new TPipeServer(pipename
, 1024, NumThreads
)); //Named pipe
110 RunThriftServer
<MyHandler
, MyProcessor
>(hndlr
, NumThreads
, transport
, svr
);
113 // Thrift server wrapper function that accepts a pipe name.
114 // This version instantiates its own handler.
115 template <class MyHandler
, class MyProcessor
>
116 void RunThriftServer (int NumThreads
, std::string pipename
)
118 boost::shared_ptr
<MyHandler
> handler(new MyHandler());
119 boost::shared_ptr
<TServer
> server
;
121 RunThriftServer
<MyHandler
, MyProcessor
>(handler
, NumThreads
, pipename
, server
);
124 // Thrift server wrapper function that accepts a socket port number.
125 // A handler must be passed in to this version.
126 template <class MyHandler
, class MyProcessor
>
127 void RunThriftServer (boost::shared_ptr
<MyHandler
> hndlr
, int NumThreads
, int Port
)
129 boost::shared_ptr
<TServerTransport
> transport(new TServerSocket(Port
));
130 boost::shared_ptr
<TServer
> server
;
131 RunThriftServer
<MyHandler
, MyProcessor
>(hndlr
, NumThreads
, transport
, server
);
134 // Thrift server wrapper function that accepts a socket port number.
135 // This version instantiates its own handler.
136 template <class MyHandler
, class MyProcessor
>
137 void RunThriftServer (int NumThreads
, int Port
)
139 boost::shared_ptr
<MyHandler
> handler(new MyHandler());
141 RunThriftServer
<MyHandler
, MyProcessor
>(handler
, NumThreads
, Port
);
145 template <class MyHandler
, class MyProcessor
>
146 void RunThriftServer (boost::shared_ptr
<MyHandler
> hndlr
, int NumThreads
, boost::shared_ptr
<TServerTransport
> transport
)
148 boost::shared_ptr
<TServer
> server
;
149 RunThriftServer
<MyHandler
, MyProcessor
>(hndlr
, NumThreads
, transport
, server
);
152 //----------------------------------------------------------------------------
153 //Connect to thrift 'server' - Socket version
154 //(both server & client side run one for bidir event signaling)
156 template <class MyClient
, class MyTransport
>
157 void ConnectToServer (boost::shared_ptr
<MyClient
> &client
, boost::shared_ptr
<MyTransport
> &transport
, int Port
)
159 //Client side connection using sockets transport.
160 boost::shared_ptr
<TTransport
> socket(new TSocket("localhost", Port
));
161 transport
.reset(new TBufferedTransport(socket
));
162 boost::shared_ptr
<TProtocol
> protocol(new TBinaryProtocol(transport
));
164 client
.reset(new MyClient(protocol
));
167 //Connect to thrift 'server' - Named Pipe version
168 template <class MyClient
, class MyTransport
>
169 void ConnectToServer (boost::shared_ptr
<MyClient
> &client
, boost::shared_ptr
<MyTransport
> &transport
, std::string pipename
)
171 //Client side connection using Named Pipe transport.
172 boost::shared_ptr
<TTransport
> pipe(new TPipe(pipename
));
173 transport
.reset(new TBufferedTransport(pipe
));
174 boost::shared_ptr
<TProtocol
> protocol(new TBinaryProtocol(transport
));
176 client
.reset(new MyClient(protocol
));
179 //Connect to thrift 'server' - Anonymous Pipe version
180 //Currently only supported under Windows
182 template <class MyClient
, class MyTransport
>
183 void ConnectToServer (boost::shared_ptr
<MyClient
> &client
, boost::shared_ptr
<MyTransport
> &transport
, HANDLE RdPipe
, HANDLE WrtPipe
)
185 //Client side connection using sockets transport.
187 boost::shared_ptr
<TTransport
> pipe(new TPipe((int)RdPipe
, (int)WrtPipe
));
188 transport
.reset(new TBufferedTransport(pipe
));
190 boost::shared_ptr
<TTransport
> socket(new TSocket("localhost"));
191 transport
.reset(new TBufferedTransport(socket
));
193 boost::shared_ptr
<TProtocol
> protocol(new TBinaryProtocol(transport
));
195 client
.reset(new MyClient(protocol
));
199 //----------------------------------------------------------------------------
200 //Launch child process and pass R/W anonymous pipe handles on cmd line.
201 //Currently only supported under Windows
203 bool LaunchAnonPipeChild(std::string app
, boost::shared_ptr
<TServerTransport
> transport
);