blob: 41a3514d018ae98a98c10a1e18a961a895ab80eb [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 Geyera019cda2019-11-09 23:24:52 +010031 Thrift.Configuration in '..\..\..\lib\delphi\src\Thrift.Configuration.pas',
Jens Geyer606f1ef2018-04-09 23:09:41 +020032 Thrift.Exception in '..\..\..\lib\delphi\src\Thrift.Exception.pas',
Roger Meier5c854612012-04-09 16:31:42 +000033 Thrift.Utils in '..\..\..\lib\delphi\src\Thrift.Utils.pas',
34 Thrift.Stream in '..\..\..\lib\delphi\src\Thrift.Stream.pas',
35 Thrift.Protocol in '..\..\..\lib\delphi\src\Thrift.Protocol.pas',
36 Thrift.Server in '..\..\..\lib\delphi\src\Thrift.Server.pas',
37 Thrift.Transport in '..\..\..\lib\delphi\src\Thrift.Transport.pas',
Jens Geyer02230912019-04-03 01:12:51 +020038 Thrift.WinHTTP in '..\..\..\lib\delphi\src\Thrift.WinHTTP.pas',
Roger Meier5c854612012-04-09 16:31:42 +000039 Shared in '..\..\gen-delphi\Shared.pas',
40 Tutorial in '..\..\gen-delphi\Tutorial.pas';
41
42
43type
44 TCalculatorHandler = class( TInterfacedObject, TCalculator.Iface)
45 protected
46 FLog : TDictionary< Integer, ISharedStruct>;
47
48 // TSharedService.Iface
49 function getStruct(key: Integer): ISharedStruct;
50
51 // TCalculator.Iface
52 procedure ping();
53 function add(num1: Integer; num2: Integer): Integer;
54 function calculate(logid: Integer; const w: IWork): Integer;
55 procedure zip();
56
57 public
58 constructor Create;
59 destructor Destroy; override;
60
61 end;
62
63 DelphiTutorialServer = class
64 public
65 class procedure Main;
66 end;
67
68
69//--- TCalculatorHandler ---------------------------------------------------
70
71
72constructor TCalculatorHandler.Create;
73begin
74 inherited Create;
75 FLog := TDictionary< Integer, ISharedStruct>.Create();
76end;
77
78
79destructor TCalculatorHandler.Destroy;
80begin
81 try
82 FreeAndNil( FLog);
83 finally
84 inherited Destroy;
85 end;
86end;
87
88
89procedure TCalculatorHandler.ping;
90begin
Jens Geyer606f1ef2018-04-09 23:09:41 +020091 WriteLn( 'ping()');
Roger Meier5c854612012-04-09 16:31:42 +000092end;
93
94
95function TCalculatorHandler.add(num1: Integer; num2: Integer): Integer;
96begin
Jens Geyer606f1ef2018-04-09 23:09:41 +020097 WriteLn( Format( 'add( %d, %d)', [num1, num2]));
Roger Meier5c854612012-04-09 16:31:42 +000098 result := num1 + num2;
99end;
100
101
102function TCalculatorHandler.calculate(logid: Integer; const w: IWork): Integer;
103var entry : ISharedStruct;
104begin
105 try
Jens Geyer606f1ef2018-04-09 23:09:41 +0200106 WriteLn( Format('calculate( %d, [%d,%d,%d])', [logid, Ord(w.Op), w.Num1, w.Num2]));
Roger Meier5c854612012-04-09 16:31:42 +0000107
108 case w.Op of
109 TOperation.ADD : result := w.Num1 + w.Num2;
110 TOperation.SUBTRACT : result := w.Num1 - w.Num2;
111 TOperation.MULTIPLY : result := w.Num1 * w.Num2;
112 TOperation.DIVIDE : result := Round( w.Num1 / w.Num2);
113 else
114 raise TInvalidOperation.Create( Ord(w.Op), 'Unknown operation');
115 end;
116
117 except
118 on e:Thrift.TException do raise; // let Thrift Exceptions pass through
119 on e:Exception do raise TInvalidOperation.Create( Ord(w.Op), e.Message); // repackage all other
120 end;
121
122 entry := TSharedStructImpl.Create;
123 entry.Key := logid;
124 entry.Value := IntToStr( result);
125 FLog.AddOrSetValue( logid, entry);
126end;
127
128
129function TCalculatorHandler.getStruct(key: Integer): ISharedStruct;
130begin
Jens Geyer606f1ef2018-04-09 23:09:41 +0200131 WriteLn( Format( 'getStruct(%d)', [key]));
Roger Meier5c854612012-04-09 16:31:42 +0000132 result := FLog[key];
133end;
134
135
136procedure TCalculatorHandler.zip;
137begin
Jens Geyer606f1ef2018-04-09 23:09:41 +0200138 WriteLn( 'zip()');
Roger Meier5c854612012-04-09 16:31:42 +0000139end;
140
141
142//--- DelphiTutorialServer ----------------------------------------------------------------------
143
144
145class procedure DelphiTutorialServer.Main;
146var handler : TCalculator.Iface;
147 processor : IProcessor;
148 transport : IServerTransport;
149 server : IServer;
150begin
151 try
152 handler := TCalculatorHandler.Create;
153 processor := TCalculator.TProcessorImpl.Create( handler);
154 transport := TServerSocketImpl.Create( 9090);
155 server := TSimpleServer.Create( processor, transport);
156
Jens Geyer606f1ef2018-04-09 23:09:41 +0200157 WriteLn( 'Starting the server...');
Roger Meier5c854612012-04-09 16:31:42 +0000158 server.Serve();
159
160 except
Jens Geyer606f1ef2018-04-09 23:09:41 +0200161 on e: Exception do WriteLn( e.Message);
Roger Meier5c854612012-04-09 16:31:42 +0000162 end;
163
Jens Geyer606f1ef2018-04-09 23:09:41 +0200164 WriteLn('done.');
Roger Meier5c854612012-04-09 16:31:42 +0000165end;
166
167
168begin
169 try
170 DelphiTutorialServer.Main;
171 except
172 on E: Exception do
173 Writeln(E.ClassName, ': ', E.Message);
174 end;
175end.