Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 1 | (* |
| 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 | program DelphiServer; |
| 20 | |
| 21 | {$APPTYPE CONSOLE} |
| 22 | {$D 'Copyright (c) 2012 The Apache Software Foundation'} |
| 23 | |
| 24 | {$Q+} // throws exceptions on numeric overflows |
| 25 | |
| 26 | uses |
| 27 | SysUtils, |
| 28 | Generics.Collections, |
| 29 | Thrift in '..\..\..\lib\delphi\src\Thrift.pas', |
| 30 | Thrift.Collections in '..\..\..\lib\delphi\src\Thrift.Collections.pas', |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 31 | Thrift.Configuration in '..\..\..\lib\delphi\src\Thrift.Configuration.pas', |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 32 | Thrift.Exception in '..\..\..\lib\delphi\src\Thrift.Exception.pas', |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 33 | 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 Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 38 | Thrift.WinHTTP in '..\..\..\lib\delphi\src\Thrift.WinHTTP.pas', |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 39 | Shared in '..\..\gen-delphi\Shared.pas', |
| 40 | Tutorial in '..\..\gen-delphi\Tutorial.pas'; |
| 41 | |
| 42 | |
| 43 | type |
| 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 | |
| 72 | constructor TCalculatorHandler.Create; |
| 73 | begin |
| 74 | inherited Create; |
| 75 | FLog := TDictionary< Integer, ISharedStruct>.Create(); |
| 76 | end; |
| 77 | |
| 78 | |
| 79 | destructor TCalculatorHandler.Destroy; |
| 80 | begin |
| 81 | try |
| 82 | FreeAndNil( FLog); |
| 83 | finally |
| 84 | inherited Destroy; |
| 85 | end; |
| 86 | end; |
| 87 | |
| 88 | |
| 89 | procedure TCalculatorHandler.ping; |
| 90 | begin |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 91 | WriteLn( 'ping()'); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 92 | end; |
| 93 | |
| 94 | |
| 95 | function TCalculatorHandler.add(num1: Integer; num2: Integer): Integer; |
| 96 | begin |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 97 | WriteLn( Format( 'add( %d, %d)', [num1, num2])); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 98 | result := num1 + num2; |
| 99 | end; |
| 100 | |
| 101 | |
| 102 | function TCalculatorHandler.calculate(logid: Integer; const w: IWork): Integer; |
| 103 | var entry : ISharedStruct; |
| 104 | begin |
| 105 | try |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 106 | WriteLn( Format('calculate( %d, [%d,%d,%d])', [logid, Ord(w.Op), w.Num1, w.Num2])); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 107 | |
| 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); |
| 126 | end; |
| 127 | |
| 128 | |
| 129 | function TCalculatorHandler.getStruct(key: Integer): ISharedStruct; |
| 130 | begin |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 131 | WriteLn( Format( 'getStruct(%d)', [key])); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 132 | result := FLog[key]; |
| 133 | end; |
| 134 | |
| 135 | |
| 136 | procedure TCalculatorHandler.zip; |
| 137 | begin |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 138 | WriteLn( 'zip()'); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 139 | end; |
| 140 | |
| 141 | |
| 142 | //--- DelphiTutorialServer ---------------------------------------------------------------------- |
| 143 | |
| 144 | |
| 145 | class procedure DelphiTutorialServer.Main; |
| 146 | var handler : TCalculator.Iface; |
| 147 | processor : IProcessor; |
| 148 | transport : IServerTransport; |
| 149 | server : IServer; |
| 150 | begin |
| 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 Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 157 | WriteLn( 'Starting the server...'); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 158 | server.Serve(); |
| 159 | |
| 160 | except |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 161 | on e: Exception do WriteLn( e.Message); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 162 | end; |
| 163 | |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 164 | WriteLn('done.'); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 165 | end; |
| 166 | |
| 167 | |
| 168 | begin |
| 169 | try |
| 170 | DelphiTutorialServer.Main; |
| 171 | except |
| 172 | on E: Exception do |
| 173 | Writeln(E.ClassName, ': ', E.Message); |
| 174 | end; |
| 175 | end. |