blob: 67cce776dfcd2184af70b5658ee7afd8420ae490 [file] [log] [blame]
Jake Farrell7ae13e12011-10-18 14:35:26 +00001(*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19
20unit TestServer;
21
22interface
23
24uses
25 SysUtils,
26 Generics.Collections,
27 Thrift.Console,
28 Thrift.Server,
29 Thrift.Transport,
30 Thrift.Collections,
31 Thrift.Utils,
32 Thrift.Test,
33 Thrift,
34 Contnrs;
35
36type
37 TTestServer = class
38 public
39 type
40
41 ITestHandler = interface( TThriftTest.Iface )
42 procedure SetServer( AServer : IServer );
43 end;
44
45 TTestHandlerImpl = class( TInterfacedObject, ITestHandler )
46 private
47 FServer : IServer;
48 protected
49 procedure testVoid();
50 function testString(thing: string): string;
51 function testByte(thing: ShortInt): ShortInt;
52 function testI32(thing: Integer): Integer;
53 function testI64(thing: Int64): Int64;
54 function testDouble(thing: Double): Double;
55 function testStruct(thing: IXtruct): IXtruct;
56 function testNest(thing: IXtruct2): IXtruct2;
57 function testMap(thing: IThriftDictionary<Integer, Integer>): IThriftDictionary<Integer, Integer>;
58 function testStringMap(thing: IThriftDictionary<string, string>): IThriftDictionary<string, string>;
59 function testSet(thing: IHashSet<Integer>): IHashSet<Integer>;
60 function testList(thing: IThriftList<Integer>): IThriftList<Integer>;
61 function testEnum(thing: TNumberz): TNumberz;
62 function testTypedef(thing: Int64): Int64;
63 function testMapMap(hello: Integer): IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
64 function testInsanity(argument: IInsanity): IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
65 function testMulti(arg0: ShortInt; arg1: Integer; arg2: Int64; arg3: IThriftDictionary<SmallInt, string>; arg4: TNumberz; arg5: Int64): IXtruct;
66 procedure testException(arg: string);
67 function testMultiException(arg0: string; arg1: string): IXtruct;
68 procedure testOneway(secondsToSleep: Integer);
69
70 procedure testStop;
71
72 procedure SetServer( AServer : IServer );
73 end;
74
75 class procedure Execute( args: array of string);
76 end;
77
78implementation
79
80{ TTestServer.TTestHandlerImpl }
81
82procedure TTestServer.TTestHandlerImpl.SetServer(AServer: IServer);
83begin
84 FServer := AServer;
85end;
86
87function TTestServer.TTestHandlerImpl.testByte(thing: ShortInt): ShortInt;
88begin
89 Console.WriteLine('testByte("' + IntToStr( thing) + '")');
90 Result := thing;
91end;
92
93function TTestServer.TTestHandlerImpl.testDouble(thing: Double): Double;
94begin
95 Console.WriteLine('testDouble("' + FloatToStr( thing ) + '")');
96 Result := thing;
97end;
98
99function TTestServer.TTestHandlerImpl.testEnum(thing: TNumberz): TNumberz;
100begin
101 Console.WriteLine('testEnum(' + IntToStr( Integer( thing)) + ')');
102 Result := thing;
103end;
104
105procedure TTestServer.TTestHandlerImpl.testException(arg: string);
106var
107 x : TXception;
108begin
109 Console.WriteLine('testException(' + arg + ')');
110 if ( arg = 'Xception') then
111 begin
112 x := TXception.Create;
113 x.ErrorCode := 1001;
114 x.Message_ := 'This is an Xception';
115 raise x;
116 end;
117end;
118
119function TTestServer.TTestHandlerImpl.testI32(thing: Integer): Integer;
120begin
121 Console.WriteLine('testI32("' + IntToStr( thing) + '")');
122 Result := thing;
123end;
124
125function TTestServer.TTestHandlerImpl.testI64(thing: Int64): Int64;
126begin
127 Console.WriteLine('testI64("' + IntToStr( thing) + '")');
128 Result := thing;
129end;
130
131function TTestServer.TTestHandlerImpl.testInsanity(
132 argument: IInsanity): IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
133var
134 hello, goodbye : IXtruct;
135 crazy : IInsanity;
136 looney : IInsanity;
137 first_map : IThriftDictionary<TNumberz, IInsanity>;
138 second_map : IThriftDictionary<TNumberz, IInsanity>;
139 insane : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
140
141begin
142
143 Console.WriteLine('testInsanity()');
144 hello := TXtructImpl.Create;
145 hello.String_thing := 'hello';
146 hello.Byte_thing := 2;
147 hello.I32_thing := 2;
148 hello.I64_thing := 2;
149
150 goodbye := TXtructImpl.Create;
151 goodbye.String_thing := 'Goodbye4';
152 goodbye.Byte_thing := 4;
153 goodbye.I32_thing := 4;
154 goodbye.I64_thing := 4;
155
156 crazy := TInsanityImpl.Create;
157 crazy.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
158 crazy.UserMap.AddOrSetValue( TNumberz.EIGHT, 8);
159 crazy.Xtructs := TThriftListImpl<IXtruct>.Create;
160 crazy.Xtructs.Add(goodbye);
161
162 looney := TInsanityImpl.Create;
163 crazy.UserMap.AddOrSetValue( TNumberz.FIVE, 5);
164 crazy.Xtructs.Add(hello);
165
166 first_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create;
167 second_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create;
168
169 first_map.AddOrSetValue( TNumberz.SIX, crazy);
170 first_map.AddOrSetValue( TNumberz.THREE, crazy);
171
172 second_map.AddOrSetValue( TNumberz.SIX, looney);
173
174 insane := TThriftDictionaryImpl<Int64, IThriftDictionary<TNumberz, IInsanity>>.Create;
175
176 insane.AddOrSetValue( 1, first_map);
177 insane.AddOrSetValue( 2, second_map);
178
179 Result := insane;
180end;
181
182function TTestServer.TTestHandlerImpl.testList(
183 thing: IThriftList<Integer>): IThriftList<Integer>;
184var
185 first : Boolean;
186 elem : Integer;
187begin
188 Console.Write('testList({');
189 first := True;
190 for elem in thing do
191 begin
192 if first then
193 begin
194 first := False;
195 end else
196 begin
197 Console.Write(', ');
198 end;
199 Console.Write( IntToStr( elem));
200 end;
201 Console.WriteLine('})');
202 Result := thing;
203end;
204
205function TTestServer.TTestHandlerImpl.testMap(
206 thing: IThriftDictionary<Integer, Integer>): IThriftDictionary<Integer, Integer>;
207var
208 first : Boolean;
209 key : Integer;
210begin
211 Console.Write('testMap({');
212 first := True;
213 for key in thing.Keys do
214 begin
215 if (first) then
216 begin
217 first := false;
218 end else
219 begin
220 Console.Write(', ');
221 end;
222 Console.Write(IntToStr(key) + ' => ' + IntToStr( thing[key]));
223 end;
224 Console.WriteLine('})');
225 Result := thing;
226end;
227
228function TTestServer.TTestHandlerImpl.TestMapMap(
229 hello: Integer): IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
230var
231 mapmap : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
232 pos : IThriftDictionary<Integer, Integer>;
233 neg : IThriftDictionary<Integer, Integer>;
234 i : Integer;
235begin
236 Console.WriteLine('testMapMap(' + IntToStr( hello) + ')');
237 mapmap := TThriftDictionaryImpl<Integer, IThriftDictionary<Integer, Integer>>.Create;
238 pos := TThriftDictionaryImpl<Integer, Integer>.Create;
239 neg := TThriftDictionaryImpl<Integer, Integer>.Create;
240
241 for i := 1 to 4 do
242 begin
243 pos.AddOrSetValue( i, i);
244 neg.AddOrSetValue( -i, -i);
245 end;
246
247 mapmap.AddOrSetValue(4, pos);
248 mapmap.AddOrSetValue( -4, neg);
249
250 Result := mapmap;
251end;
252
253function TTestServer.TTestHandlerImpl.testMulti(arg0: ShortInt; arg1: Integer;
254 arg2: Int64; arg3: IThriftDictionary<SmallInt, string>; arg4: TNumberz;
255 arg5: Int64): IXtruct;
256var
257 hello : IXtruct;
258begin
259 Console.WriteLine('testMulti()');
260 hello := TXtructImpl.Create;
261 hello.String_thing := 'Hello2';
262 hello.Byte_thing := arg0;
263 hello.I32_thing := arg1;
264 hello.I64_thing := arg2;
265 Result := hello;
266end;
267
268function TTestServer.TTestHandlerImpl.testMultiException(arg0,
269 arg1: string): IXtruct;
270var
271 x : TXception;
272 x2 : TXception2;
273begin
274 Console.WriteLine('testMultiException(' + arg0 + ', ' + arg1 + ')');
275 if ( arg0 = 'Xception') then
276 begin
277 x := TXception.Create;
278 x.ErrorCode := 1001;
Jake Farrell73a921f2011-10-31 14:07:14 +0000279 x.Message_ := 'This is an Xception';
Jake Farrell7ae13e12011-10-18 14:35:26 +0000280 raise x;
281 end else
282 if ( arg0 = 'Xception2') then
283 begin
284 x2 := TXception2.Create;
285 x2.ErrorCode := 2002;
286 x2.Struct_thing := TXtructImpl.Create;
287 x2.Struct_thing.String_thing := 'This is an Xception2';
288 raise x2;
289 end;
290
291 Result := TXtructImpl.Create;
292 Result.String_thing := arg1;
293end;
294
295function TTestServer.TTestHandlerImpl.testNest(thing: IXtruct2): IXtruct2;
296var
297 temp : IXtruct;
298begin
299 temp := thing.Struct_thing;
300 Console.WriteLine('testNest({' +
301 IntToStr( thing.Byte_thing) + ', {' +
302 '"' + temp.String_thing + '", ' +
303 IntToStr( temp.Byte_thing) + ', ' +
304 IntToStr( temp.I32_thing) + ', ' +
305 IntToStr( temp.I64_thing) + '}, ' +
306 IntToStr( temp.I32_thing) + '})');
307 Result := thing;
308end;
309
310procedure TTestServer.TTestHandlerImpl.testOneway(secondsToSleep: Integer);
311begin
312 Console.WriteLine('testOneway(' + IntToStr( secondsToSleep )+ '), sleeping...');
313 Sleep(secondsToSleep * 1000);
314 Console.WriteLine('testOneway finished');
315end;
316
317function TTestServer.TTestHandlerImpl.testSet(
318 thing: IHashSet<Integer>):IHashSet<Integer>;
319var
320 first : Boolean;
321 elem : Integer;
322begin
323 Console.Write('testSet({');
324 first := True;
325
326 for elem in thing do
327 begin
328 if first then
329 begin
330 first := False;
331 end else
332 begin
333 Console.Write( ', ');
334 end;
335 Console.Write( IntToStr( elem));
336 end;
337 Console.WriteLine('})');
338 Result := thing;
339end;
340
341procedure TTestServer.TTestHandlerImpl.testStop;
342begin
343 if FServer <> nil then
344 begin
345 FServer.Stop;
346 end;
347end;
348
349function TTestServer.TTestHandlerImpl.testString(thing: string): string;
350begin
351 Console.WriteLine('teststring("' + thing + '")');
352 Result := thing;
353end;
354
355function TTestServer.TTestHandlerImpl.testStringMap(
356 thing: IThriftDictionary<string, string>): IThriftDictionary<string, string>;
357begin
358
359end;
360
361function TTestServer.TTestHandlerImpl.testTypedef(thing: Int64): Int64;
362begin
363 Console.WriteLine('testTypedef(' + IntToStr( thing) + ')');
364 Result := thing;
365end;
366
367procedure TTestServer.TTestHandlerImpl.TestVoid;
368begin
369 Console.WriteLine('testVoid()');
370end;
371
372function TTestServer.TTestHandlerImpl.testStruct(thing: IXtruct): IXtruct;
373begin
374 Console.WriteLine('testStruct({' +
375 '"' + thing.String_thing + '", ' +
376 IntToStr( thing.Byte_thing) + ', ' +
377 IntToStr( thing.I32_thing) + ', ' +
378 IntToStr( thing.I64_thing));
379 Result := thing;
380end;
381
382{ TTestServer }
383
384class procedure TTestServer.Execute(args: array of string);
385var
386 UseBufferedSockets : Boolean;
387 UseFramed : Boolean;
388 Port : Integer;
389 testHandler : ITestHandler;
390 testProcessor : IProcessor;
391 ServerSocket : IServerTransport;
392 ServerEngine : IServer;
393 TransportFactroy : ITransportFactory;
394
395
396begin
397 try
398 UseBufferedSockets := False;
399 UseFramed := False;
400 Port := 9090;
401
402 if ( Length( args) > 0) then
403 begin
404 Port := StrToIntDef( args[0], Port);
405
406 if ( Length( args) > 0) then
407 begin
408 if ( args[0] = 'raw' ) then
409 begin
410 // as default
411 end else
412 if ( args[0] = 'buffered' ) then
413 begin
414 UseBufferedSockets := True;
415 end else
416 if ( args[0] = 'framed' ) then
417 begin
418 UseFramed := True;
419 end else
420 begin
421 // Fall back to the older boolean syntax
422 UseBufferedSockets := StrToBoolDef( args[1], UseBufferedSockets);
423 end
424 end
425 end;
426
427 testHandler := TTestHandlerImpl.Create;
428
429 testProcessor := TThriftTest.TProcessorImpl.Create( testHandler );
430 ServerSocket := TServerSocketImpl.Create( Port, 0, UseBufferedSockets );
431 if UseFramed then
432 begin
433 TransportFactroy := TFramedTransportImpl.TFactory.Create;
434 ServerEngine := TSimpleServer.Create( testProcessor, ServerSocket,
435 TransportFactroy);
436 end else
437 begin
438 ServerEngine := TSimpleServer.Create( testProcessor, ServerSocket);
439 end;
440
441 testHandler.SetServer( ServerEngine);
442
443 Console.WriteLine('Starting the server on port ' + IntToStr( Port) +
444 IfValue(UseBufferedSockets, ' with buffered socket', '') +
445 IfValue(useFramed, ' with framed transport', '') +
446 '...');
447
448 serverEngine.Serve;
449 testHandler.SetServer( nil);
450
451 except
452 on E: Exception do
453 begin
454 Console.Write( E.Message);
455 end;
456 end;
457 Console.WriteLine( 'done.');
458end;
459
460end.