blob: e44ae3b5ceebc30b4eb22b42d5705218e36ab118 [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 DelphiClient;
20
21{$APPTYPE CONSOLE}
22{$D 'Copyright (c) 2012 The Apache Software Foundation'}
23
24uses
25 SysUtils,
26 Generics.Collections,
27 Thrift in '..\..\..\lib\delphi\src\Thrift.pas',
28 Thrift.Collections in '..\..\..\lib\delphi\src\Thrift.Collections.pas',
Jens Geyera019cda2019-11-09 23:24:52 +010029 Thrift.Configuration in '..\..\..\lib\delphi\src\Thrift.Configuration.pas',
Jens Geyer606f1ef2018-04-09 23:09:41 +020030 Thrift.Exception in '..\..\..\lib\delphi\src\Thrift.Exception.pas',
Roger Meier5c854612012-04-09 16:31:42 +000031 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 Geyer02230912019-04-03 01:12:51 +020036 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 Geyer66f95362021-03-29 20:35:41 +020039 Shared in '..\gen-delphi\Shared.pas',
40 Tutorial in '..\gen-delphi\Tutorial.pas';
Roger Meier5c854612012-04-09 16:31:42 +000041
42
43type
44 DelphiTutorialClient = class
45 public
46 class procedure Main;
47 end;
48
49
50//--- DelphiTutorialClient ---------------------------------------
51
52
53class procedure DelphiTutorialClient.Main;
54var transport : ITransport;
55 protocol : IProtocol;
56 client : TCalculator.Iface;
57 work : IWork;
58 sum, quotient, diff : Integer;
59 log : ISharedStruct;
60begin
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 Geyer606f1ef2018-04-09 23:09:41 +020069 WriteLn('ping()');
Roger Meier5c854612012-04-09 16:31:42 +000070
71 sum := client.add( 1, 1);
Jens Geyer606f1ef2018-04-09 23:09:41 +020072 WriteLn( Format( '1+1=%d', [sum]));
Roger Meier5c854612012-04-09 16:31:42 +000073
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 Geyer606f1ef2018-04-09 23:09:41 +020081 WriteLn( 'Whoa we can divide by 0');
82 WriteLn( Format('1/0=%d',[quotient]));
Roger Meier5c854612012-04-09 16:31:42 +000083 except
84 on io: TInvalidOperation
Jens Geyer606f1ef2018-04-09 23:09:41 +020085 do WriteLn( 'Invalid operation: ' + io.Why);
Roger Meier5c854612012-04-09 16:31:42 +000086 end;
87
88 work.Op := TOperation.SUBTRACT;
89 work.Num1 := 15;
90 work.Num2 := 10;
91 try
92 diff := client.calculate( 1, work);
Jens Geyer606f1ef2018-04-09 23:09:41 +020093 WriteLn( Format('15-10=%d', [diff]));
Roger Meier5c854612012-04-09 16:31:42 +000094 except
95 on io: TInvalidOperation
Jens Geyer606f1ef2018-04-09 23:09:41 +020096 do WriteLn( 'Invalid operation: ' + io.Why);
Roger Meier5c854612012-04-09 16:31:42 +000097 end;
98
99 log := client.getStruct(1);
Jens Geyer606f1ef2018-04-09 23:09:41 +0200100 WriteLn( Format( 'Check log: %s', [log.Value]));
Roger Meier5c854612012-04-09 16:31:42 +0000101
102 transport.Close();
103
104 except
105 on e : Exception
Jens Geyer606f1ef2018-04-09 23:09:41 +0200106 do WriteLn( e.ClassName+': '+e.Message);
Roger Meier5c854612012-04-09 16:31:42 +0000107 end;
108end;
109
110
111begin
112 try
113 DelphiTutorialClient.Main;
114 except
115 on E: Exception do
116 Writeln(E.ClassName, ': ', E.Message);
117 end;
118end.