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