]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/contrib/transport-sample/client/client.cpp
1 // client->cpp : Defines the entry point for the console application.
3 // sample client command line app using Thrift IPC.
4 // Quick n Dirty example, may not have very robust error handling
5 // for the sake of simplicity.
14 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 //Include this before the generated includes
16 #include "ThriftCommon.h"
17 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 //Tailor these to your generated files
19 #include "../gen-cpp/SampleService.h"
20 #include "../gen-cpp/SampleCallback.h"
22 using namespace Sample
; //declared in .thrift file
23 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 void ClientListenerThreadProc();
27 bool bAnonPipe
= false;
30 std::string pipename_client
;
35 //Customized version of printf that changes the text color
36 //This depends on hConsole global being initialized
37 void hlprintf(const char* _Format
, ...)
40 SetConsoleTextAttribute(hConsole
, 0xE);
44 va_start (ap
, _Format
);
45 r
= vprintf (_Format
, ap
);
48 SetConsoleTextAttribute(hConsole
, 7);
52 //-----------------------------------------------------------------------------
53 // Client-side RPC implementations: Called by the server to the client for
54 // bidirectional eventing.
56 class SampleCallbackHandler
: virtual public SampleCallbackIf
{
58 SampleCallbackHandler() {
59 // initialization goes here
64 hlprintf("<<<Ping received from server (server-to-client event).\n");
68 //-----------------------------------------------------------------------------
72 int _tmain(int argc
, _TCHAR
* argv
[])
74 int main(int argc
, char **argv
)
77 //Process cmd line args to determine named vs anon pipes.
80 HANDLE ReadPipe
, WritePipe
;
81 hConsole
= GetStdHandle(STD_OUTPUT_HANDLE
);
84 //Process command line params
87 if(_tcscmp(argv
[1], TEXT("-sp")) == 0)
88 { //Socket Port specified
89 srvPort
= _tstoi(argv
[2]);
92 else if(_tcscmp(argv
[1], TEXT("-np")) == 0)
93 { //Named Pipe specified
95 std::wstring
wpipe(argv
[2]);
96 pipename
.resize(wpipe
.length());
97 std::copy(wpipe
.begin(), wpipe
.end(), pipename
.begin());
101 pipename_client
= pipename
+ "_client";
104 { //Anonymous Pipe specified
106 ReadPipe
= (HANDLE
)_tstoi(argv
[1]);
107 WritePipe
= (HANDLE
)_tstoi(argv
[2]);
110 printf("Anonymous pipes not (yet) supported under *NIX\n");
121 hlprintf("Thrift sample client usage:\n\n");
122 hlprintf("Socket Port to connect to: -sp <port#>\n");
123 hlprintf("Named Pipe to connect to: -np <pipename> (e.g. affpipe)\n");
124 hlprintf("Anonymous Pipe (must be launched by anon pipe creator):\n");
125 hlprintf(" <Read Handle> <Write Handle>\n");
129 //Client side connection to server.
130 boost::shared_ptr
<SampleServiceClient
> client
; //Client class from Thrift-generated code.
131 boost::shared_ptr
<TTransport
> transport
;
136 TWinsockSingleton::create();
138 hlprintf("Using socket transport port %d\n", srvPort
);
139 thriftcommon::ConnectToServer
<SampleServiceClient
, TTransport
>(client
, transport
, srvPort
);
143 hlprintf("Using Named Pipe %s\n", pipename
.c_str());
144 thriftcommon::ConnectToServer
<SampleServiceClient
, TTransport
>(client
, transport
, pipename
);
149 hlprintf("Using Anonymous Pipe transport\n");
150 thriftcommon::ConnectToServer
<SampleServiceClient
, TTransport
>(client
, transport
, ReadPipe
, WritePipe
);
155 //Start a thread to receive inbound connection from server for 2-way event signaling.
156 boost::thread
ClientListenerThread(ClientListenerThreadProc
);
162 //Notify server what to connect back on.
164 client
->ClientSideListenPort(srvPort
+ 1); //Socket
166 client
->ClientSidePipeName(pipename_client
); //Named Pipe
169 std::string hellostr
= "Hello how are you?";
170 std::string returnstr
;
171 client
->HelloThere(returnstr
, hellostr
);
172 hlprintf("\n>>>Sent: %s\n", hellostr
.c_str());
173 hlprintf("<<<Received: %s\n", returnstr
.c_str());
175 hlprintf("\n>>>Calling ServerDoSomething() which delays for 5 seconds.\n");
176 client
->ServerDoSomething();
177 hlprintf(">>>ServerDoSomething() done.\n\n");
180 } catch (TException
&tx
) {
181 hlprintf("ERROR: %s\n", tx
.what());
189 void ClientListenerThreadProc()
192 thriftcommon::RunThriftServer
<SampleCallbackHandler
, SampleCallbackProcessor
>(1, srvPort
+ 1);
194 thriftcommon::RunThriftServer
<SampleCallbackHandler
, SampleCallbackProcessor
>(1, pipename_client
);