blob: 2d35c19171a51ff30c4e7510090e7afa128615f6 [file] [log] [blame]
Jake Farrell27274222011-11-10 20:32:44 +00001(*
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
22interface
23
24uses
25 SysUtils,
26 Thrift,
27 Thrift.Protocol,
28 Thrift.Transport;
29
30type
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
105implementation
106
107{ TServerImpl }
108
109constructor TServerImpl.Create(AProcessor: IProcessor;
110 AServerTransport: IServerTransport; ALogDelegate: TLogDelegate);
111var
112 InputFactory, OutputFactory : IProtocolFactory;
113 InputTransFactory, OutputTransFactory : ITransportFactory;
114
115begin
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 );
130end;
131
132constructor TServerImpl.Create(AProcessor: IProcessor;
133 AServerTransport: IServerTransport);
134var
135 InputFactory, OutputFactory : IProtocolFactory;
136 InputTransFactory, OutputTransFactory : ITransportFactory;
137
138begin
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 );
153end;
154
155constructor TServerImpl.Create(AProcessor: IProcessor;
156 AServerTransport: IServerTransport; ATransportFactory: ITransportFactory);
157var
158 InputProtocolFactory : IProtocolFactory;
159 OutputProtocolFactory : IProtocolFactory;
160begin
161 InputProtocolFactory := TBinaryProtocolImpl.TFactory.Create;
162 OutputProtocolFactory := TBinaryProtocolImpl.TFactory.Create;
163
164 Create( AProcessor, AServerTransport, ATransportFactory, ATransportFactory,
165 InputProtocolFactory, OutputProtocolFactory, DefaultLogDelegate);
166end;
167
168constructor TServerImpl.Create(AProcessor: IProcessor;
169 AServerTransport: IServerTransport; AInputTransportFactory,
170 AOutputTransportFactory: ITransportFactory; AInputProtocolFactory,
171 AOutputProtocolFactory: IProtocolFactory;
172 ALogDelegate : TLogDelegate);
173begin
174 FProcessor := AProcessor;
175 FServerTransport := AServerTransport;
176 FInputTransportFactory := AInputTransportFactory;
177 FOutputTransportFactory := AOutputTransportFactory;
178 FInputProtocolFactory := AInputProtocolFactory;
179 FOutputProtocolFactory := AOutputProtocolFactory;
180 FLogDelegate := ALogDelegate;
181end;
182
183class procedure TServerImpl.DefaultLogDelegate( str: string);
184begin
185 Writeln( str );
186end;
187
188constructor TServerImpl.Create(AProcessor: IProcessor;
Jake Farrell806d2982011-10-26 02:33:31 +0000189 AServerTransport: IServerTransport; ATransportFactory: ITransportFactory;
190 AProtocolFactory: IProtocolFactory);
191begin
192 Create( AProcessor, AServerTransport,
193 ATransportFactory, ATransportFactory,
194 AProtocolFactory, AProtocolFactory,
195 DefaultLogDelegate);
196end;
197
198{ TSimpleServer }
Jake Farrell27274222011-11-10 20:32:44 +0000199
200constructor TSimpleServer.Create(AProcessor: IProcessor;
201 AServerTransport: IServerTransport);
202var
203 InputProtocolFactory : IProtocolFactory;
204 OutputProtocolFactory : IProtocolFactory;
205 InputTransportFactory : ITransportFactory;
206 OutputTransportFactory : ITransportFactory;
207begin
208 InputProtocolFactory := TBinaryProtocolImpl.TFactory.Create;
209 OutputProtocolFactory := TBinaryProtocolImpl.TFactory.Create;
210 InputTransportFactory := TTransportFactoryImpl.Create;
211 OutputTransportFactory := TTransportFactoryImpl.Create;
212
213 inherited Create( AProcessor, AServerTransport, InputTransportFactory,
214 OutputTransportFactory, InputProtocolFactory, OutputProtocolFactory, DefaultLogDelegate);
215end;
216
217constructor TSimpleServer.Create(AProcessor: IProcessor;
218 AServerTransport: IServerTransport; ALogDel: TServerImpl.TLogDelegate);
219var
220 InputProtocolFactory : IProtocolFactory;
221 OutputProtocolFactory : IProtocolFactory;
222 InputTransportFactory : ITransportFactory;
223 OutputTransportFactory : ITransportFactory;
224begin
225 InputProtocolFactory := TBinaryProtocolImpl.TFactory.Create;
226 OutputProtocolFactory := TBinaryProtocolImpl.TFactory.Create;
227 InputTransportFactory := TTransportFactoryImpl.Create;
228 OutputTransportFactory := TTransportFactoryImpl.Create;
229
230 inherited Create( AProcessor, AServerTransport, InputTransportFactory,
231 OutputTransportFactory, InputProtocolFactory, OutputProtocolFactory, ALogDel);
232end;
233
234constructor TSimpleServer.Create(AProcessor: IProcessor;
235 AServerTransport: IServerTransport; ATransportFactory: ITransportFactory);
236begin
237 inherited Create( AProcessor, AServerTransport, ATransportFactory,
238 ATransportFactory, TBinaryProtocolImpl.TFactory.Create, TBinaryProtocolImpl.TFactory.Create, DefaultLogDelegate);
239end;
240
241constructor TSimpleServer.Create(AProcessor: IProcessor;
242 AServerTransport: IServerTransport; ATransportFactory: ITransportFactory;
243 AProtocolFactory: IProtocolFactory);
244begin
245 inherited Create( AProcessor, AServerTransport, ATransportFactory,
246 ATransportFactory, AProtocolFactory, AProtocolFactory, DefaultLogDelegate);
247end;
248
249procedure TSimpleServer.Serve;
250var
251 client : ITransport;
252 InputTransport : ITransport;
253 OutputTransport : ITransport;
254 InputProtocol : IProtocol;
255 OutputProtocol : IProtocol;
256begin
257 try
258 FServerTransport.Listen;
259 except
260 on E: Exception do
261 begin
262 FLogDelegate( E.ToString);
263 end;
264 end;
265
266 client := nil;
267 InputTransport := nil;
268 OutputTransport := nil;
269 InputProtocol := nil;
270 OutputProtocol := nil;
271
272 while (not FStop) do
273 begin
274 try
275 client := FServerTransport.Accept;
276 FLogDelegate( 'Client Connected!');
277 InputTransport := FInputTransportFactory.GetTransport( client );
278 OutputTransport := FOutputTransportFactory.GetTransport( client );
279 InputProtocol := FInputProtocolFactory.GetProtocol( InputTransport );
280 OutputProtocol := FOutputProtocolFactory.GetProtocol( OutputTransport );
281 while ( FProcessor.Process( InputProtocol, OutputProtocol )) do
282 begin
283 if FStop then Break;
284 end;
285 except
286 on E: TTransportException do
287 begin
288 if FStop then
289 begin
290 FLogDelegate('TSimpleServer was shutting down, caught ' + E.ClassName);
291 end;
292 end;
293 on E: Exception do
294 begin
295 FLogDelegate( E.ToString );
296 end;
297 end;
298 if InputTransport <> nil then
299 begin
300 InputTransport.Close;
301 end;
302 if OutputTransport <> nil then
303 begin
304 OutputTransport.Close;
305 end;
306 end;
307
308 if FStop then
309 begin
310 try
311 FServerTransport.Close;
312 except
313 on E: TTransportException do
314 begin
315 FLogDelegate('TServerTranport failed on close: ' + E.Message);
316 end;
317 end;
318 FStop := False;
319 end;
320end;
321
322procedure TSimpleServer.Stop;
323begin
324 FStop := True;
325 FServerTransport.Close;
326end;
327
328end.