blob: 3860f5ace0b3403676b9f6beb8c990af88e791cc [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,
38 Thrift.Utils,
39 Thrift,
40 Benchmark, // in gen-delphi folder
41 Aggr, // in gen-delphi folder
42 Multiplex.Test.Common,
Jens Geyer3d556242018-01-24 19:14:32 +010043 ConsoleHelper,
Jens Geyer8a701962013-03-25 01:28:12 +020044 Contnrs;
45
46type
47 TTestServer = class
48 public type
49 ITestHandler = interface
50 ['{CAE09AAB-80FB-48E9-B3A8-7F9B96F5419A}']
51 procedure SetServer( const AServer : IServer );
52 end;
53
54 protected type
55 TTestHandlerImpl = class( TInterfacedObject, ITestHandler)
56 private
57 FServer : IServer;
58 protected
59 // ITestHandler
60 procedure SetServer( const AServer : IServer );
61
62 property Server : IServer read FServer write SetServer;
63 end;
64
65 TBenchmarkServiceImpl = class( TTestHandlerImpl, TBenchmarkService.Iface)
66 protected
67 // TBenchmarkService.Iface
68 function fibonacci(n: ShortInt): Integer;
69 end;
70
71
72 TAggrImpl = class( TTestHandlerImpl, TAggr.Iface)
73 protected
74 FList : IThriftList<Integer>;
Jens Geyerd5436f52014-10-03 19:50:38 +020075
Jens Geyer8a701962013-03-25 01:28:12 +020076 // TAggr.Iface
77 procedure addValue(value: Integer);
Jens Geyerd5436f52014-10-03 19:50:38 +020078 function getValues(): IThriftList<Integer>;
79 public
80 constructor Create;
81 destructor Destroy; override;
82 end;
Jens Geyer8a701962013-03-25 01:28:12 +020083
84 public
85 class procedure Execute( const args: array of string);
86 end;
87
Jens Geyerd5436f52014-10-03 19:50:38 +020088
Jens Geyer8a701962013-03-25 01:28:12 +020089implementation
90
91
92{ TTestServer.TTestHandlerImpl }
93
94procedure TTestServer.TTestHandlerImpl.SetServer( const AServer: IServer);
95begin
96 FServer := AServer;
97end;
98
99
100{ TTestServer.TBenchmarkServiceImpl }
101
102function TTestServer.TBenchmarkServiceImpl.fibonacci(n: ShortInt): Integer;
103var prev, next : Integer;
104begin
105 prev := 0;
106 result := 1;
107 while n > 0 do begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200108 next := result + prev;
109 prev := result;
110 result := next;
111 Dec(n);
Jens Geyer8a701962013-03-25 01:28:12 +0200112 end;
113end;
114
115{ TTestServer.TAggrImpl }
116
117constructor TTestServer.TAggrImpl.Create;
Jens Geyerd5436f52014-10-03 19:50:38 +0200118begin
Jens Geyer8a701962013-03-25 01:28:12 +0200119 inherited Create;
120 FList := TThriftListImpl<Integer>.Create;
121end;
122
Jens Geyerd5436f52014-10-03 19:50:38 +0200123
124destructor TTestServer.TAggrImpl.Destroy;
Jens Geyer8a701962013-03-25 01:28:12 +0200125begin
126 try
Jens Geyerd5436f52014-10-03 19:50:38 +0200127 FreeAndNil( FList);
128 finally
129 inherited Destroy;
130 end;
Jens Geyer8a701962013-03-25 01:28:12 +0200131end;
132
Jens Geyerd5436f52014-10-03 19:50:38 +0200133
Jens Geyer8a701962013-03-25 01:28:12 +0200134procedure TTestServer.TAggrImpl.addValue(value: Integer);
135begin
136 FList.Add( value);
137end;
138
Jens Geyerd5436f52014-10-03 19:50:38 +0200139
Jens Geyer8a701962013-03-25 01:28:12 +0200140function TTestServer.TAggrImpl.getValues(): IThriftList<Integer>;
141begin
142 result := FList;
143end;
144
Jens Geyerd5436f52014-10-03 19:50:38 +0200145
146{ TTestServer }
Jens Geyer8a701962013-03-25 01:28:12 +0200147
148class procedure TTestServer.Execute( const args: array of string);
149var
150 TransportFactory : ITransportFactory;
151 ProtocolFactory : IProtocolFactory;
152 ServerTrans : IServerTransport;
153 benchHandler : TBenchmarkService.Iface;
154 aggrHandler : TAggr.Iface;
155 benchProcessor : IProcessor;
156 aggrProcessor : IProcessor;
157 multiplex : IMultiplexedProcessor;
158 ServerEngine : IServer;
159begin
160 try
161 // create protocol factory, default to BinaryProtocol
162 ProtocolFactory := TBinaryProtocolImpl.TFactory.Create( TRUE, TRUE);
163 servertrans := TServerSocketImpl.Create( 9090, 0, FALSE);
164 TransportFactory := TFramedTransportImpl.TFactory.Create;
165
166 benchHandler := TBenchmarkServiceImpl.Create;
167 benchProcessor := TBenchmarkService.TProcessorImpl.Create( benchHandler);
168
169 aggrHandler := TAggrImpl.Create;
170 aggrProcessor := TAggr.TProcessorImpl.Create( aggrHandler);
171
172 multiplex := TMultiplexedProcessorImpl.Create;
173 multiplex.RegisterProcessor( NAME_BENCHMARKSERVICE, benchProcessor);
174 multiplex.RegisterProcessor( NAME_AGGR, aggrProcessor);
175
176 ServerEngine := TSimpleServer.Create( multiplex,
177 ServerTrans,
178 TransportFactory,
179 ProtocolFactory);
180
181 (benchHandler as ITestHandler).SetServer( ServerEngine);
182 (aggrHandler as ITestHandler).SetServer( ServerEngine);
183
184 Console.WriteLine('Starting the server ...');
185 ServerEngine.serve();
186
187 (benchHandler as ITestHandler).SetServer( nil);
188 (aggrHandler as ITestHandler).SetServer( nil);
189
190 except
191 on E: Exception do
192 begin
193 Console.Write( E.Message);
194 end;
195 end;
196 Console.WriteLine( 'done.');
197end;
198
199
200end.
201