Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [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 Thrift.Server; |
| 21 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 22 | {$I Thrift.Defines.inc} |
Jens Geyer | 26ef743 | 2013-09-23 22:01:20 +0200 | [diff] [blame] | 23 | {$I-} // prevent annoying errors with default log delegate and no console |
| 24 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 25 | interface |
| 26 | |
| 27 | uses |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 28 | {$IFDEF OLD_UNIT_NAMES} |
| 29 | Windows, SysUtils, |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame] | 30 | {$ELSE} |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 31 | Winapi.Windows, System.SysUtils, |
| 32 | {$ENDIF} |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 33 | Thrift, |
| 34 | Thrift.Protocol, |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 35 | Thrift.Transport, |
| 36 | Thrift.Configuration; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 37 | |
| 38 | type |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 39 | IServerEvents = interface |
| 40 | ['{9E2A99C5-EE85-40B2-9A52-2D1722B18176}'] |
| 41 | // Called before the server begins. |
| 42 | procedure PreServe; |
| 43 | // Called when the server transport is ready to accept requests |
| 44 | procedure PreAccept; |
| 45 | // Called when a new client has connected and the server is about to being processing. |
| 46 | function CreateProcessingContext( const input, output : IProtocol) : IProcessorEvents; |
| 47 | end; |
| 48 | |
| 49 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 50 | IServer = interface |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 51 | ['{ADC46F2D-8199-4D1C-96D2-87FD54351723}'] |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 52 | procedure Serve; |
| 53 | procedure Stop; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 54 | |
| 55 | function GetServerEvents : IServerEvents; |
| 56 | procedure SetServerEvents( const value : IServerEvents); |
| 57 | |
| 58 | property ServerEvents : IServerEvents read GetServerEvents write SetServerEvents; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 59 | end; |
| 60 | |
| 61 | TServerImpl = class abstract( TInterfacedObject, IServer ) |
| 62 | public |
| 63 | type |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 64 | TLogDelegate = reference to procedure( const str: string); |
Jens Geyer | ed99455 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 65 | strict protected |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 66 | FProcessor : IProcessor; |
| 67 | FServerTransport : IServerTransport; |
| 68 | FInputTransportFactory : ITransportFactory; |
| 69 | FOutputTransportFactory : ITransportFactory; |
| 70 | FInputProtocolFactory : IProtocolFactory; |
| 71 | FOutputProtocolFactory : IProtocolFactory; |
| 72 | FLogDelegate : TLogDelegate; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 73 | FServerEvents : IServerEvents; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 74 | FConfiguration : IThriftConfiguration; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 75 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 76 | class procedure DefaultLogDelegate( const str: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 77 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 78 | function GetServerEvents : IServerEvents; |
| 79 | procedure SetServerEvents( const value : IServerEvents); |
| 80 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 81 | procedure Serve; virtual; abstract; |
| 82 | procedure Stop; virtual; abstract; |
| 83 | public |
| 84 | constructor Create( |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 85 | const aProcessor :IProcessor; |
| 86 | const aServerTransport: IServerTransport; |
| 87 | const aInputTransportFactory : ITransportFactory; |
| 88 | const aOutputTransportFactory : ITransportFactory; |
| 89 | const aInputProtocolFactory : IProtocolFactory; |
| 90 | const aOutputProtocolFactory : IProtocolFactory; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 91 | const aConfig : IThriftConfiguration; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 92 | const aLogDelegate : TLogDelegate |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 93 | ); overload; |
| 94 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 95 | constructor Create( |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 96 | const aProcessor: IProcessor; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 97 | const aServerTransport: IServerTransport; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 98 | const aTransportFactory: ITransportFactory = nil; |
| 99 | const aProtocolFactory: IProtocolFactory = nil; |
| 100 | const aConfig : IThriftConfiguration = nil; |
| 101 | const aLogDel: TServerImpl.TLogDelegate = nil |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 102 | ); overload; |
| 103 | end; |
| 104 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 105 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 106 | TSimpleServer = class( TServerImpl) |
Jens Geyer | 2646bd6 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 107 | private |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 108 | FStop : Boolean; |
| 109 | public |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 110 | procedure Serve; override; |
| 111 | procedure Stop; override; |
| 112 | end; |
| 113 | |
| 114 | |
| 115 | implementation |
| 116 | |
| 117 | { TServerImpl } |
| 118 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 119 | constructor TServerImpl.Create( const aProcessor: IProcessor; |
| 120 | const aServerTransport: IServerTransport; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 121 | const aInputTransportFactory, aOutputTransportFactory: ITransportFactory; |
| 122 | const aInputProtocolFactory, aOutputProtocolFactory: IProtocolFactory; |
| 123 | const aConfig : IThriftConfiguration; |
| 124 | const aLogDelegate : TLogDelegate); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 125 | begin |
Jens Geyer | 718f6ee | 2013-09-06 21:02:34 +0200 | [diff] [blame] | 126 | inherited Create; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 127 | FProcessor := aProcessor; |
| 128 | FServerTransport := aServerTransport; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 129 | |
| 130 | if aConfig <> nil |
| 131 | then FConfiguration := aConfig |
| 132 | else FConfiguration := TThriftConfigurationImpl.Create; |
| 133 | |
| 134 | if aInputTransportFactory <> nil |
| 135 | then FInputTransportFactory := aInputTransportFactory |
| 136 | else FInputTransportFactory := TTransportFactoryImpl.Create; |
| 137 | |
| 138 | if aOutputTransportFactory <> nil |
| 139 | then FOutputTransportFactory := aOutputTransportFactory |
| 140 | else FOutputTransportFactory := TTransportFactoryImpl.Create; |
| 141 | |
| 142 | if aInputProtocolFactory <> nil |
| 143 | then FInputProtocolFactory := aInputProtocolFactory |
| 144 | else FInputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 145 | |
| 146 | if aOutputProtocolFactory <> nil |
| 147 | then FOutputProtocolFactory := aOutputProtocolFactory |
| 148 | else FOutputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 149 | |
| 150 | if Assigned(aLogDelegate) |
| 151 | then FLogDelegate := aLogDelegate |
| 152 | else FLogDelegate := DefaultLogDelegate; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 153 | end; |
| 154 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 155 | |
| 156 | constructor TServerImpl.Create( const aProcessor: IProcessor; |
| 157 | const aServerTransport: IServerTransport; |
| 158 | const aTransportFactory: ITransportFactory; |
| 159 | const aProtocolFactory: IProtocolFactory; |
| 160 | const aConfig : IThriftConfiguration; |
| 161 | const aLogDel: TServerImpl.TLogDelegate); |
| 162 | begin |
| 163 | Create( aProcessor, aServerTransport, |
| 164 | aTransportFactory, aTransportFactory, |
| 165 | aProtocolFactory, aProtocolFactory, |
| 166 | aConfig, aLogDel); |
| 167 | end; |
| 168 | |
| 169 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 170 | class procedure TServerImpl.DefaultLogDelegate( const str: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 171 | begin |
Jens Geyer | 26ef743 | 2013-09-23 22:01:20 +0200 | [diff] [blame] | 172 | try |
| 173 | Writeln( str); |
| 174 | if IoResult <> 0 then OutputDebugString(PChar(str)); |
| 175 | except |
| 176 | OutputDebugString(PChar(str)); |
| 177 | end; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 178 | end; |
| 179 | |
Jake Farrell | 806d298 | 2011-10-26 02:33:31 +0000 | [diff] [blame] | 180 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 181 | |
| 182 | function TServerImpl.GetServerEvents : IServerEvents; |
| 183 | begin |
| 184 | result := FServerEvents; |
| 185 | end; |
| 186 | |
| 187 | |
| 188 | procedure TServerImpl.SetServerEvents( const value : IServerEvents); |
| 189 | begin |
| 190 | // if you need more than one, provide a specialized IServerEvents implementation |
| 191 | FServerEvents := value; |
| 192 | end; |
| 193 | |
| 194 | |
Jake Farrell | 806d298 | 2011-10-26 02:33:31 +0000 | [diff] [blame] | 195 | { TSimpleServer } |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 196 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 197 | procedure TSimpleServer.Serve; |
| 198 | var |
| 199 | client : ITransport; |
| 200 | InputTransport : ITransport; |
| 201 | OutputTransport : ITransport; |
| 202 | InputProtocol : IProtocol; |
| 203 | OutputProtocol : IProtocol; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 204 | context : IProcessorEvents; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 205 | begin |
| 206 | try |
| 207 | FServerTransport.Listen; |
| 208 | except |
| 209 | on E: Exception do |
| 210 | begin |
| 211 | FLogDelegate( E.ToString); |
| 212 | end; |
| 213 | end; |
| 214 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 215 | if FServerEvents <> nil |
| 216 | then FServerEvents.PreServe; |
| 217 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 218 | client := nil; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 219 | while (not FStop) do |
| 220 | begin |
| 221 | try |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 222 | // clean up any old instances before waiting for clients |
| 223 | InputTransport := nil; |
| 224 | OutputTransport := nil; |
| 225 | InputProtocol := nil; |
| 226 | OutputProtocol := nil; |
| 227 | |
Jens Geyer | 3e8d927 | 2014-09-14 20:10:40 +0200 | [diff] [blame] | 228 | // close any old connections before before waiting for new clients |
| 229 | if client <> nil then try |
| 230 | try |
| 231 | client.Close; |
| 232 | finally |
| 233 | client := nil; |
| 234 | end; |
| 235 | except |
| 236 | // catch all, we can't do much about it at this point |
| 237 | end; |
| 238 | |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 239 | client := FServerTransport.Accept( procedure |
| 240 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 241 | if FServerEvents <> nil |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 242 | then FServerEvents.PreAccept; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 243 | end); |
| 244 | |
| 245 | if client = nil then begin |
| 246 | if FStop |
| 247 | then Abort // silent exception |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 248 | else raise TTransportExceptionUnknown.Create('ServerTransport.Accept() may not return NULL'); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 249 | end; |
| 250 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 251 | FLogDelegate( 'Client Connected!'); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 252 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 253 | InputTransport := FInputTransportFactory.GetTransport( client ); |
| 254 | OutputTransport := FOutputTransportFactory.GetTransport( client ); |
| 255 | InputProtocol := FInputProtocolFactory.GetProtocol( InputTransport ); |
| 256 | OutputProtocol := FOutputProtocolFactory.GetProtocol( OutputTransport ); |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 257 | |
| 258 | if FServerEvents <> nil |
| 259 | then context := FServerEvents.CreateProcessingContext( InputProtocol, OutputProtocol) |
| 260 | else context := nil; |
| 261 | |
| 262 | while not FStop do begin |
| 263 | if context <> nil |
| 264 | then context.Processing( client); |
| 265 | if not FProcessor.Process( InputProtocol, OutputProtocol, context) |
| 266 | then Break; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 267 | end; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 268 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 269 | except |
Jens Geyer | c140bb9 | 2019-11-27 22:18:12 +0100 | [diff] [blame] | 270 | on E: TTransportException do begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 271 | if FStop |
| 272 | then FLogDelegate('TSimpleServer was shutting down, caught ' + E.ToString) |
| 273 | else FLogDelegate( E.ToString); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 274 | end; |
Jens Geyer | c140bb9 | 2019-11-27 22:18:12 +0100 | [diff] [blame] | 275 | on E: Exception do begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 276 | FLogDelegate( E.ToString); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 277 | end; |
| 278 | end; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 279 | |
Jens Geyer | c140bb9 | 2019-11-27 22:18:12 +0100 | [diff] [blame] | 280 | if context <> nil then begin |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 281 | context.CleanupContext; |
| 282 | context := nil; |
| 283 | end; |
| 284 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 285 | if InputTransport <> nil then |
| 286 | begin |
| 287 | InputTransport.Close; |
| 288 | end; |
| 289 | if OutputTransport <> nil then |
| 290 | begin |
| 291 | OutputTransport.Close; |
| 292 | end; |
| 293 | end; |
| 294 | |
| 295 | if FStop then |
| 296 | begin |
| 297 | try |
| 298 | FServerTransport.Close; |
| 299 | except |
| 300 | on E: TTransportException do |
| 301 | begin |
| 302 | FLogDelegate('TServerTranport failed on close: ' + E.Message); |
| 303 | end; |
| 304 | end; |
| 305 | FStop := False; |
| 306 | end; |
| 307 | end; |
| 308 | |
| 309 | procedure TSimpleServer.Stop; |
| 310 | begin |
| 311 | FStop := True; |
| 312 | FServerTransport.Close; |
| 313 | end; |
| 314 | |
| 315 | end. |