summaryrefslogtreecommitdiff
path: root/contrib/transport-sample/server/server.cpp
blob: dba8368b7fc9976bdfee0e77f6252558465c15dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// server.cpp : Defines the entry point for the console application.
//
// sample server command line app using Thrift IPC.
//
// This is a simple demonstration of full duplex RPC. That is, each
// side runs both a client and server to enable bidirectional event 
// signaling.
//

#ifdef _WIN32
#  include "stdafx.h"
#else
#  include "config.h"
#endif

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Include this before the generated includes
#include "ThriftCommon.h"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Tailor these to your generated files
#include "../gen-cpp/SampleService.h"
#include "../gen-cpp/SampleCallback.h"

using namespace Sample; //declared in .thrift file
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

int16_t ClientPort_;
std::string ClientPipeName_;
void S2CThreadProc();

//-----------------------------------------------------------------------------
// RPC implementations
//
class SampleServiceHandler : virtual public SampleServiceIf {
 public:
  SampleServiceHandler() {
    // Your initialization goes here
  }

  void HelloThere(std::string& _return, const std::string& HelloString) {
    // Your implementation goes here
    printf("<<<HelloThere() received string: %s\n", HelloString.c_str());
	_return = "Good thank you.";
  }

  void ServerDoSomething() {
    // Your implementation goes here
    printf("ServerDoSomething(): Simulating work for 5 seconds\n");
    Sleep(5000);
    printf("ServerDoSomething(): Done\n");
  }

  void ClientSideListenPort(const int16_t ClientListenPort)
  {
	ClientPort_ = ClientListenPort;
	ClientPipeName_ = "";
#ifdef _WIN32
	printf(">>>Connecting to client on port %d\n", ClientPort_);
	boost::thread Connect2ClientThread(S2CThreadProc);
#endif
  }

  void ClientSidePipeName(const std::string& ClientPipeName)
  {
	ClientPipeName_ = ClientPipeName;
	ClientPort_ = 0;
#ifdef _WIN32
	printf(">>>Connecting to client pipe %s\n", ClientPipeName_.c_str());
	boost::thread Connect2ClientThread(S2CThreadProc);
#endif
  }
};
//-----------------------------------------------------------------------------

#ifdef _WIN32
int _tmain(int argc, _TCHAR* argv[])
#else
int main(int argc, char **argv)
#endif
{
	int port;
	std::string pipename; //e.g. "affpipe"

	bool usage = false;

	//Process command line params
	if(argc > 1)
	{
		if(_tcscmp(argv[1], TEXT("-sp")) == 0)
		{	//Socket Port specified
			port = _tstoi(argv[2]);
#ifdef _WIN32
			TWinsockSingleton::create();
#endif
			// Start the thrift server which is a blocking call.
			thriftcommon::RunThriftServer<SampleServiceHandler, SampleServiceProcessor>(10, port);
		}
		else if(_tcscmp(argv[1], TEXT("-np")) == 0)
		{	//Named Pipe specified
#ifdef _WIN32
			std::wstring wpipe(argv[2]);
			pipename.resize(wpipe.length());
			std::copy(wpipe.begin(), wpipe.end(), pipename.begin());
#else
			pipename = argv[2];
#endif
			printf("Using Named Pipe %s\n", pipename.c_str());

			//Thrift over Named Pipe.
			thriftcommon::RunThriftServer<SampleServiceHandler, SampleServiceProcessor>(10, pipename);
		}
		else if(_tcscmp(argv[1], TEXT("-ap")) == 0)
		{	//Anonymous Pipe specified
			//This is more involved because the child needs to be launched 
			//after the transport is created but before the blocking server 
			//call.
#ifdef _WIN32
			boost::shared_ptr<TServerTransport> transport(new TPipeServer()); //Anonymous pipe
			thriftcommon::LaunchAnonPipeChild(".\\client.exe", transport);
			boost::shared_ptr<SampleServiceHandler> handler(new SampleServiceHandler());
			thriftcommon::RunThriftServer<SampleServiceHandler, SampleServiceProcessor>(handler, 10, transport);
#else
			printf("Anonymous pipes not (yet) supported under *NIX\n");
#endif
		}
		else
			usage = true;
	}
	else
		usage = true;

	if(usage)
	{
		printf("Thrift sample server usage:\n\n");
		printf("Socket Port :   -sp <port#>\n");
		printf("Named Pipe :    -np <pipename> (e.g. affpipe)\n");
		printf("Anonymous Pipe: -ap\n");
	}
	return 0;
}


//Thread Routine that connects to the 'client'.
void S2CThreadProc()
{
	//Master server's connection to client-side's server.
	boost::shared_ptr<SampleCallbackClient> clientsrv; //Client class from Thrift-generated code.
	boost::shared_ptr<TTransport> transport;
	if(ClientPort_ != 0)
		thriftcommon::ConnectToServer<SampleCallbackClient, TTransport>(clientsrv, transport, ClientPort_);
	if(!ClientPipeName_.empty())
		thriftcommon::ConnectToServer<SampleCallbackClient, TTransport>(clientsrv, transport, ClientPipeName_);

	try {
		transport->open();

		clientsrv->pingclient();
		Sleep(1500);
		clientsrv->pingclient();
		Sleep(1500);
		clientsrv->pingclient();

		transport->close();
	} catch (TException &tx) {
		printf("ERROR: %s\n", tx.what());
	}
}