Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [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 | unit TestServer; |
| 21 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 22 | {$I ../src/Thrift.Defines.inc} |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 23 | {$WARN SYMBOL_PLATFORM OFF} |
| 24 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 25 | {.$DEFINE RunEndless} // activate to interactively stress-test the server stop routines via Ctrl+C |
| 26 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 27 | interface |
| 28 | |
| 29 | uses |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 30 | Windows, SysUtils, |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 31 | Generics.Collections, |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 32 | Thrift.Server, |
| 33 | Thrift.Transport, |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 34 | Thrift.Transport.Pipes, |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 35 | Thrift.Protocol, |
| 36 | Thrift.Protocol.JSON, |
Jens Geyer | f0e6331 | 2015-03-01 18:47:49 +0100 | [diff] [blame] | 37 | Thrift.Protocol.Compact, |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 38 | Thrift.Collections, |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 39 | Thrift.Configuration, |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 40 | Thrift.Utils, |
| 41 | Thrift.Test, |
| 42 | Thrift, |
| 43 | TestConstants, |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 44 | TestServerEvents, |
Jens Geyer | 3d55624 | 2018-01-24 19:14:32 +0100 | [diff] [blame] | 45 | ConsoleHelper, |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 46 | Contnrs; |
| 47 | |
| 48 | type |
| 49 | TTestServer = class |
| 50 | public |
| 51 | type |
| 52 | |
| 53 | ITestHandler = interface( TThriftTest.Iface ) |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 54 | procedure SetServer( const AServer : IServer ); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 55 | procedure TestStop; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 56 | end; |
| 57 | |
| 58 | TTestHandlerImpl = class( TInterfacedObject, ITestHandler ) |
| 59 | private |
| 60 | FServer : IServer; |
| 61 | protected |
| 62 | procedure testVoid(); |
Jens Geyer | 39ba6b7 | 2015-09-22 00:00:49 +0200 | [diff] [blame] | 63 | function testBool(thing: Boolean): Boolean; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 64 | function testString(const thing: string): string; |
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 65 | function testByte(thing: ShortInt): ShortInt; |
| 66 | function testI32(thing: Integer): Integer; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 67 | function testI64(const thing: Int64): Int64; |
| 68 | function testDouble(const thing: Double): Double; |
Jens Geyer | fd1b358 | 2014-12-13 23:42:58 +0100 | [diff] [blame] | 69 | function testBinary(const thing: TBytes): TBytes; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 70 | 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 Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 76 | function testEnum(thing: TNumberz): TNumberz; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 77 | function testTypedef(const thing: Int64): Int64; |
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 78 | function testMapMap(hello: Integer): IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 79 | 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 Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 83 | procedure testOneway(secondsToSleep: Integer); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 84 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 85 | procedure TestStop; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 86 | procedure SetServer( const AServer : IServer ); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 87 | end; |
| 88 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 89 | class procedure PrintCmdLineHelp; |
| 90 | class procedure InvalidArgs; |
| 91 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 92 | class procedure LaunchAnonPipeChild( const app : string; const transport : IAnonymousPipeServerTransport); |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 93 | class procedure Execute( const args: array of string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 94 | end; |
| 95 | |
| 96 | implementation |
| 97 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 98 | |
| 99 | var g_Handler : TTestServer.ITestHandler = nil; |
| 100 | |
| 101 | |
| 102 | function MyConsoleEventHandler( dwCtrlType : DWORD) : BOOL; stdcall; |
| 103 | // Note that this Handler procedure is called from another thread |
| 104 | var handler : TTestServer.ITestHandler; |
| 105 | begin |
| 106 | result := TRUE; |
| 107 | try |
| 108 | case dwCtrlType of |
| 109 | CTRL_C_EVENT : Console.WriteLine( 'Ctrl+C pressed'); |
| 110 | CTRL_BREAK_EVENT : Console.WriteLine( 'Ctrl+Break pressed'); |
| 111 | CTRL_CLOSE_EVENT : Console.WriteLine( 'Received CloseTask signal'); |
| 112 | CTRL_LOGOFF_EVENT : Console.WriteLine( 'Received LogOff signal'); |
| 113 | CTRL_SHUTDOWN_EVENT : Console.WriteLine( 'Received Shutdown signal'); |
| 114 | else |
| 115 | Console.WriteLine( 'Received console event #'+IntToStr(Integer(dwCtrlType))); |
| 116 | end; |
| 117 | |
| 118 | handler := g_Handler; |
| 119 | if handler <> nil then handler.TestStop; |
| 120 | |
| 121 | except |
| 122 | // catch all |
| 123 | end; |
| 124 | end; |
| 125 | |
| 126 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 127 | { TTestServer.TTestHandlerImpl } |
| 128 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 129 | procedure TTestServer.TTestHandlerImpl.SetServer( const AServer: IServer); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 130 | begin |
| 131 | FServer := AServer; |
| 132 | end; |
| 133 | |
| 134 | function TTestServer.TTestHandlerImpl.testByte(thing: ShortInt): ShortInt; |
| 135 | begin |
| 136 | Console.WriteLine('testByte("' + IntToStr( thing) + '")'); |
| 137 | Result := thing; |
| 138 | end; |
| 139 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 140 | function TTestServer.TTestHandlerImpl.testDouble( const thing: Double): Double; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 141 | begin |
| 142 | Console.WriteLine('testDouble("' + FloatToStr( thing ) + '")'); |
| 143 | Result := thing; |
| 144 | end; |
| 145 | |
Jens Geyer | fd1b358 | 2014-12-13 23:42:58 +0100 | [diff] [blame] | 146 | function TTestServer.TTestHandlerImpl.testBinary(const thing: TBytes): TBytes; |
| 147 | begin |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 148 | Console.WriteLine('testBinary('+IntToStr(Length(thing)) + ' bytes)'); |
Jens Geyer | fd1b358 | 2014-12-13 23:42:58 +0100 | [diff] [blame] | 149 | Result := thing; |
| 150 | end; |
| 151 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 152 | function TTestServer.TTestHandlerImpl.testEnum(thing: TNumberz): TNumberz; |
| 153 | begin |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 154 | Console.WriteLine('testEnum(' + EnumUtils<TNumberz>.ToString(Ord(thing)) + ')'); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 155 | Result := thing; |
| 156 | end; |
| 157 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 158 | procedure TTestServer.TTestHandlerImpl.testException(const arg: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 159 | begin |
| 160 | Console.WriteLine('testException(' + arg + ')'); |
Jens Geyer | c140bb9 | 2019-11-27 22:18:12 +0100 | [diff] [blame] | 161 | if ( arg = 'Xception') then begin |
Roger Meier | bb6de7a | 2012-05-04 23:35:45 +0000 | [diff] [blame] | 162 | raise TXception.Create( 1001, arg); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 163 | end; |
Roger Meier | bb6de7a | 2012-05-04 23:35:45 +0000 | [diff] [blame] | 164 | |
Jens Geyer | c140bb9 | 2019-11-27 22:18:12 +0100 | [diff] [blame] | 165 | if (arg = 'TException') then begin |
Jens Geyer | 92d8062 | 2018-05-02 22:28:44 +0200 | [diff] [blame] | 166 | raise TException.Create('TException'); |
Roger Meier | bb6de7a | 2012-05-04 23:35:45 +0000 | [diff] [blame] | 167 | end; |
| 168 | |
| 169 | // else do not throw anything |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 170 | end; |
| 171 | |
| 172 | function TTestServer.TTestHandlerImpl.testI32(thing: Integer): Integer; |
| 173 | begin |
| 174 | Console.WriteLine('testI32("' + IntToStr( thing) + '")'); |
| 175 | Result := thing; |
| 176 | end; |
| 177 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 178 | function TTestServer.TTestHandlerImpl.testI64( const thing: Int64): Int64; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 179 | begin |
| 180 | Console.WriteLine('testI64("' + IntToStr( thing) + '")'); |
| 181 | Result := thing; |
| 182 | end; |
| 183 | |
| 184 | function TTestServer.TTestHandlerImpl.testInsanity( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 185 | const argument: IInsanity): IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 186 | var |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 187 | looney : IInsanity; |
| 188 | first_map : IThriftDictionary<TNumberz, IInsanity>; |
| 189 | second_map : IThriftDictionary<TNumberz, IInsanity>; |
| 190 | insane : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>; |
| 191 | |
| 192 | begin |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 193 | Console.Write('testInsanity('); |
| 194 | if argument <> nil then Console.Write(argument.ToString); |
| 195 | Console.WriteLine(')'); |
| 196 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 197 | |
Jens Geyer | 540e346 | 2016-12-28 14:25:41 +0100 | [diff] [blame] | 198 | (** |
| 199 | * So you think you've got this all worked, out eh? |
| 200 | * |
| 201 | * Creates a the returned map with these values and prints it out: |
| 202 | * { 1 => { 2 => argument, |
| 203 | * 3 => argument, |
| 204 | * }, |
| 205 | * 2 => { 6 => <empty Insanity struct>, }, |
| 206 | * } |
| 207 | * @return map<UserId, map<Numberz,Insanity>> - a map with the above values |
| 208 | *) |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 209 | |
| 210 | first_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create; |
| 211 | second_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create; |
| 212 | |
Jens Geyer | 540e346 | 2016-12-28 14:25:41 +0100 | [diff] [blame] | 213 | first_map.AddOrSetValue( TNumberz.TWO, argument); |
| 214 | first_map.AddOrSetValue( TNumberz.THREE, argument); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 215 | |
Jens Geyer | 540e346 | 2016-12-28 14:25:41 +0100 | [diff] [blame] | 216 | looney := TInsanityImpl.Create; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 217 | second_map.AddOrSetValue( TNumberz.SIX, looney); |
| 218 | |
| 219 | insane := TThriftDictionaryImpl<Int64, IThriftDictionary<TNumberz, IInsanity>>.Create; |
| 220 | |
| 221 | insane.AddOrSetValue( 1, first_map); |
| 222 | insane.AddOrSetValue( 2, second_map); |
| 223 | |
| 224 | Result := insane; |
| 225 | end; |
| 226 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 227 | function TTestServer.TTestHandlerImpl.testList( const thing: IThriftList<Integer>): IThriftList<Integer>; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 228 | begin |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 229 | Console.Write('testList('); |
| 230 | if thing <> nil then Console.Write(thing.ToString); |
| 231 | Console.WriteLine(')'); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 232 | Result := thing; |
| 233 | end; |
| 234 | |
| 235 | function TTestServer.TTestHandlerImpl.testMap( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 236 | const thing: IThriftDictionary<Integer, Integer>): IThriftDictionary<Integer, Integer>; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 237 | begin |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 238 | Console.Write('testMap('); |
| 239 | if thing <> nil then Console.Write(thing.ToString); |
| 240 | Console.WriteLine(')'); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 241 | Result := thing; |
| 242 | end; |
| 243 | |
| 244 | function TTestServer.TTestHandlerImpl.TestMapMap( |
| 245 | hello: Integer): IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>; |
| 246 | var |
| 247 | mapmap : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>; |
| 248 | pos : IThriftDictionary<Integer, Integer>; |
| 249 | neg : IThriftDictionary<Integer, Integer>; |
| 250 | i : Integer; |
| 251 | begin |
| 252 | Console.WriteLine('testMapMap(' + IntToStr( hello) + ')'); |
| 253 | mapmap := TThriftDictionaryImpl<Integer, IThriftDictionary<Integer, Integer>>.Create; |
| 254 | pos := TThriftDictionaryImpl<Integer, Integer>.Create; |
| 255 | neg := TThriftDictionaryImpl<Integer, Integer>.Create; |
| 256 | |
| 257 | for i := 1 to 4 do |
| 258 | begin |
| 259 | pos.AddOrSetValue( i, i); |
| 260 | neg.AddOrSetValue( -i, -i); |
| 261 | end; |
| 262 | |
| 263 | mapmap.AddOrSetValue(4, pos); |
| 264 | mapmap.AddOrSetValue( -4, neg); |
| 265 | |
| 266 | Result := mapmap; |
| 267 | end; |
| 268 | |
| 269 | function TTestServer.TTestHandlerImpl.testMulti(arg0: ShortInt; arg1: Integer; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 270 | const arg2: Int64; const arg3: IThriftDictionary<SmallInt, string>; |
| 271 | arg4: TNumberz; const arg5: Int64): IXtruct; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 272 | var |
| 273 | hello : IXtruct; |
| 274 | begin |
| 275 | Console.WriteLine('testMulti()'); |
| 276 | hello := TXtructImpl.Create; |
| 277 | hello.String_thing := 'Hello2'; |
| 278 | hello.Byte_thing := arg0; |
| 279 | hello.I32_thing := arg1; |
| 280 | hello.I64_thing := arg2; |
| 281 | Result := hello; |
| 282 | end; |
| 283 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 284 | function TTestServer.TTestHandlerImpl.testMultiException( const arg0, arg1: string): IXtruct; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 285 | var |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 286 | x2 : TXception2; |
| 287 | begin |
| 288 | Console.WriteLine('testMultiException(' + arg0 + ', ' + arg1 + ')'); |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 289 | if ( arg0 = 'Xception') then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 290 | raise TXception.Create( 1001, 'This is an Xception'); // test the new rich CTOR |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 291 | end; |
| 292 | |
| 293 | if ( arg0 = 'Xception2') then begin |
Jake Farrell | 343c61d | 2011-12-09 02:29:56 +0000 | [diff] [blame] | 294 | x2 := TXception2.Create; // the old way still works too? |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 295 | x2.ErrorCode := 2002; |
| 296 | x2.Struct_thing := TXtructImpl.Create; |
| 297 | x2.Struct_thing.String_thing := 'This is an Xception2'; |
Jake Farrell | ac10256 | 2011-11-23 14:30:41 +0000 | [diff] [blame] | 298 | x2.UpdateMessageProperty; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 299 | raise x2; |
| 300 | end; |
| 301 | |
| 302 | Result := TXtructImpl.Create; |
| 303 | Result.String_thing := arg1; |
| 304 | end; |
| 305 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 306 | function TTestServer.TTestHandlerImpl.testNest( const thing: IXtruct2): IXtruct2; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 307 | begin |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 308 | Console.Write('testNest('); |
| 309 | if thing <> nil then Console.Write(thing.ToString); |
| 310 | Console.WriteLine(')'); |
| 311 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 312 | Result := thing; |
| 313 | end; |
| 314 | |
| 315 | procedure TTestServer.TTestHandlerImpl.testOneway(secondsToSleep: Integer); |
| 316 | begin |
| 317 | Console.WriteLine('testOneway(' + IntToStr( secondsToSleep )+ '), sleeping...'); |
| 318 | Sleep(secondsToSleep * 1000); |
| 319 | Console.WriteLine('testOneway finished'); |
| 320 | end; |
| 321 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 322 | function TTestServer.TTestHandlerImpl.testSet( const thing: IHashSet<Integer>):IHashSet<Integer>; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 323 | begin |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 324 | Console.Write('testSet('); |
| 325 | if thing <> nil then Console.Write(thing.ToString); |
| 326 | Console.WriteLine(')');; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 327 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 328 | Result := thing; |
| 329 | end; |
| 330 | |
| 331 | procedure TTestServer.TTestHandlerImpl.testStop; |
| 332 | begin |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 333 | if FServer <> nil then begin |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 334 | FServer.Stop; |
| 335 | end; |
| 336 | end; |
| 337 | |
Jens Geyer | 39ba6b7 | 2015-09-22 00:00:49 +0200 | [diff] [blame] | 338 | function TTestServer.TTestHandlerImpl.testBool(thing: Boolean): Boolean; |
| 339 | begin |
| 340 | Console.WriteLine('testBool(' + BoolToStr(thing,true) + ')'); |
| 341 | Result := thing; |
| 342 | end; |
| 343 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 344 | function TTestServer.TTestHandlerImpl.testString( const thing: string): string; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 345 | begin |
| 346 | Console.WriteLine('teststring("' + thing + '")'); |
| 347 | Result := thing; |
| 348 | end; |
| 349 | |
| 350 | function TTestServer.TTestHandlerImpl.testStringMap( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 351 | const thing: IThriftDictionary<string, string>): IThriftDictionary<string, string>; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 352 | begin |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 353 | Console.Write('testStringMap('); |
| 354 | if thing <> nil then Console.Write(thing.ToString); |
| 355 | Console.WriteLine(')'); |
| 356 | |
Roger Meier | bb6de7a | 2012-05-04 23:35:45 +0000 | [diff] [blame] | 357 | Result := thing; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 358 | end; |
| 359 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 360 | function TTestServer.TTestHandlerImpl.testTypedef( const thing: Int64): Int64; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 361 | begin |
| 362 | Console.WriteLine('testTypedef(' + IntToStr( thing) + ')'); |
| 363 | Result := thing; |
| 364 | end; |
| 365 | |
| 366 | procedure TTestServer.TTestHandlerImpl.TestVoid; |
| 367 | begin |
| 368 | Console.WriteLine('testVoid()'); |
| 369 | end; |
| 370 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 371 | function TTestServer.TTestHandlerImpl.testStruct( const thing: IXtruct): IXtruct; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 372 | begin |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 373 | Console.Write('testStruct('); |
| 374 | if thing <> nil then Console.Write(thing.ToString); |
| 375 | Console.WriteLine(')'); |
| 376 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 377 | Result := thing; |
| 378 | end; |
| 379 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 380 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 381 | { TTestServer } |
| 382 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 383 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 384 | class procedure TTestServer.PrintCmdLineHelp; |
| 385 | const HELPTEXT = ' [options]'#10 |
| 386 | + #10 |
| 387 | + 'Allowed options:'#10 |
| 388 | + ' -h [ --help ] produce help message'#10 |
| 389 | + ' --port arg (=9090) Port number to listen'#10 |
| 390 | + ' --domain-socket arg Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)'#10 |
| 391 | + ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10 |
| 392 | + ' --server-type arg (=simple) type of server, "simple", "thread-pool",'#10 |
| 393 | + ' "threaded", or "nonblocking"'#10 |
| 394 | + ' --transport arg (=socket) transport: buffered, framed, http, anonpipe'#10 |
| 395 | + ' --protocol arg (=binary) protocol: binary, compact, json'#10 |
| 396 | + ' --ssl Encrypted Transport using SSL'#10 |
| 397 | + ' --processor-events processor-events'#10 |
| 398 | + ' -n [ --workers ] arg (=4) Number of thread pools workers. Only valid for'#10 |
| 399 | + ' thread-pool server type'#10 |
| 400 | ; |
| 401 | begin |
| 402 | Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT); |
| 403 | end; |
| 404 | |
| 405 | class procedure TTestServer.InvalidArgs; |
| 406 | begin |
| 407 | Console.WriteLine( 'Invalid args.'); |
| 408 | Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information'); |
| 409 | Abort; |
| 410 | end; |
| 411 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 412 | class procedure TTestServer.LaunchAnonPipeChild( const app : string; const transport : IAnonymousPipeServerTransport); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 413 | //Launch child process and pass R/W anonymous pipe handles on cmd line. |
| 414 | //This is a simple example and does not include elevation or other |
| 415 | //advanced features. |
| 416 | var pi : PROCESS_INFORMATION; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 417 | si : STARTUPINFO; |
| 418 | sArg, sHandles, sCmdLine : string; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 419 | i : Integer; |
| 420 | begin |
| 421 | GetStartupInfo( si); //set startupinfo for the spawned process |
| 422 | |
| 423 | // preformat handles args |
| 424 | sHandles := Format( '%d %d', |
| 425 | [ Integer(transport.ClientAnonRead), |
| 426 | Integer(transport.ClientAnonWrite)]); |
| 427 | |
| 428 | // pass all settings to client |
| 429 | sCmdLine := app; |
| 430 | for i := 1 to ParamCount do begin |
| 431 | sArg := ParamStr(i); |
| 432 | |
| 433 | // add anonymous handles and quote strings where appropriate |
| 434 | if sArg = '-anon' |
| 435 | then sArg := sArg +' '+ sHandles |
| 436 | else begin |
| 437 | if Pos(' ',sArg) > 0 |
| 438 | then sArg := '"'+sArg+'"'; |
| 439 | end;; |
| 440 | |
| 441 | sCmdLine := sCmdLine +' '+ sArg; |
| 442 | end; |
| 443 | |
| 444 | // spawn the child process |
| 445 | Console.WriteLine('Starting client '+sCmdLine); |
| 446 | Win32Check( CreateProcess( nil, PChar(sCmdLine), nil,nil,TRUE,0,nil,nil,si,pi)); |
| 447 | |
| 448 | CloseHandle( pi.hThread); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 449 | CloseHandle( pi.hProcess); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 450 | end; |
| 451 | |
| 452 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 453 | class procedure TTestServer.Execute( const args: array of string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 454 | var |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 455 | Port : Integer; |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 456 | ServerEvents : Boolean; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 457 | sPipeName : string; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 458 | testHandler : ITestHandler; |
| 459 | testProcessor : IProcessor; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 460 | ServerTrans : IServerTransport; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 461 | ServerEngine : IServer; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 462 | anonymouspipe : IAnonymousPipeServerTransport; |
| 463 | namedpipe : INamedPipeServerTransport; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 464 | TransportFactory : ITransportFactory; |
| 465 | ProtocolFactory : IProtocolFactory; |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 466 | i, numWorker : Integer; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 467 | s : string; |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 468 | protType : TKnownProtocol; |
| 469 | servertype : TServerType; |
| 470 | endpoint : TEndpointTransport; |
| 471 | layered : TLayeredTransports; |
| 472 | UseSSL : Boolean; // include where appropriate (TLayeredTransport?) |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 473 | begin |
| 474 | try |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 475 | ServerEvents := FALSE; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 476 | protType := prot_Binary; |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 477 | servertype := srv_Simple; |
| 478 | endpoint := trns_Sockets; |
| 479 | layered := []; |
| 480 | UseSSL := FALSE; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 481 | Port := 9090; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 482 | sPipeName := ''; |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 483 | numWorker := 4; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 484 | |
| 485 | i := 0; |
| 486 | while ( i < Length(args) ) do begin |
| 487 | s := args[i]; |
| 488 | Inc(i); |
| 489 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 490 | // Allowed options: |
| 491 | if (s = '-h') or (s = '--help') then begin |
| 492 | // -h [ --help ] produce help message |
| 493 | PrintCmdLineHelp; |
| 494 | Exit; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 495 | end |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 496 | else if (s = '--port') then begin |
| 497 | // --port arg (=9090) Port number to listen |
| 498 | s := args[i]; |
| 499 | Inc(i); |
| 500 | Port := StrToIntDef( s, Port); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 501 | end |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 502 | else if (s = '--domain-socket') then begin |
| 503 | // --domain-socket arg Unix Domain Socket (e.g. /tmp/ThriftTest.thrift) |
| 504 | raise Exception.Create('domain-socket not supported'); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 505 | end |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 506 | else if (s = '--named-pipe') then begin |
| 507 | // --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe) |
| 508 | endpoint := trns_NamedPipes; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 509 | sPipeName := args[i]; // -pipe <name> |
| 510 | Inc( i ); |
| 511 | end |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 512 | else if (s = '--server-type') then begin |
| 513 | // --server-type arg (=simple) type of server, |
| 514 | // arg = "simple", "thread-pool", "threaded", or "nonblocking" |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 515 | s := args[i]; |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 516 | Inc(i); |
| 517 | |
| 518 | if s = 'simple' then servertype := srv_Simple |
| 519 | else if s = 'thread-pool' then servertype := srv_Threadpool |
| 520 | else if s = 'threaded' then servertype := srv_Threaded |
| 521 | else if s = 'nonblocking' then servertype := srv_Nonblocking |
| 522 | else InvalidArgs; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 523 | end |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 524 | else if (s = '--transport') then begin |
| 525 | // --transport arg (=buffered) transport: buffered, framed, http |
| 526 | s := args[i]; |
| 527 | Inc(i); |
| 528 | |
| 529 | if s = 'buffered' then Include( layered, trns_Buffered) |
| 530 | else if s = 'framed' then Include( layered, trns_Framed) |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 531 | else if s = 'http' then endpoint := trns_MsxmlHttp |
| 532 | else if s = 'winhttp' then endpoint := trns_WinHttp |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 533 | else if s = 'anonpipe' then endpoint := trns_AnonPipes |
| 534 | else InvalidArgs; |
| 535 | end |
| 536 | else if (s = '--protocol') then begin |
| 537 | // --protocol arg (=binary) protocol: binary, compact, json |
| 538 | s := args[i]; |
| 539 | Inc(i); |
| 540 | |
| 541 | if s = 'binary' then protType := prot_Binary |
| 542 | else if s = 'compact' then protType := prot_Compact |
| 543 | else if s = 'json' then protType := prot_JSON |
| 544 | else InvalidArgs; |
| 545 | end |
| 546 | else if (s = '--ssl') then begin |
| 547 | // --ssl Encrypted Transport using SSL |
| 548 | UseSSL := TRUE; |
| 549 | end |
| 550 | else if (s = '--processor-events') then begin |
| 551 | // --processor-events processor-events |
| 552 | ServerEvents := TRUE; |
| 553 | end |
| 554 | else if (s = '-n') or (s = '--workers') then begin |
| 555 | // -n [ --workers ] arg (=4) Number of thread pools workers. |
| 556 | // Only valid for thread-pool server type |
| 557 | s := args[i]; |
| 558 | numWorker := StrToIntDef(s,0); |
| 559 | if numWorker > 0 |
| 560 | then Inc(i) |
| 561 | else numWorker := 4; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 562 | end |
| 563 | else begin |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 564 | InvalidArgs; |
| 565 | end; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 566 | end; |
| 567 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 568 | |
| 569 | Console.WriteLine('Server configuration: '); |
| 570 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 571 | // create protocol factory, default to BinaryProtocol |
| 572 | case protType of |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 573 | prot_Binary : ProtocolFactory := TBinaryProtocolImpl.TFactory.Create( BINARY_STRICT_READ, BINARY_STRICT_WRITE); |
| 574 | prot_JSON : ProtocolFactory := TJSONProtocolImpl.TFactory.Create; |
Jens Geyer | f0e6331 | 2015-03-01 18:47:49 +0100 | [diff] [blame] | 575 | prot_Compact : ProtocolFactory := TCompactProtocolImpl.TFactory.Create; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 576 | else |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 577 | raise Exception.Create('Unhandled protocol'); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 578 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 579 | ASSERT( ProtocolFactory <> nil); |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 580 | Console.WriteLine('- '+THRIFT_PROTOCOLS[protType]+' protocol'); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 581 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 582 | case endpoint of |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 583 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 584 | trns_Sockets : begin |
| 585 | Console.WriteLine('- sockets (port '+IntToStr(port)+')'); |
| 586 | if (trns_Buffered in layered) then Console.WriteLine('- buffered'); |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 587 | servertrans := TServerSocketImpl.Create( Port, DEFAULT_THRIFT_TIMEOUT, (trns_Buffered in layered)); |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 588 | end; |
| 589 | |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 590 | trns_MsxmlHttp, |
| 591 | trns_WinHttp : begin |
| 592 | raise Exception.Create('HTTP server transport not implemented'); |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 593 | end; |
| 594 | |
| 595 | trns_NamedPipes : begin |
| 596 | Console.WriteLine('- named pipe ('+sPipeName+')'); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 597 | namedpipe := TNamedPipeServerTransportImpl.Create( sPipeName, 4096, PIPE_UNLIMITED_INSTANCES, INFINITE); |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 598 | servertrans := namedpipe; |
| 599 | end; |
| 600 | |
| 601 | trns_AnonPipes : begin |
| 602 | Console.WriteLine('- anonymous pipes'); |
| 603 | anonymouspipe := TAnonymousPipeServerTransportImpl.Create; |
| 604 | servertrans := anonymouspipe; |
| 605 | end |
| 606 | |
| 607 | else |
| 608 | raise Exception.Create('Unhandled endpoint transport'); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 609 | end; |
| 610 | ASSERT( servertrans <> nil); |
| 611 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 612 | if UseSSL then begin |
| 613 | raise Exception.Create('SSL not implemented'); |
| 614 | end; |
| 615 | |
| 616 | if (trns_Framed in layered) then begin |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 617 | Console.WriteLine('- framed transport'); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 618 | TransportFactory := TFramedTransportImpl.TFactory.Create; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 619 | end |
| 620 | else begin |
| 621 | TransportFactory := TTransportFactoryImpl.Create; |
| 622 | end; |
| 623 | ASSERT( TransportFactory <> nil); |
| 624 | |
| 625 | testHandler := TTestHandlerImpl.Create; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 626 | testProcessor := TThriftTest.TProcessorImpl.Create( testHandler ); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 627 | |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 628 | case servertype of |
| 629 | srv_Simple : begin |
| 630 | ServerEngine := TSimpleServer.Create( testProcessor, ServerTrans, TransportFactory, ProtocolFactory); |
| 631 | end; |
| 632 | |
| 633 | srv_Nonblocking : begin |
| 634 | raise Exception.Create(SERVER_TYPES[servertype]+' server not implemented'); |
| 635 | end; |
| 636 | |
| 637 | srv_Threadpool, |
| 638 | srv_Threaded: begin |
| 639 | if numWorker > 1 then {use here}; |
| 640 | raise Exception.Create(SERVER_TYPES[servertype]+' server not implemented'); |
| 641 | end; |
| 642 | |
| 643 | else |
| 644 | raise Exception.Create('Unhandled server type'); |
| 645 | end; |
| 646 | ASSERT( ServerEngine <> nil); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 647 | |
| 648 | testHandler.SetServer( ServerEngine); |
| 649 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 650 | // test events? |
| 651 | if ServerEvents then begin |
| 652 | Console.WriteLine('- server events test enabled'); |
| 653 | ServerEngine.ServerEvents := TServerEventsImpl.Create; |
| 654 | end; |
| 655 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 656 | // start the client now when we have the anon handles, but before the server starts |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 657 | if endpoint = trns_AnonPipes |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 658 | then LaunchAnonPipeChild( ExtractFilePath(ParamStr(0))+'client.exe', anonymouspipe); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 659 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 660 | // install Ctrl+C handler before the server starts |
| 661 | g_Handler := testHandler; |
| 662 | SetConsoleCtrlHandler( @MyConsoleEventHandler, TRUE); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 663 | |
| 664 | Console.WriteLine(''); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 665 | repeat |
| 666 | Console.WriteLine('Starting the server ...'); |
| 667 | serverEngine.Serve; |
| 668 | until {$IFDEF RunEndless} FALSE {$ELSE} TRUE {$ENDIF}; |
| 669 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 670 | testHandler.SetServer( nil); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 671 | g_Handler := nil; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 672 | |
| 673 | except |
Jens Geyer | f8a1b7a | 2014-09-24 00:26:46 +0200 | [diff] [blame] | 674 | on E: EAbort do raise; |
| 675 | on E: Exception do begin |
| 676 | Console.WriteLine( E.Message + #10 + E.StackTrace ); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 677 | end; |
| 678 | end; |
| 679 | Console.WriteLine( 'done.'); |
| 680 | end; |
| 681 | |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 682 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 683 | end. |