blob: adbbccf1a6846e82de5e549a0661580e8e46b143 [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,
Jake Farrell27274222011-11-10 20:32:44 +000032 Thrift.Server,
33 Thrift.Transport,
Roger Meier3bef8c22012-10-06 06:58:00 +000034 Thrift.Transport.Pipes,
Jake Farrell27274222011-11-10 20:32:44 +000035 Thrift.Protocol,
36 Thrift.Protocol.JSON,
Jens Geyerf0e63312015-03-01 18:47:49 +010037 Thrift.Protocol.Compact,
Jake Farrell27274222011-11-10 20:32:44 +000038 Thrift.Collections,
Jens Geyera019cda2019-11-09 23:24:52 +010039 Thrift.Configuration,
Jake Farrell27274222011-11-10 20:32:44 +000040 Thrift.Utils,
41 Thrift.Test,
42 Thrift,
43 TestConstants,
Jens Geyer01640402013-09-25 21:12:21 +020044 TestServerEvents,
Jens Geyer3d556242018-01-24 19:14:32 +010045 ConsoleHelper,
Jake Farrell27274222011-11-10 20:32:44 +000046 Contnrs;
47
48type
49 TTestServer = class
50 public
51 type
52
53 ITestHandler = interface( TThriftTest.Iface )
Roger Meier333bbf32012-01-08 21:51:08 +000054 procedure SetServer( const AServer : IServer );
Jens Geyer06045cf2013-03-27 20:26:25 +020055 procedure TestStop;
Jake Farrell27274222011-11-10 20:32:44 +000056 end;
57
58 TTestHandlerImpl = class( TInterfacedObject, ITestHandler )
Jens Geyeraeda9872020-03-22 15:01:28 +010059 strict private
Jake Farrell27274222011-11-10 20:32:44 +000060 FServer : IServer;
Jens Geyeraeda9872020-03-22 15:01:28 +010061 strict protected
Jake Farrell27274222011-11-10 20:32:44 +000062 procedure testVoid();
Jens Geyer39ba6b72015-09-22 00:00:49 +020063 function testBool(thing: Boolean): Boolean;
Roger Meier333bbf32012-01-08 21:51:08 +000064 function testString(const thing: string): string;
Jake Farrell7ae13e12011-10-18 14:35:26 +000065 function testByte(thing: ShortInt): ShortInt;
66 function testI32(thing: Integer): Integer;
Roger Meier333bbf32012-01-08 21:51:08 +000067 function testI64(const thing: Int64): Int64;
68 function testDouble(const thing: Double): Double;
Jens Geyerfd1b3582014-12-13 23:42:58 +010069 function testBinary(const thing: TBytes): TBytes;
Roger Meier333bbf32012-01-08 21:51:08 +000070 function testStruct(const thing: IXtruct): IXtruct;
71 function testNest(const thing: IXtruct2): IXtruct2;
72 function testMap(const thing: IThriftDictionary<Integer, Integer>): IThriftDictionary<Integer, Integer>;
73 function testStringMap(const thing: IThriftDictionary<string, string>): IThriftDictionary<string, string>;
74 function testSet(const thing: IHashSet<Integer>): IHashSet<Integer>;
75 function testList(const thing: IThriftList<Integer>): IThriftList<Integer>;
Jake Farrell7ae13e12011-10-18 14:35:26 +000076 function testEnum(thing: TNumberz): TNumberz;
Roger Meier333bbf32012-01-08 21:51:08 +000077 function testTypedef(const thing: Int64): Int64;
Jake Farrell7ae13e12011-10-18 14:35:26 +000078 function testMapMap(hello: Integer): IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
Roger Meier333bbf32012-01-08 21:51:08 +000079 function testInsanity(const argument: IInsanity): IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
80 function testMulti(arg0: ShortInt; arg1: Integer; const arg2: Int64; const arg3: IThriftDictionary<SmallInt, string>; arg4: TNumberz; const arg5: Int64): IXtruct;
81 procedure testException(const arg: string);
82 function testMultiException(const arg0: string; const arg1: string): IXtruct;
Jake Farrell7ae13e12011-10-18 14:35:26 +000083 procedure testOneway(secondsToSleep: Integer);
Jake Farrell27274222011-11-10 20:32:44 +000084
Jens Geyer06045cf2013-03-27 20:26:25 +020085 procedure TestStop;
Roger Meier333bbf32012-01-08 21:51:08 +000086 procedure SetServer( const AServer : IServer );
Jake Farrell27274222011-11-10 20:32:44 +000087 end;
88
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020089 class procedure PrintCmdLineHelp;
90 class procedure InvalidArgs;
Jens Geyeraeda9872020-03-22 15:01:28 +010091 class function IsSwitch( const aArgument, aSwitch : string; out sValue : string) : Boolean;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +020092
Jens Geyer06045cf2013-03-27 20:26:25 +020093 class procedure LaunchAnonPipeChild( const app : string; const transport : IAnonymousPipeServerTransport);
Jens Geyeraeda9872020-03-22 15:01:28 +010094 class procedure Execute( const arguments : array of string);
Jake Farrell27274222011-11-10 20:32:44 +000095 end;
96
97implementation
98
Jens Geyer06045cf2013-03-27 20:26:25 +020099
100var g_Handler : TTestServer.ITestHandler = nil;
101
102
103function MyConsoleEventHandler( dwCtrlType : DWORD) : BOOL; stdcall;
104// Note that this Handler procedure is called from another thread
105var handler : TTestServer.ITestHandler;
106begin
107 result := TRUE;
108 try
109 case dwCtrlType of
110 CTRL_C_EVENT : Console.WriteLine( 'Ctrl+C pressed');
111 CTRL_BREAK_EVENT : Console.WriteLine( 'Ctrl+Break pressed');
112 CTRL_CLOSE_EVENT : Console.WriteLine( 'Received CloseTask signal');
113 CTRL_LOGOFF_EVENT : Console.WriteLine( 'Received LogOff signal');
114 CTRL_SHUTDOWN_EVENT : Console.WriteLine( 'Received Shutdown signal');
115 else
116 Console.WriteLine( 'Received console event #'+IntToStr(Integer(dwCtrlType)));
117 end;
118
119 handler := g_Handler;
120 if handler <> nil then handler.TestStop;
121
122 except
123 // catch all
124 end;
125end;
126
127
Jake Farrell27274222011-11-10 20:32:44 +0000128{ TTestServer.TTestHandlerImpl }
129
Roger Meier333bbf32012-01-08 21:51:08 +0000130procedure TTestServer.TTestHandlerImpl.SetServer( const AServer: IServer);
Jake Farrell27274222011-11-10 20:32:44 +0000131begin
132 FServer := AServer;
133end;
134
135function TTestServer.TTestHandlerImpl.testByte(thing: ShortInt): ShortInt;
136begin
137 Console.WriteLine('testByte("' + IntToStr( thing) + '")');
138 Result := thing;
139end;
140
Roger Meier333bbf32012-01-08 21:51:08 +0000141function TTestServer.TTestHandlerImpl.testDouble( const thing: Double): Double;
Jake Farrell27274222011-11-10 20:32:44 +0000142begin
143 Console.WriteLine('testDouble("' + FloatToStr( thing ) + '")');
144 Result := thing;
145end;
146
Jens Geyerfd1b3582014-12-13 23:42:58 +0100147function TTestServer.TTestHandlerImpl.testBinary(const thing: TBytes): TBytes;
148begin
Jens Geyerbd1a2732019-06-26 22:52:44 +0200149 Console.WriteLine('testBinary('+IntToStr(Length(thing)) + ' bytes)');
Jens Geyerfd1b3582014-12-13 23:42:58 +0100150 Result := thing;
151end;
152
Jake Farrell27274222011-11-10 20:32:44 +0000153function TTestServer.TTestHandlerImpl.testEnum(thing: TNumberz): TNumberz;
154begin
Jens Geyer8f7487e2019-05-09 22:21:32 +0200155 Console.WriteLine('testEnum(' + EnumUtils<TNumberz>.ToString(Ord(thing)) + ')');
Jake Farrell27274222011-11-10 20:32:44 +0000156 Result := thing;
157end;
158
Roger Meier333bbf32012-01-08 21:51:08 +0000159procedure TTestServer.TTestHandlerImpl.testException(const arg: string);
Jake Farrell27274222011-11-10 20:32:44 +0000160begin
161 Console.WriteLine('testException(' + arg + ')');
Jens Geyerc140bb92019-11-27 22:18:12 +0100162 if ( arg = 'Xception') then begin
Roger Meierbb6de7a2012-05-04 23:35:45 +0000163 raise TXception.Create( 1001, arg);
Jake Farrell27274222011-11-10 20:32:44 +0000164 end;
Roger Meierbb6de7a2012-05-04 23:35:45 +0000165
Jens Geyerc140bb92019-11-27 22:18:12 +0100166 if (arg = 'TException') then begin
Jens Geyer92d80622018-05-02 22:28:44 +0200167 raise TException.Create('TException');
Roger Meierbb6de7a2012-05-04 23:35:45 +0000168 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
Jens Geyer8f7487e2019-05-09 22:21:32 +0200194 Console.Write('testInsanity(');
195 if argument <> nil then Console.Write(argument.ToString);
196 Console.WriteLine(')');
197
Jake Farrell27274222011-11-10 20:32:44 +0000198
Jens Geyer540e3462016-12-28 14:25:41 +0100199 (**
200 * So you think you've got this all worked, out eh?
201 *
202 * Creates a the returned map with these values and prints it out:
203 * { 1 => { 2 => argument,
204 * 3 => argument,
205 * },
206 * 2 => { 6 => <empty Insanity struct>, },
207 * }
208 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
209 *)
Jake Farrell27274222011-11-10 20:32:44 +0000210
211 first_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create;
212 second_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create;
213
Jens Geyer540e3462016-12-28 14:25:41 +0100214 first_map.AddOrSetValue( TNumberz.TWO, argument);
215 first_map.AddOrSetValue( TNumberz.THREE, argument);
Jake Farrell27274222011-11-10 20:32:44 +0000216
Jens Geyer540e3462016-12-28 14:25:41 +0100217 looney := TInsanityImpl.Create;
Jake Farrell27274222011-11-10 20:32:44 +0000218 second_map.AddOrSetValue( TNumberz.SIX, looney);
219
220 insane := TThriftDictionaryImpl<Int64, IThriftDictionary<TNumberz, IInsanity>>.Create;
221
222 insane.AddOrSetValue( 1, first_map);
223 insane.AddOrSetValue( 2, second_map);
224
225 Result := insane;
226end;
227
Jens Geyer8f7487e2019-05-09 22:21:32 +0200228function TTestServer.TTestHandlerImpl.testList( const thing: IThriftList<Integer>): IThriftList<Integer>;
Jake Farrell27274222011-11-10 20:32:44 +0000229begin
Jens Geyer8f7487e2019-05-09 22:21:32 +0200230 Console.Write('testList(');
231 if thing <> nil then Console.Write(thing.ToString);
232 Console.WriteLine(')');
Jake Farrell27274222011-11-10 20:32:44 +0000233 Result := thing;
234end;
235
236function TTestServer.TTestHandlerImpl.testMap(
Roger Meier333bbf32012-01-08 21:51:08 +0000237 const thing: IThriftDictionary<Integer, Integer>): IThriftDictionary<Integer, Integer>;
Jake Farrell27274222011-11-10 20:32:44 +0000238begin
Jens Geyer8f7487e2019-05-09 22:21:32 +0200239 Console.Write('testMap(');
240 if thing <> nil then Console.Write(thing.ToString);
241 Console.WriteLine(')');
Jake Farrell27274222011-11-10 20:32:44 +0000242 Result := thing;
243end;
244
245function TTestServer.TTestHandlerImpl.TestMapMap(
246 hello: Integer): IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
247var
248 mapmap : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
249 pos : IThriftDictionary<Integer, Integer>;
250 neg : IThriftDictionary<Integer, Integer>;
251 i : Integer;
252begin
253 Console.WriteLine('testMapMap(' + IntToStr( hello) + ')');
254 mapmap := TThriftDictionaryImpl<Integer, IThriftDictionary<Integer, Integer>>.Create;
255 pos := TThriftDictionaryImpl<Integer, Integer>.Create;
256 neg := TThriftDictionaryImpl<Integer, Integer>.Create;
257
258 for i := 1 to 4 do
259 begin
260 pos.AddOrSetValue( i, i);
261 neg.AddOrSetValue( -i, -i);
262 end;
263
264 mapmap.AddOrSetValue(4, pos);
265 mapmap.AddOrSetValue( -4, neg);
266
267 Result := mapmap;
268end;
269
270function TTestServer.TTestHandlerImpl.testMulti(arg0: ShortInt; arg1: Integer;
Roger Meier333bbf32012-01-08 21:51:08 +0000271 const arg2: Int64; const arg3: IThriftDictionary<SmallInt, string>;
272 arg4: TNumberz; const arg5: Int64): IXtruct;
Jake Farrell27274222011-11-10 20:32:44 +0000273var
274 hello : IXtruct;
275begin
276 Console.WriteLine('testMulti()');
277 hello := TXtructImpl.Create;
278 hello.String_thing := 'Hello2';
279 hello.Byte_thing := arg0;
280 hello.I32_thing := arg1;
281 hello.I64_thing := arg2;
282 Result := hello;
283end;
284
Roger Meier333bbf32012-01-08 21:51:08 +0000285function TTestServer.TTestHandlerImpl.testMultiException( const arg0, arg1: string): IXtruct;
Jake Farrell27274222011-11-10 20:32:44 +0000286var
Jake Farrell27274222011-11-10 20:32:44 +0000287 x2 : TXception2;
288begin
289 Console.WriteLine('testMultiException(' + arg0 + ', ' + arg1 + ')');
Jens Geyer8f7487e2019-05-09 22:21:32 +0200290 if ( arg0 = 'Xception') then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200291 raise TXception.Create( 1001, 'This is an Xception'); // test the new rich CTOR
Jens Geyer8f7487e2019-05-09 22:21:32 +0200292 end;
293
294 if ( arg0 = 'Xception2') then begin
Jake Farrell343c61d2011-12-09 02:29:56 +0000295 x2 := TXception2.Create; // the old way still works too?
Jake Farrell27274222011-11-10 20:32:44 +0000296 x2.ErrorCode := 2002;
297 x2.Struct_thing := TXtructImpl.Create;
298 x2.Struct_thing.String_thing := 'This is an Xception2';
Jake Farrellac102562011-11-23 14:30:41 +0000299 x2.UpdateMessageProperty;
Jake Farrell27274222011-11-10 20:32:44 +0000300 raise x2;
301 end;
302
303 Result := TXtructImpl.Create;
304 Result.String_thing := arg1;
305end;
306
Roger Meier333bbf32012-01-08 21:51:08 +0000307function TTestServer.TTestHandlerImpl.testNest( const thing: IXtruct2): IXtruct2;
Jake Farrell27274222011-11-10 20:32:44 +0000308begin
Jens Geyer8f7487e2019-05-09 22:21:32 +0200309 Console.Write('testNest(');
310 if thing <> nil then Console.Write(thing.ToString);
311 Console.WriteLine(')');
312
Jake Farrell27274222011-11-10 20:32:44 +0000313 Result := thing;
314end;
315
316procedure TTestServer.TTestHandlerImpl.testOneway(secondsToSleep: Integer);
317begin
318 Console.WriteLine('testOneway(' + IntToStr( secondsToSleep )+ '), sleeping...');
319 Sleep(secondsToSleep * 1000);
320 Console.WriteLine('testOneway finished');
321end;
322
Jens Geyer8f7487e2019-05-09 22:21:32 +0200323function TTestServer.TTestHandlerImpl.testSet( const thing: IHashSet<Integer>):IHashSet<Integer>;
Jake Farrell27274222011-11-10 20:32:44 +0000324begin
Jens Geyer8f7487e2019-05-09 22:21:32 +0200325 Console.Write('testSet(');
326 if thing <> nil then Console.Write(thing.ToString);
327 Console.WriteLine(')');;
Jake Farrell27274222011-11-10 20:32:44 +0000328
Jake Farrell27274222011-11-10 20:32:44 +0000329 Result := thing;
330end;
331
332procedure TTestServer.TTestHandlerImpl.testStop;
333begin
Jens Geyer8f7487e2019-05-09 22:21:32 +0200334 if FServer <> nil then begin
Jake Farrell27274222011-11-10 20:32:44 +0000335 FServer.Stop;
336 end;
337end;
338
Jens Geyer39ba6b72015-09-22 00:00:49 +0200339function TTestServer.TTestHandlerImpl.testBool(thing: Boolean): Boolean;
340begin
341 Console.WriteLine('testBool(' + BoolToStr(thing,true) + ')');
342 Result := thing;
343end;
344
Roger Meier333bbf32012-01-08 21:51:08 +0000345function TTestServer.TTestHandlerImpl.testString( const thing: string): string;
Jake Farrell27274222011-11-10 20:32:44 +0000346begin
347 Console.WriteLine('teststring("' + thing + '")');
348 Result := thing;
349end;
350
351function TTestServer.TTestHandlerImpl.testStringMap(
Roger Meier333bbf32012-01-08 21:51:08 +0000352 const thing: IThriftDictionary<string, string>): IThriftDictionary<string, string>;
Jake Farrell27274222011-11-10 20:32:44 +0000353begin
Jens Geyer8f7487e2019-05-09 22:21:32 +0200354 Console.Write('testStringMap(');
355 if thing <> nil then Console.Write(thing.ToString);
356 Console.WriteLine(')');
357
Roger Meierbb6de7a2012-05-04 23:35:45 +0000358 Result := thing;
Jake Farrell27274222011-11-10 20:32:44 +0000359end;
360
Roger Meier333bbf32012-01-08 21:51:08 +0000361function TTestServer.TTestHandlerImpl.testTypedef( const thing: Int64): Int64;
Jake Farrell27274222011-11-10 20:32:44 +0000362begin
363 Console.WriteLine('testTypedef(' + IntToStr( thing) + ')');
364 Result := thing;
365end;
366
367procedure TTestServer.TTestHandlerImpl.TestVoid;
368begin
369 Console.WriteLine('testVoid()');
370end;
371
Roger Meier333bbf32012-01-08 21:51:08 +0000372function TTestServer.TTestHandlerImpl.testStruct( const thing: IXtruct): IXtruct;
Jake Farrell27274222011-11-10 20:32:44 +0000373begin
Jens Geyer8f7487e2019-05-09 22:21:32 +0200374 Console.Write('testStruct(');
375 if thing <> nil then Console.Write(thing.ToString);
376 Console.WriteLine(')');
377
Jake Farrell27274222011-11-10 20:32:44 +0000378 Result := thing;
379end;
380
Roger Meier3bef8c22012-10-06 06:58:00 +0000381
Jake Farrell27274222011-11-10 20:32:44 +0000382{ TTestServer }
383
Roger Meier3bef8c22012-10-06 06:58:00 +0000384
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200385class procedure TTestServer.PrintCmdLineHelp;
386const HELPTEXT = ' [options]'#10
387 + #10
388 + 'Allowed options:'#10
Jens Geyeraeda9872020-03-22 15:01:28 +0100389 + ' -h | --help Produces this help message'#10
390 + ' --port=arg (9090) Port number to connect'#10
391 + ' --pipe=arg Windows Named Pipe (e.g. MyThriftPipe)'#10
392 + ' --anon-pipes Windows Anonymous Pipes server, auto-starts client.exe'#10
393 + ' --server-type=arg (simple) Type of server (simple, thread-pool, threaded, nonblocking)'#10
394 + ' --transport=arg (sockets) Transport: buffered, framed, anonpipe'#10
395 + ' --protocol=arg (binary) Protocol: binary, compact, json'#10
396 + ' --ssl Encrypted Transport using SSL'#10
397 + ' --processor-events Enable processor-events'#10
398 + ' -n=num | --workers=num (4) Number of thread-pool server workers'#10
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200399 ;
400begin
401 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
402end;
403
404class procedure TTestServer.InvalidArgs;
405begin
406 Console.WriteLine( 'Invalid args.');
407 Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
408 Abort;
409end;
410
Jens Geyeraeda9872020-03-22 15:01:28 +0100411class function TTestServer.IsSwitch( const aArgument, aSwitch : string; out sValue : string) : Boolean;
412begin
413 sValue := '';
414 result := (Copy( aArgument, 1, Length(aSwitch)) = aSwitch);
415 if result then begin
416 if (Copy( aArgument, 1, Length(aSwitch)+1) = (aSwitch+'='))
417 then sValue := Copy( aArgument, Length(aSwitch)+2, MAXINT);
418 end;
419end;
420
Jens Geyer06045cf2013-03-27 20:26:25 +0200421class procedure TTestServer.LaunchAnonPipeChild( const app : string; const transport : IAnonymousPipeServerTransport);
Roger Meier3bef8c22012-10-06 06:58:00 +0000422//Launch child process and pass R/W anonymous pipe handles on cmd line.
423//This is a simple example and does not include elevation or other
424//advanced features.
425var pi : PROCESS_INFORMATION;
Jens Geyerd5436f52014-10-03 19:50:38 +0200426 si : STARTUPINFO;
427 sArg, sHandles, sCmdLine : string;
Roger Meier3bef8c22012-10-06 06:58:00 +0000428 i : Integer;
429begin
430 GetStartupInfo( si); //set startupinfo for the spawned process
431
432 // preformat handles args
433 sHandles := Format( '%d %d',
434 [ Integer(transport.ClientAnonRead),
435 Integer(transport.ClientAnonWrite)]);
436
437 // pass all settings to client
438 sCmdLine := app;
439 for i := 1 to ParamCount do begin
440 sArg := ParamStr(i);
441
442 // add anonymous handles and quote strings where appropriate
Jens Geyeraeda9872020-03-22 15:01:28 +0100443 if sArg = '--anon-pipes'
Roger Meier3bef8c22012-10-06 06:58:00 +0000444 then sArg := sArg +' '+ sHandles
445 else begin
446 if Pos(' ',sArg) > 0
447 then sArg := '"'+sArg+'"';
448 end;;
449
450 sCmdLine := sCmdLine +' '+ sArg;
451 end;
452
453 // spawn the child process
454 Console.WriteLine('Starting client '+sCmdLine);
455 Win32Check( CreateProcess( nil, PChar(sCmdLine), nil,nil,TRUE,0,nil,nil,si,pi));
456
457 CloseHandle( pi.hThread);
Jens Geyeraeda9872020-03-22 15:01:28 +0100458 CloseHandle( pi.hProcess);
Roger Meier3bef8c22012-10-06 06:58:00 +0000459end;
460
461
Jens Geyeraeda9872020-03-22 15:01:28 +0100462class procedure TTestServer.Execute( const arguments : array of string);
Jake Farrell27274222011-11-10 20:32:44 +0000463var
Jake Farrell27274222011-11-10 20:32:44 +0000464 Port : Integer;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200465 ServerEvents : Boolean;
Roger Meier3bef8c22012-10-06 06:58:00 +0000466 sPipeName : string;
Jake Farrell27274222011-11-10 20:32:44 +0000467 testHandler : ITestHandler;
468 testProcessor : IProcessor;
Roger Meier3bef8c22012-10-06 06:58:00 +0000469 ServerTrans : IServerTransport;
Jake Farrell27274222011-11-10 20:32:44 +0000470 ServerEngine : IServer;
Jens Geyer06045cf2013-03-27 20:26:25 +0200471 anonymouspipe : IAnonymousPipeServerTransport;
472 namedpipe : INamedPipeServerTransport;
Jake Farrell27274222011-11-10 20:32:44 +0000473 TransportFactory : ITransportFactory;
474 ProtocolFactory : IProtocolFactory;
Jens Geyeraeda9872020-03-22 15:01:28 +0100475 iArg, numWorker : Integer;
476 sArg, sValue : string;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200477 protType : TKnownProtocol;
478 servertype : TServerType;
479 endpoint : TEndpointTransport;
480 layered : TLayeredTransports;
481 UseSSL : Boolean; // include where appropriate (TLayeredTransport?)
Jens Geyer20a86d62021-04-02 11:34:08 +0200482 config : IThriftConfiguration;
483const
484 PIPEFLAGS = [ TNamedPipeFlag.OnlyLocalClients];
Jake Farrell27274222011-11-10 20:32:44 +0000485begin
486 try
Jens Geyer01640402013-09-25 21:12:21 +0200487 ServerEvents := FALSE;
Jake Farrell27274222011-11-10 20:32:44 +0000488 protType := prot_Binary;
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200489 servertype := srv_Simple;
490 endpoint := trns_Sockets;
491 layered := [];
492 UseSSL := FALSE;
Jake Farrell27274222011-11-10 20:32:44 +0000493 Port := 9090;
Roger Meier3bef8c22012-10-06 06:58:00 +0000494 sPipeName := '';
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200495 numWorker := 4;
Jake Farrell27274222011-11-10 20:32:44 +0000496
Jens Geyeraeda9872020-03-22 15:01:28 +0100497 iArg := 0;
498 while iArg < Length(arguments) do begin
499 sArg := arguments[iArg];
500 Inc(iArg);
Jake Farrell27274222011-11-10 20:32:44 +0000501
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200502 // Allowed options:
Jens Geyeraeda9872020-03-22 15:01:28 +0100503 if IsSwitch( sArg, '-h', sValue)
504 or IsSwitch( sArg, '--help', sValue)
505 then begin
506 // -h | --help produce help message
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200507 PrintCmdLineHelp;
508 Exit;
Roger Meier3bef8c22012-10-06 06:58:00 +0000509 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100510 else if IsSwitch( sArg, '--port', sValue) then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200511 // --port arg (=9090) Port number to listen
Jens Geyeraeda9872020-03-22 15:01:28 +0100512 Port := StrToIntDef( sValue, Port);
Roger Meier3bef8c22012-10-06 06:58:00 +0000513 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100514 else if IsSwitch( sArg, '--anon-pipes', sValue) then begin
515 endpoint := trns_AnonPipes;
Roger Meier3bef8c22012-10-06 06:58:00 +0000516 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100517 else if IsSwitch( sArg, '--pipe', sValue) then begin
Jens Geyer4a33b182020-03-22 13:46:34 +0100518 // --pipe arg Windows Named Pipe (e.g. MyThriftPipe)
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200519 endpoint := trns_NamedPipes;
Jens Geyeraeda9872020-03-22 15:01:28 +0100520 sPipeName := sValue; // --pipe <name>
Roger Meier3bef8c22012-10-06 06:58:00 +0000521 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100522 else if IsSwitch( sArg, '--server-type', sValue) then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200523 // --server-type arg (=simple) type of server,
524 // arg = "simple", "thread-pool", "threaded", or "nonblocking"
Jens Geyeraeda9872020-03-22 15:01:28 +0100525 if sValue = 'simple' then servertype := srv_Simple
526 else if sValue = 'thread-pool' then servertype := srv_Threadpool
527 else if sValue = 'threaded' then servertype := srv_Threaded
528 else if sValue = 'nonblocking' then servertype := srv_Nonblocking
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200529 else InvalidArgs;
Jens Geyer01640402013-09-25 21:12:21 +0200530 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100531 else if IsSwitch( sArg, '--transport', sValue) then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200532 // --transport arg (=buffered) transport: buffered, framed, http
Jens Geyeraeda9872020-03-22 15:01:28 +0100533 if sValue = 'buffered' then Include( layered, trns_Buffered)
534 else if sValue = 'framed' then Include( layered, trns_Framed)
535 else if sValue = 'http' then endpoint := trns_MsxmlHttp
536 else if sValue = 'winhttp' then endpoint := trns_WinHttp
537 else if sValue = 'anonpipe' then endpoint := trns_AnonPipes
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200538 else InvalidArgs;
539 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100540 else if IsSwitch( sArg, '--protocol', sValue) then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200541 // --protocol arg (=binary) protocol: binary, compact, json
Jens Geyeraeda9872020-03-22 15:01:28 +0100542 if sValue = 'binary' then protType := prot_Binary
543 else if sValue = 'compact' then protType := prot_Compact
544 else if sValue = 'json' then protType := prot_JSON
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200545 else InvalidArgs;
546 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100547 else if IsSwitch( sArg, '--ssl', sValue) then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200548 // --ssl Encrypted Transport using SSL
549 UseSSL := TRUE;
550 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100551 else if IsSwitch( sArg, '--processor-events', sValue) then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200552 // --processor-events processor-events
553 ServerEvents := TRUE;
554 end
Jens Geyeraeda9872020-03-22 15:01:28 +0100555 else if IsSwitch( sArg, '-n', sValue) or IsSwitch( sArg, '--workers', sValue) then begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200556 // -n [ --workers ] arg (=4) Number of thread pools workers.
557 // Only valid for thread-pool server type
Jens Geyeraeda9872020-03-22 15:01:28 +0100558 numWorker := StrToIntDef(sValue,4);
Jens Geyer01640402013-09-25 21:12:21 +0200559 end
560 else begin
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200561 InvalidArgs;
562 end;
Jake Farrell27274222011-11-10 20:32:44 +0000563 end;
564
Roger Meier3bef8c22012-10-06 06:58:00 +0000565
566 Console.WriteLine('Server configuration: ');
567
Jake Farrell27274222011-11-10 20:32:44 +0000568 // create protocol factory, default to BinaryProtocol
569 case protType of
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200570 prot_Binary : ProtocolFactory := TBinaryProtocolImpl.TFactory.Create( BINARY_STRICT_READ, BINARY_STRICT_WRITE);
571 prot_JSON : ProtocolFactory := TJSONProtocolImpl.TFactory.Create;
Jens Geyerf0e63312015-03-01 18:47:49 +0100572 prot_Compact : ProtocolFactory := TCompactProtocolImpl.TFactory.Create;
Jake Farrell27274222011-11-10 20:32:44 +0000573 else
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200574 raise Exception.Create('Unhandled protocol');
Jake Farrell27274222011-11-10 20:32:44 +0000575 end;
Roger Meier3bef8c22012-10-06 06:58:00 +0000576 ASSERT( ProtocolFactory <> nil);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200577 Console.WriteLine('- '+THRIFT_PROTOCOLS[protType]+' protocol');
Jake Farrell27274222011-11-10 20:32:44 +0000578
Jens Geyer20a86d62021-04-02 11:34:08 +0200579 config := nil; // TODO
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200580 case endpoint of
Jake Farrell27274222011-11-10 20:32:44 +0000581
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200582 trns_Sockets : begin
583 Console.WriteLine('- sockets (port '+IntToStr(port)+')');
584 if (trns_Buffered in layered) then Console.WriteLine('- buffered');
Jens Geyer41f47af2019-11-09 23:24:52 +0100585 servertrans := TServerSocketImpl.Create( Port, DEFAULT_THRIFT_TIMEOUT, (trns_Buffered in layered));
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200586 end;
587
Jens Geyer02230912019-04-03 01:12:51 +0200588 trns_MsxmlHttp,
589 trns_WinHttp : begin
590 raise Exception.Create('HTTP server transport not implemented');
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200591 end;
592
593 trns_NamedPipes : begin
594 Console.WriteLine('- named pipe ('+sPipeName+')');
Jens Geyer20a86d62021-04-02 11:34:08 +0200595 namedpipe := TNamedPipeServerTransportImpl.Create( sPipeName, PIPEFLAGS, config);
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200596 servertrans := namedpipe;
597 end;
598
599 trns_AnonPipes : begin
600 Console.WriteLine('- anonymous pipes');
601 anonymouspipe := TAnonymousPipeServerTransportImpl.Create;
602 servertrans := anonymouspipe;
603 end
604
605 else
606 raise Exception.Create('Unhandled endpoint transport');
Roger Meier3bef8c22012-10-06 06:58:00 +0000607 end;
608 ASSERT( servertrans <> nil);
609
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200610 if UseSSL then begin
611 raise Exception.Create('SSL not implemented');
612 end;
613
614 if (trns_Framed in layered) then begin
Roger Meier3bef8c22012-10-06 06:58:00 +0000615 Console.WriteLine('- framed transport');
Jens Geyera019cda2019-11-09 23:24:52 +0100616 TransportFactory := TFramedTransportImpl.TFactory.Create;
Roger Meier3bef8c22012-10-06 06:58:00 +0000617 end
618 else begin
619 TransportFactory := TTransportFactoryImpl.Create;
620 end;
621 ASSERT( TransportFactory <> nil);
622
623 testHandler := TTestHandlerImpl.Create;
Jake Farrell27274222011-11-10 20:32:44 +0000624 testProcessor := TThriftTest.TProcessorImpl.Create( testHandler );
Jake Farrell27274222011-11-10 20:32:44 +0000625
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200626 case servertype of
627 srv_Simple : begin
628 ServerEngine := TSimpleServer.Create( testProcessor, ServerTrans, TransportFactory, ProtocolFactory);
629 end;
630
631 srv_Nonblocking : begin
632 raise Exception.Create(SERVER_TYPES[servertype]+' server not implemented');
633 end;
634
635 srv_Threadpool,
636 srv_Threaded: begin
637 if numWorker > 1 then {use here};
638 raise Exception.Create(SERVER_TYPES[servertype]+' server not implemented');
639 end;
640
641 else
642 raise Exception.Create('Unhandled server type');
643 end;
644 ASSERT( ServerEngine <> nil);
Jake Farrell27274222011-11-10 20:32:44 +0000645
646 testHandler.SetServer( ServerEngine);
647
Jens Geyer01640402013-09-25 21:12:21 +0200648 // test events?
649 if ServerEvents then begin
650 Console.WriteLine('- server events test enabled');
651 ServerEngine.ServerEvents := TServerEventsImpl.Create;
652 end;
653
Roger Meier3bef8c22012-10-06 06:58:00 +0000654 // start the client now when we have the anon handles, but before the server starts
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200655 if endpoint = trns_AnonPipes
Roger Meier79655fb2012-10-20 20:59:41 +0000656 then LaunchAnonPipeChild( ExtractFilePath(ParamStr(0))+'client.exe', anonymouspipe);
Jake Farrell27274222011-11-10 20:32:44 +0000657
Jens Geyer06045cf2013-03-27 20:26:25 +0200658 // install Ctrl+C handler before the server starts
659 g_Handler := testHandler;
660 SetConsoleCtrlHandler( @MyConsoleEventHandler, TRUE);
Roger Meier3bef8c22012-10-06 06:58:00 +0000661
662 Console.WriteLine('');
Jens Geyer06045cf2013-03-27 20:26:25 +0200663 repeat
664 Console.WriteLine('Starting the server ...');
665 serverEngine.Serve;
666 until {$IFDEF RunEndless} FALSE {$ELSE} TRUE {$ENDIF};
667
Jake Farrell27274222011-11-10 20:32:44 +0000668 testHandler.SetServer( nil);
Jens Geyer06045cf2013-03-27 20:26:25 +0200669 g_Handler := nil;
Jake Farrell27274222011-11-10 20:32:44 +0000670
671 except
Jens Geyerf8a1b7a2014-09-24 00:26:46 +0200672 on E: EAbort do raise;
673 on E: Exception do begin
674 Console.WriteLine( E.Message + #10 + E.StackTrace );
Jake Farrell27274222011-11-10 20:32:44 +0000675 end;
676 end;
677 Console.WriteLine( 'done.');
678end;
679
Jens Geyer06045cf2013-03-27 20:26:25 +0200680
Jake Farrell27274222011-11-10 20:32:44 +0000681end.