blob: 97041d0b9a8e2ffe5fcf43a795a61bff50f24fb5 [file] [log] [blame]
Jake Farrell27274222011-11-10 20:32:44 +00001(*
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
20unit TestServer;
21
Jens Geyer9f7f11e2016-04-14 21:37:11 +020022{$I ../src/Thrift.Defines.inc}
Roger Meier3bef8c22012-10-06 06:58:00 +000023{$WARN SYMBOL_PLATFORM OFF}
24
Jens Geyer06045cf2013-03-27 20:26:25 +020025{.$DEFINE RunEndless} // activate to interactively stress-test the server stop routines via Ctrl+C
26
Jake Farrell27274222011-11-10 20:32:44 +000027interface
28
29uses
Roger Meier3bef8c22012-10-06 06:58:00 +000030 Windows, SysUtils,
Jake Farrell27274222011-11-10 20:32:44 +000031 Generics.Collections,
32 Thrift.Console,
33 Thrift.Server,
34 Thrift.Transport,
Roger Meier3bef8c22012-10-06 06:58:00 +000035 Thrift.Transport.Pipes,
Jake Farrell27274222011-11-10 20:32:44 +000036 Thrift.Protocol,
37 Thrift.Protocol.JSON,
Jens Geyerf0e63312015-03-01 18:47:49 +010038 Thrift.Protocol.Compact,
Jake Farrell27274222011-11-10 20:32:44 +000039 Thrift.Collections,
40 Thrift.Utils,
41 Thrift.Test,
42 Thrift,
43 TestConstants,
Jens Geyer01640402013-09-25 21:12:21 +020044 TestServerEvents,
Jake Farrell27274222011-11-10 20:32:44 +000045 Contnrs;
46
47type
48 TTestServer = class
49 public
50 type
51
52 ITestHandler = interface( TThriftTest.Iface )
Roger Meier333bbf32012-01-08 21:51:08 +000053 procedure SetServer( const AServer : IServer );
Jens Geyer06045cf2013-03-27 20:26:25 +020054 procedure TestStop;
Jake Farrell27274222011-11-10 20:32:44 +000055 end;
56
57 TTestHandlerImpl = class( TInterfacedObject, ITestHandler )
58 private
59 FServer : IServer;
60 protected
61 procedure testVoid();
Jens Geyer39ba6b72015-09-22 00:00:49 +020062 function testBool(thing: Boolean): Boolean;
Roger Meier333bbf32012-01-08 21:51:08 +000063 function testString(const thing: string): string;
Jake Farrell7ae13e12011-10-18 14:35:26 +000064 function testByte(thing: ShortInt): ShortInt;
65 function testI32(thing: Integer): Integer;
Roger Meier333bbf32012-01-08 21:51:08 +000066 function testI64(const thing: Int64): Int64;
67 function testDouble(const thing: Double): Double;
Jens Geyerfd1b3582014-12-13 23:42:58 +010068 function testBinary(const thing: TBytes): TBytes;
Roger Meier333bbf32012-01-08 21:51:08 +000069 function testStruct(const thing: IXtruct): IXtruct;
70 function testNest(const thing: IXtruct2): IXtruct2;
71 function testMap(const thing: IThriftDictionary<Integer, Integer>): IThriftDictionary<Integer, Integer>;
72 function testStringMap(const thing: IThriftDictionary<string, string>): IThriftDictionary<string, string>;
73 function testSet(const thing: IHashSet<Integer>): IHashSet<Integer>;
74 function testList(const thing: IThriftList<Integer>): IThriftList<Integer>;
Jake Farrell7ae13e12011-10-18 14:35:26 +000075 function testEnum(thing: TNumberz): TNumberz;
Roger Meier333bbf32012-01-08 21:51:08 +000076 function testTypedef(const thing: Int64): Int64;
Jake Farrell7ae13e12011-10-18 14:35:26 +000077 function testMapMap(hello: Integer): IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
Roger Meier333bbf32012-01-08 21:51:08 +000078 function testInsanity(const argument: IInsanity): IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
79 function testMulti(arg0: ShortInt; arg1: Integer; const arg2: Int64; const arg3: IThriftDictionary<SmallInt, string>; arg4: TNumberz; const arg5: Int64): IXtruct;
80 procedure testException(const arg: string);
81 function testMultiException(const arg0: string; const arg1: string): IXtruct;
Jake Farrell7ae13e12011-10-18 14:35:26 +000082 procedure testOneway(secondsToSleep: Integer);
Jake Farrell27274222011-11-10 20:32:44 +000083
Jens Geyer06045cf2013-03-27 20:26:25 +020084 procedure TestStop;
Roger Meier333bbf32012-01-08 21:51:08 +000085 procedure SetServer( const AServer : IServer );
Jake Farrell27274222011-11-10 20:32:44 +000086 end;
87
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020088 class procedure PrintCmdLineHelp;
89 class procedure InvalidArgs;
90
Jens Geyer06045cf2013-03-27 20:26:25 +020091 class procedure LaunchAnonPipeChild( const app : string; const transport : IAnonymousPipeServerTransport);
Roger Meier333bbf32012-01-08 21:51:08 +000092 class procedure Execute( const args: array of string);
Jake Farrell27274222011-11-10 20:32:44 +000093 end;
94
95implementation
96
Jens Geyer06045cf2013-03-27 20:26:25 +020097
98var g_Handler : TTestServer.ITestHandler = nil;
99
100
101function MyConsoleEventHandler( dwCtrlType : DWORD) : BOOL; stdcall;
102// Note that this Handler procedure is called from another thread
103var handler : TTestServer.ITestHandler;
104begin
105 result := TRUE;
106 try
107 case dwCtrlType of
108 CTRL_C_EVENT : Console.WriteLine( 'Ctrl+C pressed');
109 CTRL_BREAK_EVENT : Console.WriteLine( 'Ctrl+Break pressed');
110 CTRL_CLOSE_EVENT : Console.WriteLine( 'Received CloseTask signal');
111 CTRL_LOGOFF_EVENT : Console.WriteLine( 'Received LogOff signal');
112 CTRL_SHUTDOWN_EVENT : Console.WriteLine( 'Received Shutdown signal');
113 else
114 Console.WriteLine( 'Received console event #'+IntToStr(Integer(dwCtrlType)));
115 end;
116
117 handler := g_Handler;
118 if handler <> nil then handler.TestStop;
119
120 except
121 // catch all
122 end;
123end;
124
125
Jake Farrell27274222011-11-10 20:32:44 +0000126{ TTestServer.TTestHandlerImpl }
127
Roger Meier333bbf32012-01-08 21:51:08 +0000128procedure TTestServer.TTestHandlerImpl.SetServer( const AServer: IServer);
Jake Farrell27274222011-11-10 20:32:44 +0000129begin
130 FServer := AServer;
131end;
132
133function TTestServer.TTestHandlerImpl.testByte(thing: ShortInt): ShortInt;
134begin
135 Console.WriteLine('testByte("' + IntToStr( thing) + '")');
136 Result := thing;
137end;
138
Roger Meier333bbf32012-01-08 21:51:08 +0000139function TTestServer.TTestHandlerImpl.testDouble( const thing: Double): Double;
Jake Farrell27274222011-11-10 20:32:44 +0000140begin
141 Console.WriteLine('testDouble("' + FloatToStr( thing ) + '")');
142 Result := thing;
143end;
144
Jens Geyerfd1b3582014-12-13 23:42:58 +0100145function TTestServer.TTestHandlerImpl.testBinary(const thing: TBytes): TBytes;
146begin
147 Console.WriteLine('testBinary("' + BytesToHex( thing ) + '")');
148 Result := thing;
149end;
150
Jake Farrell27274222011-11-10 20:32:44 +0000151function TTestServer.TTestHandlerImpl.testEnum(thing: TNumberz): TNumberz;
152begin
153 Console.WriteLine('testEnum(' + IntToStr( Integer( thing)) + ')');
154 Result := thing;
155end;
156
Roger Meier333bbf32012-01-08 21:51:08 +0000157procedure TTestServer.TTestHandlerImpl.testException(const arg: string);
Jake Farrell27274222011-11-10 20:32:44 +0000158begin
159 Console.WriteLine('testException(' + arg + ')');
160 if ( arg = 'Xception') then
161 begin
Roger Meierbb6de7a2012-05-04 23:35:45 +0000162 raise TXception.Create( 1001, arg);
Jake Farrell27274222011-11-10 20:32:44 +0000163 end;
Roger Meierbb6de7a2012-05-04 23:35:45 +0000164
165 if (arg = 'TException') then
166 begin
167 raise TException.Create('');
168 end;
169
170 // else do not throw anything
Jake Farrell27274222011-11-10 20:32:44 +0000171end;
172
173function TTestServer.TTestHandlerImpl.testI32(thing: Integer): Integer;
174begin
175 Console.WriteLine('testI32("' + IntToStr( thing) + '")');
176 Result := thing;
177end;
178
Roger Meier333bbf32012-01-08 21:51:08 +0000179function TTestServer.TTestHandlerImpl.testI64( const thing: Int64): Int64;
Jake Farrell27274222011-11-10 20:32:44 +0000180begin
181 Console.WriteLine('testI64("' + IntToStr( thing) + '")');
182 Result := thing;
183end;
184
185function TTestServer.TTestHandlerImpl.testInsanity(
Roger Meier333bbf32012-01-08 21:51:08 +0000186 const argument: IInsanity): IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
Jake Farrell27274222011-11-10 20:32:44 +0000187var
Jake Farrell27274222011-11-10 20:32:44 +0000188 looney : IInsanity;
189 first_map : IThriftDictionary<TNumberz, IInsanity>;
190 second_map : IThriftDictionary<TNumberz, IInsanity>;
191 insane : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
192
193begin
Jake Farrell27274222011-11-10 20:32:44 +0000194 Console.WriteLine('testInsanity()');
Jake Farrell27274222011-11-10 20:32:44 +0000195
Jens Geyer540e3462016-12-28 14:25:41 +0100196 (**
197 * So you think you've got this all worked, out eh?
198 *
199 * Creates a the returned map with these values and prints it out:
200 * { 1 => { 2 => argument,
201 * 3 => argument,
202 * },
203 * 2 => { 6 => <empty Insanity struct>, },
204 * }
205 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
206 *)
Jake Farrell27274222011-11-10 20:32:44 +0000207
208 first_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create;
209 second_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create;
210
Jens Geyer540e3462016-12-28 14:25:41 +0100211 first_map.AddOrSetValue( TNumberz.TWO, argument);
212 first_map.AddOrSetValue( TNumberz.THREE, argument);
Jake Farrell27274222011-11-10 20:32:44 +0000213
Jens Geyer540e3462016-12-28 14:25:41 +0100214 looney := TInsanityImpl.Create;
Jake Farrell27274222011-11-10 20:32:44 +0000215 second_map.AddOrSetValue( TNumberz.SIX, looney);
216
217 insane := TThriftDictionaryImpl<Int64, IThriftDictionary<TNumberz, IInsanity>>.Create;
218
219 insane.AddOrSetValue( 1, first_map);
220 insane.AddOrSetValue( 2, second_map);
221
222 Result := insane;
223end;
224
225function TTestServer.TTestHandlerImpl.testList(
Roger Meier333bbf32012-01-08 21:51:08 +0000226 const thing: IThriftList<Integer>): IThriftList<Integer>;
Jake Farrell27274222011-11-10 20:32:44 +0000227var
228 first : Boolean;
229 elem : Integer;
230begin
231 Console.Write('testList({');
232 first := True;
233 for elem in thing do
234 begin
235 if first then
236 begin
237 first := False;
238 end else
239 begin
240 Console.Write(', ');
241 end;
242 Console.Write( IntToStr( elem));
243 end;
244 Console.WriteLine('})');
245 Result := thing;
246end;
247
248function TTestServer.TTestHandlerImpl.testMap(
Roger Meier333bbf32012-01-08 21:51:08 +0000249 const thing: IThriftDictionary<Integer, Integer>): IThriftDictionary<Integer, Integer>;
Jake Farrell27274222011-11-10 20:32:44 +0000250var
251 first : Boolean;
252 key : Integer;
253begin
254 Console.Write('testMap({');
255 first := True;
256 for key in thing.Keys do
257 begin
258 if (first) then
259 begin
260 first := false;
261 end else
262 begin
263 Console.Write(', ');
264 end;
265 Console.Write(IntToStr(key) + ' => ' + IntToStr( thing[key]));
266 end;
267 Console.WriteLine('})');
268 Result := thing;
269end;
270
271function TTestServer.TTestHandlerImpl.TestMapMap(
272 hello: Integer): IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
273var
274 mapmap : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
275 pos : IThriftDictionary<Integer, Integer>;
276 neg : IThriftDictionary<Integer, Integer>;
277 i : Integer;
278begin
279 Console.WriteLine('testMapMap(' + IntToStr( hello) + ')');
280 mapmap := TThriftDictionaryImpl<Integer, IThriftDictionary<Integer, Integer>>.Create;
281 pos := TThriftDictionaryImpl<Integer, Integer>.Create;
282 neg := TThriftDictionaryImpl<Integer, Integer>.Create;
283
284 for i := 1 to 4 do
285 begin
286 pos.AddOrSetValue( i, i);
287 neg.AddOrSetValue( -i, -i);
288 end;
289
290 mapmap.AddOrSetValue(4, pos);
291 mapmap.AddOrSetValue( -4, neg);
292
293 Result := mapmap;
294end;
295
296function TTestServer.TTestHandlerImpl.testMulti(arg0: ShortInt; arg1: Integer;
Roger Meier333bbf32012-01-08 21:51:08 +0000297 const arg2: Int64; const arg3: IThriftDictionary<SmallInt, string>;
298 arg4: TNumberz; const arg5: Int64): IXtruct;
Jake Farrell27274222011-11-10 20:32:44 +0000299var
300 hello : IXtruct;
301begin
302 Console.WriteLine('testMulti()');
303 hello := TXtructImpl.Create;
304 hello.String_thing := 'Hello2';
305 hello.Byte_thing := arg0;
306 hello.I32_thing := arg1;
307 hello.I64_thing := arg2;
308 Result := hello;
309end;
310
Roger Meier333bbf32012-01-08 21:51:08 +0000311function TTestServer.TTestHandlerImpl.testMultiException( const arg0, arg1: string): IXtruct;
Jake Farrell27274222011-11-10 20:32:44 +0000312var
Jake Farrell27274222011-11-10 20:32:44 +0000313 x2 : TXception2;
314begin
315 Console.WriteLine('testMultiException(' + arg0 + ', ' + arg1 + ')');
316 if ( arg0 = 'Xception') then
317 begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200318 raise TXception.Create( 1001, 'This is an Xception'); // test the new rich CTOR
Jake Farrell27274222011-11-10 20:32:44 +0000319 end else
320 if ( arg0 = 'Xception2') then
321 begin
Jake Farrell343c61d2011-12-09 02:29:56 +0000322 x2 := TXception2.Create; // the old way still works too?
Jake Farrell27274222011-11-10 20:32:44 +0000323 x2.ErrorCode := 2002;
324 x2.Struct_thing := TXtructImpl.Create;
325 x2.Struct_thing.String_thing := 'This is an Xception2';
Jake Farrellac102562011-11-23 14:30:41 +0000326 x2.UpdateMessageProperty;
Jake Farrell27274222011-11-10 20:32:44 +0000327 raise x2;
328 end;
329
330 Result := TXtructImpl.Create;
331 Result.String_thing := arg1;
332end;
333
Roger Meier333bbf32012-01-08 21:51:08 +0000334function TTestServer.TTestHandlerImpl.testNest( const thing: IXtruct2): IXtruct2;
Jake Farrell27274222011-11-10 20:32:44 +0000335var
336 temp : IXtruct;
337begin
338 temp := thing.Struct_thing;
339 Console.WriteLine('testNest({' +
340 IntToStr( thing.Byte_thing) + ', {' +
341 '"' + temp.String_thing + '", ' +
342 IntToStr( temp.Byte_thing) + ', ' +
343 IntToStr( temp.I32_thing) + ', ' +
344 IntToStr( temp.I64_thing) + '}, ' +
345 IntToStr( temp.I32_thing) + '})');
346 Result := thing;
347end;
348
349procedure TTestServer.TTestHandlerImpl.testOneway(secondsToSleep: Integer);
350begin
351 Console.WriteLine('testOneway(' + IntToStr( secondsToSleep )+ '), sleeping...');
352 Sleep(secondsToSleep * 1000);
353 Console.WriteLine('testOneway finished');
354end;
355
356function TTestServer.TTestHandlerImpl.testSet(
Roger Meier333bbf32012-01-08 21:51:08 +0000357 const thing: IHashSet<Integer>):IHashSet<Integer>;
Jake Farrell27274222011-11-10 20:32:44 +0000358var
359 first : Boolean;
360 elem : Integer;
361begin
362 Console.Write('testSet({');
363 first := True;
364
365 for elem in thing do
366 begin
367 if first then
368 begin
369 first := False;
370 end else
371 begin
372 Console.Write( ', ');
373 end;
374 Console.Write( IntToStr( elem));
375 end;
376 Console.WriteLine('})');
377 Result := thing;
378end;
379
380procedure TTestServer.TTestHandlerImpl.testStop;
381begin
382 if FServer <> nil then
383 begin
384 FServer.Stop;
385 end;
386end;
387
Jens Geyer39ba6b72015-09-22 00:00:49 +0200388function TTestServer.TTestHandlerImpl.testBool(thing: Boolean): Boolean;
389begin
390 Console.WriteLine('testBool(' + BoolToStr(thing,true) + ')');
391 Result := thing;
392end;
393
Roger Meier333bbf32012-01-08 21:51:08 +0000394function TTestServer.TTestHandlerImpl.testString( const thing: string): string;
Jake Farrell27274222011-11-10 20:32:44 +0000395begin
396 Console.WriteLine('teststring("' + thing + '")');
397 Result := thing;
398end;
399
400function TTestServer.TTestHandlerImpl.testStringMap(
Roger Meier333bbf32012-01-08 21:51:08 +0000401 const thing: IThriftDictionary<string, string>): IThriftDictionary<string, string>;
Roger Meierbb6de7a2012-05-04 23:35:45 +0000402var
403 first : Boolean;
404 key : string;
Jake Farrell27274222011-11-10 20:32:44 +0000405begin
Roger Meierbb6de7a2012-05-04 23:35:45 +0000406 Console.Write('testStringMap({');
407 first := True;
408 for key in thing.Keys do
409 begin
410 if (first) then
411 begin
412 first := false;
413 end else
414 begin
415 Console.Write(', ');
416 end;
417 Console.Write(key + ' => ' + thing[key]);
418 end;
419 Console.WriteLine('})');
420 Result := thing;
Jake Farrell27274222011-11-10 20:32:44 +0000421end;
422
Roger Meier333bbf32012-01-08 21:51:08 +0000423function TTestServer.TTestHandlerImpl.testTypedef( const thing: Int64): Int64;
Jake Farrell27274222011-11-10 20:32:44 +0000424begin
425 Console.WriteLine('testTypedef(' + IntToStr( thing) + ')');
426 Result := thing;
427end;
428
429procedure TTestServer.TTestHandlerImpl.TestVoid;
430begin
431 Console.WriteLine('testVoid()');
432end;
433
Roger Meier333bbf32012-01-08 21:51:08 +0000434function TTestServer.TTestHandlerImpl.testStruct( const thing: IXtruct): IXtruct;
Jake Farrell27274222011-11-10 20:32:44 +0000435begin
436 Console.WriteLine('testStruct({' +
437 '"' + thing.String_thing + '", ' +
438 IntToStr( thing.Byte_thing) + ', ' +
439 IntToStr( thing.I32_thing) + ', ' +
440 IntToStr( thing.I64_thing));
441 Result := thing;
442end;
443
Roger Meier3bef8c22012-10-06 06:58:00 +0000444
Jake Farrell27274222011-11-10 20:32:44 +0000445{ TTestServer }
446
Roger Meier3bef8c22012-10-06 06:58:00 +0000447
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200448class procedure TTestServer.PrintCmdLineHelp;
449const HELPTEXT = ' [options]'#10
450 + #10
451 + 'Allowed options:'#10
452 + ' -h [ --help ] produce help message'#10
453 + ' --port arg (=9090) Port number to listen'#10
454 + ' --domain-socket arg Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)'#10
455 + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
456 + ' --server-type arg (=simple) type of server, "simple", "thread-pool",'#10
457 + ' "threaded", or "nonblocking"'#10
458 + ' --transport arg (=socket) transport: buffered, framed, http, anonpipe'#10
459 + ' --protocol arg (=binary) protocol: binary, compact, json'#10
460 + ' --ssl Encrypted Transport using SSL'#10
461 + ' --processor-events processor-events'#10
462 + ' -n [ --workers ] arg (=4) Number of thread pools workers. Only valid for'#10
463 + ' thread-pool server type'#10
464 ;
465begin
466 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
467end;
468
469class procedure TTestServer.InvalidArgs;
470begin
471 Console.WriteLine( 'Invalid args.');
472 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
473 Abort;
474end;
475
Jens Geyer06045cf2013-03-27 20:26:25 +0200476class procedure TTestServer.LaunchAnonPipeChild( const app : string; const transport : IAnonymousPipeServerTransport);
Roger Meier3bef8c22012-10-06 06:58:00 +0000477//Launch child process and pass R/W anonymous pipe handles on cmd line.
478//This is a simple example and does not include elevation or other
479//advanced features.
480var pi : PROCESS_INFORMATION;
Jens Geyerd5436f52014-10-03 19:50:38 +0200481 si : STARTUPINFO;
482 sArg, sHandles, sCmdLine : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000483 i : Integer;
484begin
485 GetStartupInfo( si); //set startupinfo for the spawned process
486
487 // preformat handles args
488 sHandles := Format( '%d %d',
489 [ Integer(transport.ClientAnonRead),
490 Integer(transport.ClientAnonWrite)]);
491
492 // pass all settings to client
493 sCmdLine := app;
494 for i := 1 to ParamCount do begin
495 sArg := ParamStr(i);
496
497 // add anonymous handles and quote strings where appropriate
498 if sArg = '-anon'
499 then sArg := sArg +' '+ sHandles
500 else begin
501 if Pos(' ',sArg) > 0
502 then sArg := '"'+sArg+'"';
503 end;;
504
505 sCmdLine := sCmdLine +' '+ sArg;
506 end;
507
508 // spawn the child process
509 Console.WriteLine('Starting client '+sCmdLine);
510 Win32Check( CreateProcess( nil, PChar(sCmdLine), nil,nil,TRUE,0,nil,nil,si,pi));
511
512 CloseHandle( pi.hThread);
Jens Geyerd5436f52014-10-03 19:50:38 +0200513 CloseHandle( pi.hProcess);
Roger Meier3bef8c22012-10-06 06:58:00 +0000514end;
515
516
Roger Meier333bbf32012-01-08 21:51:08 +0000517class procedure TTestServer.Execute( const args: array of string);
Jake Farrell27274222011-11-10 20:32:44 +0000518var
Jake Farrell27274222011-11-10 20:32:44 +0000519 Port : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200520 ServerEvents : Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +0000521 sPipeName : string;
Jake Farrell27274222011-11-10 20:32:44 +0000522 testHandler : ITestHandler;
523 testProcessor : IProcessor;
Roger Meier3bef8c22012-10-06 06:58:00 +0000524 ServerTrans : IServerTransport;
Jake Farrell27274222011-11-10 20:32:44 +0000525 ServerEngine : IServer;
Jens Geyer06045cf2013-03-27 20:26:25 +0200526 anonymouspipe : IAnonymousPipeServerTransport;
527 namedpipe : INamedPipeServerTransport;
Jake Farrell27274222011-11-10 20:32:44 +0000528 TransportFactory : ITransportFactory;
529 ProtocolFactory : IProtocolFactory;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200530 i, numWorker : Integer;
Jake Farrell27274222011-11-10 20:32:44 +0000531 s : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200532 protType : TKnownProtocol;
533 servertype : TServerType;
534 endpoint : TEndpointTransport;
535 layered : TLayeredTransports;
536 UseSSL : Boolean; // include where appropriate (TLayeredTransport?)
Jake Farrell27274222011-11-10 20:32:44 +0000537begin
538 try
Jens Geyer01640402013-09-25 21:12:21 +0200539 ServerEvents := FALSE;
Jake Farrell27274222011-11-10 20:32:44 +0000540 protType := prot_Binary;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200541 servertype := srv_Simple;
542 endpoint := trns_Sockets;
543 layered := [];
544 UseSSL := FALSE;
Jake Farrell27274222011-11-10 20:32:44 +0000545 Port := 9090;
Roger Meier3bef8c22012-10-06 06:58:00 +0000546 sPipeName := '';
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200547 numWorker := 4;
Jake Farrell27274222011-11-10 20:32:44 +0000548
549 i := 0;
550 while ( i < Length(args) ) do begin
551 s := args[i];
552 Inc(i);
553
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200554 // Allowed options:
555 if (s = '-h') or (s = '--help') then begin
556 // -h [ --help ] produce help message
557 PrintCmdLineHelp;
558 Exit;
Roger Meier3bef8c22012-10-06 06:58:00 +0000559 end
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200560 else if (s = '--port') then begin
561 // --port arg (=9090) Port number to listen
562 s := args[i];
563 Inc(i);
564 Port := StrToIntDef( s, Port);
Roger Meier3bef8c22012-10-06 06:58:00 +0000565 end
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200566 else if (s = '--domain-socket') then begin
567 // --domain-socket arg Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)
568 raise Exception.Create('domain-socket not supported');
Roger Meier3bef8c22012-10-06 06:58:00 +0000569 end
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200570 else if (s = '--named-pipe') then begin
571 // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
572 endpoint := trns_NamedPipes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000573 sPipeName := args[i]; // -pipe <name>
574 Inc( i );
575 end
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200576 else if (s = '--server-type') then begin
577 // --server-type arg (=simple) type of server,
578 // arg = "simple", "thread-pool", "threaded", or "nonblocking"
Jake Farrell27274222011-11-10 20:32:44 +0000579 s := args[i];
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200580 Inc(i);
581
582 if s = 'simple' then servertype := srv_Simple
583 else if s = 'thread-pool' then servertype := srv_Threadpool
584 else if s = 'threaded' then servertype := srv_Threaded
585 else if s = 'nonblocking' then servertype := srv_Nonblocking
586 else InvalidArgs;
Jens Geyer01640402013-09-25 21:12:21 +0200587 end
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200588 else if (s = '--transport') then begin
589 // --transport arg (=buffered) transport: buffered, framed, http
590 s := args[i];
591 Inc(i);
592
593 if s = 'buffered' then Include( layered, trns_Buffered)
594 else if s = 'framed' then Include( layered, trns_Framed)
595 else if s = 'http' then endpoint := trns_Http
596 else if s = 'anonpipe' then endpoint := trns_AnonPipes
597 else InvalidArgs;
598 end
599 else if (s = '--protocol') then begin
600 // --protocol arg (=binary) protocol: binary, compact, json
601 s := args[i];
602 Inc(i);
603
604 if s = 'binary' then protType := prot_Binary
605 else if s = 'compact' then protType := prot_Compact
606 else if s = 'json' then protType := prot_JSON
607 else InvalidArgs;
608 end
609 else if (s = '--ssl') then begin
610 // --ssl Encrypted Transport using SSL
611 UseSSL := TRUE;
612 end
613 else if (s = '--processor-events') then begin
614 // --processor-events processor-events
615 ServerEvents := TRUE;
616 end
617 else if (s = '-n') or (s = '--workers') then begin
618 // -n [ --workers ] arg (=4) Number of thread pools workers.
619 // Only valid for thread-pool server type
620 s := args[i];
621 numWorker := StrToIntDef(s,0);
622 if numWorker > 0
623 then Inc(i)
624 else numWorker := 4;
Jens Geyer01640402013-09-25 21:12:21 +0200625 end
626 else begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200627 InvalidArgs;
628 end;
Jake Farrell27274222011-11-10 20:32:44 +0000629 end;
630
Roger Meier3bef8c22012-10-06 06:58:00 +0000631
632 Console.WriteLine('Server configuration: ');
633
Jake Farrell27274222011-11-10 20:32:44 +0000634 // create protocol factory, default to BinaryProtocol
635 case protType of
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200636 prot_Binary : ProtocolFactory := TBinaryProtocolImpl.TFactory.Create( BINARY_STRICT_READ, BINARY_STRICT_WRITE);
637 prot_JSON : ProtocolFactory := TJSONProtocolImpl.TFactory.Create;
Jens Geyerf0e63312015-03-01 18:47:49 +0100638 prot_Compact : ProtocolFactory := TCompactProtocolImpl.TFactory.Create;
Jake Farrell27274222011-11-10 20:32:44 +0000639 else
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200640 raise Exception.Create('Unhandled protocol');
Jake Farrell27274222011-11-10 20:32:44 +0000641 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000642 ASSERT( ProtocolFactory <> nil);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200643 Console.WriteLine('- '+THRIFT_PROTOCOLS[protType]+' protocol');
Jake Farrell27274222011-11-10 20:32:44 +0000644
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200645 case endpoint of
Jake Farrell27274222011-11-10 20:32:44 +0000646
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200647 trns_Sockets : begin
648 Console.WriteLine('- sockets (port '+IntToStr(port)+')');
649 if (trns_Buffered in layered) then Console.WriteLine('- buffered');
650 servertrans := TServerSocketImpl.Create( Port, 0, (trns_Buffered in layered));
651 end;
652
653 trns_Http : begin
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200654 raise Exception.Create(ENDPOINT_TRANSPORTS[endpoint]+' server transport not implemented');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200655 end;
656
657 trns_NamedPipes : begin
658 Console.WriteLine('- named pipe ('+sPipeName+')');
Jens Geyer2ad6c302015-02-26 19:38:53 +0100659 namedpipe := TNamedPipeServerTransportImpl.Create( sPipeName, 4096, PIPE_UNLIMITED_INSTANCES);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200660 servertrans := namedpipe;
661 end;
662
663 trns_AnonPipes : begin
664 Console.WriteLine('- anonymous pipes');
665 anonymouspipe := TAnonymousPipeServerTransportImpl.Create;
666 servertrans := anonymouspipe;
667 end
668
669 else
670 raise Exception.Create('Unhandled endpoint transport');
Roger Meier3bef8c22012-10-06 06:58:00 +0000671 end;
672 ASSERT( servertrans <> nil);
673
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200674 if UseSSL then begin
675 raise Exception.Create('SSL not implemented');
676 end;
677
678 if (trns_Framed in layered) then begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000679 Console.WriteLine('- framed transport');
680 TransportFactory := TFramedTransportImpl.TFactory.Create
681 end
682 else begin
683 TransportFactory := TTransportFactoryImpl.Create;
684 end;
685 ASSERT( TransportFactory <> nil);
686
687 testHandler := TTestHandlerImpl.Create;
Jake Farrell27274222011-11-10 20:32:44 +0000688 testProcessor := TThriftTest.TProcessorImpl.Create( testHandler );
Jake Farrell27274222011-11-10 20:32:44 +0000689
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200690 case servertype of
691 srv_Simple : begin
692 ServerEngine := TSimpleServer.Create( testProcessor, ServerTrans, TransportFactory, ProtocolFactory);
693 end;
694
695 srv_Nonblocking : begin
696 raise Exception.Create(SERVER_TYPES[servertype]+' server not implemented');
697 end;
698
699 srv_Threadpool,
700 srv_Threaded: begin
701 if numWorker > 1 then {use here};
702 raise Exception.Create(SERVER_TYPES[servertype]+' server not implemented');
703 end;
704
705 else
706 raise Exception.Create('Unhandled server type');
707 end;
708 ASSERT( ServerEngine <> nil);
Jake Farrell27274222011-11-10 20:32:44 +0000709
710 testHandler.SetServer( ServerEngine);
711
Jens Geyer01640402013-09-25 21:12:21 +0200712 // test events?
713 if ServerEvents then begin
714 Console.WriteLine('- server events test enabled');
715 ServerEngine.ServerEvents := TServerEventsImpl.Create;
716 end;
717
Roger Meier3bef8c22012-10-06 06:58:00 +0000718 // start the client now when we have the anon handles, but before the server starts
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200719 if endpoint = trns_AnonPipes
Roger Meier79655fb2012-10-20 20:59:41 +0000720 then LaunchAnonPipeChild( ExtractFilePath(ParamStr(0))+'client.exe', anonymouspipe);
Jake Farrell27274222011-11-10 20:32:44 +0000721
Jens Geyer06045cf2013-03-27 20:26:25 +0200722 // install Ctrl+C handler before the server starts
723 g_Handler := testHandler;
724 SetConsoleCtrlHandler( @MyConsoleEventHandler, TRUE);
Roger Meier3bef8c22012-10-06 06:58:00 +0000725
726 Console.WriteLine('');
Jens Geyer06045cf2013-03-27 20:26:25 +0200727 repeat
728 Console.WriteLine('Starting the server ...');
729 serverEngine.Serve;
730 until {$IFDEF RunEndless} FALSE {$ELSE} TRUE {$ENDIF};
731
Jake Farrell27274222011-11-10 20:32:44 +0000732 testHandler.SetServer( nil);
Jens Geyer06045cf2013-03-27 20:26:25 +0200733 g_Handler := nil;
Jake Farrell27274222011-11-10 20:32:44 +0000734
735 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200736 on E: EAbort do raise;
737 on E: Exception do begin
738 Console.WriteLine( E.Message + #10 + E.StackTrace );
Jake Farrell27274222011-11-10 20:32:44 +0000739 end;
740 end;
741 Console.WriteLine( 'done.');
742end;
743
Jens Geyer06045cf2013-03-27 20:26:25 +0200744
Jake Farrell27274222011-11-10 20:32:44 +0000745end.