2011-11-10 20:32:44 +00:00
|
|
|
(*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*)
|
|
|
|
|
|
|
|
unit TestServer;
|
|
|
|
|
2016-04-14 19:37:11 +00:00
|
|
|
{$I ../src/Thrift.Defines.inc}
|
2012-10-06 06:58:00 +00:00
|
|
|
{$WARN SYMBOL_PLATFORM OFF}
|
|
|
|
|
2013-03-27 18:26:25 +00:00
|
|
|
{.$DEFINE RunEndless} // activate to interactively stress-test the server stop routines via Ctrl+C
|
|
|
|
|
2011-11-10 20:32:44 +00:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2012-10-06 06:58:00 +00:00
|
|
|
Windows, SysUtils,
|
2011-11-10 20:32:44 +00:00
|
|
|
Generics.Collections,
|
|
|
|
Thrift.Console,
|
|
|
|
Thrift.Server,
|
|
|
|
Thrift.Transport,
|
2012-10-06 06:58:00 +00:00
|
|
|
Thrift.Transport.Pipes,
|
2011-11-10 20:32:44 +00:00
|
|
|
Thrift.Protocol,
|
|
|
|
Thrift.Protocol.JSON,
|
2015-03-01 17:47:49 +00:00
|
|
|
Thrift.Protocol.Compact,
|
2011-11-10 20:32:44 +00:00
|
|
|
Thrift.Collections,
|
|
|
|
Thrift.Utils,
|
|
|
|
Thrift.Test,
|
|
|
|
Thrift,
|
|
|
|
TestConstants,
|
2013-09-25 19:12:21 +00:00
|
|
|
TestServerEvents,
|
2011-11-10 20:32:44 +00:00
|
|
|
Contnrs;
|
|
|
|
|
|
|
|
type
|
|
|
|
TTestServer = class
|
|
|
|
public
|
|
|
|
type
|
|
|
|
|
|
|
|
ITestHandler = interface( TThriftTest.Iface )
|
2012-01-08 21:51:08 +00:00
|
|
|
procedure SetServer( const AServer : IServer );
|
2013-03-27 18:26:25 +00:00
|
|
|
procedure TestStop;
|
2011-11-10 20:32:44 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
TTestHandlerImpl = class( TInterfacedObject, ITestHandler )
|
|
|
|
private
|
|
|
|
FServer : IServer;
|
|
|
|
protected
|
|
|
|
procedure testVoid();
|
2015-09-21 22:00:49 +00:00
|
|
|
function testBool(thing: Boolean): Boolean;
|
2012-01-08 21:51:08 +00:00
|
|
|
function testString(const thing: string): string;
|
2011-10-18 14:35:26 +00:00
|
|
|
function testByte(thing: ShortInt): ShortInt;
|
|
|
|
function testI32(thing: Integer): Integer;
|
2012-01-08 21:51:08 +00:00
|
|
|
function testI64(const thing: Int64): Int64;
|
|
|
|
function testDouble(const thing: Double): Double;
|
2014-12-13 22:42:58 +00:00
|
|
|
function testBinary(const thing: TBytes): TBytes;
|
2012-01-08 21:51:08 +00:00
|
|
|
function testStruct(const thing: IXtruct): IXtruct;
|
|
|
|
function testNest(const thing: IXtruct2): IXtruct2;
|
|
|
|
function testMap(const thing: IThriftDictionary<Integer, Integer>): IThriftDictionary<Integer, Integer>;
|
|
|
|
function testStringMap(const thing: IThriftDictionary<string, string>): IThriftDictionary<string, string>;
|
|
|
|
function testSet(const thing: IHashSet<Integer>): IHashSet<Integer>;
|
|
|
|
function testList(const thing: IThriftList<Integer>): IThriftList<Integer>;
|
2011-10-18 14:35:26 +00:00
|
|
|
function testEnum(thing: TNumberz): TNumberz;
|
2012-01-08 21:51:08 +00:00
|
|
|
function testTypedef(const thing: Int64): Int64;
|
2011-10-18 14:35:26 +00:00
|
|
|
function testMapMap(hello: Integer): IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
|
2012-01-08 21:51:08 +00:00
|
|
|
function testInsanity(const argument: IInsanity): IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
|
|
|
|
function testMulti(arg0: ShortInt; arg1: Integer; const arg2: Int64; const arg3: IThriftDictionary<SmallInt, string>; arg4: TNumberz; const arg5: Int64): IXtruct;
|
|
|
|
procedure testException(const arg: string);
|
|
|
|
function testMultiException(const arg0: string; const arg1: string): IXtruct;
|
2011-10-18 14:35:26 +00:00
|
|
|
procedure testOneway(secondsToSleep: Integer);
|
2011-11-10 20:32:44 +00:00
|
|
|
|
2013-03-27 18:26:25 +00:00
|
|
|
procedure TestStop;
|
2012-01-08 21:51:08 +00:00
|
|
|
procedure SetServer( const AServer : IServer );
|
2011-11-10 20:32:44 +00:00
|
|
|
end;
|
|
|
|
|
2014-09-23 22:26:46 +00:00
|
|
|
class procedure PrintCmdLineHelp;
|
|
|
|
class procedure InvalidArgs;
|
|
|
|
|
2013-03-27 18:26:25 +00:00
|
|
|
class procedure LaunchAnonPipeChild( const app : string; const transport : IAnonymousPipeServerTransport);
|
2012-01-08 21:51:08 +00:00
|
|
|
class procedure Execute( const args: array of string);
|
2011-11-10 20:32:44 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2013-03-27 18:26:25 +00:00
|
|
|
|
|
|
|
var g_Handler : TTestServer.ITestHandler = nil;
|
|
|
|
|
|
|
|
|
|
|
|
function MyConsoleEventHandler( dwCtrlType : DWORD) : BOOL; stdcall;
|
|
|
|
// Note that this Handler procedure is called from another thread
|
|
|
|
var handler : TTestServer.ITestHandler;
|
|
|
|
begin
|
|
|
|
result := TRUE;
|
|
|
|
try
|
|
|
|
case dwCtrlType of
|
|
|
|
CTRL_C_EVENT : Console.WriteLine( 'Ctrl+C pressed');
|
|
|
|
CTRL_BREAK_EVENT : Console.WriteLine( 'Ctrl+Break pressed');
|
|
|
|
CTRL_CLOSE_EVENT : Console.WriteLine( 'Received CloseTask signal');
|
|
|
|
CTRL_LOGOFF_EVENT : Console.WriteLine( 'Received LogOff signal');
|
|
|
|
CTRL_SHUTDOWN_EVENT : Console.WriteLine( 'Received Shutdown signal');
|
|
|
|
else
|
|
|
|
Console.WriteLine( 'Received console event #'+IntToStr(Integer(dwCtrlType)));
|
|
|
|
end;
|
|
|
|
|
|
|
|
handler := g_Handler;
|
|
|
|
if handler <> nil then handler.TestStop;
|
|
|
|
|
|
|
|
except
|
|
|
|
// catch all
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2011-11-10 20:32:44 +00:00
|
|
|
{ TTestServer.TTestHandlerImpl }
|
|
|
|
|
2012-01-08 21:51:08 +00:00
|
|
|
procedure TTestServer.TTestHandlerImpl.SetServer( const AServer: IServer);
|
2011-11-10 20:32:44 +00:00
|
|
|
begin
|
|
|
|
FServer := AServer;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestServer.TTestHandlerImpl.testByte(thing: ShortInt): ShortInt;
|
|
|
|
begin
|
|
|
|
Console.WriteLine('testByte("' + IntToStr( thing) + '")');
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
2012-01-08 21:51:08 +00:00
|
|
|
function TTestServer.TTestHandlerImpl.testDouble( const thing: Double): Double;
|
2011-11-10 20:32:44 +00:00
|
|
|
begin
|
|
|
|
Console.WriteLine('testDouble("' + FloatToStr( thing ) + '")');
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
2014-12-13 22:42:58 +00:00
|
|
|
function TTestServer.TTestHandlerImpl.testBinary(const thing: TBytes): TBytes;
|
|
|
|
begin
|
|
|
|
Console.WriteLine('testBinary("' + BytesToHex( thing ) + '")');
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
2011-11-10 20:32:44 +00:00
|
|
|
function TTestServer.TTestHandlerImpl.testEnum(thing: TNumberz): TNumberz;
|
|
|
|
begin
|
|
|
|
Console.WriteLine('testEnum(' + IntToStr( Integer( thing)) + ')');
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
2012-01-08 21:51:08 +00:00
|
|
|
procedure TTestServer.TTestHandlerImpl.testException(const arg: string);
|
2011-11-10 20:32:44 +00:00
|
|
|
begin
|
|
|
|
Console.WriteLine('testException(' + arg + ')');
|
|
|
|
if ( arg = 'Xception') then
|
|
|
|
begin
|
2012-05-04 23:35:45 +00:00
|
|
|
raise TXception.Create( 1001, arg);
|
2011-11-10 20:32:44 +00:00
|
|
|
end;
|
2012-05-04 23:35:45 +00:00
|
|
|
|
|
|
|
if (arg = 'TException') then
|
|
|
|
begin
|
|
|
|
raise TException.Create('');
|
|
|
|
end;
|
|
|
|
|
|
|
|
// else do not throw anything
|
2011-11-10 20:32:44 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestServer.TTestHandlerImpl.testI32(thing: Integer): Integer;
|
|
|
|
begin
|
|
|
|
Console.WriteLine('testI32("' + IntToStr( thing) + '")');
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
2012-01-08 21:51:08 +00:00
|
|
|
function TTestServer.TTestHandlerImpl.testI64( const thing: Int64): Int64;
|
2011-11-10 20:32:44 +00:00
|
|
|
begin
|
|
|
|
Console.WriteLine('testI64("' + IntToStr( thing) + '")');
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestServer.TTestHandlerImpl.testInsanity(
|
2012-01-08 21:51:08 +00:00
|
|
|
const argument: IInsanity): IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
|
2011-11-10 20:32:44 +00:00
|
|
|
var
|
|
|
|
looney : IInsanity;
|
|
|
|
first_map : IThriftDictionary<TNumberz, IInsanity>;
|
|
|
|
second_map : IThriftDictionary<TNumberz, IInsanity>;
|
|
|
|
insane : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Console.WriteLine('testInsanity()');
|
|
|
|
|
2016-12-28 13:25:41 +00:00
|
|
|
(**
|
|
|
|
* So you think you've got this all worked, out eh?
|
|
|
|
*
|
|
|
|
* Creates a the returned map with these values and prints it out:
|
|
|
|
* { 1 => { 2 => argument,
|
|
|
|
* 3 => argument,
|
|
|
|
* },
|
|
|
|
* 2 => { 6 => <empty Insanity struct>, },
|
|
|
|
* }
|
|
|
|
* @return map<UserId, map<Numberz,Insanity>> - a map with the above values
|
|
|
|
*)
|
2011-11-10 20:32:44 +00:00
|
|
|
|
|
|
|
first_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create;
|
|
|
|
second_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create;
|
|
|
|
|
2016-12-28 13:25:41 +00:00
|
|
|
first_map.AddOrSetValue( TNumberz.TWO, argument);
|
|
|
|
first_map.AddOrSetValue( TNumberz.THREE, argument);
|
2011-11-10 20:32:44 +00:00
|
|
|
|
2016-12-28 13:25:41 +00:00
|
|
|
looney := TInsanityImpl.Create;
|
2011-11-10 20:32:44 +00:00
|
|
|
second_map.AddOrSetValue( TNumberz.SIX, looney);
|
|
|
|
|
|
|
|
insane := TThriftDictionaryImpl<Int64, IThriftDictionary<TNumberz, IInsanity>>.Create;
|
|
|
|
|
|
|
|
insane.AddOrSetValue( 1, first_map);
|
|
|
|
insane.AddOrSetValue( 2, second_map);
|
|
|
|
|
|
|
|
Result := insane;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestServer.TTestHandlerImpl.testList(
|
2012-01-08 21:51:08 +00:00
|
|
|
const thing: IThriftList<Integer>): IThriftList<Integer>;
|
2011-11-10 20:32:44 +00:00
|
|
|
var
|
|
|
|
first : Boolean;
|
|
|
|
elem : Integer;
|
|
|
|
begin
|
|
|
|
Console.Write('testList({');
|
|
|
|
first := True;
|
|
|
|
for elem in thing do
|
|
|
|
begin
|
|
|
|
if first then
|
|
|
|
begin
|
|
|
|
first := False;
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
Console.Write(', ');
|
|
|
|
end;
|
|
|
|
Console.Write( IntToStr( elem));
|
|
|
|
end;
|
|
|
|
Console.WriteLine('})');
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestServer.TTestHandlerImpl.testMap(
|
2012-01-08 21:51:08 +00:00
|
|
|
const thing: IThriftDictionary<Integer, Integer>): IThriftDictionary<Integer, Integer>;
|
2011-11-10 20:32:44 +00:00
|
|
|
var
|
|
|
|
first : Boolean;
|
|
|
|
key : Integer;
|
|
|
|
begin
|
|
|
|
Console.Write('testMap({');
|
|
|
|
first := True;
|
|
|
|
for key in thing.Keys do
|
|
|
|
begin
|
|
|
|
if (first) then
|
|
|
|
begin
|
|
|
|
first := false;
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
Console.Write(', ');
|
|
|
|
end;
|
|
|
|
Console.Write(IntToStr(key) + ' => ' + IntToStr( thing[key]));
|
|
|
|
end;
|
|
|
|
Console.WriteLine('})');
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestServer.TTestHandlerImpl.TestMapMap(
|
|
|
|
hello: Integer): IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
|
|
|
|
var
|
|
|
|
mapmap : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
|
|
|
|
pos : IThriftDictionary<Integer, Integer>;
|
|
|
|
neg : IThriftDictionary<Integer, Integer>;
|
|
|
|
i : Integer;
|
|
|
|
begin
|
|
|
|
Console.WriteLine('testMapMap(' + IntToStr( hello) + ')');
|
|
|
|
mapmap := TThriftDictionaryImpl<Integer, IThriftDictionary<Integer, Integer>>.Create;
|
|
|
|
pos := TThriftDictionaryImpl<Integer, Integer>.Create;
|
|
|
|
neg := TThriftDictionaryImpl<Integer, Integer>.Create;
|
|
|
|
|
|
|
|
for i := 1 to 4 do
|
|
|
|
begin
|
|
|
|
pos.AddOrSetValue( i, i);
|
|
|
|
neg.AddOrSetValue( -i, -i);
|
|
|
|
end;
|
|
|
|
|
|
|
|
mapmap.AddOrSetValue(4, pos);
|
|
|
|
mapmap.AddOrSetValue( -4, neg);
|
|
|
|
|
|
|
|
Result := mapmap;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestServer.TTestHandlerImpl.testMulti(arg0: ShortInt; arg1: Integer;
|
2012-01-08 21:51:08 +00:00
|
|
|
const arg2: Int64; const arg3: IThriftDictionary<SmallInt, string>;
|
|
|
|
arg4: TNumberz; const arg5: Int64): IXtruct;
|
2011-11-10 20:32:44 +00:00
|
|
|
var
|
|
|
|
hello : IXtruct;
|
|
|
|
begin
|
|
|
|
Console.WriteLine('testMulti()');
|
|
|
|
hello := TXtructImpl.Create;
|
|
|
|
hello.String_thing := 'Hello2';
|
|
|
|
hello.Byte_thing := arg0;
|
|
|
|
hello.I32_thing := arg1;
|
|
|
|
hello.I64_thing := arg2;
|
|
|
|
Result := hello;
|
|
|
|
end;
|
|
|
|
|
2012-01-08 21:51:08 +00:00
|
|
|
function TTestServer.TTestHandlerImpl.testMultiException( const arg0, arg1: string): IXtruct;
|
2011-11-10 20:32:44 +00:00
|
|
|
var
|
|
|
|
x2 : TXception2;
|
|
|
|
begin
|
|
|
|
Console.WriteLine('testMultiException(' + arg0 + ', ' + arg1 + ')');
|
|
|
|
if ( arg0 = 'Xception') then
|
|
|
|
begin
|
2014-10-03 17:50:38 +00:00
|
|
|
raise TXception.Create( 1001, 'This is an Xception'); // test the new rich CTOR
|
2011-11-10 20:32:44 +00:00
|
|
|
end else
|
|
|
|
if ( arg0 = 'Xception2') then
|
|
|
|
begin
|
2011-12-09 02:29:56 +00:00
|
|
|
x2 := TXception2.Create; // the old way still works too?
|
2011-11-10 20:32:44 +00:00
|
|
|
x2.ErrorCode := 2002;
|
|
|
|
x2.Struct_thing := TXtructImpl.Create;
|
|
|
|
x2.Struct_thing.String_thing := 'This is an Xception2';
|
2011-11-23 14:30:41 +00:00
|
|
|
x2.UpdateMessageProperty;
|
2011-11-10 20:32:44 +00:00
|
|
|
raise x2;
|
|
|
|
end;
|
|
|
|
|
|
|
|
Result := TXtructImpl.Create;
|
|
|
|
Result.String_thing := arg1;
|
|
|
|
end;
|
|
|
|
|
2012-01-08 21:51:08 +00:00
|
|
|
function TTestServer.TTestHandlerImpl.testNest( const thing: IXtruct2): IXtruct2;
|
2011-11-10 20:32:44 +00:00
|
|
|
var
|
|
|
|
temp : IXtruct;
|
|
|
|
begin
|
|
|
|
temp := thing.Struct_thing;
|
|
|
|
Console.WriteLine('testNest({' +
|
|
|
|
IntToStr( thing.Byte_thing) + ', {' +
|
|
|
|
'"' + temp.String_thing + '", ' +
|
|
|
|
IntToStr( temp.Byte_thing) + ', ' +
|
|
|
|
IntToStr( temp.I32_thing) + ', ' +
|
|
|
|
IntToStr( temp.I64_thing) + '}, ' +
|
|
|
|
IntToStr( temp.I32_thing) + '})');
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTestServer.TTestHandlerImpl.testOneway(secondsToSleep: Integer);
|
|
|
|
begin
|
|
|
|
Console.WriteLine('testOneway(' + IntToStr( secondsToSleep )+ '), sleeping...');
|
|
|
|
Sleep(secondsToSleep * 1000);
|
|
|
|
Console.WriteLine('testOneway finished');
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestServer.TTestHandlerImpl.testSet(
|
2012-01-08 21:51:08 +00:00
|
|
|
const thing: IHashSet<Integer>):IHashSet<Integer>;
|
2011-11-10 20:32:44 +00:00
|
|
|
var
|
|
|
|
first : Boolean;
|
|
|
|
elem : Integer;
|
|
|
|
begin
|
|
|
|
Console.Write('testSet({');
|
|
|
|
first := True;
|
|
|
|
|
|
|
|
for elem in thing do
|
|
|
|
begin
|
|
|
|
if first then
|
|
|
|
begin
|
|
|
|
first := False;
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
Console.Write( ', ');
|
|
|
|
end;
|
|
|
|
Console.Write( IntToStr( elem));
|
|
|
|
end;
|
|
|
|
Console.WriteLine('})');
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTestServer.TTestHandlerImpl.testStop;
|
|
|
|
begin
|
|
|
|
if FServer <> nil then
|
|
|
|
begin
|
|
|
|
FServer.Stop;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2015-09-21 22:00:49 +00:00
|
|
|
function TTestServer.TTestHandlerImpl.testBool(thing: Boolean): Boolean;
|
|
|
|
begin
|
|
|
|
Console.WriteLine('testBool(' + BoolToStr(thing,true) + ')');
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
2012-01-08 21:51:08 +00:00
|
|
|
function TTestServer.TTestHandlerImpl.testString( const thing: string): string;
|
2011-11-10 20:32:44 +00:00
|
|
|
begin
|
|
|
|
Console.WriteLine('teststring("' + thing + '")');
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TTestServer.TTestHandlerImpl.testStringMap(
|
2012-01-08 21:51:08 +00:00
|
|
|
const thing: IThriftDictionary<string, string>): IThriftDictionary<string, string>;
|
2012-05-04 23:35:45 +00:00
|
|
|
var
|
|
|
|
first : Boolean;
|
|
|
|
key : string;
|
2011-11-10 20:32:44 +00:00
|
|
|
begin
|
2012-05-04 23:35:45 +00:00
|
|
|
Console.Write('testStringMap({');
|
|
|
|
first := True;
|
|
|
|
for key in thing.Keys do
|
|
|
|
begin
|
|
|
|
if (first) then
|
|
|
|
begin
|
|
|
|
first := false;
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
Console.Write(', ');
|
|
|
|
end;
|
|
|
|
Console.Write(key + ' => ' + thing[key]);
|
|
|
|
end;
|
|
|
|
Console.WriteLine('})');
|
|
|
|
Result := thing;
|
2011-11-10 20:32:44 +00:00
|
|
|
end;
|
|
|
|
|
2012-01-08 21:51:08 +00:00
|
|
|
function TTestServer.TTestHandlerImpl.testTypedef( const thing: Int64): Int64;
|
2011-11-10 20:32:44 +00:00
|
|
|
begin
|
|
|
|
Console.WriteLine('testTypedef(' + IntToStr( thing) + ')');
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TTestServer.TTestHandlerImpl.TestVoid;
|
|
|
|
begin
|
|
|
|
Console.WriteLine('testVoid()');
|
|
|
|
end;
|
|
|
|
|
2012-01-08 21:51:08 +00:00
|
|
|
function TTestServer.TTestHandlerImpl.testStruct( const thing: IXtruct): IXtruct;
|
2011-11-10 20:32:44 +00:00
|
|
|
begin
|
|
|
|
Console.WriteLine('testStruct({' +
|
|
|
|
'"' + thing.String_thing + '", ' +
|
|
|
|
IntToStr( thing.Byte_thing) + ', ' +
|
|
|
|
IntToStr( thing.I32_thing) + ', ' +
|
|
|
|
IntToStr( thing.I64_thing));
|
|
|
|
Result := thing;
|
|
|
|
end;
|
|
|
|
|
2012-10-06 06:58:00 +00:00
|
|
|
|
2011-11-10 20:32:44 +00:00
|
|
|
{ TTestServer }
|
|
|
|
|
2012-10-06 06:58:00 +00:00
|
|
|
|
2014-09-23 22:26:46 +00:00
|
|
|
class procedure TTestServer.PrintCmdLineHelp;
|
|
|
|
const HELPTEXT = ' [options]'#10
|
|
|
|
+ #10
|
|
|
|
+ 'Allowed options:'#10
|
|
|
|
+ ' -h [ --help ] produce help message'#10
|
|
|
|
+ ' --port arg (=9090) Port number to listen'#10
|
|
|
|
+ ' --domain-socket arg Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)'#10
|
|
|
|
+ ' --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)'#10
|
|
|
|
+ ' --server-type arg (=simple) type of server, "simple", "thread-pool",'#10
|
|
|
|
+ ' "threaded", or "nonblocking"'#10
|
|
|
|
+ ' --transport arg (=socket) transport: buffered, framed, http, anonpipe'#10
|
|
|
|
+ ' --protocol arg (=binary) protocol: binary, compact, json'#10
|
|
|
|
+ ' --ssl Encrypted Transport using SSL'#10
|
|
|
|
+ ' --processor-events processor-events'#10
|
|
|
|
+ ' -n [ --workers ] arg (=4) Number of thread pools workers. Only valid for'#10
|
|
|
|
+ ' thread-pool server type'#10
|
|
|
|
;
|
|
|
|
begin
|
|
|
|
Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + HELPTEXT);
|
|
|
|
end;
|
|
|
|
|
|
|
|
class procedure TTestServer.InvalidArgs;
|
|
|
|
begin
|
|
|
|
Console.WriteLine( 'Invalid args.');
|
|
|
|
Console.WriteLine( ChangeFileExt(ExtractFileName(ParamStr(0)),'') + ' -h for more information');
|
|
|
|
Abort;
|
|
|
|
end;
|
|
|
|
|
2013-03-27 18:26:25 +00:00
|
|
|
class procedure TTestServer.LaunchAnonPipeChild( const app : string; const transport : IAnonymousPipeServerTransport);
|
2012-10-06 06:58:00 +00:00
|
|
|
//Launch child process and pass R/W anonymous pipe handles on cmd line.
|
|
|
|
//This is a simple example and does not include elevation or other
|
|
|
|
//advanced features.
|
|
|
|
var pi : PROCESS_INFORMATION;
|
2014-10-03 17:50:38 +00:00
|
|
|
si : STARTUPINFO;
|
|
|
|
sArg, sHandles, sCmdLine : string;
|
2012-10-06 06:58:00 +00:00
|
|
|
i : Integer;
|
|
|
|
begin
|
|
|
|
GetStartupInfo( si); //set startupinfo for the spawned process
|
|
|
|
|
|
|
|
// preformat handles args
|
|
|
|
sHandles := Format( '%d %d',
|
|
|
|
[ Integer(transport.ClientAnonRead),
|
|
|
|
Integer(transport.ClientAnonWrite)]);
|
|
|
|
|
|
|
|
// pass all settings to client
|
|
|
|
sCmdLine := app;
|
|
|
|
for i := 1 to ParamCount do begin
|
|
|
|
sArg := ParamStr(i);
|
|
|
|
|
|
|
|
// add anonymous handles and quote strings where appropriate
|
|
|
|
if sArg = '-anon'
|
|
|
|
then sArg := sArg +' '+ sHandles
|
|
|
|
else begin
|
|
|
|
if Pos(' ',sArg) > 0
|
|
|
|
then sArg := '"'+sArg+'"';
|
|
|
|
end;;
|
|
|
|
|
|
|
|
sCmdLine := sCmdLine +' '+ sArg;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// spawn the child process
|
|
|
|
Console.WriteLine('Starting client '+sCmdLine);
|
|
|
|
Win32Check( CreateProcess( nil, PChar(sCmdLine), nil,nil,TRUE,0,nil,nil,si,pi));
|
|
|
|
|
|
|
|
CloseHandle( pi.hThread);
|
2014-10-03 17:50:38 +00:00
|
|
|
CloseHandle( pi.hProcess);
|
2012-10-06 06:58:00 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2012-01-08 21:51:08 +00:00
|
|
|
class procedure TTestServer.Execute( const args: array of string);
|
2011-11-10 20:32:44 +00:00
|
|
|
var
|
|
|
|
Port : Integer;
|
2014-09-23 22:26:46 +00:00
|
|
|
ServerEvents : Boolean;
|
2012-10-06 06:58:00 +00:00
|
|
|
sPipeName : string;
|
2011-11-10 20:32:44 +00:00
|
|
|
testHandler : ITestHandler;
|
|
|
|
testProcessor : IProcessor;
|
2012-10-06 06:58:00 +00:00
|
|
|
ServerTrans : IServerTransport;
|
2011-11-10 20:32:44 +00:00
|
|
|
ServerEngine : IServer;
|
2013-03-27 18:26:25 +00:00
|
|
|
anonymouspipe : IAnonymousPipeServerTransport;
|
|
|
|
namedpipe : INamedPipeServerTransport;
|
2011-11-10 20:32:44 +00:00
|
|
|
TransportFactory : ITransportFactory;
|
|
|
|
ProtocolFactory : IProtocolFactory;
|
2014-09-23 22:26:46 +00:00
|
|
|
i, numWorker : Integer;
|
2011-11-10 20:32:44 +00:00
|
|
|
s : string;
|
2014-09-23 22:26:46 +00:00
|
|
|
protType : TKnownProtocol;
|
|
|
|
servertype : TServerType;
|
|
|
|
endpoint : TEndpointTransport;
|
|
|
|
layered : TLayeredTransports;
|
|
|
|
UseSSL : Boolean; // include where appropriate (TLayeredTransport?)
|
2011-11-10 20:32:44 +00:00
|
|
|
begin
|
|
|
|
try
|
2013-09-25 19:12:21 +00:00
|
|
|
ServerEvents := FALSE;
|
2011-11-10 20:32:44 +00:00
|
|
|
protType := prot_Binary;
|
2014-09-23 22:26:46 +00:00
|
|
|
servertype := srv_Simple;
|
|
|
|
endpoint := trns_Sockets;
|
|
|
|
layered := [];
|
|
|
|
UseSSL := FALSE;
|
2011-11-10 20:32:44 +00:00
|
|
|
Port := 9090;
|
2012-10-06 06:58:00 +00:00
|
|
|
sPipeName := '';
|
2014-09-23 22:26:46 +00:00
|
|
|
numWorker := 4;
|
2011-11-10 20:32:44 +00:00
|
|
|
|
|
|
|
i := 0;
|
|
|
|
while ( i < Length(args) ) do begin
|
|
|
|
s := args[i];
|
|
|
|
Inc(i);
|
|
|
|
|
2014-09-23 22:26:46 +00:00
|
|
|
// Allowed options:
|
|
|
|
if (s = '-h') or (s = '--help') then begin
|
|
|
|
// -h [ --help ] produce help message
|
|
|
|
PrintCmdLineHelp;
|
|
|
|
Exit;
|
2012-10-06 06:58:00 +00:00
|
|
|
end
|
2014-09-23 22:26:46 +00:00
|
|
|
else if (s = '--port') then begin
|
|
|
|
// --port arg (=9090) Port number to listen
|
|
|
|
s := args[i];
|
|
|
|
Inc(i);
|
|
|
|
Port := StrToIntDef( s, Port);
|
2012-10-06 06:58:00 +00:00
|
|
|
end
|
2014-09-23 22:26:46 +00:00
|
|
|
else if (s = '--domain-socket') then begin
|
|
|
|
// --domain-socket arg Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)
|
|
|
|
raise Exception.Create('domain-socket not supported');
|
2012-10-06 06:58:00 +00:00
|
|
|
end
|
2014-09-23 22:26:46 +00:00
|
|
|
else if (s = '--named-pipe') then begin
|
|
|
|
// --named-pipe arg Windows Named Pipe (e.g. MyThriftPipe)
|
|
|
|
endpoint := trns_NamedPipes;
|
2012-10-06 06:58:00 +00:00
|
|
|
sPipeName := args[i]; // -pipe <name>
|
|
|
|
Inc( i );
|
|
|
|
end
|
2014-09-23 22:26:46 +00:00
|
|
|
else if (s = '--server-type') then begin
|
|
|
|
// --server-type arg (=simple) type of server,
|
|
|
|
// arg = "simple", "thread-pool", "threaded", or "nonblocking"
|
|
|
|
s := args[i];
|
|
|
|
Inc(i);
|
|
|
|
|
|
|
|
if s = 'simple' then servertype := srv_Simple
|
|
|
|
else if s = 'thread-pool' then servertype := srv_Threadpool
|
|
|
|
else if s = 'threaded' then servertype := srv_Threaded
|
|
|
|
else if s = 'nonblocking' then servertype := srv_Nonblocking
|
|
|
|
else InvalidArgs;
|
2012-10-06 06:58:00 +00:00
|
|
|
end
|
2014-09-23 22:26:46 +00:00
|
|
|
else if (s = '--transport') then begin
|
|
|
|
// --transport arg (=buffered) transport: buffered, framed, http
|
2011-11-10 20:32:44 +00:00
|
|
|
s := args[i];
|
2014-09-23 22:26:46 +00:00
|
|
|
Inc(i);
|
|
|
|
|
|
|
|
if s = 'buffered' then Include( layered, trns_Buffered)
|
|
|
|
else if s = 'framed' then Include( layered, trns_Framed)
|
|
|
|
else if s = 'http' then endpoint := trns_Http
|
|
|
|
else if s = 'anonpipe' then endpoint := trns_AnonPipes
|
|
|
|
else InvalidArgs;
|
|
|
|
end
|
|
|
|
else if (s = '--protocol') then begin
|
|
|
|
// --protocol arg (=binary) protocol: binary, compact, json
|
|
|
|
s := args[i];
|
|
|
|
Inc(i);
|
|
|
|
|
|
|
|
if s = 'binary' then protType := prot_Binary
|
|
|
|
else if s = 'compact' then protType := prot_Compact
|
|
|
|
else if s = 'json' then protType := prot_JSON
|
|
|
|
else InvalidArgs;
|
2013-09-25 19:12:21 +00:00
|
|
|
end
|
2014-09-23 22:26:46 +00:00
|
|
|
else if (s = '--ssl') then begin
|
|
|
|
// --ssl Encrypted Transport using SSL
|
|
|
|
UseSSL := TRUE;
|
2013-09-25 19:12:21 +00:00
|
|
|
end
|
2014-09-23 22:26:46 +00:00
|
|
|
else if (s = '--processor-events') then begin
|
|
|
|
// --processor-events processor-events
|
|
|
|
ServerEvents := TRUE;
|
2011-11-10 20:32:44 +00:00
|
|
|
end
|
2014-09-23 22:26:46 +00:00
|
|
|
else if (s = '-n') or (s = '--workers') then begin
|
|
|
|
// -n [ --workers ] arg (=4) Number of thread pools workers.
|
|
|
|
// Only valid for thread-pool server type
|
|
|
|
s := args[i];
|
|
|
|
numWorker := StrToIntDef(s,0);
|
|
|
|
if numWorker > 0
|
|
|
|
then Inc(i)
|
|
|
|
else numWorker := 4;
|
|
|
|
end
|
|
|
|
else begin
|
|
|
|
InvalidArgs;
|
|
|
|
end;
|
2011-11-10 20:32:44 +00:00
|
|
|
end;
|
|
|
|
|
2012-10-06 06:58:00 +00:00
|
|
|
|
|
|
|
Console.WriteLine('Server configuration: ');
|
|
|
|
|
2011-11-10 20:32:44 +00:00
|
|
|
// create protocol factory, default to BinaryProtocol
|
|
|
|
case protType of
|
2014-09-23 22:26:46 +00:00
|
|
|
prot_Binary : ProtocolFactory := TBinaryProtocolImpl.TFactory.Create( BINARY_STRICT_READ, BINARY_STRICT_WRITE);
|
|
|
|
prot_JSON : ProtocolFactory := TJSONProtocolImpl.TFactory.Create;
|
2015-03-01 17:47:49 +00:00
|
|
|
prot_Compact : ProtocolFactory := TCompactProtocolImpl.TFactory.Create;
|
2011-11-10 20:32:44 +00:00
|
|
|
else
|
2014-09-23 22:26:46 +00:00
|
|
|
raise Exception.Create('Unhandled protocol');
|
2011-11-10 20:32:44 +00:00
|
|
|
end;
|
2012-10-06 06:58:00 +00:00
|
|
|
ASSERT( ProtocolFactory <> nil);
|
2014-09-23 22:26:46 +00:00
|
|
|
Console.WriteLine('- '+THRIFT_PROTOCOLS[protType]+' protocol');
|
2012-10-06 06:58:00 +00:00
|
|
|
|
2014-09-23 22:26:46 +00:00
|
|
|
case endpoint of
|
2012-10-06 06:58:00 +00:00
|
|
|
|
2014-09-23 22:26:46 +00:00
|
|
|
trns_Sockets : begin
|
|
|
|
Console.WriteLine('- sockets (port '+IntToStr(port)+')');
|
|
|
|
if (trns_Buffered in layered) then Console.WriteLine('- buffered');
|
|
|
|
servertrans := TServerSocketImpl.Create( Port, 0, (trns_Buffered in layered));
|
|
|
|
end;
|
|
|
|
|
|
|
|
trns_Http : begin
|
2016-04-14 19:37:11 +00:00
|
|
|
raise Exception.Create(ENDPOINT_TRANSPORTS[endpoint]+' server transport not implemented');
|
2014-09-23 22:26:46 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
trns_NamedPipes : begin
|
|
|
|
Console.WriteLine('- named pipe ('+sPipeName+')');
|
2015-02-26 18:38:53 +00:00
|
|
|
namedpipe := TNamedPipeServerTransportImpl.Create( sPipeName, 4096, PIPE_UNLIMITED_INSTANCES);
|
2014-09-23 22:26:46 +00:00
|
|
|
servertrans := namedpipe;
|
|
|
|
end;
|
|
|
|
|
|
|
|
trns_AnonPipes : begin
|
|
|
|
Console.WriteLine('- anonymous pipes');
|
|
|
|
anonymouspipe := TAnonymousPipeServerTransportImpl.Create;
|
|
|
|
servertrans := anonymouspipe;
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
raise Exception.Create('Unhandled endpoint transport');
|
2012-10-06 06:58:00 +00:00
|
|
|
end;
|
|
|
|
ASSERT( servertrans <> nil);
|
|
|
|
|
2014-09-23 22:26:46 +00:00
|
|
|
if UseSSL then begin
|
|
|
|
raise Exception.Create('SSL not implemented');
|
|
|
|
end;
|
|
|
|
|
|
|
|
if (trns_Framed in layered) then begin
|
2012-10-06 06:58:00 +00:00
|
|
|
Console.WriteLine('- framed transport');
|
|
|
|
TransportFactory := TFramedTransportImpl.TFactory.Create
|
|
|
|
end
|
|
|
|
else begin
|
|
|
|
TransportFactory := TTransportFactoryImpl.Create;
|
|
|
|
end;
|
|
|
|
ASSERT( TransportFactory <> nil);
|
2011-11-10 20:32:44 +00:00
|
|
|
|
2012-10-06 06:58:00 +00:00
|
|
|
testHandler := TTestHandlerImpl.Create;
|
2011-11-10 20:32:44 +00:00
|
|
|
testProcessor := TThriftTest.TProcessorImpl.Create( testHandler );
|
|
|
|
|
2014-09-23 22:26:46 +00:00
|
|
|
case servertype of
|
|
|
|
srv_Simple : begin
|
|
|
|
ServerEngine := TSimpleServer.Create( testProcessor, ServerTrans, TransportFactory, ProtocolFactory);
|
|
|
|
end;
|
|
|
|
|
|
|
|
srv_Nonblocking : begin
|
|
|
|
raise Exception.Create(SERVER_TYPES[servertype]+' server not implemented');
|
|
|
|
end;
|
|
|
|
|
|
|
|
srv_Threadpool,
|
|
|
|
srv_Threaded: begin
|
|
|
|
if numWorker > 1 then {use here};
|
|
|
|
raise Exception.Create(SERVER_TYPES[servertype]+' server not implemented');
|
|
|
|
end;
|
|
|
|
|
|
|
|
else
|
|
|
|
raise Exception.Create('Unhandled server type');
|
|
|
|
end;
|
|
|
|
ASSERT( ServerEngine <> nil);
|
2011-11-10 20:32:44 +00:00
|
|
|
|
|
|
|
testHandler.SetServer( ServerEngine);
|
|
|
|
|
2013-09-25 19:12:21 +00:00
|
|
|
// test events?
|
|
|
|
if ServerEvents then begin
|
|
|
|
Console.WriteLine('- server events test enabled');
|
|
|
|
ServerEngine.ServerEvents := TServerEventsImpl.Create;
|
|
|
|
end;
|
|
|
|
|
2012-10-06 06:58:00 +00:00
|
|
|
// start the client now when we have the anon handles, but before the server starts
|
2014-09-23 22:26:46 +00:00
|
|
|
if endpoint = trns_AnonPipes
|
2012-10-20 20:59:41 +00:00
|
|
|
then LaunchAnonPipeChild( ExtractFilePath(ParamStr(0))+'client.exe', anonymouspipe);
|
2012-10-06 06:58:00 +00:00
|
|
|
|
2013-03-27 18:26:25 +00:00
|
|
|
// install Ctrl+C handler before the server starts
|
|
|
|
g_Handler := testHandler;
|
|
|
|
SetConsoleCtrlHandler( @MyConsoleEventHandler, TRUE);
|
2011-11-10 20:32:44 +00:00
|
|
|
|
2012-10-06 06:58:00 +00:00
|
|
|
Console.WriteLine('');
|
2013-03-27 18:26:25 +00:00
|
|
|
repeat
|
|
|
|
Console.WriteLine('Starting the server ...');
|
|
|
|
serverEngine.Serve;
|
|
|
|
until {$IFDEF RunEndless} FALSE {$ELSE} TRUE {$ENDIF};
|
|
|
|
|
2011-11-10 20:32:44 +00:00
|
|
|
testHandler.SetServer( nil);
|
2013-03-27 18:26:25 +00:00
|
|
|
g_Handler := nil;
|
2011-11-10 20:32:44 +00:00
|
|
|
|
|
|
|
except
|
2014-09-23 22:26:46 +00:00
|
|
|
on E: EAbort do raise;
|
|
|
|
on E: Exception do begin
|
|
|
|
Console.WriteLine( E.Message + #10 + E.StackTrace );
|
2011-11-10 20:32:44 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
Console.WriteLine( 'done.');
|
|
|
|
end;
|
|
|
|
|
2013-03-27 18:26:25 +00:00
|
|
|
|
2011-11-10 20:32:44 +00:00
|
|
|
end.
|