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