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 | */ |
| 19 | |
| 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.*; |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 27 | import haxe.io.Path; |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 28 | |
| 29 | using StringTools; |
| 30 | |
| 31 | |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 32 | enum ProtocolType { |
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 | |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 37 | enum EndpointTransport { |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 38 | socket; |
| 39 | http; |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | enum ServerType { |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 43 | simple; |
| 44 | /* |
| 45 | threadpool; |
| 46 | threaded; |
| 47 | nonblocking; |
| 48 | */ |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | |
| 52 | class Arguments |
| 53 | { |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 54 | public var printHelpOnly(default,null) : Bool = false; |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 55 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 56 | public var server(default,null) : Bool = false; |
| 57 | public var servertype(default,null) : ServerType = simple; |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 58 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 59 | public var host(default,null) : String = "localhost"; |
| 60 | public var port(default,null) : Int = 9090; |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 61 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 62 | public var protocol(default,null) : ProtocolType = binary; |
| 63 | public var transport(default,null) : EndpointTransport = socket; |
| 64 | public var framed(default,null) : Bool = false; |
| 65 | public var buffered(default,null) : Bool = false; |
| 66 | |
| 67 | public var numIterations(default,null) : Int = 1; |
| 68 | public var numThreads(default,null) : Int = 1; |
| 69 | public var skipSpeedTest(default,null) : Bool = false; |
| 70 | |
| 71 | |
| 72 | public function new() { |
| 73 | #if sys |
| 74 | try { |
| 75 | ParseArgs(); |
| 76 | } catch (e : String) { |
| 77 | trace(GetHelp()); |
| 78 | throw e; |
| 79 | } |
| 80 | #else |
| 81 | trace("WN: Platform does not support program arguments, using defaults."); |
| 82 | #end |
| 83 | } |
| 84 | |
| 85 | #if sys |
| 86 | |
| 87 | private static function GetHelp() : String { |
| 88 | var sProg = Path.withoutDirectory( Sys.executablePath()); |
| 89 | return "\n" |
| 90 | +sProg+" [client|server] [options]\n" |
| 91 | +"\n" |
| 92 | +"Modus: Either client or server, the default is client.\n" |
| 93 | +"\n" |
| 94 | +"Common options:\n" |
| 95 | +" -h [ --help ] produce help message\n" |
| 96 | +" --port arg (=9090) Port number to listen / connect to\n" |
| 97 | /* not supported yet |
| 98 | +" --domain-socket arg Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)\n" |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 99 | +" --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)\n" |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 100 | */ |
| 101 | +" --protocol arg (=binary) protocol: binary, compact, json\n" |
| 102 | /* not supported yet |
| 103 | +" --ssl Encrypted Transport using SSL\n" |
| 104 | */ |
| 105 | +"\n" |
| 106 | +"Server only options:\n" |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 107 | +" --transport arg (=sockets) Transport: buffered, framed, http, anonpipe\n" |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 108 | /* not supported yet |
| 109 | +" --processor-events processor-events\n" |
| 110 | +" --server-type arg (=simple) type of server, \"simple\", \"thread-pool\", \n" |
| 111 | +" \"threaded\", or \"nonblocking\"\n" |
| 112 | +" -n [ --workers ] arg (=4) Number of thread pools workers. Only valid for \n" |
| 113 | +" thread-pool server type\n" |
| 114 | */ |
| 115 | +"\n" |
| 116 | +"Client only options:\n" |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 117 | +" --host arg (=localhost) Host to connect\n" |
| 118 | +" --transport arg (=sockets) Transport: buffered, framed, http, evhttp\n" |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 119 | /* not supported yet |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 120 | +" --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles)\n" |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 121 | */ |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 122 | +" -n [ --testloops ] arg (=1) Number of Tests\n" |
| 123 | +" -t [ --threads ] arg (=1) Number of Test threads\n" |
| 124 | +" --skip-speed-test Skip the speed test\n" |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 125 | +"\n" |
| 126 | +"All arguments are optional.\n" |
| 127 | ; |
| 128 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 129 | |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 130 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 131 | private function ParseArgs() : Void { |
| 132 | |
| 133 | var args = Sys.args().copy(); |
| 134 | if( (args == null) || (args.length <= 0)) { |
| 135 | server = false; |
| 136 | numThreads = 1; |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | var arg = args.shift(); |
| 141 | if ( arg == "client") { |
| 142 | server = false; |
| 143 | numThreads = 1; |
| 144 | } |
| 145 | else if ( arg == "server") { |
| 146 | server = true; |
| 147 | numThreads = 4; |
| 148 | } |
| 149 | else if ( (arg == "-h") || (arg == "--help")) { |
| 150 | // -h [ --help ] produce help message |
| 151 | Sys.println( GetHelp()); |
| 152 | printHelpOnly = true; |
| 153 | return; |
| 154 | } |
| 155 | else { |
| 156 | throw "First argument must be 'server' or 'client'"; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | while( args.length > 0) { |
| 161 | arg = args.shift(); |
| 162 | |
| 163 | if ( (arg == "-h") || (arg == "--help")) { |
| 164 | // -h [ --help ] produce help message |
| 165 | Sys.println( GetHelp()); |
| 166 | printHelpOnly = true; |
| 167 | return; |
| 168 | } |
| 169 | else if (arg == "--port") { |
| 170 | // --port arg (=9090) Port number to listen |
| 171 | arg = args.shift(); |
| 172 | var tmp = Std.parseInt(arg); |
| 173 | if( tmp != null) { |
| 174 | port = tmp; |
| 175 | } else { |
| 176 | throw "Invalid port number "+arg; |
| 177 | } |
| 178 | } |
| 179 | else if (arg == "--domain-socket") { |
| 180 | // --domain-socket arg Unix Domain Socket (e.g. /tmp/ThriftTest.thrift) |
| 181 | throw "domain sockets not supported yet"; |
| 182 | } |
| 183 | else if (arg == "--named-pipe") { |
| 184 | // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe) |
| 185 | throw "named pipes not supported yet"; |
| 186 | } |
| 187 | else if (arg == "--protocol") { |
| 188 | // --protocol arg (=binary) protocol: binary, compact, json |
| 189 | arg = args.shift(); |
| 190 | if( arg == "binary") { |
| 191 | protocol = binary; |
| 192 | } else if( arg == "compact") { |
| 193 | throw "Compact protocol not supported yet"; |
| 194 | } else if( arg == "json") { |
| 195 | protocol = json; |
| 196 | } else { |
| 197 | InvalidArg(arg); |
| 198 | } |
| 199 | } |
| 200 | else if (arg == "--ssl") { |
| 201 | // --ssl Encrypted Transport using SSL |
| 202 | throw "SSL not supported yet"; |
| 203 | } |
| 204 | else { |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 205 | //Server only options: |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 206 | if( server) { |
| 207 | ParseServerArgument( arg, args); |
| 208 | } else { |
| 209 | ParseClientArgument( arg, args); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 214 | |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 215 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 216 | private function ParseServerArgument( arg : String, args : Array<String>) : Void { |
| 217 | if (arg == "--transport") { |
| 218 | // --transport arg (=sockets) Transport: buffered, framed, http, anonpipe |
| 219 | arg = args.shift(); |
| 220 | if( arg == "buffered") { |
| 221 | buffered = true; |
| 222 | } else if( arg == "framed") { |
| 223 | framed = true; |
| 224 | } else if( arg == "http") { |
| 225 | transport = http; |
| 226 | } else if( arg == "anonpipe") { |
| 227 | throw "Anon pipes transport not supported yet"; |
| 228 | } else { |
| 229 | InvalidArg(arg); |
| 230 | } |
| 231 | } |
| 232 | else if (arg == "--processor-events") { |
| 233 | throw "Processor events not supported yet"; |
| 234 | } |
| 235 | else if (arg == "--server-type") { |
| 236 | // --server-type arg (=simple) type of server, |
| 237 | // one of "simple", "thread-pool", "threaded", "nonblocking" |
| 238 | arg = args.shift(); |
| 239 | if( arg == "simple") { |
| 240 | servertype = simple; |
| 241 | } else if( arg == "thread-pool") { |
| 242 | throw arg+" server not supported yet"; |
| 243 | } else if( arg == "threaded") { |
| 244 | throw arg+" server not supported yet"; |
| 245 | } else if( arg == "nonblocking") { |
| 246 | throw arg+" server not supported yet"; |
| 247 | } else { |
| 248 | InvalidArg(arg); |
| 249 | } |
| 250 | } |
| 251 | else if ((arg == "-n") || (arg == "--workers")) { |
| 252 | // -n [ --workers ] arg (=4) Number of thread pools workers. Only valid for |
| 253 | // thread-pool server type |
| 254 | arg = args.shift(); |
| 255 | var tmp = Std.parseInt(arg); |
| 256 | if( tmp != null) { |
| 257 | numThreads = tmp; |
| 258 | } else{ |
| 259 | throw "Invalid number "+arg; |
| 260 | } |
| 261 | } |
| 262 | else { |
| 263 | InvalidArg(arg); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | |
| 268 | private function ParseClientArgument( arg : String, args : Array<String>) : Void { |
| 269 | if (arg == "--host") { |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 270 | // --host arg (=localhost) Host to connect |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 271 | host = args.shift(); |
| 272 | } |
| 273 | else if (arg == "--transport") { |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 274 | // --transport arg (=sockets) Transport: buffered, framed, http, evhttp |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 275 | arg = args.shift(); |
| 276 | if( arg == "buffered") { |
| 277 | buffered = true; |
| 278 | } else if( arg == "framed") { |
| 279 | framed = true; |
| 280 | } else if( arg == "http") { |
| 281 | transport = http; |
| 282 | } else if( arg == "evhttp") { |
| 283 | throw "evhttp transport not supported yet"; |
| 284 | } else { |
| 285 | InvalidArg(arg); |
| 286 | } |
| 287 | } |
| 288 | else if (arg == "--anon-pipes") { |
| 289 | // --anon-pipes hRead hWrite Windows Anonymous Pipes pair (handles) |
| 290 | throw "Anon pipes transport not supported yet"; |
| 291 | } |
| 292 | else if ((arg == "-n") || (arg == "--testloops")) { |
| 293 | // -n [ --testloops ] arg (=1) Number of Tests |
| 294 | arg = args.shift(); |
| 295 | var tmp = Std.parseInt(arg); |
| 296 | if( tmp != null) { |
| 297 | numIterations = tmp; |
| 298 | } else { |
| 299 | throw "Invalid number "+arg; |
| 300 | } |
| 301 | } |
| 302 | else if ((arg == "-t") || (arg == "--threads")) { |
| 303 | // -t [ --threads ] arg (=1) Number of Test threads |
| 304 | arg = args.shift(); |
| 305 | var tmp = Std.parseInt(arg); |
| 306 | if( tmp != null) { |
| 307 | numThreads = tmp; |
| 308 | } else { |
| 309 | throw "Invalid number "+arg; |
| 310 | } |
| 311 | } |
| 312 | else if (arg == "--skip-speed-test") { |
| 313 | // --skip-speed-test Skip the speed test |
| 314 | skipSpeedTest = true; |
| 315 | } |
| 316 | else { |
| 317 | InvalidArg(arg); |
| 318 | } |
| 319 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 320 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 321 | |
| 322 | #end |
| 323 | |
| 324 | |
| 325 | private function InvalidArg( arg : String) : Void { |
| 326 | throw 'Invalid argument $arg'; |
| 327 | } |
Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame] | 328 | } |