blob: 8f168c103c382772f71733c075791a2c2812652c [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() {
54 #if ! (flash || js)
55 try {
56 ParseArgs();
57 } catch (e : String) {
58 trace(e);
59 trace(GetHelp());
60 return;
61 }
62 #end
63
64 try {
65 if (server)
66 RunServer();
67 else
68 RunClient();
69 } catch (e : String) {
70 trace(e);
71 }
72
73 trace("Completed.");
Jens Geyerbd52f1a2014-07-28 01:25:30 +020074 }
75
Jens Geyerbd52f1a2014-07-28 01:25:30 +020076
Jens Geyerb5028322014-11-09 02:38:11 +010077 #if ! (flash || js)
Jens Geyerbd52f1a2014-07-28 01:25:30 +020078
Jens Geyerb5028322014-11-09 02:38:11 +010079 private static function GetHelp() : String {
80 return Sys.executablePath()+" modus trnsOption transport protocol\n"
81 +"Options:\n"
82 +" modus: client, server (default: client)\n"
83 +" trnsOption: framed, buffered (default: none)\n"
84 +" transport: socket, http (default: socket)\n"
85 +" protocol: binary, json (default: binary)\n"
86 +"\n"
87 +"All arguments are optional.\n";
88 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +020089
Jens Geyerbd52f1a2014-07-28 01:25:30 +020090
Jens Geyerb5028322014-11-09 02:38:11 +010091 private static function ParseArgs() : Void {
92 var step = 0;
93 for (arg in Sys.args()) {
Jens Geyerbd52f1a2014-07-28 01:25:30 +020094
Jens Geyerb5028322014-11-09 02:38:11 +010095 // server|client
96 switch(step) {
97 case 0:
98 ++step;
99 if ( arg == "client")
100 server = false;
101 else if ( arg == "server")
102 server = true;
103 else
104 throw "First argument must be 'server' or 'client'";
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200105
Jens Geyerb5028322014-11-09 02:38:11 +0100106 case 1:
107 if ( arg == "framed") {
108 framed = true;
109 } else if ( arg == "buffered") {
110 buffered = true;
111 } else if ( arg == "socket") {
112 trns = socket;
113 ++step;
114 } else if ( arg == "http") {
115 trns = http;
116 ++step;
117 } else {
118 throw "Unknown transport "+arg;
119 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200120
Jens Geyerb5028322014-11-09 02:38:11 +0100121 case 2:
122 if ( arg == "binary") {
123 prot = binary;
124 ++step;
125 } else if ( arg == "json") {
126 prot = json;
127 ++step;
128 } else {
129 throw "Unknown protocol "+arg;
130 }
131
132 default:
133 throw "Unexpected argument "+arg;
134 }
135
136 if ( framed && buffered)
137 {
138 trace("WN: framed supersedes buffered");
139 }
140
141 }
142 }
143
144 #end
145
146 private static function ClientSetup() : Calculator {
147 trace("Client configuration:");
148
149 // endpoint transport
150 var transport : TTransport;
151 switch(trns)
152 {
153 case socket:
154 trace('- socket transport $targetHost:$targetPort');
155 transport = new TSocket( targetHost, targetPort);
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200156 case http:
Jens Geyerb5028322014-11-09 02:38:11 +0100157 trace('- HTTP transport $targetHost');
158 transport = new THttpClient( targetHost);
159 default:
160 throw "Unhandled transport";
161 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200162
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200163
Jens Geyerb5028322014-11-09 02:38:11 +0100164 // optinal layered transport
165 if ( framed) {
Jens Geyer05c1bff2014-11-29 19:29:58 +0100166 trace("- framed transport");
Jens Geyerb5028322014-11-09 02:38:11 +0100167 transport = new TFramedTransport(transport);
168 } else if ( buffered) {
Jens Geyer05c1bff2014-11-29 19:29:58 +0100169 trace("- buffered transport");
170 transport = new TBufferedTransport(transport);
Jens Geyerb5028322014-11-09 02:38:11 +0100171 }
172
173
174 // protocol
175 var protocol : TProtocol;
176 switch(prot)
177 {
178 case binary:
179 trace("- binary protocol");
180 protocol = new TBinaryProtocol( transport);
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200181 case json:
Jens Geyerb5028322014-11-09 02:38:11 +0100182 trace("- JSON protocol");
183 protocol = new TJSONProtocol( transport);
184 default:
185 throw "Unhandled protocol";
186 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200187
188
Jens Geyerb5028322014-11-09 02:38:11 +0100189 // put everything together
190 transport.open();
191 return new CalculatorImpl(protocol,protocol);
192 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200193
194
Jens Geyerb5028322014-11-09 02:38:11 +0100195 private static function RunClient() : Void {
196 var client = ClientSetup();
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200197
Jens Geyerb5028322014-11-09 02:38:11 +0100198 try {
199 client.ping();
200 trace("ping() successful");
201 } catch(error : TException) {
202 trace('ping() failed: $error');
203 } catch(error : Dynamic) {
204 trace('ping() failed: $error');
205 }
206
207 try {
208 var sum = client.add( 1, 1);
209 trace('1+1=$sum');
210 } catch(error : TException) {
211 trace('add() failed: $error');
212 } catch(error : Dynamic) {
213 trace('add() failed: $error');
214 }
215
216
217 var work = new tutorial.Work();
218 work.op = tutorial.Operation.DIVIDE;
219 work.num1 = 1;
220 work.num2 = 0;
221 try {
222 var quotient = client.calculate( 1, work);
223 trace('Whoa we can divide by 0! Result = $quotient');
224 } catch(error : TException) {
225 trace('calculate() failed: $error');
226 } catch(error : Dynamic) {
227 trace('calculate() failed: $error');
228 }
229
230 work.op = tutorial.Operation.SUBTRACT;
231 work.num1 = 15;
232 work.num2 = 10;
233 try {
234 var diff = client.calculate( 1, work);
235 trace('15-10=$diff');
236 } catch(error : TException) {
237 trace('calculate() failed: $error');
238 } catch(error : Dynamic) {
239 trace('calculate() failed: $error');
240 }
241
242
243 try {
244 var log : SharedStruct = client.getStruct( 1);
245 var logval = log.value;
246 trace('Check log: $logval');
247 } catch(error : TException) {
248 trace('getStruct() failed: $error');
249 } catch(error : Dynamic) {
250 trace('getStruct() failed: $error');
251 }
252 }
253
254
255 private static function ServerSetup() : TServer {
256 trace("Server configuration:");
257
258 // endpoint transport
259 var transport : TServerTransport = null;
260 switch(trns)
261 {
262 case socket:
263 #if (flash || js)
264 throw 'current platform does not support socket servers';
265 #else
266 trace('- socket transport port $targetPort');
267 transport = new TServerSocket( targetPort);
268 #end
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200269 case http:
Jens Geyerb5028322014-11-09 02:38:11 +0100270 throw "HTTP server not implemented yet";
271 //trace("- http transport");
272 //transport = new THttpClient( targetHost);
273 default:
274 throw "Unhandled transport";
275 }
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200276
Jens Geyerb5028322014-11-09 02:38:11 +0100277 // optional: layered transport
278 var transfactory : TTransportFactory = null;
279 if ( framed) {
Jens Geyer05c1bff2014-11-29 19:29:58 +0100280 trace("- framed transport");
Jens Geyerb5028322014-11-09 02:38:11 +0100281 transfactory = new TFramedTransportFactory();
282 } else if ( buffered) {
Jens Geyer05c1bff2014-11-29 19:29:58 +0100283 trace("- buffered transport");
284 transfactory = new TBufferedTransportFactory();
Jens Geyerb5028322014-11-09 02:38:11 +0100285 }
286
287 // protocol
288 var protfactory : TProtocolFactory = null;
289 switch(prot)
290 {
291 case binary:
292 trace("- binary protocol");
293 protfactory = new TBinaryProtocolFactory();
294 case json:
295 trace("- JSON protocol");
296 protfactory = new TJSONProtocolFactory();
297 default:
298 throw "Unhandled protocol";
299 }
300
301 var handler = new CalculatorHandler();
302 var processor = new CalculatorProcessor(handler);
303 var server = new TSimpleServer( processor, transport, transfactory, protfactory);
304 return server;
305 }
306
307
308 private static function RunServer() : Void {
309 try
310 {
311 var server = ServerSetup();
312
313 trace("\nStarting the server...");
314 server.Serve();
315 }
316 catch( e : Dynamic)
317 {
318 trace('RunServer() failed: $e');
319 }
320 trace("done.");
321 }
322
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200323}
Jens Geyerb5028322014-11-09 02:38:11 +0100324