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 | 26ef743 | 2013-09-23 22:01:20 +0200 | [diff] [blame] | 22 | {$I-} // prevent annoying errors with default log delegate and no console |
| 23 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 24 | interface |
| 25 | |
| 26 | uses |
Jens Geyer | 26ef743 | 2013-09-23 22:01:20 +0200 | [diff] [blame] | 27 | Windows, SysUtils, |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 28 | Thrift, |
| 29 | Thrift.Protocol, |
| 30 | Thrift.Transport; |
| 31 | |
| 32 | type |
| 33 | IServer = interface |
| 34 | ['{CF9F56C6-BB39-4C7D-877B-43B416572CE6}'] |
| 35 | procedure Serve; |
| 36 | procedure Stop; |
| 37 | end; |
| 38 | |
| 39 | TServerImpl = class abstract( TInterfacedObject, IServer ) |
| 40 | public |
| 41 | type |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 42 | TLogDelegate = reference to procedure( const str: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 43 | protected |
| 44 | FProcessor : IProcessor; |
| 45 | FServerTransport : IServerTransport; |
| 46 | FInputTransportFactory : ITransportFactory; |
| 47 | FOutputTransportFactory : ITransportFactory; |
| 48 | FInputProtocolFactory : IProtocolFactory; |
| 49 | FOutputProtocolFactory : IProtocolFactory; |
| 50 | FLogDelegate : TLogDelegate; |
| 51 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 52 | class procedure DefaultLogDelegate( const str: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 53 | |
| 54 | procedure Serve; virtual; abstract; |
| 55 | procedure Stop; virtual; abstract; |
| 56 | public |
| 57 | constructor Create( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 58 | const AProcessor :IProcessor; |
| 59 | const AServerTransport: IServerTransport; |
| 60 | const AInputTransportFactory : ITransportFactory; |
| 61 | const AOutputTransportFactory : ITransportFactory; |
| 62 | const AInputProtocolFactory : IProtocolFactory; |
| 63 | const AOutputProtocolFactory : IProtocolFactory; |
| 64 | const ALogDelegate : TLogDelegate |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 65 | ); overload; |
| 66 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 67 | constructor Create( |
| 68 | const AProcessor :IProcessor; |
| 69 | const AServerTransport: IServerTransport |
| 70 | ); overload; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 71 | |
| 72 | constructor Create( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 73 | const AProcessor :IProcessor; |
| 74 | const AServerTransport: IServerTransport; |
| 75 | const ALogDelegate: TLogDelegate |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 76 | ); overload; |
| 77 | |
| 78 | constructor Create( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 79 | const AProcessor :IProcessor; |
| 80 | const AServerTransport: IServerTransport; |
| 81 | const ATransportFactory : ITransportFactory |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 82 | ); overload; |
| 83 | |
| 84 | constructor Create( |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 85 | const AProcessor :IProcessor; |
| 86 | const AServerTransport: IServerTransport; |
| 87 | const ATransportFactory : ITransportFactory; |
| 88 | const AProtocolFactory : IProtocolFactory |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 89 | ); overload; |
| 90 | end; |
| 91 | |
| 92 | TSimpleServer = class( TServerImpl) |
| 93 | private |
| 94 | FStop : Boolean; |
| 95 | public |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 96 | constructor Create( const AProcessor: IProcessor; const AServerTransport: IServerTransport); overload; |
| 97 | constructor Create( const AProcessor: IProcessor; const AServerTransport: IServerTransport; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 98 | ALogDel: TServerImpl.TLogDelegate); overload; |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 99 | constructor Create( const AProcessor: IProcessor; const AServerTransport: IServerTransport; |
| 100 | const ATransportFactory: ITransportFactory); overload; |
| 101 | constructor Create( const AProcessor: IProcessor; const AServerTransport: IServerTransport; |
| 102 | const ATransportFactory: ITransportFactory; const AProtocolFactory: IProtocolFactory); overload; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 103 | |
| 104 | procedure Serve; override; |
| 105 | procedure Stop; override; |
| 106 | end; |
| 107 | |
| 108 | |
| 109 | implementation |
| 110 | |
| 111 | { TServerImpl } |
| 112 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 113 | constructor TServerImpl.Create( const AProcessor: IProcessor; |
| 114 | const AServerTransport: IServerTransport; const ALogDelegate: TLogDelegate); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 115 | var |
| 116 | InputFactory, OutputFactory : IProtocolFactory; |
| 117 | InputTransFactory, OutputTransFactory : ITransportFactory; |
| 118 | |
| 119 | begin |
| 120 | InputFactory := TBinaryProtocolImpl.TFactory.Create; |
| 121 | OutputFactory := TBinaryProtocolImpl.TFactory.Create; |
| 122 | InputTransFactory := TTransportFactoryImpl.Create; |
| 123 | OutputTransFactory := TTransportFactoryImpl.Create; |
| 124 | |
Jens Geyer | 718f6ee | 2013-09-06 21:02:34 +0200 | [diff] [blame] | 125 | //no inherited; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 126 | Create( |
| 127 | AProcessor, |
| 128 | AServerTransport, |
| 129 | InputTransFactory, |
| 130 | OutputTransFactory, |
| 131 | InputFactory, |
| 132 | OutputFactory, |
| 133 | ALogDelegate |
| 134 | ); |
| 135 | end; |
| 136 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 137 | constructor TServerImpl.Create(const AProcessor: IProcessor; |
| 138 | const AServerTransport: IServerTransport); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 139 | var |
| 140 | InputFactory, OutputFactory : IProtocolFactory; |
| 141 | InputTransFactory, OutputTransFactory : ITransportFactory; |
| 142 | |
| 143 | begin |
| 144 | InputFactory := TBinaryProtocolImpl.TFactory.Create; |
| 145 | OutputFactory := TBinaryProtocolImpl.TFactory.Create; |
| 146 | InputTransFactory := TTransportFactoryImpl.Create; |
| 147 | OutputTransFactory := TTransportFactoryImpl.Create; |
| 148 | |
Jens Geyer | 718f6ee | 2013-09-06 21:02:34 +0200 | [diff] [blame] | 149 | //no inherited; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 150 | Create( |
| 151 | AProcessor, |
| 152 | AServerTransport, |
| 153 | InputTransFactory, |
| 154 | OutputTransFactory, |
| 155 | InputFactory, |
| 156 | OutputFactory, |
| 157 | DefaultLogDelegate |
| 158 | ); |
| 159 | end; |
| 160 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 161 | constructor TServerImpl.Create(const AProcessor: IProcessor; |
| 162 | const AServerTransport: IServerTransport; const ATransportFactory: ITransportFactory); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 163 | var |
| 164 | InputProtocolFactory : IProtocolFactory; |
| 165 | OutputProtocolFactory : IProtocolFactory; |
| 166 | begin |
| 167 | InputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 168 | OutputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 169 | |
Jens Geyer | 718f6ee | 2013-09-06 21:02:34 +0200 | [diff] [blame] | 170 | //no inherited; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 171 | Create( AProcessor, AServerTransport, ATransportFactory, ATransportFactory, |
| 172 | InputProtocolFactory, OutputProtocolFactory, DefaultLogDelegate); |
| 173 | end; |
| 174 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 175 | constructor TServerImpl.Create(const AProcessor: IProcessor; |
| 176 | const AServerTransport: IServerTransport; |
| 177 | const AInputTransportFactory, AOutputTransportFactory: ITransportFactory; |
| 178 | const AInputProtocolFactory, AOutputProtocolFactory: IProtocolFactory; |
| 179 | const ALogDelegate : TLogDelegate); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 180 | begin |
Jens Geyer | 718f6ee | 2013-09-06 21:02:34 +0200 | [diff] [blame] | 181 | inherited Create; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 182 | FProcessor := AProcessor; |
| 183 | FServerTransport := AServerTransport; |
| 184 | FInputTransportFactory := AInputTransportFactory; |
| 185 | FOutputTransportFactory := AOutputTransportFactory; |
| 186 | FInputProtocolFactory := AInputProtocolFactory; |
| 187 | FOutputProtocolFactory := AOutputProtocolFactory; |
| 188 | FLogDelegate := ALogDelegate; |
| 189 | end; |
| 190 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 191 | class procedure TServerImpl.DefaultLogDelegate( const str: string); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 192 | begin |
Jens Geyer | 26ef743 | 2013-09-23 22:01:20 +0200 | [diff] [blame] | 193 | try |
| 194 | Writeln( str); |
| 195 | if IoResult <> 0 then OutputDebugString(PChar(str)); |
| 196 | except |
| 197 | OutputDebugString(PChar(str)); |
| 198 | end; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 199 | end; |
| 200 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 201 | constructor TServerImpl.Create( const AProcessor: IProcessor; |
| 202 | const AServerTransport: IServerTransport; const ATransportFactory: ITransportFactory; |
| 203 | const AProtocolFactory: IProtocolFactory); |
Jake Farrell | 806d298 | 2011-10-26 02:33:31 +0000 | [diff] [blame] | 204 | begin |
Jens Geyer | 718f6ee | 2013-09-06 21:02:34 +0200 | [diff] [blame] | 205 | //no inherited; |
Jake Farrell | 806d298 | 2011-10-26 02:33:31 +0000 | [diff] [blame] | 206 | Create( AProcessor, AServerTransport, |
| 207 | ATransportFactory, ATransportFactory, |
| 208 | AProtocolFactory, AProtocolFactory, |
| 209 | DefaultLogDelegate); |
| 210 | end; |
| 211 | |
| 212 | { TSimpleServer } |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 213 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 214 | constructor TSimpleServer.Create( const AProcessor: IProcessor; |
| 215 | const AServerTransport: IServerTransport); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 216 | var |
| 217 | InputProtocolFactory : IProtocolFactory; |
| 218 | OutputProtocolFactory : IProtocolFactory; |
| 219 | InputTransportFactory : ITransportFactory; |
| 220 | OutputTransportFactory : ITransportFactory; |
| 221 | begin |
| 222 | InputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 223 | OutputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 224 | InputTransportFactory := TTransportFactoryImpl.Create; |
| 225 | OutputTransportFactory := TTransportFactoryImpl.Create; |
| 226 | |
| 227 | inherited Create( AProcessor, AServerTransport, InputTransportFactory, |
| 228 | OutputTransportFactory, InputProtocolFactory, OutputProtocolFactory, DefaultLogDelegate); |
| 229 | end; |
| 230 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 231 | constructor TSimpleServer.Create( const AProcessor: IProcessor; |
| 232 | const AServerTransport: IServerTransport; ALogDel: TServerImpl.TLogDelegate); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 233 | var |
| 234 | InputProtocolFactory : IProtocolFactory; |
| 235 | OutputProtocolFactory : IProtocolFactory; |
| 236 | InputTransportFactory : ITransportFactory; |
| 237 | OutputTransportFactory : ITransportFactory; |
| 238 | begin |
| 239 | InputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 240 | OutputProtocolFactory := TBinaryProtocolImpl.TFactory.Create; |
| 241 | InputTransportFactory := TTransportFactoryImpl.Create; |
| 242 | OutputTransportFactory := TTransportFactoryImpl.Create; |
| 243 | |
| 244 | inherited Create( AProcessor, AServerTransport, InputTransportFactory, |
| 245 | OutputTransportFactory, InputProtocolFactory, OutputProtocolFactory, ALogDel); |
| 246 | end; |
| 247 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 248 | constructor TSimpleServer.Create( const AProcessor: IProcessor; |
| 249 | const AServerTransport: IServerTransport; const ATransportFactory: ITransportFactory); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 250 | begin |
| 251 | inherited Create( AProcessor, AServerTransport, ATransportFactory, |
| 252 | ATransportFactory, TBinaryProtocolImpl.TFactory.Create, TBinaryProtocolImpl.TFactory.Create, DefaultLogDelegate); |
| 253 | end; |
| 254 | |
Roger Meier | 333bbf3 | 2012-01-08 21:51:08 +0000 | [diff] [blame] | 255 | constructor TSimpleServer.Create( const AProcessor: IProcessor; |
| 256 | const AServerTransport: IServerTransport; const ATransportFactory: ITransportFactory; |
| 257 | const AProtocolFactory: IProtocolFactory); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 258 | begin |
| 259 | inherited Create( AProcessor, AServerTransport, ATransportFactory, |
| 260 | ATransportFactory, AProtocolFactory, AProtocolFactory, DefaultLogDelegate); |
| 261 | end; |
| 262 | |
| 263 | procedure TSimpleServer.Serve; |
| 264 | var |
| 265 | client : ITransport; |
| 266 | InputTransport : ITransport; |
| 267 | OutputTransport : ITransport; |
| 268 | InputProtocol : IProtocol; |
| 269 | OutputProtocol : IProtocol; |
| 270 | begin |
| 271 | try |
| 272 | FServerTransport.Listen; |
| 273 | except |
| 274 | on E: Exception do |
| 275 | begin |
| 276 | FLogDelegate( E.ToString); |
| 277 | end; |
| 278 | end; |
| 279 | |
| 280 | client := nil; |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 281 | while (not FStop) do |
| 282 | begin |
| 283 | try |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 284 | // clean up any old instances before waiting for clients |
| 285 | InputTransport := nil; |
| 286 | OutputTransport := nil; |
| 287 | InputProtocol := nil; |
| 288 | OutputProtocol := nil; |
| 289 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 290 | client := FServerTransport.Accept; |
| 291 | FLogDelegate( 'Client Connected!'); |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 292 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 293 | InputTransport := FInputTransportFactory.GetTransport( client ); |
| 294 | OutputTransport := FOutputTransportFactory.GetTransport( client ); |
| 295 | InputProtocol := FInputProtocolFactory.GetProtocol( InputTransport ); |
| 296 | OutputProtocol := FOutputProtocolFactory.GetProtocol( OutputTransport ); |
| 297 | while ( FProcessor.Process( InputProtocol, OutputProtocol )) do |
| 298 | begin |
| 299 | if FStop then Break; |
| 300 | end; |
Jens Geyer | 06045cf | 2013-03-27 20:26:25 +0200 | [diff] [blame] | 301 | |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 302 | except |
| 303 | on E: TTransportException do |
| 304 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 305 | if FStop |
| 306 | then FLogDelegate('TSimpleServer was shutting down, caught ' + E.ToString) |
| 307 | else FLogDelegate( E.ToString); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 308 | end; |
| 309 | on E: Exception do |
| 310 | begin |
Roger Meier | 79655fb | 2012-10-20 20:59:41 +0000 | [diff] [blame] | 311 | FLogDelegate( E.ToString); |
Jake Farrell | 2727422 | 2011-11-10 20:32:44 +0000 | [diff] [blame] | 312 | end; |
| 313 | end; |
| 314 | if InputTransport <> nil then |
| 315 | begin |
| 316 | InputTransport.Close; |
| 317 | end; |
| 318 | if OutputTransport <> nil then |
| 319 | begin |
| 320 | OutputTransport.Close; |
| 321 | end; |
| 322 | end; |
| 323 | |
| 324 | if FStop then |
| 325 | begin |
| 326 | try |
| 327 | FServerTransport.Close; |
| 328 | except |
| 329 | on E: TTransportException do |
| 330 | begin |
| 331 | FLogDelegate('TServerTranport failed on close: ' + E.Message); |
| 332 | end; |
| 333 | end; |
| 334 | FStop := False; |
| 335 | end; |
| 336 | end; |
| 337 | |
| 338 | procedure TSimpleServer.Stop; |
| 339 | begin |
| 340 | FStop := True; |
| 341 | FServerTransport.Close; |
| 342 | end; |
| 343 | |
| 344 | end. |