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() { |
Oleksii Prudkyi | 39a09ac | 2016-05-19 16:55:11 +0300 | [diff] [blame] | 54 | |
| 55 | #if ! (flash || js || phpwebserver) |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 56 | try { |
| 57 | ParseArgs(); |
| 58 | } catch (e : String) { |
| 59 | trace(e); |
| 60 | trace(GetHelp()); |
| 61 | return; |
| 62 | } |
Oleksii Prudkyi | 39a09ac | 2016-05-19 16:55:11 +0300 | [diff] [blame] | 63 | |
| 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 Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 74 | #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 Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Oleksii Prudkyi | 39a09ac | 2016-05-19 16:55:11 +0300 | [diff] [blame] | 88 | #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 Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 111 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 112 | #if ! (flash || js) |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 113 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 114 | 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 Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 124 | |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 125 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 126 | private static function ParseArgs() : Void { |
| 127 | var step = 0; |
| 128 | for (arg in Sys.args()) { |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 129 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 130 | // 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 Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 140 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 141 | 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 Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 155 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 156 | 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 Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 191 | case http: |
Oleksii Prudkyi | 39a09ac | 2016-05-19 16:55:11 +0300 | [diff] [blame] | 192 | var uri = 'http://${targetHost}:${targetPort}'; |
| 193 | trace('- HTTP transport $uri'); |
| 194 | transport = new THttpClient(uri); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 195 | default: |
| 196 | throw "Unhandled transport"; |
| 197 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 198 | |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 199 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 200 | // optinal layered transport |
| 201 | if ( framed) { |
Jens Geyer | 05c1bff | 2014-11-29 19:29:58 +0100 | [diff] [blame] | 202 | trace("- framed transport"); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 203 | transport = new TFramedTransport(transport); |
| 204 | } else if ( buffered) { |
Jens Geyer | 05c1bff | 2014-11-29 19:29:58 +0100 | [diff] [blame] | 205 | trace("- buffered transport"); |
| 206 | transport = new TBufferedTransport(transport); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 207 | } |
| 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 Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 217 | case json: |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 218 | trace("- JSON protocol"); |
| 219 | protocol = new TJSONProtocol( transport); |
| 220 | default: |
| 221 | throw "Unhandled protocol"; |
| 222 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 223 | |
| 224 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 225 | // put everything together |
| 226 | transport.open(); |
| 227 | return new CalculatorImpl(protocol,protocol); |
| 228 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 229 | |
| 230 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 231 | private static function RunClient() : Void { |
| 232 | var client = ClientSetup(); |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 233 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 234 | 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 Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 305 | case http: |
Oleksii Prudkyi | 39a09ac | 2016-05-19 16:55:11 +0300 | [diff] [blame] | 306 | #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 Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 320 | default: |
| 321 | throw "Unhandled transport"; |
| 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 | // optional: layered transport |
| 325 | var transfactory : TTransportFactory = null; |
| 326 | if ( framed) { |
Jens Geyer | 05c1bff | 2014-11-29 19:29:58 +0100 | [diff] [blame] | 327 | trace("- framed transport"); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 328 | transfactory = new TFramedTransportFactory(); |
| 329 | } else if ( buffered) { |
Jens Geyer | 05c1bff | 2014-11-29 19:29:58 +0100 | [diff] [blame] | 330 | trace("- buffered transport"); |
| 331 | transfactory = new TBufferedTransportFactory(); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 332 | } |
| 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 Prudkyi | 39a09ac | 2016-05-19 16:55:11 +0300 | [diff] [blame] | 351 | #if phpwebserver |
| 352 | server.runOnce = true; |
| 353 | #end |
| 354 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 355 | 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 Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 374 | } |
Oleksii Prudkyi | 39a09ac | 2016-05-19 16:55:11 +0300 | [diff] [blame] | 375 | |