blob: 0f380b0a6981a49013f89ba92ded723ce0ceb2c5 [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',
29 Thrift.Console in '..\..\..\lib\delphi\src\Thrift.Console.pas',
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',
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;
65 Console.WriteLine('ping()');
66
67 sum := client.add( 1, 1);
68 Console.WriteLine( Format( '1+1=%d', [sum]));
69
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);
77 Console.WriteLine( 'Whoa we can divide by 0');
78 Console.WriteLine( Format('1/0=%d',[quotient]));
79 except
80 on io: TInvalidOperation
81 do Console.WriteLine( 'Invalid operation: ' + io.Why);
82 end;
83
84 work.Op := TOperation.SUBTRACT;
85 work.Num1 := 15;
86 work.Num2 := 10;
87 try
88 diff := client.calculate( 1, work);
89 Console.WriteLine( Format('15-10=%d', [diff]));
90 except
91 on io: TInvalidOperation
92 do Console.WriteLine( 'Invalid operation: ' + io.Why);
93 end;
94
95 log := client.getStruct(1);
96 Console.WriteLine( Format( 'Check log: %s', [log.Value]));
97
98 transport.Close();
99
100 except
101 on e : Exception
102 do Console.WriteLine( e.ClassName+': '+e.Message);
103 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.