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