blob: 0bfe96fef04480297518bc58ef0285cabe853cf5 [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',
Jens Geyer606f1ef2018-04-09 23:09:41 +020028 Thrift.Exception in '..\..\src\Thrift.Exception.pas',
Jens Geyerbea9bbe2016-04-20 00:02:40 +020029 Thrift.Socket in '..\..\src\Thrift.Socket.pas',
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000030 Thrift.Transport in '..\..\src\Thrift.Transport.pas',
31 Thrift.Protocol in '..\..\src\Thrift.Protocol.pas',
32 Thrift.Protocol.JSON in '..\..\src\Thrift.Protocol.JSON.pas',
33 Thrift.Collections in '..\..\src\Thrift.Collections.pas',
34 Thrift.Server in '..\..\src\Thrift.Server.pas',
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000035 Thrift.Utils in '..\..\src\Thrift.Utils.pas',
Jens Geyer02230912019-04-03 01:12:51 +020036 Thrift.WinHTTP in '..\..\src\Thrift.WinHTTP.pas',
Jens Geyereab29a02014-11-09 23:32:50 +010037 Thrift.TypeRegistry in '..\..\src\Thrift.TypeRegistry.pas',
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000038 Thrift.Stream in '..\..\src\Thrift.Stream.pas';
Jake Farrell6cd63ec2012-08-29 02:04:35 +000039
40const
41 REQUEST_EXT = '.request';
42 RESPONSE_EXT = '.response';
43
44
45function CreatePing : IPing;
46begin
47 result := TPingImpl.Create;
Jens Geyer17c3ad92017-09-05 20:31:27 +020048 result.Version1 := Tskiptest_version_1Constants.SKIPTESTSERVICE_VERSION;
Jake Farrell6cd63ec2012-08-29 02:04:35 +000049end;
50
51
52type
53 TDummyServer = class( TInterfacedObject, TSkipTestService.Iface)
54 protected
55 // TSkipTestService.Iface
56 procedure PingPong(const ping: IPing);
57 end;
58
59
60procedure TDummyServer.PingPong(const ping: IPing);
61// TSkipTestService.Iface
62begin
63 Writeln('- performing request from version '+IntToStr(ping.Version1)+' client');
64end;
65
66
67function CreateProtocol( protfact : IProtocolFactory; stm : TStream; aForInput : Boolean) : IProtocol;
68var adapt : IThriftStream;
69 trans : ITransport;
70begin
71 adapt := TThriftStreamAdapterDelphi.Create( stm, FALSE);
72 if aForInput
73 then trans := TStreamTransportImpl.Create( adapt, nil)
74 else trans := TStreamTransportImpl.Create( nil, adapt);
75 result := protfact.GetProtocol( trans);
76end;
77
78
79procedure CreateRequest( protfact : IProtocolFactory; fname : string);
80var stm : TFileStream;
81 ping : IPing;
82 proto : IProtocol;
83 client : TSkipTestService.TClient; // we need access to send/recv_pingpong()
84 cliRef : IUnknown; // holds the refcount
85begin
86 Writeln('- creating new request');
87 stm := TFileStream.Create( fname+REQUEST_EXT+'.tmp', fmCreate);
88 try
89 ping := CreatePing;
90
91 // save request data
92 proto := CreateProtocol( protfact, stm, FALSE);
93 client := TSkipTestService.TClient.Create( nil, proto);
94 cliRef := client as IUnknown;
95 client.send_PingPong( ping);
96
97 finally
98 client := nil; // not Free!
99 cliRef := nil;
100 stm.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100101 if client = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000102 end;
103
104 DeleteFile( fname+REQUEST_EXT);
105 RenameFile( fname+REQUEST_EXT+'.tmp', fname+REQUEST_EXT);
106end;
107
108
109procedure ReadResponse( protfact : IProtocolFactory; fname : string);
110var stm : TFileStream;
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000111 proto : IProtocol;
112 client : TSkipTestService.TClient; // we need access to send/recv_pingpong()
113 cliRef : IUnknown; // holds the refcount
114begin
115 Writeln('- reading response');
116 stm := TFileStream.Create( fname+RESPONSE_EXT, fmOpenRead);
117 try
118 // save request data
119 proto := CreateProtocol( protfact, stm, TRUE);
120 client := TSkipTestService.TClient.Create( proto, nil);
121 cliRef := client as IUnknown;
122 client.recv_PingPong;
123
124 finally
125 client := nil; // not Free!
126 cliRef := nil;
127 stm.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100128 if client = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000129 end;
130end;
131
132
133procedure ProcessFile( protfact : IProtocolFactory; fname : string);
134var stmIn, stmOut : TFileStream;
135 protIn, protOut : IProtocol;
136 server : IProcessor;
137begin
138 Writeln('- processing request');
139 stmOut := nil;
140 stmIn := TFileStream.Create( fname+REQUEST_EXT, fmOpenRead);
141 try
142 stmOut := TFileStream.Create( fname+RESPONSE_EXT+'.tmp', fmCreate);
143
144 // process request and write response data
145 protIn := CreateProtocol( protfact, stmIn, TRUE);
146 protOut := CreateProtocol( protfact, stmOut, FALSE);
147
148 server := TSkipTestService.TProcessorImpl.Create( TDummyServer.Create);
149 server.Process( protIn, protOut);
150
151 finally
152 server := nil; // not Free!
153 stmIn.Free;
154 stmOut.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100155 if server = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000156 end;
157
158 DeleteFile( fname+RESPONSE_EXT);
159 RenameFile( fname+RESPONSE_EXT+'.tmp', fname+RESPONSE_EXT);
160end;
161
162
163procedure Test( protfact : IProtocolFactory; fname : string);
164begin
165 // try to read an existing request
166 if FileExists( fname + REQUEST_EXT) then begin
167 ProcessFile( protfact, fname);
168 ReadResponse( protfact, fname);
169 end;
170
171 // create a new request and try to process
172 CreateRequest( protfact, fname);
173 ProcessFile( protfact, fname);
174 ReadResponse( protfact, fname);
175end;
176
177
178const
179 FILE_BINARY = 'pingpong.bin';
180 FILE_JSON = 'pingpong.json';
181begin
182 try
Jens Geyer17c3ad92017-09-05 20:31:27 +0200183 Writeln( 'Delphi SkipTest '+IntToStr(Tskiptest_version_1Constants.SKIPTESTSERVICE_VERSION)+' using '+Thrift.Version);
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000184
185 Writeln;
186 Writeln('Binary protocol');
187 Test( TBinaryProtocolImpl.TFactory.Create, FILE_BINARY);
188
189 Writeln;
190 Writeln('JSON protocol');
191 Test( TJSONProtocolImpl.TFactory.Create, FILE_JSON);
192
193 Writeln;
194 Writeln('Test completed without errors.');
195 Writeln;
196 Write('Press ENTER to close ...'); Readln;
197 except
198 on E: Exception do
199 Writeln(E.ClassName, ': ', E.Message);
200 end;
201end.
202