blob: fc9997a3fb348d9bf892d6af004554a6d5ac78ba [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',
Jens Geyer02230912019-04-03 01:12:51 +020037 Thrift.WinHTTP in '..\..\..\lib\delphi\src\Thrift.WinHTTP.pas',
Roger Meier5c854612012-04-09 16:31:42 +000038 Shared in '..\..\gen-delphi\Shared.pas',
39 Tutorial in '..\..\gen-delphi\Tutorial.pas';
40
41
42type
43 TCalculatorHandler = class( TInterfacedObject, TCalculator.Iface)
44 protected
45 FLog : TDictionary< Integer, ISharedStruct>;
46
47 // TSharedService.Iface
48 function getStruct(key: Integer): ISharedStruct;
49
50 // TCalculator.Iface
51 procedure ping();
52 function add(num1: Integer; num2: Integer): Integer;
53 function calculate(logid: Integer; const w: IWork): Integer;
54 procedure zip();
55
56 public
57 constructor Create;
58 destructor Destroy; override;
59
60 end;
61
62 DelphiTutorialServer = class
63 public
64 class procedure Main;
65 end;
66
67
68//--- TCalculatorHandler ---------------------------------------------------
69
70
71constructor TCalculatorHandler.Create;
72begin
73 inherited Create;
74 FLog := TDictionary< Integer, ISharedStruct>.Create();
75end;
76
77
78destructor TCalculatorHandler.Destroy;
79begin
80 try
81 FreeAndNil( FLog);
82 finally
83 inherited Destroy;
84 end;
85end;
86
87
88procedure TCalculatorHandler.ping;
89begin
Jens Geyer606f1ef2018-04-09 23:09:41 +020090 WriteLn( 'ping()');
Roger Meier5c854612012-04-09 16:31:42 +000091end;
92
93
94function TCalculatorHandler.add(num1: Integer; num2: Integer): Integer;
95begin
Jens Geyer606f1ef2018-04-09 23:09:41 +020096 WriteLn( Format( 'add( %d, %d)', [num1, num2]));
Roger Meier5c854612012-04-09 16:31:42 +000097 result := num1 + num2;
98end;
99
100
101function TCalculatorHandler.calculate(logid: Integer; const w: IWork): Integer;
102var entry : ISharedStruct;
103begin
104 try
Jens Geyer606f1ef2018-04-09 23:09:41 +0200105 WriteLn( Format('calculate( %d, [%d,%d,%d])', [logid, Ord(w.Op), w.Num1, w.Num2]));
Roger Meier5c854612012-04-09 16:31:42 +0000106
107 case w.Op of
108 TOperation.ADD : result := w.Num1 + w.Num2;
109 TOperation.SUBTRACT : result := w.Num1 - w.Num2;
110 TOperation.MULTIPLY : result := w.Num1 * w.Num2;
111 TOperation.DIVIDE : result := Round( w.Num1 / w.Num2);
112 else
113 raise TInvalidOperation.Create( Ord(w.Op), 'Unknown operation');
114 end;
115
116 except
117 on e:Thrift.TException do raise; // let Thrift Exceptions pass through
118 on e:Exception do raise TInvalidOperation.Create( Ord(w.Op), e.Message); // repackage all other
119 end;
120
121 entry := TSharedStructImpl.Create;
122 entry.Key := logid;
123 entry.Value := IntToStr( result);
124 FLog.AddOrSetValue( logid, entry);
125end;
126
127
128function TCalculatorHandler.getStruct(key: Integer): ISharedStruct;
129begin
Jens Geyer606f1ef2018-04-09 23:09:41 +0200130 WriteLn( Format( 'getStruct(%d)', [key]));
Roger Meier5c854612012-04-09 16:31:42 +0000131 result := FLog[key];
132end;
133
134
135procedure TCalculatorHandler.zip;
136begin
Jens Geyer606f1ef2018-04-09 23:09:41 +0200137 WriteLn( 'zip()');
Roger Meier5c854612012-04-09 16:31:42 +0000138end;
139
140
141//--- DelphiTutorialServer ----------------------------------------------------------------------
142
143
144class procedure DelphiTutorialServer.Main;
145var handler : TCalculator.Iface;
146 processor : IProcessor;
147 transport : IServerTransport;
148 server : IServer;
149begin
150 try
151 handler := TCalculatorHandler.Create;
152 processor := TCalculator.TProcessorImpl.Create( handler);
153 transport := TServerSocketImpl.Create( 9090);
154 server := TSimpleServer.Create( processor, transport);
155
Jens Geyer606f1ef2018-04-09 23:09:41 +0200156 WriteLn( 'Starting the server...');
Roger Meier5c854612012-04-09 16:31:42 +0000157 server.Serve();
158
159 except
Jens Geyer606f1ef2018-04-09 23:09:41 +0200160 on e: Exception do WriteLn( e.Message);
Roger Meier5c854612012-04-09 16:31:42 +0000161 end;
162
Jens Geyer606f1ef2018-04-09 23:09:41 +0200163 WriteLn('done.');
Roger Meier5c854612012-04-09 16:31:42 +0000164end;
165
166
167begin
168 try
169 DelphiTutorialServer.Main;
170 except
171 on E: Exception do
172 Writeln(E.ClassName, ': ', E.Message);
173 end;
174end.