blob: ab57ba09c3979294fcdeb881e5dc955197b04121 [file] [log] [blame]
Jake Farrell6cd63ec2012-08-29 02:04:35 +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 *)
19
20program skiptest_version1;
21
22{$APPTYPE CONSOLE}
23
24uses
25 Classes, Windows, SysUtils,
26 Skiptest.One,
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000027 Thrift in '..\..\src\Thrift.pas',
28 Thrift.Transport in '..\..\src\Thrift.Transport.pas',
29 Thrift.Protocol in '..\..\src\Thrift.Protocol.pas',
30 Thrift.Protocol.JSON in '..\..\src\Thrift.Protocol.JSON.pas',
31 Thrift.Collections in '..\..\src\Thrift.Collections.pas',
32 Thrift.Server in '..\..\src\Thrift.Server.pas',
33 Thrift.Console in '..\..\src\Thrift.Console.pas',
34 Thrift.Utils in '..\..\src\Thrift.Utils.pas',
Jens Geyereab29a02014-11-09 23:32:50 +010035 Thrift.TypeRegistry in '..\..\src\Thrift.TypeRegistry.pas',
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000036 Thrift.Stream in '..\..\src\Thrift.Stream.pas';
Jake Farrell6cd63ec2012-08-29 02:04:35 +000037
38const
39 REQUEST_EXT = '.request';
40 RESPONSE_EXT = '.response';
41
42
43function CreatePing : IPing;
44begin
45 result := TPingImpl.Create;
46 result.Version1 := Skiptest.One.TConstants.SKIPTESTSERVICE_VERSION;
47end;
48
49
50type
51 TDummyServer = class( TInterfacedObject, TSkipTestService.Iface)
52 protected
53 // TSkipTestService.Iface
54 procedure PingPong(const ping: IPing);
55 end;
56
57
58procedure TDummyServer.PingPong(const ping: IPing);
59// TSkipTestService.Iface
60begin
61 Writeln('- performing request from version '+IntToStr(ping.Version1)+' client');
62end;
63
64
65function CreateProtocol( protfact : IProtocolFactory; stm : TStream; aForInput : Boolean) : IProtocol;
66var adapt : IThriftStream;
67 trans : ITransport;
68begin
69 adapt := TThriftStreamAdapterDelphi.Create( stm, FALSE);
70 if aForInput
71 then trans := TStreamTransportImpl.Create( adapt, nil)
72 else trans := TStreamTransportImpl.Create( nil, adapt);
73 result := protfact.GetProtocol( trans);
74end;
75
76
77procedure CreateRequest( protfact : IProtocolFactory; fname : string);
78var stm : TFileStream;
79 ping : IPing;
80 proto : IProtocol;
81 client : TSkipTestService.TClient; // we need access to send/recv_pingpong()
82 cliRef : IUnknown; // holds the refcount
83begin
84 Writeln('- creating new request');
85 stm := TFileStream.Create( fname+REQUEST_EXT+'.tmp', fmCreate);
86 try
87 ping := CreatePing;
88
89 // save request data
90 proto := CreateProtocol( protfact, stm, FALSE);
91 client := TSkipTestService.TClient.Create( nil, proto);
92 cliRef := client as IUnknown;
93 client.send_PingPong( ping);
94
95 finally
96 client := nil; // not Free!
97 cliRef := nil;
98 stm.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +010099 if client = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000100 end;
101
102 DeleteFile( fname+REQUEST_EXT);
103 RenameFile( fname+REQUEST_EXT+'.tmp', fname+REQUEST_EXT);
104end;
105
106
107procedure ReadResponse( protfact : IProtocolFactory; fname : string);
108var stm : TFileStream;
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000109 proto : IProtocol;
110 client : TSkipTestService.TClient; // we need access to send/recv_pingpong()
111 cliRef : IUnknown; // holds the refcount
112begin
113 Writeln('- reading response');
114 stm := TFileStream.Create( fname+RESPONSE_EXT, fmOpenRead);
115 try
116 // save request data
117 proto := CreateProtocol( protfact, stm, TRUE);
118 client := TSkipTestService.TClient.Create( proto, nil);
119 cliRef := client as IUnknown;
120 client.recv_PingPong;
121
122 finally
123 client := nil; // not Free!
124 cliRef := nil;
125 stm.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100126 if client = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000127 end;
128end;
129
130
131procedure ProcessFile( protfact : IProtocolFactory; fname : string);
132var stmIn, stmOut : TFileStream;
133 protIn, protOut : IProtocol;
134 server : IProcessor;
135begin
136 Writeln('- processing request');
137 stmOut := nil;
138 stmIn := TFileStream.Create( fname+REQUEST_EXT, fmOpenRead);
139 try
140 stmOut := TFileStream.Create( fname+RESPONSE_EXT+'.tmp', fmCreate);
141
142 // process request and write response data
143 protIn := CreateProtocol( protfact, stmIn, TRUE);
144 protOut := CreateProtocol( protfact, stmOut, FALSE);
145
146 server := TSkipTestService.TProcessorImpl.Create( TDummyServer.Create);
147 server.Process( protIn, protOut);
148
149 finally
150 server := nil; // not Free!
151 stmIn.Free;
152 stmOut.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100153 if server = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000154 end;
155
156 DeleteFile( fname+RESPONSE_EXT);
157 RenameFile( fname+RESPONSE_EXT+'.tmp', fname+RESPONSE_EXT);
158end;
159
160
161procedure Test( protfact : IProtocolFactory; fname : string);
162begin
163 // try to read an existing request
164 if FileExists( fname + REQUEST_EXT) then begin
165 ProcessFile( protfact, fname);
166 ReadResponse( protfact, fname);
167 end;
168
169 // create a new request and try to process
170 CreateRequest( protfact, fname);
171 ProcessFile( protfact, fname);
172 ReadResponse( protfact, fname);
173end;
174
175
176const
177 FILE_BINARY = 'pingpong.bin';
178 FILE_JSON = 'pingpong.json';
179begin
180 try
181 Writeln( 'Delphi SkipTest '+IntToStr(TConstants.SKIPTESTSERVICE_VERSION)+' using '+Thrift.Version);
182
183 Writeln;
184 Writeln('Binary protocol');
185 Test( TBinaryProtocolImpl.TFactory.Create, FILE_BINARY);
186
187 Writeln;
188 Writeln('JSON protocol');
189 Test( TJSONProtocolImpl.TFactory.Create, FILE_JSON);
190
191 Writeln;
192 Writeln('Test completed without errors.');
193 Writeln;
194 Write('Press ENTER to close ...'); Readln;
195 except
196 on E: Exception do
197 Writeln(E.ClassName, ': ', E.Message);
198 end;
199end.
200