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