blob: 41d8945476c4b5c7a0bd1ecee2e91771e3ffa6e1 [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 */
19
20package;
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 {
33 binary;
34 json;
35}
36
37enum Trns {
38 socket;
39 http;
40}
41
42class Main {
43
44 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;
49
50 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.");
74 }
75
76
77 #if ! (flash || js)
78
79 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 }
89
90
91 private static function ParseArgs() : Void {
92 var step = 0;
93 for (arg in Sys.args()) {
94
95 // 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'";
105
106 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 }
120
121 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);
156 case http:
Jens Geyerfea00ac2014-10-01 02:22:48 +0200157 trace('- HTTP transport $targetHost');
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200158 transport = new THttpClient( targetHost);
Jens Geyerbd52f1a2014-07-28 01:25:30 +0200159 default:
160 throw "Unhandled transport";
161 }
162
163
164 // optinal layered transport
165 if ( framed) {
166 trace("- framed transport");
167 transport = new TFramedTransport(transport);
168 } else if ( buffered) {
169 trace("- buffered transport");
170 throw "TBufferedTransport not implemented yet";
171 //transport = new TBufferedTransport(transport);
172 }
173
174
175 // protocol
176 var protocol : TProtocol;
177 switch(prot)
178 {
179 case binary:
180 trace("- binary protocol");
181 protocol = new TBinaryProtocol( transport);
182 case json:
183 trace("- JSON protocol");
184 protocol = new TJSONProtocol( transport);
185 default:
186 throw "Unhandled protocol";
187 }
188
189
190 // put everything together
191 transport.open();
192 return new CalculatorImpl(protocol,protocol);
193 }
194
195
196 private static function RunClient() : Void {
197 var client = ClientSetup();
198
199 try {
200 client.ping();
201 trace("ping() successful");
202 } catch(error : TException) {
203 trace('ping() failed: $error');
204 } catch(error : Dynamic) {
205 trace('ping() failed: $error');
206 }
207
208 try {
209 var sum = client.add( 1, 1);
210 trace('1+1=$sum');
211 } catch(error : TException) {
212 trace('add() failed: $error');
213 } catch(error : Dynamic) {
214 trace('add() failed: $error');
215 }
216
217
218 var work = new tutorial.Work();
219 work.op = tutorial.Operation.DIVIDE;
220 work.num1 = 1;
221 work.num2 = 0;
222 try {
223 var quotient = client.calculate( 1, work);
224 trace('Whoa we can divide by 0! Result = $quotient');
225 } catch(error : TException) {
226 trace('calculate() failed: $error');
227 } catch(error : Dynamic) {
228 trace('calculate() failed: $error');
229 }
230
231 work.op = tutorial.Operation.SUBTRACT;
232 work.num1 = 15;
233 work.num2 = 10;
234 try {
235 var diff = client.calculate( 1, work);
236 trace('15-10=$diff');
237 } catch(error : TException) {
238 trace('calculate() failed: $error');
239 } catch(error : Dynamic) {
240 trace('calculate() failed: $error');
241 }
242
243
244 try {
245 var log : SharedStruct = client.getStruct( 1);
246 var logval = log.value;
247 trace('Check log: $logval');
248 } catch(error : TException) {
249 trace('getStruct() failed: $error');
250 } catch(error : Dynamic) {
251 trace('getStruct() failed: $error');
252 }
253 }
254
255
256 private static function ServerSetup() : TServer {
257 trace("Server configuration:");
258
259 // endpoint transport
260 var transport : TServerTransport = null;
261 switch(trns)
262 {
263 case socket:
264 #if (flash || js)
265 throw 'current platform does not support socket servers';
266 #else
267 trace('- socket transport port $targetPort');
268 transport = new TServerSocket( targetPort);
269 #end
270 case http:
271 throw "HTTP server not implemented yet";
272 //trace("- http transport");
273 //transport = new THttpClient( targetHost);
274 default:
275 throw "Unhandled transport";
276 }
277
278 // optional: layered transport
279 var transfactory : TTransportFactory = null;
280 if ( framed) {
281 trace("- framed transport");
282 transfactory = new TFramedTransportFactory();
283 } else if ( buffered) {
284 trace("- buffered transport");
285 throw "TBufferedTransport not implemented yet";
286 //transfactory = new TBufferedTransportFactory();
287 }
288
289 // protocol
290 var protfactory : TProtocolFactory = null;
291 switch(prot)
292 {
293 case binary:
294 trace("- binary protocol");
295 protfactory = new TBinaryProtocolFactory();
296 case json:
297 trace("- JSON protocol");
298 protfactory = new TJSONProtocolFactory();
299 default:
300 throw "Unhandled protocol";
301 }
302
303 var handler = new CalculatorHandler();
304 var processor = new CalculatorProcessor(handler);
305 var server = new TSimpleServer( processor, transport, transfactory, protfactory);
306 return server;
307 }
308
309
310 private static function RunServer() : Void {
311 try
312 {
313 var server = ServerSetup();
314
315 trace("\nStarting the server...");
316 server.Serve();
317 }
318 catch( e : Dynamic)
319 {
320 trace('RunServer() failed: $e');
321 }
322 trace("done.");
323 }
324
325}