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