blob: a23ff37ea8a07c9f8ac87a6f3a1d35ca6df53a75 [file] [log] [blame]
Jens Geyer8a701962013-03-25 01:28:12 +02001(*
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 Multiplex.Server.Main;
21
22{$WARN SYMBOL_PLATFORM OFF}
23
24{.$DEFINE RunEndless} // activate to interactively stress-test the server stop routines via Ctrl+C
25
26interface
27
28uses
29 Windows, SysUtils,
30 Generics.Collections,
Jens Geyer8a701962013-03-25 01:28:12 +020031 Thrift.Server,
32 Thrift.Transport,
33 Thrift.Transport.Pipes,
34 Thrift.Protocol,
35 Thrift.Protocol.Multiplex,
36 Thrift.Processor.Multiplex,
37 Thrift.Collections,
Jens Geyera019cda2019-11-09 23:24:52 +010038 Thrift.Configuration,
Jens Geyer8a701962013-03-25 01:28:12 +020039 Thrift.Utils,
40 Thrift,
41 Benchmark, // in gen-delphi folder
42 Aggr, // in gen-delphi folder
43 Multiplex.Test.Common,
Jens Geyer3d556242018-01-24 19:14:32 +010044 ConsoleHelper,
Jens Geyer8a701962013-03-25 01:28:12 +020045 Contnrs;
46
47type
48 TTestServer = class
49 public type
50 ITestHandler = interface
51 ['{CAE09AAB-80FB-48E9-B3A8-7F9B96F5419A}']
52 procedure SetServer( const AServer : IServer );
53 end;
54
55 protected type
56 TTestHandlerImpl = class( TInterfacedObject, ITestHandler)
57 private
58 FServer : IServer;
59 protected
60 // ITestHandler
61 procedure SetServer( const AServer : IServer );
62
63 property Server : IServer read FServer write SetServer;
64 end;
65
66 TBenchmarkServiceImpl = class( TTestHandlerImpl, TBenchmarkService.Iface)
67 protected
68 // TBenchmarkService.Iface
69 function fibonacci(n: ShortInt): Integer;
70 end;
71
72
73 TAggrImpl = class( TTestHandlerImpl, TAggr.Iface)
74 protected
75 FList : IThriftList<Integer>;
Jens Geyerd5436f52014-10-03 19:50:38 +020076
Jens Geyer8a701962013-03-25 01:28:12 +020077 // TAggr.Iface
78 procedure addValue(value: Integer);
Jens Geyerd5436f52014-10-03 19:50:38 +020079 function getValues(): IThriftList<Integer>;
80 public
81 constructor Create;
82 destructor Destroy; override;
83 end;
Jens Geyer8a701962013-03-25 01:28:12 +020084
85 public
86 class procedure Execute( const args: array of string);
87 end;
88
Jens Geyerd5436f52014-10-03 19:50:38 +020089
Jens Geyer8a701962013-03-25 01:28:12 +020090implementation
91
92
93{ TTestServer.TTestHandlerImpl }
94
95procedure TTestServer.TTestHandlerImpl.SetServer( const AServer: IServer);
96begin
97 FServer := AServer;
98end;
99
100
101{ TTestServer.TBenchmarkServiceImpl }
102
103function TTestServer.TBenchmarkServiceImpl.fibonacci(n: ShortInt): Integer;
104var prev, next : Integer;
105begin
106 prev := 0;
107 result := 1;
108 while n > 0 do begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200109 next := result + prev;
110 prev := result;
111 result := next;
112 Dec(n);
Jens Geyer8a701962013-03-25 01:28:12 +0200113 end;
114end;
115
116{ TTestServer.TAggrImpl }
117
118constructor TTestServer.TAggrImpl.Create;
Jens Geyerd5436f52014-10-03 19:50:38 +0200119begin
Jens Geyer8a701962013-03-25 01:28:12 +0200120 inherited Create;
121 FList := TThriftListImpl<Integer>.Create;
122end;
123
Jens Geyerd5436f52014-10-03 19:50:38 +0200124
125destructor TTestServer.TAggrImpl.Destroy;
Jens Geyer8a701962013-03-25 01:28:12 +0200126begin
127 try
Jens Geyerd5436f52014-10-03 19:50:38 +0200128 FreeAndNil( FList);
129 finally
130 inherited Destroy;
131 end;
Jens Geyer8a701962013-03-25 01:28:12 +0200132end;
133
Jens Geyerd5436f52014-10-03 19:50:38 +0200134
Jens Geyer8a701962013-03-25 01:28:12 +0200135procedure TTestServer.TAggrImpl.addValue(value: Integer);
136begin
137 FList.Add( value);
138end;
139
Jens Geyerd5436f52014-10-03 19:50:38 +0200140
Jens Geyer8a701962013-03-25 01:28:12 +0200141function TTestServer.TAggrImpl.getValues(): IThriftList<Integer>;
142begin
143 result := FList;
144end;
145
Jens Geyerd5436f52014-10-03 19:50:38 +0200146
147{ TTestServer }
Jens Geyer8a701962013-03-25 01:28:12 +0200148
149class procedure TTestServer.Execute( const args: array of string);
150var
151 TransportFactory : ITransportFactory;
152 ProtocolFactory : IProtocolFactory;
153 ServerTrans : IServerTransport;
154 benchHandler : TBenchmarkService.Iface;
155 aggrHandler : TAggr.Iface;
156 benchProcessor : IProcessor;
157 aggrProcessor : IProcessor;
158 multiplex : IMultiplexedProcessor;
159 ServerEngine : IServer;
Jens Geyera019cda2019-11-09 23:24:52 +0100160 config : IThriftConfiguration;
Jens Geyer8a701962013-03-25 01:28:12 +0200161begin
162 try
Jens Geyera019cda2019-11-09 23:24:52 +0100163 config := TThriftConfigurationImpl.Create;
164
Jens Geyer8a701962013-03-25 01:28:12 +0200165 // create protocol factory, default to BinaryProtocol
166 ProtocolFactory := TBinaryProtocolImpl.TFactory.Create( TRUE, TRUE);
Jens Geyera019cda2019-11-09 23:24:52 +0100167 servertrans := TServerSocketImpl.Create( 9090, DEFAULT_THRIFT_TIMEOUT, FALSE, config);
Jens Geyer8a701962013-03-25 01:28:12 +0200168 TransportFactory := TFramedTransportImpl.TFactory.Create;
169
170 benchHandler := TBenchmarkServiceImpl.Create;
171 benchProcessor := TBenchmarkService.TProcessorImpl.Create( benchHandler);
172
173 aggrHandler := TAggrImpl.Create;
174 aggrProcessor := TAggr.TProcessorImpl.Create( aggrHandler);
175
176 multiplex := TMultiplexedProcessorImpl.Create;
177 multiplex.RegisterProcessor( NAME_BENCHMARKSERVICE, benchProcessor);
178 multiplex.RegisterProcessor( NAME_AGGR, aggrProcessor);
179
180 ServerEngine := TSimpleServer.Create( multiplex,
181 ServerTrans,
182 TransportFactory,
183 ProtocolFactory);
184
185 (benchHandler as ITestHandler).SetServer( ServerEngine);
186 (aggrHandler as ITestHandler).SetServer( ServerEngine);
187
188 Console.WriteLine('Starting the server ...');
189 ServerEngine.serve();
190
191 (benchHandler as ITestHandler).SetServer( nil);
192 (aggrHandler as ITestHandler).SetServer( nil);
193
194 except
195 on E: Exception do
196 begin
197 Console.Write( E.Message);
198 end;
199 end;
200 Console.WriteLine( 'done.');
201end;
202
203
204end.
205