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 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 22 | {$WARN SYMBOL_PLATFORM OFF} |
| 23 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 24 | interface |
| 25 | |
| 26 | uses |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 27 | Windows, SysUtils, |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 28 | Generics.Collections, |
| 29 | Thrift.Console, |
| 30 | Thrift.Server, |
| 31 | Thrift.Transport, |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 32 | Thrift.Transport.Pipes, |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 33 | Thrift.Protocol, |
| 34 | Thrift.Protocol.JSON, |
| 35 | Thrift.Collections, |
| 36 | Thrift.Utils, |
| 37 | Thrift.Test, |
| 38 | Thrift, |
| 39 | TestConstants, |
| 40 | Contnrs; |
| 41 | |
| 42 | type |
| 43 | TTestServer = class |
| 44 | public |
| 45 | type |
| 46 | |
| 47 | ITestHandler = interface( TThriftTest.Iface ) |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 48 | procedure SetServer( const AServer : IServer ); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 49 | end; |
| 50 | |
| 51 | TTestHandlerImpl = class( TInterfacedObject, ITestHandler ) |
| 52 | private |
| 53 | FServer : IServer; |
| 54 | protected |
| 55 | procedure testVoid(); |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 56 | function testString(const thing: string): string; |
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 57 | function testByte(thing: ShortInt): ShortInt; |
| 58 | function testI32(thing: Integer): Integer; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 59 | function testI64(const thing: Int64): Int64; |
| 60 | function testDouble(const thing: Double): Double; |
| 61 | function testStruct(const thing: IXtruct): IXtruct; |
| 62 | function testNest(const thing: IXtruct2): IXtruct2; |
| 63 | function testMap(const thing: IThriftDictionary<Integer, Integer>): IThriftDictionary<Integer, Integer>; |
| 64 | function testStringMap(const thing: IThriftDictionary<string, string>): IThriftDictionary<string, string>; |
| 65 | function testSet(const thing: IHashSet<Integer>): IHashSet<Integer>; |
| 66 | function testList(const thing: IThriftList<Integer>): IThriftList<Integer>; |
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 67 | function testEnum(thing: TNumberz): TNumberz; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 68 | function testTypedef(const thing: Int64): Int64; |
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 69 | function testMapMap(hello: Integer): IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 70 | function testInsanity(const argument: IInsanity): IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>; |
| 71 | function testMulti(arg0: ShortInt; arg1: Integer; const arg2: Int64; const arg3: IThriftDictionary<SmallInt, string>; arg4: TNumberz; const arg5: Int64): IXtruct; |
| 72 | procedure testException(const arg: string); |
| 73 | function testMultiException(const arg0: string; const arg1: string): IXtruct; |
Jake Farrell | 7ae13e1 | 2011-10-18 14:35:26 +0000 | [diff] [blame] | 74 | procedure testOneway(secondsToSleep: Integer); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 75 | |
| 76 | procedure testStop; |
| 77 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 78 | procedure SetServer( const AServer : IServer ); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 79 | end; |
| 80 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 81 | class procedure LaunchAnonPipeChild( const app : string; const transport : IAnonymousServerPipe); |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 82 | class procedure Execute( const args: array of string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 83 | end; |
| 84 | |
| 85 | implementation |
| 86 | |
| 87 | { TTestServer.TTestHandlerImpl } |
| 88 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 89 | procedure TTestServer.TTestHandlerImpl.SetServer( const AServer: IServer); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 90 | begin |
| 91 | FServer := AServer; |
| 92 | end; |
| 93 | |
| 94 | function TTestServer.TTestHandlerImpl.testByte(thing: ShortInt): ShortInt; |
| 95 | begin |
| 96 | Console.WriteLine('testByte("' + IntToStr( thing) + '")'); |
| 97 | Result := thing; |
| 98 | end; |
| 99 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 100 | function TTestServer.TTestHandlerImpl.testDouble( const thing: Double): Double; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 101 | begin |
| 102 | Console.WriteLine('testDouble("' + FloatToStr( thing ) + '")'); |
| 103 | Result := thing; |
| 104 | end; |
| 105 | |
| 106 | function TTestServer.TTestHandlerImpl.testEnum(thing: TNumberz): TNumberz; |
| 107 | begin |
| 108 | Console.WriteLine('testEnum(' + IntToStr( Integer( thing)) + ')'); |
| 109 | Result := thing; |
| 110 | end; |
| 111 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 112 | procedure TTestServer.TTestHandlerImpl.testException(const arg: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 113 | begin |
| 114 | Console.WriteLine('testException(' + arg + ')'); |
| 115 | if ( arg = 'Xception') then |
| 116 | begin |
Roger Meier | bb6de7a | 2012-05-04 23:35:45 +0000 | [diff] [blame] | 117 | raise TXception.Create( 1001, arg); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 118 | end; |
Roger Meier | bb6de7a | 2012-05-04 23:35:45 +0000 | [diff] [blame] | 119 | |
| 120 | if (arg = 'TException') then |
| 121 | begin |
| 122 | raise TException.Create(''); |
| 123 | end; |
| 124 | |
| 125 | // else do not throw anything |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 126 | end; |
| 127 | |
| 128 | function TTestServer.TTestHandlerImpl.testI32(thing: Integer): Integer; |
| 129 | begin |
| 130 | Console.WriteLine('testI32("' + IntToStr( thing) + '")'); |
| 131 | Result := thing; |
| 132 | end; |
| 133 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 134 | function TTestServer.TTestHandlerImpl.testI64( const thing: Int64): Int64; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 135 | begin |
| 136 | Console.WriteLine('testI64("' + IntToStr( thing) + '")'); |
| 137 | Result := thing; |
| 138 | end; |
| 139 | |
| 140 | function TTestServer.TTestHandlerImpl.testInsanity( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 141 | const argument: IInsanity): IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 142 | var |
| 143 | hello, goodbye : IXtruct; |
| 144 | crazy : IInsanity; |
| 145 | looney : IInsanity; |
| 146 | first_map : IThriftDictionary<TNumberz, IInsanity>; |
| 147 | second_map : IThriftDictionary<TNumberz, IInsanity>; |
| 148 | insane : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>; |
| 149 | |
| 150 | begin |
| 151 | |
| 152 | Console.WriteLine('testInsanity()'); |
| 153 | hello := TXtructImpl.Create; |
Roger Meier | bb6de7a | 2012-05-04 23:35:45 +0000 | [diff] [blame] | 154 | hello.String_thing := 'Hello2'; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 155 | hello.Byte_thing := 2; |
| 156 | hello.I32_thing := 2; |
| 157 | hello.I64_thing := 2; |
| 158 | |
| 159 | goodbye := TXtructImpl.Create; |
| 160 | goodbye.String_thing := 'Goodbye4'; |
| 161 | goodbye.Byte_thing := 4; |
| 162 | goodbye.I32_thing := 4; |
| 163 | goodbye.I64_thing := 4; |
| 164 | |
| 165 | crazy := TInsanityImpl.Create; |
| 166 | crazy.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create; |
| 167 | crazy.UserMap.AddOrSetValue( TNumberz.EIGHT, 8); |
| 168 | crazy.Xtructs := TThriftListImpl<IXtruct>.Create; |
| 169 | crazy.Xtructs.Add(goodbye); |
| 170 | |
| 171 | looney := TInsanityImpl.Create; |
| 172 | crazy.UserMap.AddOrSetValue( TNumberz.FIVE, 5); |
| 173 | crazy.Xtructs.Add(hello); |
| 174 | |
| 175 | first_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create; |
| 176 | second_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create; |
| 177 | |
Roger Meier | bb6de7a | 2012-05-04 23:35:45 +0000 | [diff] [blame] | 178 | first_map.AddOrSetValue( TNumberz.TWO, crazy); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 179 | first_map.AddOrSetValue( TNumberz.THREE, crazy); |
| 180 | |
| 181 | second_map.AddOrSetValue( TNumberz.SIX, looney); |
| 182 | |
| 183 | insane := TThriftDictionaryImpl<Int64, IThriftDictionary<TNumberz, IInsanity>>.Create; |
| 184 | |
| 185 | insane.AddOrSetValue( 1, first_map); |
| 186 | insane.AddOrSetValue( 2, second_map); |
| 187 | |
| 188 | Result := insane; |
| 189 | end; |
| 190 | |
| 191 | function TTestServer.TTestHandlerImpl.testList( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 192 | const thing: IThriftList<Integer>): IThriftList<Integer>; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 193 | var |
| 194 | first : Boolean; |
| 195 | elem : Integer; |
| 196 | begin |
| 197 | Console.Write('testList({'); |
| 198 | first := True; |
| 199 | for elem in thing do |
| 200 | begin |
| 201 | if first then |
| 202 | begin |
| 203 | first := False; |
| 204 | end else |
| 205 | begin |
| 206 | Console.Write(', '); |
| 207 | end; |
| 208 | Console.Write( IntToStr( elem)); |
| 209 | end; |
| 210 | Console.WriteLine('})'); |
| 211 | Result := thing; |
| 212 | end; |
| 213 | |
| 214 | function TTestServer.TTestHandlerImpl.testMap( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 215 | const thing: IThriftDictionary<Integer, Integer>): IThriftDictionary<Integer, Integer>; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 216 | var |
| 217 | first : Boolean; |
| 218 | key : Integer; |
| 219 | begin |
| 220 | Console.Write('testMap({'); |
| 221 | first := True; |
| 222 | for key in thing.Keys do |
| 223 | begin |
| 224 | if (first) then |
| 225 | begin |
| 226 | first := false; |
| 227 | end else |
| 228 | begin |
| 229 | Console.Write(', '); |
| 230 | end; |
| 231 | Console.Write(IntToStr(key) + ' => ' + IntToStr( thing[key])); |
| 232 | end; |
| 233 | Console.WriteLine('})'); |
| 234 | Result := thing; |
| 235 | end; |
| 236 | |
| 237 | function TTestServer.TTestHandlerImpl.TestMapMap( |
| 238 | hello: Integer): IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>; |
| 239 | var |
| 240 | mapmap : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>; |
| 241 | pos : IThriftDictionary<Integer, Integer>; |
| 242 | neg : IThriftDictionary<Integer, Integer>; |
| 243 | i : Integer; |
| 244 | begin |
| 245 | Console.WriteLine('testMapMap(' + IntToStr( hello) + ')'); |
| 246 | mapmap := TThriftDictionaryImpl<Integer, IThriftDictionary<Integer, Integer>>.Create; |
| 247 | pos := TThriftDictionaryImpl<Integer, Integer>.Create; |
| 248 | neg := TThriftDictionaryImpl<Integer, Integer>.Create; |
| 249 | |
| 250 | for i := 1 to 4 do |
| 251 | begin |
| 252 | pos.AddOrSetValue( i, i); |
| 253 | neg.AddOrSetValue( -i, -i); |
| 254 | end; |
| 255 | |
| 256 | mapmap.AddOrSetValue(4, pos); |
| 257 | mapmap.AddOrSetValue( -4, neg); |
| 258 | |
| 259 | Result := mapmap; |
| 260 | end; |
| 261 | |
| 262 | function TTestServer.TTestHandlerImpl.testMulti(arg0: ShortInt; arg1: Integer; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 263 | const arg2: Int64; const arg3: IThriftDictionary<SmallInt, string>; |
| 264 | arg4: TNumberz; const arg5: Int64): IXtruct; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 265 | var |
| 266 | hello : IXtruct; |
| 267 | begin |
| 268 | Console.WriteLine('testMulti()'); |
| 269 | hello := TXtructImpl.Create; |
| 270 | hello.String_thing := 'Hello2'; |
| 271 | hello.Byte_thing := arg0; |
| 272 | hello.I32_thing := arg1; |
| 273 | hello.I64_thing := arg2; |
| 274 | Result := hello; |
| 275 | end; |
| 276 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 277 | function TTestServer.TTestHandlerImpl.testMultiException( const arg0, arg1: string): IXtruct; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 278 | var |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 279 | x2 : TXception2; |
| 280 | begin |
| 281 | Console.WriteLine('testMultiException(' + arg0 + ', ' + arg1 + ')'); |
| 282 | if ( arg0 = 'Xception') then |
| 283 | begin |
Jake Farrell | 343c61d | 2011-12-09 02:29:56 +0000 | [diff] [blame] | 284 | raise TXception.Create( 1001, 'This is an Xception'); // test the new rich CTOR |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 285 | end else |
| 286 | if ( arg0 = 'Xception2') then |
| 287 | begin |
Jake Farrell | 343c61d | 2011-12-09 02:29:56 +0000 | [diff] [blame] | 288 | x2 := TXception2.Create; // the old way still works too? |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 289 | x2.ErrorCode := 2002; |
| 290 | x2.Struct_thing := TXtructImpl.Create; |
| 291 | x2.Struct_thing.String_thing := 'This is an Xception2'; |
Jake Farrell | ac10256 | 2011-11-23 14:30:41 +0000 | [diff] [blame] | 292 | x2.UpdateMessageProperty; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 293 | raise x2; |
| 294 | end; |
| 295 | |
| 296 | Result := TXtructImpl.Create; |
| 297 | Result.String_thing := arg1; |
| 298 | end; |
| 299 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 300 | function TTestServer.TTestHandlerImpl.testNest( const thing: IXtruct2): IXtruct2; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 301 | var |
| 302 | temp : IXtruct; |
| 303 | begin |
| 304 | temp := thing.Struct_thing; |
| 305 | Console.WriteLine('testNest({' + |
| 306 | IntToStr( thing.Byte_thing) + ', {' + |
| 307 | '"' + temp.String_thing + '", ' + |
| 308 | IntToStr( temp.Byte_thing) + ', ' + |
| 309 | IntToStr( temp.I32_thing) + ', ' + |
| 310 | IntToStr( temp.I64_thing) + '}, ' + |
| 311 | IntToStr( temp.I32_thing) + '})'); |
| 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 | |
| 322 | function TTestServer.TTestHandlerImpl.testSet( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 323 | const thing: IHashSet<Integer>):IHashSet<Integer>; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 324 | var |
| 325 | first : Boolean; |
| 326 | elem : Integer; |
| 327 | begin |
| 328 | Console.Write('testSet({'); |
| 329 | first := True; |
| 330 | |
| 331 | for elem in thing do |
| 332 | begin |
| 333 | if first then |
| 334 | begin |
| 335 | first := False; |
| 336 | end else |
| 337 | begin |
| 338 | Console.Write( ', '); |
| 339 | end; |
| 340 | Console.Write( IntToStr( elem)); |
| 341 | end; |
| 342 | Console.WriteLine('})'); |
| 343 | Result := thing; |
| 344 | end; |
| 345 | |
| 346 | procedure TTestServer.TTestHandlerImpl.testStop; |
| 347 | begin |
| 348 | if FServer <> nil then |
| 349 | begin |
| 350 | FServer.Stop; |
| 351 | end; |
| 352 | end; |
| 353 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 354 | function TTestServer.TTestHandlerImpl.testString( const thing: string): string; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 355 | begin |
| 356 | Console.WriteLine('teststring("' + thing + '")'); |
| 357 | Result := thing; |
| 358 | end; |
| 359 | |
| 360 | function TTestServer.TTestHandlerImpl.testStringMap( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 361 | const thing: IThriftDictionary<string, string>): IThriftDictionary<string, string>; |
Roger Meier | bb6de7a | 2012-05-04 23:35:45 +0000 | [diff] [blame] | 362 | var |
| 363 | first : Boolean; |
| 364 | key : string; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 365 | begin |
Roger Meier | bb6de7a | 2012-05-04 23:35:45 +0000 | [diff] [blame] | 366 | Console.Write('testStringMap({'); |
| 367 | first := True; |
| 368 | for key in thing.Keys do |
| 369 | begin |
| 370 | if (first) then |
| 371 | begin |
| 372 | first := false; |
| 373 | end else |
| 374 | begin |
| 375 | Console.Write(', '); |
| 376 | end; |
| 377 | Console.Write(key + ' => ' + thing[key]); |
| 378 | end; |
| 379 | Console.WriteLine('})'); |
| 380 | Result := thing; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 381 | end; |
| 382 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 383 | function TTestServer.TTestHandlerImpl.testTypedef( const thing: Int64): Int64; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 384 | begin |
| 385 | Console.WriteLine('testTypedef(' + IntToStr( thing) + ')'); |
| 386 | Result := thing; |
| 387 | end; |
| 388 | |
| 389 | procedure TTestServer.TTestHandlerImpl.TestVoid; |
| 390 | begin |
| 391 | Console.WriteLine('testVoid()'); |
| 392 | end; |
| 393 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 394 | function TTestServer.TTestHandlerImpl.testStruct( const thing: IXtruct): IXtruct; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 395 | begin |
| 396 | Console.WriteLine('testStruct({' + |
| 397 | '"' + thing.String_thing + '", ' + |
| 398 | IntToStr( thing.Byte_thing) + ', ' + |
| 399 | IntToStr( thing.I32_thing) + ', ' + |
| 400 | IntToStr( thing.I64_thing)); |
| 401 | Result := thing; |
| 402 | end; |
| 403 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 404 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 405 | { TTestServer } |
| 406 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 407 | |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 408 | class procedure TTestServer.LaunchAnonPipeChild( const app : string; const transport : IAnonymousServerPipe); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 409 | //Launch child process and pass R/W anonymous pipe handles on cmd line. |
| 410 | //This is a simple example and does not include elevation or other |
| 411 | //advanced features. |
| 412 | var pi : PROCESS_INFORMATION; |
| 413 | si : STARTUPINFO; |
| 414 | sArg, sHandles, sCmdLine : string; |
| 415 | i : Integer; |
| 416 | begin |
| 417 | GetStartupInfo( si); //set startupinfo for the spawned process |
| 418 | |
| 419 | // preformat handles args |
| 420 | sHandles := Format( '%d %d', |
| 421 | [ Integer(transport.ClientAnonRead), |
| 422 | Integer(transport.ClientAnonWrite)]); |
| 423 | |
| 424 | // pass all settings to client |
| 425 | sCmdLine := app; |
| 426 | for i := 1 to ParamCount do begin |
| 427 | sArg := ParamStr(i); |
| 428 | |
| 429 | // add anonymous handles and quote strings where appropriate |
| 430 | if sArg = '-anon' |
| 431 | then sArg := sArg +' '+ sHandles |
| 432 | else begin |
| 433 | if Pos(' ',sArg) > 0 |
| 434 | then sArg := '"'+sArg+'"'; |
| 435 | end;; |
| 436 | |
| 437 | sCmdLine := sCmdLine +' '+ sArg; |
| 438 | end; |
| 439 | |
| 440 | // spawn the child process |
| 441 | Console.WriteLine('Starting client '+sCmdLine); |
| 442 | Win32Check( CreateProcess( nil, PChar(sCmdLine), nil,nil,TRUE,0,nil,nil,si,pi)); |
| 443 | |
| 444 | CloseHandle( pi.hThread); |
| 445 | CloseHandle( pi.hProcess); |
| 446 | end; |
| 447 | |
| 448 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 449 | class procedure TTestServer.Execute( const args: array of string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 450 | var |
| 451 | UseBufferedSockets : Boolean; |
| 452 | UseFramed : Boolean; |
| 453 | Port : Integer; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 454 | AnonPipe : Boolean; |
| 455 | sPipeName : string; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 456 | testHandler : ITestHandler; |
| 457 | testProcessor : IProcessor; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 458 | ServerTrans : IServerTransport; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 459 | ServerEngine : IServer; |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 460 | anonymouspipe : IAnonymousServerPipe; |
| 461 | namedpipe : INamedServerPipe; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 462 | TransportFactory : ITransportFactory; |
| 463 | ProtocolFactory : IProtocolFactory; |
| 464 | i : Integer; |
| 465 | s : string; |
| 466 | protType, p : TKnownProtocol; |
| 467 | begin |
| 468 | try |
| 469 | UseBufferedSockets := False; |
| 470 | UseFramed := False; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 471 | AnonPipe := FALSE; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 472 | protType := prot_Binary; |
| 473 | Port := 9090; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 474 | sPipeName := ''; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 475 | |
| 476 | i := 0; |
| 477 | while ( i < Length(args) ) do begin |
| 478 | s := args[i]; |
| 479 | Inc(i); |
| 480 | |
| 481 | if StrToIntDef( s, -1) > 0 then |
| 482 | begin |
| 483 | Port := StrToIntDef( s, Port); |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 484 | end |
| 485 | else if ( s = 'raw' ) then |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 486 | begin |
| 487 | // as default |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 488 | end |
| 489 | else if ( s = 'buffered' ) then |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 490 | begin |
| 491 | UseBufferedSockets := True; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 492 | end |
| 493 | else if ( s = 'framed' ) then |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 494 | begin |
| 495 | UseFramed := True; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 496 | end |
| 497 | else if (s = '-pipe') then |
| 498 | begin |
| 499 | sPipeName := args[i]; // -pipe <name> |
| 500 | Inc( i ); |
| 501 | end |
| 502 | else if (s = '-anon') then |
| 503 | begin |
| 504 | AnonPipe := TRUE; |
| 505 | end |
| 506 | else if (s = '-prot') then // -prot JSON|binary |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 507 | begin |
| 508 | s := args[i]; |
| 509 | Inc( i ); |
| 510 | for p:= Low(TKnownProtocol) to High(TKnownProtocol) do begin |
| 511 | if SameText( s, KNOWN_PROTOCOLS[p]) then begin |
| 512 | protType := p; |
| 513 | Break; |
| 514 | end; |
| 515 | end; |
| 516 | end else |
| 517 | begin |
| 518 | // Fall back to the older boolean syntax |
| 519 | UseBufferedSockets := StrToBoolDef( args[1], UseBufferedSockets); |
| 520 | end |
| 521 | end; |
| 522 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 523 | |
| 524 | Console.WriteLine('Server configuration: '); |
| 525 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 526 | // create protocol factory, default to BinaryProtocol |
| 527 | case protType of |
| 528 | prot_Binary: ProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 529 | prot_JSON : ProtocolFactory := TJSONProtocolImpl.TFactory.Create; |
| 530 | else |
| 531 | ASSERT( FALSE); // unhandled case! |
| 532 | ProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 533 | end; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 534 | ASSERT( ProtocolFactory <> nil); |
| 535 | Console.WriteLine('- '+KNOWN_PROTOCOLS[protType]+' protocol'); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 536 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 537 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 538 | if sPipeName <> '' then begin |
| 539 | Console.WriteLine('- named pipe ('+sPipeName+')'); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 540 | namedpipe := TNamedServerPipeImpl.Create( sPipeName); |
| 541 | servertrans := namedpipe; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 542 | end |
| 543 | else if AnonPipe then begin |
| 544 | Console.WriteLine('- anonymous pipes'); |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 545 | anonymouspipe := TAnonymousServerPipeImpl.Create; |
| 546 | servertrans := anonymouspipe; |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 547 | end |
| 548 | else begin |
| 549 | Console.WriteLine('- sockets (port '+IntToStr(port)+')'); |
| 550 | if UseBufferedSockets then Console.WriteLine('- buffered sockets'); |
| 551 | servertrans := TServerSocketImpl.Create( Port, 0, UseBufferedSockets); |
| 552 | end; |
| 553 | ASSERT( servertrans <> nil); |
| 554 | |
| 555 | if UseFramed then begin |
| 556 | Console.WriteLine('- framed transport'); |
| 557 | TransportFactory := TFramedTransportImpl.TFactory.Create |
| 558 | end |
| 559 | else begin |
| 560 | TransportFactory := TTransportFactoryImpl.Create; |
| 561 | end; |
| 562 | ASSERT( TransportFactory <> nil); |
| 563 | |
| 564 | testHandler := TTestHandlerImpl.Create; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 565 | testProcessor := TThriftTest.TProcessorImpl.Create( testHandler ); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 566 | |
| 567 | ServerEngine := TSimpleServer.Create( testProcessor, |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 568 | ServerTrans, |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 569 | TransportFactory, |
| 570 | ProtocolFactory); |
| 571 | |
| 572 | testHandler.SetServer( ServerEngine); |
| 573 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 574 | // start the client now when we have the anon handles, but before the server starts |
| 575 | if AnonPipe |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 576 | then LaunchAnonPipeChild( ExtractFilePath(ParamStr(0))+'client.exe', anonymouspipe); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 577 | |
Roger Meier | 3bef8c2 | 2012-10-06 06:58:00 +0000 | [diff] [blame] | 578 | |
| 579 | Console.WriteLine(''); |
| 580 | Console.WriteLine('Starting the server ...'); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 581 | serverEngine.Serve; |
| 582 | testHandler.SetServer( nil); |
| 583 | |
| 584 | except |
| 585 | on E: Exception do |
| 586 | begin |
| 587 | Console.Write( E.Message); |
| 588 | end; |
| 589 | end; |
| 590 | Console.WriteLine( 'done.'); |
| 591 | end; |
| 592 | |
| 593 | end. |