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