blob: 74d0d45c6a54f7ca0806ff3b8a8dc3b3ba628fbf [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 Geyer606f1ef2018-04-09 23:09:41 +020029 Thrift.Exception in '..\..\..\lib\delphi\src\Thrift.Exception.pas',
Roger Meier5c854612012-04-09 16:31:42 +000030 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',
35 Shared in '..\..\gen-delphi\Shared.pas',
36 Tutorial in '..\..\gen-delphi\Tutorial.pas';
37
38
39type
40 DelphiTutorialClient = class
41 public
42 class procedure Main;
43 end;
44
45
46//--- DelphiTutorialClient ---------------------------------------
47
48
49class procedure DelphiTutorialClient.Main;
50var transport : ITransport;
51 protocol : IProtocol;
52 client : TCalculator.Iface;
53 work : IWork;
54 sum, quotient, diff : Integer;
55 log : ISharedStruct;
56begin
57 try
58 transport := TSocketImpl.Create( 'localhost', 9090);
59 protocol := TBinaryProtocolImpl.Create( transport);
60 client := TCalculator.TClient.Create( protocol);
61
62 transport.Open;
63
64 client.ping;
Jens Geyer606f1ef2018-04-09 23:09:41 +020065 WriteLn('ping()');
Roger Meier5c854612012-04-09 16:31:42 +000066
67 sum := client.add( 1, 1);
Jens Geyer606f1ef2018-04-09 23:09:41 +020068 WriteLn( Format( '1+1=%d', [sum]));
Roger Meier5c854612012-04-09 16:31:42 +000069
70 work := TWorkImpl.Create;
71
72 work.Op := TOperation.DIVIDE;
73 work.Num1 := 1;
74 work.Num2 := 0;
75 try
76 quotient := client.calculate(1, work);
Jens Geyer606f1ef2018-04-09 23:09:41 +020077 WriteLn( 'Whoa we can divide by 0');
78 WriteLn( Format('1/0=%d',[quotient]));
Roger Meier5c854612012-04-09 16:31:42 +000079 except
80 on io: TInvalidOperation
Jens Geyer606f1ef2018-04-09 23:09:41 +020081 do WriteLn( 'Invalid operation: ' + io.Why);
Roger Meier5c854612012-04-09 16:31:42 +000082 end;
83
84 work.Op := TOperation.SUBTRACT;
85 work.Num1 := 15;
86 work.Num2 := 10;
87 try
88 diff := client.calculate( 1, work);
Jens Geyer606f1ef2018-04-09 23:09:41 +020089 WriteLn( Format('15-10=%d', [diff]));
Roger Meier5c854612012-04-09 16:31:42 +000090 except
91 on io: TInvalidOperation
Jens Geyer606f1ef2018-04-09 23:09:41 +020092 do WriteLn( 'Invalid operation: ' + io.Why);
Roger Meier5c854612012-04-09 16:31:42 +000093 end;
94
95 log := client.getStruct(1);
Jens Geyer606f1ef2018-04-09 23:09:41 +020096 WriteLn( Format( 'Check log: %s', [log.Value]));
Roger Meier5c854612012-04-09 16:31:42 +000097
98 transport.Close();
99
100 except
101 on e : Exception
Jens Geyer606f1ef2018-04-09 23:09:41 +0200102 do WriteLn( e.ClassName+': '+e.Message);
Roger Meier5c854612012-04-09 16:31:42 +0000103 end;
104end;
105
106
107begin
108 try
109 DelphiTutorialClient.Main;
110 except
111 on E: Exception do
112 Writeln(E.ClassName, ': ', E.Message);
113 end;
114end.