Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 1 | /* |
| 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 Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 19 | |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 20 | package; |
| 21 | |
| 22 | import org.apache.thrift.*; |
| 23 | import org.apache.thrift.protocol.*; |
| 24 | import org.apache.thrift.transport.*; |
| 25 | import org.apache.thrift.server.*; |
| 26 | import org.apache.thrift.meta_data.*; |
| 27 | |
| 28 | import tutorial.*; |
| 29 | import shared.*; |
| 30 | |
| 31 | |
| 32 | enum Prot { |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 33 | binary; |
| 34 | json; |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | enum Trns { |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 38 | socket; |
| 39 | http; |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | class Main { |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 43 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 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; |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 49 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 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."); |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 76 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 77 | #if ! (flash || js) |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 78 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 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 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 89 | |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 90 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 91 | private static function ParseArgs() : Void { |
| 92 | var step = 0; |
| 93 | for (arg in Sys.args()) { |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 94 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 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'"; |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 105 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 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 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 120 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 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); |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 156 | case http: |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 157 | trace('- HTTP transport $targetHost'); |
| 158 | transport = new THttpClient( targetHost); |
| 159 | default: |
| 160 | throw "Unhandled transport"; |
| 161 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 162 | |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 163 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 164 | // optinal layered transport |
| 165 | if ( framed) { |
Jens Geyer | 05c1bff | 2014-11-29 19:29:58 +0100 | [diff] [blame] | 166 | trace("- framed transport"); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 167 | transport = new TFramedTransport(transport); |
| 168 | } else if ( buffered) { |
Jens Geyer | 05c1bff | 2014-11-29 19:29:58 +0100 | [diff] [blame] | 169 | trace("- buffered transport"); |
| 170 | transport = new TBufferedTransport(transport); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 171 | } |
| 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 Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 181 | case json: |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 182 | trace("- JSON protocol"); |
| 183 | protocol = new TJSONProtocol( transport); |
| 184 | default: |
| 185 | throw "Unhandled protocol"; |
| 186 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 187 | |
| 188 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 189 | // put everything together |
| 190 | transport.open(); |
| 191 | return new CalculatorImpl(protocol,protocol); |
| 192 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 193 | |
| 194 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 195 | private static function RunClient() : Void { |
| 196 | var client = ClientSetup(); |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 197 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 198 | 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 Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 269 | case http: |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 270 | throw "HTTP server not implemented yet"; |
| 271 | //trace("- http transport"); |
| 272 | //transport = new THttpClient( targetHost); |
| 273 | default: |
| 274 | throw "Unhandled transport"; |
| 275 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 276 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 277 | // optional: layered transport |
| 278 | var transfactory : TTransportFactory = null; |
| 279 | if ( framed) { |
Jens Geyer | 05c1bff | 2014-11-29 19:29:58 +0100 | [diff] [blame] | 280 | trace("- framed transport"); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 281 | transfactory = new TFramedTransportFactory(); |
| 282 | } else if ( buffered) { |
Jens Geyer | 05c1bff | 2014-11-29 19:29:58 +0100 | [diff] [blame] | 283 | trace("- buffered transport"); |
| 284 | transfactory = new TBufferedTransportFactory(); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 285 | } |
| 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 Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 323 | } |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 324 | |