blob: 367b5e7755a4d0df42136dc42309219fada32a5a [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,
27 Thrift in '..\..\..\lib\delphi\src\Thrift.pas',
28 Thrift.Transport in '..\..\..\lib\delphi\src\Thrift.Transport.pas',
29 Thrift.Protocol in '..\..\..\lib\delphi\src\Thrift.Protocol.pas',
30 Thrift.Protocol.JSON in '..\..\..\lib\delphi\src\Thrift.Protocol.JSON.pas',
31 Thrift.Collections in '..\..\..\lib\delphi\src\Thrift.Collections.pas',
32 Thrift.Server in '..\..\..\lib\delphi\src\Thrift.Server.pas',
33 Thrift.Console in '..\..\..\lib\delphi\src\Thrift.Console.pas',
34 Thrift.Utils in '..\..\..\lib\delphi\src\Thrift.Utils.pas',
35 Thrift.Stream in '..\..\..\lib\delphi\src\Thrift.Stream.pas';
36
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;
108 ping : IPing;
109 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;
126 if client = nil then {warning supressed};
127 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;
153 if server = nil then {warning supressed};
154 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