Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [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 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 | |
| 26 | interface |
| 27 | |
| 28 | uses |
| 29 | Windows, SysUtils, |
| 30 | Generics.Collections, |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 31 | 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 Geyer | 3d55624 | 2018-01-24 19:14:32 +0100 | [diff] [blame] | 43 | ConsoleHelper, |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 44 | Contnrs; |
| 45 | |
| 46 | type |
| 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 Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 75 | |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 76 | // TAggr.Iface |
| 77 | procedure addValue(value: Integer); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 78 | function getValues(): IThriftList<Integer>; |
| 79 | public |
| 80 | constructor Create; |
| 81 | destructor Destroy; override; |
| 82 | end; |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 83 | |
| 84 | public |
| 85 | class procedure Execute( const args: array of string); |
| 86 | end; |
| 87 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 88 | |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 89 | implementation |
| 90 | |
| 91 | |
| 92 | { TTestServer.TTestHandlerImpl } |
| 93 | |
| 94 | procedure TTestServer.TTestHandlerImpl.SetServer( const AServer: IServer); |
| 95 | begin |
| 96 | FServer := AServer; |
| 97 | end; |
| 98 | |
| 99 | |
| 100 | { TTestServer.TBenchmarkServiceImpl } |
| 101 | |
| 102 | function TTestServer.TBenchmarkServiceImpl.fibonacci(n: ShortInt): Integer; |
| 103 | var prev, next : Integer; |
| 104 | begin |
| 105 | prev := 0; |
| 106 | result := 1; |
| 107 | while n > 0 do begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 108 | next := result + prev; |
| 109 | prev := result; |
| 110 | result := next; |
| 111 | Dec(n); |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 112 | end; |
| 113 | end; |
| 114 | |
| 115 | { TTestServer.TAggrImpl } |
| 116 | |
| 117 | constructor TTestServer.TAggrImpl.Create; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 118 | begin |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 119 | inherited Create; |
| 120 | FList := TThriftListImpl<Integer>.Create; |
| 121 | end; |
| 122 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 123 | |
| 124 | destructor TTestServer.TAggrImpl.Destroy; |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 125 | begin |
| 126 | try |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 127 | FreeAndNil( FList); |
| 128 | finally |
| 129 | inherited Destroy; |
| 130 | end; |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 131 | end; |
| 132 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 133 | |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 134 | procedure TTestServer.TAggrImpl.addValue(value: Integer); |
| 135 | begin |
| 136 | FList.Add( value); |
| 137 | end; |
| 138 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 139 | |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 140 | function TTestServer.TAggrImpl.getValues(): IThriftList<Integer>; |
| 141 | begin |
| 142 | result := FList; |
| 143 | end; |
| 144 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 145 | |
| 146 | { TTestServer } |
Jens Geyer | 8a70196 | 2013-03-25 01:28:12 +0200 | [diff] [blame] | 147 | |
| 148 | class procedure TTestServer.Execute( const args: array of string); |
| 149 | var |
| 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; |
| 159 | begin |
| 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.'); |
| 197 | end; |
| 198 | |
| 199 | |
| 200 | end. |
| 201 | |