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 DelphiClient; |
| 20 | |
| 21 | {$APPTYPE CONSOLE} |
| 22 | {$D 'Copyright (c) 2012 The Apache Software Foundation'} |
| 23 | |
| 24 | uses |
| 25 | SysUtils, |
| 26 | Generics.Collections, |
| 27 | Thrift in '..\..\..\lib\delphi\src\Thrift.pas', |
| 28 | Thrift.Collections in '..\..\..\lib\delphi\src\Thrift.Collections.pas', |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 29 | Thrift.Configuration in '..\..\..\lib\delphi\src\Thrift.Configuration.pas', |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 30 | Thrift.Exception in '..\..\..\lib\delphi\src\Thrift.Exception.pas', |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 31 | Thrift.Utils in '..\..\..\lib\delphi\src\Thrift.Utils.pas', |
| 32 | Thrift.Stream in '..\..\..\lib\delphi\src\Thrift.Stream.pas', |
| 33 | Thrift.Protocol in '..\..\..\lib\delphi\src\Thrift.Protocol.pas', |
| 34 | Thrift.Server in '..\..\..\lib\delphi\src\Thrift.Server.pas', |
| 35 | Thrift.Transport in '..\..\..\lib\delphi\src\Thrift.Transport.pas', |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 36 | Thrift.Transport.WinHTTP in '..\..\..\lib\delphi\src\Thrift.Transport.WinHTTP.pas', |
| 37 | Thrift.Transport.MsxmlHTTP in '..\..\..\lib\delphi\src\Thrift.Transport.MsxmlHTTP.pas', |
| 38 | Thrift.WinHTTP in '..\..\..\lib\delphi\src\Thrift.WinHTTP.pas', |
Jens Geyer | 66f9536 | 2021-03-29 20:35:41 +0200 | [diff] [blame^] | 39 | Shared in '..\gen-delphi\Shared.pas', |
| 40 | Tutorial in '..\gen-delphi\Tutorial.pas'; |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | type |
| 44 | DelphiTutorialClient = class |
| 45 | public |
| 46 | class procedure Main; |
| 47 | end; |
| 48 | |
| 49 | |
| 50 | //--- DelphiTutorialClient --------------------------------------- |
| 51 | |
| 52 | |
| 53 | class procedure DelphiTutorialClient.Main; |
| 54 | var transport : ITransport; |
| 55 | protocol : IProtocol; |
| 56 | client : TCalculator.Iface; |
| 57 | work : IWork; |
| 58 | sum, quotient, diff : Integer; |
| 59 | log : ISharedStruct; |
| 60 | begin |
| 61 | try |
| 62 | transport := TSocketImpl.Create( 'localhost', 9090); |
| 63 | protocol := TBinaryProtocolImpl.Create( transport); |
| 64 | client := TCalculator.TClient.Create( protocol); |
| 65 | |
| 66 | transport.Open; |
| 67 | |
| 68 | client.ping; |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 69 | WriteLn('ping()'); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 70 | |
| 71 | sum := client.add( 1, 1); |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 72 | WriteLn( Format( '1+1=%d', [sum])); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 73 | |
| 74 | work := TWorkImpl.Create; |
| 75 | |
| 76 | work.Op := TOperation.DIVIDE; |
| 77 | work.Num1 := 1; |
| 78 | work.Num2 := 0; |
| 79 | try |
| 80 | quotient := client.calculate(1, work); |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 81 | WriteLn( 'Whoa we can divide by 0'); |
| 82 | WriteLn( Format('1/0=%d',[quotient])); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 83 | except |
| 84 | on io: TInvalidOperation |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 85 | do WriteLn( 'Invalid operation: ' + io.Why); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 86 | end; |
| 87 | |
| 88 | work.Op := TOperation.SUBTRACT; |
| 89 | work.Num1 := 15; |
| 90 | work.Num2 := 10; |
| 91 | try |
| 92 | diff := client.calculate( 1, work); |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 93 | WriteLn( Format('15-10=%d', [diff])); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 94 | except |
| 95 | on io: TInvalidOperation |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 96 | do WriteLn( 'Invalid operation: ' + io.Why); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 97 | end; |
| 98 | |
| 99 | log := client.getStruct(1); |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 100 | WriteLn( Format( 'Check log: %s', [log.Value])); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 101 | |
| 102 | transport.Close(); |
| 103 | |
| 104 | except |
| 105 | on e : Exception |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 106 | do WriteLn( e.ClassName+': '+e.Message); |
Roger Meier | 5c85461 | 2012-04-09 16:31:42 +0000 | [diff] [blame] | 107 | end; |
| 108 | end; |
| 109 | |
| 110 | |
| 111 | begin |
| 112 | try |
| 113 | DelphiTutorialClient.Main; |
| 114 | except |
| 115 | on E: Exception do |
| 116 | Writeln(E.ClassName, ': ', E.Message); |
| 117 | end; |
| 118 | end. |