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