blob: 6bebe71648defb7660d22d32f9f3427ef77dbeeb [file] [log] [blame]
Jens Geyerbd52f1a2014-07-28 01:25:30 +02001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Jens Geyerb5028322014-11-09 02:38:11 +010019
Jens Geyerbd52f1a2014-07-28 01:25:30 +020020package;
21
22import org.apache.thrift.*;
23import org.apache.thrift.protocol.*;
24import org.apache.thrift.transport.*;
25import org.apache.thrift.server.*;
26import org.apache.thrift.meta_data.*;
27
28import tutorial.*;
29import shared.*;
30
31
32enum Prot {
Jens Geyerb5028322014-11-09 02:38:11 +010033 binary;
34 json;
Jens Geyerbd52f1a2014-07-28 01:25:30 +020035}
36
37enum Trns {
Jens Geyerb5028322014-11-09 02:38:11 +010038 socket;
39 http;
Jens Geyerbd52f1a2014-07-28 01:25:30 +020040}
41
42class Main {
Jens Geyerbd52f1a2014-07-28 01:25:30 +020043
Jens Geyerb5028322014-11-09 02:38:11 +010044 private static var server : Bool = false;
45 private static var framed : Bool = false;
46 private static var buffered : Bool = false;
47 private static var prot : Prot = binary;
48 private static var trns : Trns = socket;
Jens Geyerbd52f1a2014-07-28 01:25:30 +020049
Jens Geyerb5028322014-11-09 02:38:11 +010050 private static var targetHost : String = "localhost";
51 private static var targetPort : Int = 9090;
52
53 static function main() {
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +030054
55 #if ! (flash || js || phpwebserver)
Jens Geyerb5028322014-11-09 02:38:11 +010056 try {
57 ParseArgs();
58 } catch (e : String) {
59 trace(e);
60 trace(GetHelp());
61 return;
62 }
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +030063
64 #elseif phpwebserver
65 //forcing server
66 server = true;
67 trns = http;
68 initPhpWebServer();
69 //check method
70 if(php.Web.getMethod() != 'POST') {
71 Sys.println('http endpoint for thrift test server');
72 return;
73 }
Jens Geyerb5028322014-11-09 02:38:11 +010074 #end
75
76 try {
77 if (server)
78 RunServer();
79 else
80 RunClient();
81 } catch (e : String) {
82 trace(e);
83 }
84
85 trace("Completed.");
Jens Geyerbd52f1a2014-07-28 01:25:30 +020086 }
87
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +030088 #if phpwebserver
89 private static function initPhpWebServer()
90 {
91 //remap trace to error log
92 haxe.Log.trace = function(v:Dynamic, ?infos:haxe.PosInfos)
93 {
94 // handle trace
95 var newValue : Dynamic;
96 if (infos != null && infos.customParams!=null) {
97 var extra:String = "";
98 for( v in infos.customParams )
99 extra += "," + v;
100 newValue = v + extra;
101 }
102 else {
103 newValue = v;
104 }
105 var msg = infos != null ? infos.fileName + ':' + infos.lineNumber + ': ' : '';
106 Sys.stderr().writeString('${msg}${newValue}\n');
107 }
108 }
109 #end
110
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200111
Jens Geyerb5028322014-11-09 02:38:11 +0100112 #if ! (flash || js)
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200113
Jens Geyerb5028322014-11-09 02:38:11 +0100114 private static function GetHelp() : String {
115 return Sys.executablePath()+" modus trnsOption transport protocol\n"
116 +"Options:\n"
117 +" modus: client, server (default: client)\n"
118 +" trnsOption: framed, buffered (default: none)\n"
119 +" transport: socket, http (default: socket)\n"
120 +" protocol: binary, json (default: binary)\n"
121 +"\n"
122 +"All arguments are optional.\n";
123 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200124
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200125
Jens Geyerb5028322014-11-09 02:38:11 +0100126 private static function ParseArgs() : Void {
127 var step = 0;
128 for (arg in Sys.args()) {
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200129
Jens Geyerb5028322014-11-09 02:38:11 +0100130 // server|client
131 switch(step) {
132 case 0:
133 ++step;
134 if ( arg == "client")
135 server = false;
136 else if ( arg == "server")
137 server = true;
138 else
139 throw "First argument must be 'server' or 'client'";
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200140
Jens Geyerb5028322014-11-09 02:38:11 +0100141 case 1:
142 if ( arg == "framed") {
143 framed = true;
144 } else if ( arg == "buffered") {
145 buffered = true;
146 } else if ( arg == "socket") {
147 trns = socket;
148 ++step;
149 } else if ( arg == "http") {
150 trns = http;
151 ++step;
152 } else {
153 throw "Unknown transport "+arg;
154 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200155
Jens Geyerb5028322014-11-09 02:38:11 +0100156 case 2:
157 if ( arg == "binary") {
158 prot = binary;
159 ++step;
160 } else if ( arg == "json") {
161 prot = json;
162 ++step;
163 } else {
164 throw "Unknown protocol "+arg;
165 }
166
167 default:
168 throw "Unexpected argument "+arg;
169 }
170
171 if ( framed && buffered)
172 {
173 trace("WN: framed supersedes buffered");
174 }
175
176 }
177 }
178
179 #end
180
181 private static function ClientSetup() : Calculator {
182 trace("Client configuration:");
183
184 // endpoint transport
185 var transport : TTransport;
186 switch(trns)
187 {
188 case socket:
189 trace('- socket transport $targetHost:$targetPort');
190 transport = new TSocket( targetHost, targetPort);
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200191 case http:
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +0300192 var uri = 'http://${targetHost}:${targetPort}';
193 trace('- HTTP transport $uri');
194 transport = new THttpClient(uri);
Jens Geyerb5028322014-11-09 02:38:11 +0100195 default:
196 throw "Unhandled transport";
197 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200198
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200199
Jens Geyerb5028322014-11-09 02:38:11 +0100200 // optinal layered transport
201 if ( framed) {
Jens Geyer05c1bff2014-11-29 19:29:58 +0100202 trace("- framed transport");
Jens Geyerb5028322014-11-09 02:38:11 +0100203 transport = new TFramedTransport(transport);
204 } else if ( buffered) {
Jens Geyer05c1bff2014-11-29 19:29:58 +0100205 trace("- buffered transport");
206 transport = new TBufferedTransport(transport);
Jens Geyerb5028322014-11-09 02:38:11 +0100207 }
208
209
210 // protocol
211 var protocol : TProtocol;
212 switch(prot)
213 {
214 case binary:
215 trace("- binary protocol");
216 protocol = new TBinaryProtocol( transport);
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200217 case json:
Jens Geyerb5028322014-11-09 02:38:11 +0100218 trace("- JSON protocol");
219 protocol = new TJSONProtocol( transport);
220 default:
221 throw "Unhandled protocol";
222 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200223
224
Jens Geyerb5028322014-11-09 02:38:11 +0100225 // put everything together
226 transport.open();
227 return new CalculatorImpl(protocol,protocol);
228 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200229
230
Jens Geyerb5028322014-11-09 02:38:11 +0100231 private static function RunClient() : Void {
232 var client = ClientSetup();
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200233
Jens Geyerb5028322014-11-09 02:38:11 +0100234 try {
235 client.ping();
236 trace("ping() successful");
237 } catch(error : TException) {
238 trace('ping() failed: $error');
239 } catch(error : Dynamic) {
240 trace('ping() failed: $error');
241 }
242
243 try {
244 var sum = client.add( 1, 1);
245 trace('1+1=$sum');
246 } catch(error : TException) {
247 trace('add() failed: $error');
248 } catch(error : Dynamic) {
249 trace('add() failed: $error');
250 }
251
252
253 var work = new tutorial.Work();
254 work.op = tutorial.Operation.DIVIDE;
255 work.num1 = 1;
256 work.num2 = 0;
257 try {
258 var quotient = client.calculate( 1, work);
259 trace('Whoa we can divide by 0! Result = $quotient');
260 } catch(error : TException) {
261 trace('calculate() failed: $error');
262 } catch(error : Dynamic) {
263 trace('calculate() failed: $error');
264 }
265
266 work.op = tutorial.Operation.SUBTRACT;
267 work.num1 = 15;
268 work.num2 = 10;
269 try {
270 var diff = client.calculate( 1, work);
271 trace('15-10=$diff');
272 } catch(error : TException) {
273 trace('calculate() failed: $error');
274 } catch(error : Dynamic) {
275 trace('calculate() failed: $error');
276 }
277
278
279 try {
280 var log : SharedStruct = client.getStruct( 1);
281 var logval = log.value;
282 trace('Check log: $logval');
283 } catch(error : TException) {
284 trace('getStruct() failed: $error');
285 } catch(error : Dynamic) {
286 trace('getStruct() failed: $error');
287 }
288 }
289
290
291 private static function ServerSetup() : TServer {
292 trace("Server configuration:");
293
294 // endpoint transport
295 var transport : TServerTransport = null;
296 switch(trns)
297 {
298 case socket:
299 #if (flash || js)
300 throw 'current platform does not support socket servers';
301 #else
302 trace('- socket transport port $targetPort');
303 transport = new TServerSocket( targetPort);
304 #end
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200305 case http:
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +0300306 #if !phpwebserver
307 throw "HTTP server not implemented yet";
308 //trace("- http transport");
309 //transport = new THttpClient( targetHost);
310 #else
311 trace("- http transport");
312 transport = new TWrappingServerTransport(
313 new TStreamTransport(
314 new TFileStream("php://input", Read),
315 new TFileStream("php://output", Append)
316 )
317 );
318
319 #end
Jens Geyerb5028322014-11-09 02:38:11 +0100320 default:
321 throw "Unhandled transport";
322 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200323
Jens Geyerb5028322014-11-09 02:38:11 +0100324 // optional: layered transport
325 var transfactory : TTransportFactory = null;
326 if ( framed) {
Jens Geyer05c1bff2014-11-29 19:29:58 +0100327 trace("- framed transport");
Jens Geyerb5028322014-11-09 02:38:11 +0100328 transfactory = new TFramedTransportFactory();
329 } else if ( buffered) {
Jens Geyer05c1bff2014-11-29 19:29:58 +0100330 trace("- buffered transport");
331 transfactory = new TBufferedTransportFactory();
Jens Geyerb5028322014-11-09 02:38:11 +0100332 }
333
334 // protocol
335 var protfactory : TProtocolFactory = null;
336 switch(prot)
337 {
338 case binary:
339 trace("- binary protocol");
340 protfactory = new TBinaryProtocolFactory();
341 case json:
342 trace("- JSON protocol");
343 protfactory = new TJSONProtocolFactory();
344 default:
345 throw "Unhandled protocol";
346 }
347
348 var handler = new CalculatorHandler();
349 var processor = new CalculatorProcessor(handler);
350 var server = new TSimpleServer( processor, transport, transfactory, protfactory);
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +0300351 #if phpwebserver
352 server.runOnce = true;
353 #end
354
Jens Geyerb5028322014-11-09 02:38:11 +0100355 return server;
356 }
357
358
359 private static function RunServer() : Void {
360 try
361 {
362 var server = ServerSetup();
363
364 trace("\nStarting the server...");
365 server.Serve();
366 }
367 catch( e : Dynamic)
368 {
369 trace('RunServer() failed: $e');
370 }
371 trace("done.");
372 }
373
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200374}
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +0300375