blob: a56549f8cb9254e41443bbb9b4fe0dcdad8da13e [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 Geyer2dcefad2021-02-25 09:42:52 +010035 compact;
Jens Geyerbd52f1a2014-07-28 01:25:30 +020036}
37
38enum Trns {
Jens Geyerb5028322014-11-09 02:38:11 +010039 socket;
40 http;
Jens Geyerbd52f1a2014-07-28 01:25:30 +020041}
42
43class Main {
Jens Geyerbd52f1a2014-07-28 01:25:30 +020044
Jens Geyerb5028322014-11-09 02:38:11 +010045 private static var server : Bool = false;
46 private static var framed : Bool = false;
47 private static var buffered : Bool = false;
48 private static var prot : Prot = binary;
49 private static var trns : Trns = socket;
Jens Geyerbd52f1a2014-07-28 01:25:30 +020050
Jens Geyerb5028322014-11-09 02:38:11 +010051 private static var targetHost : String = "localhost";
52 private static var targetPort : Int = 9090;
53
54 static function main() {
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +030055
56 #if ! (flash || js || phpwebserver)
Jens Geyerb5028322014-11-09 02:38:11 +010057 try {
58 ParseArgs();
59 } catch (e : String) {
60 trace(e);
61 trace(GetHelp());
62 return;
63 }
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +030064
65 #elseif phpwebserver
66 //forcing server
67 server = true;
68 trns = http;
69 initPhpWebServer();
70 //check method
71 if(php.Web.getMethod() != 'POST') {
72 Sys.println('http endpoint for thrift test server');
73 return;
74 }
Jens Geyerb5028322014-11-09 02:38:11 +010075 #end
76
77 try {
78 if (server)
79 RunServer();
80 else
81 RunClient();
82 } catch (e : String) {
83 trace(e);
84 }
85
86 trace("Completed.");
Jens Geyerbd52f1a2014-07-28 01:25:30 +020087 }
88
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +030089 #if phpwebserver
90 private static function initPhpWebServer()
91 {
92 //remap trace to error log
93 haxe.Log.trace = function(v:Dynamic, ?infos:haxe.PosInfos)
94 {
95 // handle trace
96 var newValue : Dynamic;
97 if (infos != null && infos.customParams!=null) {
98 var extra:String = "";
99 for( v in infos.customParams )
100 extra += "," + v;
101 newValue = v + extra;
102 }
103 else {
104 newValue = v;
105 }
106 var msg = infos != null ? infos.fileName + ':' + infos.lineNumber + ': ' : '';
107 Sys.stderr().writeString('${msg}${newValue}\n');
108 }
109 }
110 #end
111
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200112
Jens Geyerb5028322014-11-09 02:38:11 +0100113 #if ! (flash || js)
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200114
Jens Geyerb5028322014-11-09 02:38:11 +0100115 private static function GetHelp() : String {
Jens Geyer2dcefad2021-02-25 09:42:52 +0100116 return Sys.programPath+" modus layered transport protocol\n"
Jens Geyerb5028322014-11-09 02:38:11 +0100117 +"Options:\n"
Jens Geyer2dcefad2021-02-25 09:42:52 +0100118 +" modus: client, server (default: client)\n"
119 +" layered: framed, buffered (default: none)\n"
120 +" transport: socket, http (default: socket)\n"
121 +" protocol: binary, json, compact (default: binary)\n"
Jens Geyerb5028322014-11-09 02:38:11 +0100122 +"\n"
123 +"All arguments are optional.\n";
124 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200125
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200126
Jens Geyerb5028322014-11-09 02:38:11 +0100127 private static function ParseArgs() : Void {
128 var step = 0;
129 for (arg in Sys.args()) {
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200130
Jens Geyerb5028322014-11-09 02:38:11 +0100131 // server|client
132 switch(step) {
133 case 0:
134 ++step;
135 if ( arg == "client")
136 server = false;
137 else if ( arg == "server")
138 server = true;
139 else
140 throw "First argument must be 'server' or 'client'";
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200141
Jens Geyerb5028322014-11-09 02:38:11 +0100142 case 1:
143 if ( arg == "framed") {
144 framed = true;
145 } else if ( arg == "buffered") {
146 buffered = true;
147 } else if ( arg == "socket") {
148 trns = socket;
149 ++step;
150 } else if ( arg == "http") {
151 trns = http;
152 ++step;
153 } else {
154 throw "Unknown transport "+arg;
155 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200156
Jens Geyerb5028322014-11-09 02:38:11 +0100157 case 2:
158 if ( arg == "binary") {
159 prot = binary;
160 ++step;
161 } else if ( arg == "json") {
162 prot = json;
163 ++step;
Jens Geyer2dcefad2021-02-25 09:42:52 +0100164 } else if ( arg == "compact") {
165 prot = compact;
166 ++step;
Jens Geyerb5028322014-11-09 02:38:11 +0100167 } else {
168 throw "Unknown protocol "+arg;
169 }
170
171 default:
172 throw "Unexpected argument "+arg;
173 }
174
175 if ( framed && buffered)
176 {
177 trace("WN: framed supersedes buffered");
178 }
179
180 }
181 }
182
183 #end
184
185 private static function ClientSetup() : Calculator {
186 trace("Client configuration:");
187
188 // endpoint transport
189 var transport : TTransport;
190 switch(trns)
191 {
192 case socket:
193 trace('- socket transport $targetHost:$targetPort');
194 transport = new TSocket( targetHost, targetPort);
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200195 case http:
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +0300196 var uri = 'http://${targetHost}:${targetPort}';
197 trace('- HTTP transport $uri');
198 transport = new THttpClient(uri);
Jens Geyerb5028322014-11-09 02:38:11 +0100199 default:
200 throw "Unhandled transport";
201 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200202
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200203
Jens Geyerb5028322014-11-09 02:38:11 +0100204 // optinal layered transport
205 if ( framed) {
Jens Geyer05c1bff2014-11-29 19:29:58 +0100206 trace("- framed transport");
Jens Geyerb5028322014-11-09 02:38:11 +0100207 transport = new TFramedTransport(transport);
208 } else if ( buffered) {
Jens Geyer05c1bff2014-11-29 19:29:58 +0100209 trace("- buffered transport");
210 transport = new TBufferedTransport(transport);
Jens Geyerb5028322014-11-09 02:38:11 +0100211 }
212
213
214 // protocol
215 var protocol : TProtocol;
216 switch(prot)
217 {
218 case binary:
219 trace("- binary protocol");
220 protocol = new TBinaryProtocol( transport);
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200221 case json:
Jens Geyerb5028322014-11-09 02:38:11 +0100222 trace("- JSON protocol");
223 protocol = new TJSONProtocol( transport);
Jens Geyer2dcefad2021-02-25 09:42:52 +0100224 case compact:
225 trace("- compact protocol");
226 protocol = new TCompactProtocol( transport);
Jens Geyerb5028322014-11-09 02:38:11 +0100227 default:
228 throw "Unhandled protocol";
229 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200230
231
Jens Geyerb5028322014-11-09 02:38:11 +0100232 // put everything together
233 transport.open();
234 return new CalculatorImpl(protocol,protocol);
235 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200236
237
Jens Geyerb5028322014-11-09 02:38:11 +0100238 private static function RunClient() : Void {
239 var client = ClientSetup();
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200240
Jens Geyerb5028322014-11-09 02:38:11 +0100241 try {
Jens Geyer2dcefad2021-02-25 09:42:52 +0100242 client.ping();
Jens Geyerb5028322014-11-09 02:38:11 +0100243 trace("ping() successful");
244 } catch(error : TException) {
245 trace('ping() failed: $error');
246 } catch(error : Dynamic) {
247 trace('ping() failed: $error');
248 }
249
250 try {
251 var sum = client.add( 1, 1);
252 trace('1+1=$sum');
253 } catch(error : TException) {
254 trace('add() failed: $error');
255 } catch(error : Dynamic) {
256 trace('add() failed: $error');
257 }
258
259
260 var work = new tutorial.Work();
261 work.op = tutorial.Operation.DIVIDE;
262 work.num1 = 1;
263 work.num2 = 0;
264 try {
265 var quotient = client.calculate( 1, work);
266 trace('Whoa we can divide by 0! Result = $quotient');
267 } catch(error : TException) {
268 trace('calculate() failed: $error');
269 } catch(error : Dynamic) {
270 trace('calculate() failed: $error');
271 }
272
273 work.op = tutorial.Operation.SUBTRACT;
274 work.num1 = 15;
275 work.num2 = 10;
276 try {
277 var diff = client.calculate( 1, work);
278 trace('15-10=$diff');
279 } catch(error : TException) {
280 trace('calculate() failed: $error');
281 } catch(error : Dynamic) {
282 trace('calculate() failed: $error');
283 }
284
285
286 try {
287 var log : SharedStruct = client.getStruct( 1);
288 var logval = log.value;
289 trace('Check log: $logval');
290 } catch(error : TException) {
291 trace('getStruct() failed: $error');
292 } catch(error : Dynamic) {
293 trace('getStruct() failed: $error');
294 }
295 }
296
297
298 private static function ServerSetup() : TServer {
299 trace("Server configuration:");
300
301 // endpoint transport
302 var transport : TServerTransport = null;
303 switch(trns)
304 {
305 case socket:
306 #if (flash || js)
307 throw 'current platform does not support socket servers';
308 #else
309 trace('- socket transport port $targetPort');
310 transport = new TServerSocket( targetPort);
311 #end
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200312 case http:
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +0300313 #if !phpwebserver
314 throw "HTTP server not implemented yet";
315 //trace("- http transport");
316 //transport = new THttpClient( targetHost);
317 #else
318 trace("- http transport");
319 transport = new TWrappingServerTransport(
Jens Geyer2dcefad2021-02-25 09:42:52 +0100320 new TStreamTransport(
321 new TFileStream("php://input", Read),
322 new TFileStream("php://output", Append),
323 null
324 )
325 );
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +0300326
327 #end
Jens Geyerb5028322014-11-09 02:38:11 +0100328 default:
329 throw "Unhandled transport";
330 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200331
Jens Geyerb5028322014-11-09 02:38:11 +0100332 // optional: layered transport
333 var transfactory : TTransportFactory = null;
334 if ( framed) {
Jens Geyer05c1bff2014-11-29 19:29:58 +0100335 trace("- framed transport");
Jens Geyerb5028322014-11-09 02:38:11 +0100336 transfactory = new TFramedTransportFactory();
337 } else if ( buffered) {
Jens Geyer05c1bff2014-11-29 19:29:58 +0100338 trace("- buffered transport");
339 transfactory = new TBufferedTransportFactory();
Jens Geyerb5028322014-11-09 02:38:11 +0100340 }
341
342 // protocol
343 var protfactory : TProtocolFactory = null;
344 switch(prot)
345 {
346 case binary:
347 trace("- binary protocol");
348 protfactory = new TBinaryProtocolFactory();
349 case json:
350 trace("- JSON protocol");
351 protfactory = new TJSONProtocolFactory();
Jens Geyer2dcefad2021-02-25 09:42:52 +0100352 case compact:
353 trace("- compact protocol");
354 protfactory = new TCompactProtocolFactory();
Jens Geyerb5028322014-11-09 02:38:11 +0100355 default:
356 throw "Unhandled protocol";
357 }
358
Jens Geyer2dcefad2021-02-25 09:42:52 +0100359 var handler : Calculator_service = new CalculatorHandler();
Jens Geyerb5028322014-11-09 02:38:11 +0100360 var processor = new CalculatorProcessor(handler);
361 var server = new TSimpleServer( processor, transport, transfactory, protfactory);
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +0300362 #if phpwebserver
363 server.runOnce = true;
364 #end
365
Jens Geyerb5028322014-11-09 02:38:11 +0100366 return server;
367 }
368
369
370 private static function RunServer() : Void {
371 try
372 {
373 var server = ServerSetup();
374
375 trace("\nStarting the server...");
376 server.Serve();
377 }
378 catch( e : Dynamic)
379 {
380 trace('RunServer() failed: $e');
381 }
382 trace("done.");
383 }
384
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200385}
Oleksii Prudkyi39a09ac2016-05-19 16:55:11 +0300386