blob: 9d54a2e66ecd2990561416be71b2b5c922fbcd40 [file] [log] [blame]
Roger Meier5c854612012-04-09 16:31:42 +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 *)
19program DelphiServer;
20
21{$APPTYPE CONSOLE}
22{$D 'Copyright (c) 2012 The Apache Software Foundation'}
23
24{$Q+} // throws exceptions on numeric overflows
25
26uses
27 SysUtils,
28 Generics.Collections,
29 Thrift in '..\..\..\lib\delphi\src\Thrift.pas',
30 Thrift.Collections in '..\..\..\lib\delphi\src\Thrift.Collections.pas',
31 Thrift.Console in '..\..\..\lib\delphi\src\Thrift.Console.pas',
32 Thrift.Utils in '..\..\..\lib\delphi\src\Thrift.Utils.pas',
33 Thrift.Stream in '..\..\..\lib\delphi\src\Thrift.Stream.pas',
34 Thrift.Protocol in '..\..\..\lib\delphi\src\Thrift.Protocol.pas',
35 Thrift.Server in '..\..\..\lib\delphi\src\Thrift.Server.pas',
36 Thrift.Transport in '..\..\..\lib\delphi\src\Thrift.Transport.pas',
37 Shared in '..\..\gen-delphi\Shared.pas',
38 Tutorial in '..\..\gen-delphi\Tutorial.pas';
39
40
41type
42 TCalculatorHandler = class( TInterfacedObject, TCalculator.Iface)
43 protected
44 FLog : TDictionary< Integer, ISharedStruct>;
45
46 // TSharedService.Iface
47 function getStruct(key: Integer): ISharedStruct;
48
49 // TCalculator.Iface
50 procedure ping();
51 function add(num1: Integer; num2: Integer): Integer;
52 function calculate(logid: Integer; const w: IWork): Integer;
53 procedure zip();
54
55 public
56 constructor Create;
57 destructor Destroy; override;
58
59 end;
60
61 DelphiTutorialServer = class
62 public
63 class procedure Main;
64 end;
65
66
67//--- TCalculatorHandler ---------------------------------------------------
68
69
70constructor TCalculatorHandler.Create;
71begin
72 inherited Create;
73 FLog := TDictionary< Integer, ISharedStruct>.Create();
74end;
75
76
77destructor TCalculatorHandler.Destroy;
78begin
79 try
80 FreeAndNil( FLog);
81 finally
82 inherited Destroy;
83 end;
84end;
85
86
87procedure TCalculatorHandler.ping;
88begin
89 Console.WriteLine( 'ping()');
90end;
91
92
93function TCalculatorHandler.add(num1: Integer; num2: Integer): Integer;
94begin
95 Console.WriteLine( Format( 'add( %d, %d)', [num1, num2]));
96 result := num1 + num2;
97end;
98
99
100function TCalculatorHandler.calculate(logid: Integer; const w: IWork): Integer;
101var entry : ISharedStruct;
102begin
103 try
104 Console.WriteLine( Format('calculate( %d, [%d,%d,%d])', [logid, Ord(w.Op), w.Num1, w.Num2]));
105
106 case w.Op of
107 TOperation.ADD : result := w.Num1 + w.Num2;
108 TOperation.SUBTRACT : result := w.Num1 - w.Num2;
109 TOperation.MULTIPLY : result := w.Num1 * w.Num2;
110 TOperation.DIVIDE : result := Round( w.Num1 / w.Num2);
111 else
112 raise TInvalidOperation.Create( Ord(w.Op), 'Unknown operation');
113 end;
114
115 except
116 on e:Thrift.TException do raise; // let Thrift Exceptions pass through
117 on e:Exception do raise TInvalidOperation.Create( Ord(w.Op), e.Message); // repackage all other
118 end;
119
120 entry := TSharedStructImpl.Create;
121 entry.Key := logid;
122 entry.Value := IntToStr( result);
123 FLog.AddOrSetValue( logid, entry);
124end;
125
126
127function TCalculatorHandler.getStruct(key: Integer): ISharedStruct;
128begin
129 Console.WriteLine( Format( 'getStruct(%d)', [key]));
130 result := FLog[key];
131end;
132
133
134procedure TCalculatorHandler.zip;
135begin
136 Console.WriteLine( 'zip()');
137end;
138
139
140//--- DelphiTutorialServer ----------------------------------------------------------------------
141
142
143class procedure DelphiTutorialServer.Main;
144var handler : TCalculator.Iface;
145 processor : IProcessor;
146 transport : IServerTransport;
147 server : IServer;
148begin
149 try
150 handler := TCalculatorHandler.Create;
151 processor := TCalculator.TProcessorImpl.Create( handler);
152 transport := TServerSocketImpl.Create( 9090);
153 server := TSimpleServer.Create( processor, transport);
154
155 Console.WriteLine( 'Starting the server...');
156 server.Serve();
157
158 except
159 on e: Exception do Console.WriteLine( e.Message);
160 end;
161
162 Console.WriteLine('done.');
163end;
164
165
166begin
167 try
168 DelphiTutorialServer.Main;
169 except
170 on E: Exception do
171 Writeln(E.ClassName, ': ', E.Message);
172 end;
173end.