blob: 5f42e7e1089f5c9d489f6f9fc32c769cc8b5e3e0 [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',
Jens Geyer606f1ef2018-04-09 23:09:41 +020031 Thrift.Exception in '..\..\..\lib\delphi\src\Thrift.Exception.pas',
Roger Meier5c854612012-04-09 16:31:42 +000032 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
Jens Geyer606f1ef2018-04-09 23:09:41 +020089 WriteLn( 'ping()');
Roger Meier5c854612012-04-09 16:31:42 +000090end;
91
92
93function TCalculatorHandler.add(num1: Integer; num2: Integer): Integer;
94begin
Jens Geyer606f1ef2018-04-09 23:09:41 +020095 WriteLn( Format( 'add( %d, %d)', [num1, num2]));
Roger Meier5c854612012-04-09 16:31:42 +000096 result := num1 + num2;
97end;
98
99
100function TCalculatorHandler.calculate(logid: Integer; const w: IWork): Integer;
101var entry : ISharedStruct;
102begin
103 try
Jens Geyer606f1ef2018-04-09 23:09:41 +0200104 WriteLn( Format('calculate( %d, [%d,%d,%d])', [logid, Ord(w.Op), w.Num1, w.Num2]));
Roger Meier5c854612012-04-09 16:31:42 +0000105
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
Jens Geyer606f1ef2018-04-09 23:09:41 +0200129 WriteLn( Format( 'getStruct(%d)', [key]));
Roger Meier5c854612012-04-09 16:31:42 +0000130 result := FLog[key];
131end;
132
133
134procedure TCalculatorHandler.zip;
135begin
Jens Geyer606f1ef2018-04-09 23:09:41 +0200136 WriteLn( 'zip()');
Roger Meier5c854612012-04-09 16:31:42 +0000137end;
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
Jens Geyer606f1ef2018-04-09 23:09:41 +0200155 WriteLn( 'Starting the server...');
Roger Meier5c854612012-04-09 16:31:42 +0000156 server.Serve();
157
158 except
Jens Geyer606f1ef2018-04-09 23:09:41 +0200159 on e: Exception do WriteLn( e.Message);
Roger Meier5c854612012-04-09 16:31:42 +0000160 end;
161
Jens Geyer606f1ef2018-04-09 23:09:41 +0200162 WriteLn('done.');
Roger Meier5c854612012-04-09 16:31:42 +0000163end;
164
165
166begin
167 try
168 DelphiTutorialServer.Main;
169 except
170 on E: Exception do
171 Writeln(E.ClassName, ': ', E.Message);
172 end;
173end.