blob: 4ea9eb3e99217c53bbc08771adfa4861bd9913ca [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',
Jens Geyer02230912019-04-03 01:12:51 +020035 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 Meier5c854612012-04-09 16:31:42 +000038 Shared in '..\..\gen-delphi\Shared.pas',
39 Tutorial in '..\..\gen-delphi\Tutorial.pas';
40
41
42type
43 DelphiTutorialClient = class
44 public
45 class procedure Main;
46 end;
47
48
49//--- DelphiTutorialClient ---------------------------------------
50
51
52class procedure DelphiTutorialClient.Main;
53var transport : ITransport;
54 protocol : IProtocol;
55 client : TCalculator.Iface;
56 work : IWork;
57 sum, quotient, diff : Integer;
58 log : ISharedStruct;
59begin
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 Geyer606f1ef2018-04-09 23:09:41 +020068 WriteLn('ping()');
Roger Meier5c854612012-04-09 16:31:42 +000069
70 sum := client.add( 1, 1);
Jens Geyer606f1ef2018-04-09 23:09:41 +020071 WriteLn( Format( '1+1=%d', [sum]));
Roger Meier5c854612012-04-09 16:31:42 +000072
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 Geyer606f1ef2018-04-09 23:09:41 +020080 WriteLn( 'Whoa we can divide by 0');
81 WriteLn( Format('1/0=%d',[quotient]));
Roger Meier5c854612012-04-09 16:31:42 +000082 except
83 on io: TInvalidOperation
Jens Geyer606f1ef2018-04-09 23:09:41 +020084 do WriteLn( 'Invalid operation: ' + io.Why);
Roger Meier5c854612012-04-09 16:31:42 +000085 end;
86
87 work.Op := TOperation.SUBTRACT;
88 work.Num1 := 15;
89 work.Num2 := 10;
90 try
91 diff := client.calculate( 1, work);
Jens Geyer606f1ef2018-04-09 23:09:41 +020092 WriteLn( Format('15-10=%d', [diff]));
Roger Meier5c854612012-04-09 16:31:42 +000093 except
94 on io: TInvalidOperation
Jens Geyer606f1ef2018-04-09 23:09:41 +020095 do WriteLn( 'Invalid operation: ' + io.Why);
Roger Meier5c854612012-04-09 16:31:42 +000096 end;
97
98 log := client.getStruct(1);
Jens Geyer606f1ef2018-04-09 23:09:41 +020099 WriteLn( Format( 'Check log: %s', [log.Value]));
Roger Meier5c854612012-04-09 16:31:42 +0000100
101 transport.Close();
102
103 except
104 on e : Exception
Jens Geyer606f1ef2018-04-09 23:09:41 +0200105 do WriteLn( e.ClassName+': '+e.Message);
Roger Meier5c854612012-04-09 16:31:42 +0000106 end;
107end;
108
109
110begin
111 try
112 DelphiTutorialClient.Main;
113 except
114 on E: Exception do
115 Writeln(E.ClassName, ': ', E.Message);
116 end;
117end.