VBTK/Client.pm untested

filter: Statementstmt 0.0% 0 / 148 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 223 CC 15 SCAR 21.2
CC
15
Cov
0%
getUrl
20.0
setStatus
12.0
setBaseline
12.0
CRAP 8.3
1 #! /bin/perl
2 #############################################################################
3 #
4 # NOTE: This file under revision control using RCS
5 # Any changes made without RCS will be lost
6 #
7 # $Source: /usr/local/cvsroot/vbtk/VBTK/Client.pm,v $
8 # $Revision: 1.9 $
9 # $Date: 2002/03/04 20:53:06 $
10 # $Author: bhenry $
11 # $Locker: $
12 # $State: Exp $
13 #
14 # Purpose: A common perl library used to simplify communication between
15 # the vbserver process and it's various client processes.
16 #
17 # Copyright (C) 1996 - 2002 Brent Henry
18 #
19 # This program is free software; you can redistribute it and/or
20 # modify it under the terms of version 2 of the GNU General Public
21 # License as published by the Free Software Foundation available at:
22 # http://www.gnu.org/copyleft/gpl.html
23 #
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 #
29 #############################################################################
30 #
31 #
32 # REVISION HISTORY:
33 #
34 # $Log: Client.pm,v $
35 # Revision 1.9 2002/03/04 20:53:06 bhenry
36 # *** empty log message ***
37 #
38 # Revision 1.8 2002/03/04 16:49:08 bhenry
39 # Changed requirement back to perl 5.6.0
40 #
41 # Revision 1.7 2002/03/02 00:53:54 bhenry
42 # Documentation updates
43 #
44 # Revision 1.6 2002/02/08 02:16:04 bhenry
45 # *** empty log message ***
46 #
47  
48 package VBTK::Client;
49  
50 use 5.6.0;
51 use strict;
52 use warnings;
53 # I like using undef as a value so I'm turning off the uninitialized warnings
54 no warnings qw(uninitialized);
55  
56 use VBTK::Common;
57 use LWP::UserAgent;
58 use URI::Escape;
59 use Storable qw(freeze thaw);
60  
61 our $VERSION = '0.01';
62  
63 our $VERBOSE = $ENV{VERBOSE};
64 our $SEND_TIMEOUT = 30;
65 our $RELAY_TIMEOUT = 10;
66  
67 #-------------------------------------------------------------------------------
68 # Function: new
69 # Description: Object constructor. Allocates memory for all class members
70 # Input Parms:
71 # Output Parms: Pointer to class
72 #-------------------------------------------------------------------------------
73 sub new
74 {
75 my $type = shift;
76 my $self = {};
77 bless $self, $type;
78  
79 $self->set(@_);
80  
81 $self->{RemoteURI} = $::VBURI if (! defined $self->{RemoteURI});
82  
83 my $ua = new LWP::UserAgent;
84 $ua->agent("VBTK " . $ua->agent);
85 $ua->timeout($SEND_TIMEOUT);
86  
87 $self->{ua} = $ua;
88  
89 ($self);
90 }
91  
92 #-------------------------------------------------------------------------------
93 # Function: setStatus
94 # Description: Set the status of an object on a vbserver, if any value is
95 # passed to the 4th argument, then all messages will be suppressed.
96 # Input Parms: Object Name, Status, Text Message
97 # Output Parms: Return code
98 #-------------------------------------------------------------------------------
99 sub setStatus
100 {
101 my $obj = shift;
102 my $ua = $obj->{ua};
103 my $RemoteURI = $obj->{RemoteURI};
104 my %args = @_;
105 my (@content,$key,$value,$safeValue);
106  
107 &log("Sending status to '$RemoteURI'") if ($VERBOSE > 1);
108  
109 while(($key,$value) = each %args)
110 {
111 $safeValue = &uriEscape($value);
112 push(@content,"$key=$safeValue");
113 }
114  
115 # Create a request
116 my $req = new HTTP::Request POST => "$RemoteURI/setStatusRaw.phtml";
117 $req->content_type('application/x-www-form-urlencoded');
118 $req->content( join('&',@content) );
119  
120 # Pass request to the user agent and get a response back
121 my $res = $ua->request($req);
122  
123 # Check the reply
124 if((! $res->is_success)||($res->content !~ /OK/))
125 {
126 &error("Bad result from vbserver - " . $res->content);
127 return -1;
128 }
129  
130 (0);
131 }
132  
133 #-------------------------------------------------------------------------------
134 # Function: setBaseline
135 # Description: Set the baseline text of an object on a vbserver
136 # Input Parms: Object Name, Baseline Text
137 # Output Parms: Return code
138 #-------------------------------------------------------------------------------
139 sub setBaseline
140 {
141 my $obj = shift;
142 my $ua = $obj->{ua};
143 my $RemoteURI = $obj->{RemoteURI};
144 my %args = @_;
145 my (@content,$key,$value,$safeValue);
146  
147 &log("Sending baseline to '$RemoteURI'") if ($VERBOSE > 1);
148  
149 while(($key,$value) = each %args)
150 {
151 $safeValue = &uriEscape($value);
152 push(@content,"$key=$safeValue");
153 }
154  
155 # Create a request
156 my $req = new HTTP::Request POST => "$RemoteURI/setBaselineRaw.phtml";
157 $req->content_type('application/x-www-form-urlencoded');
158 $req->content( join('&',@content) );
159  
160 # Pass request to the user agent and get a response back
161 my $res = $ua->request($req);
162  
163 # Check the reply
164 if((! $res->is_success)||($res->content !~ /OK/))
165 {
166 &error("Bad result from vbserver - " . $res->content);
167 return undef;
168 }
169  
170 (1);
171 }
172  
173 #-------------------------------------------------------------------------------
174 # Function: getStatus
175 # Description: Retrieve the status of an object
176 # Input Parms: Object Name
177 # Output Parms: Result Status
178 #-------------------------------------------------------------------------------
179 sub getStatus
180 {
181 my $obj = shift;
182 my $ua = $obj->{ua};
183 my $RemoteURI = $obj->{RemoteURI};
184  
185 my ($name) = @_;
186  
187 &log("Retrieving status for '$name'") if ($VERBOSE > 1);
188  
189 # Create a request
190 my $req = new HTTP::Request GET => "$RemoteURI/getStatusRaw.phtml?name=$name";
191 $req->content_type('application/x-www-form-urlencoded');
192  
193 # Pass request to the user agent and get a response back
194 my $res = $ua->request($req);
195  
196 # Check the reply
197 if(! $res->is_success)
198 {
199 return -1;
200 }
201  
202 # Return the resulting text
203 ($res->content);
204 }
205  
206 #-------------------------------------------------------------------------------
207 # Function: getGraphDbLastTimestamp
208 # Description: Retrieve the last timestamp in the graphdb for this object.
209 # Input Parms: Object Name
210 # Output Parms: Timestamp or Undef if object does not exist
211 #-------------------------------------------------------------------------------
212 sub getGraphDbLastTimestamp
213 {
214 my $obj = shift;
215 my $ua = $obj->{ua};
216 my $RemoteURI = $obj->{RemoteURI};
217  
218 my ($name) = @_;
219  
220 # Create a request
221 my $req = new HTTP::Request GET => "$RemoteURI/getGraphDbLastTimestamp.phtml?name=$name";
222 $req->content_type('application/x-www-form-urlencoded');
223  
224 # Pass request to the user agent and get a response back
225 my $res = $ua->request($req);
226  
227 # Check the reply
228 if(! $res->is_success)
229 {
230 return undef;
231 }
232  
233 # Return the resulting text
234 ($res->content);
235 }
236  
237 #-------------------------------------------------------------------------------
238 # Function: uriEscape
239 # Description: Escape out all non alpha-numeric characters in preparation for
240 # transmission over a HTTP request
241 # Input Parms: String
242 # Output Parms: Escaped String
243 #-------------------------------------------------------------------------------
244 sub uriEscape
245 {
246 my ($var) = @_;
247  
248 # Escape out any unsafe characters in the text
249 my $safeVar = uri_escape($var,"^A-Za-z0-9\-_.!~*'()");
250  
251 ($safeVar);
252 }
253  
254 #-------------------------------------------------------------------------------
255 # Function: getUrl
256 # Description: Access the specified URL and retrieve the html
257 # Input Parms: URL
258 # Output Parms: Resulting HTML
259 #-------------------------------------------------------------------------------
260 sub getUrl
261 {
262 my $url = shift;
263  
264 my $ua = new LWP::UserAgent;
265 $ua->agent("VBTK " . $ua->agent);
266 $ua->timeout($RELAY_TIMEOUT);
267  
268 &log("Retrieving URL '$url'") if ($VERBOSE > 1);
269  
270 # Create a request and submit it
271 my $req = new HTTP::Request GET => "$url";
272 $req->content_type('application/x-www-form-urlencoded');
273  
274 my $res = $ua->request($req);
275  
276 # Check the reply
277 if(! $res->is_success)
278 {
279 return undef;
280 }
281  
282 my $content = $res->content;
283 return undef if ($content =~ /^\s*$/);
284  
285 # Return the resulting text
286 ($res->content);
287 }
288  
289 #-------------------------------------------------------------------------------
290 # Function: getBaseline
291 # Description: Retrieve the baseline text for the specified object.
292 # Input Parms: VBObjName
293 # Output Parms: Baseline Text
294 #-------------------------------------------------------------------------------
295 sub getBaseline
296 {
297 my $obj = shift;
298 my $ua = $obj->{ua};
299 my $RemoteURI = $obj->{RemoteURI};
300  
301 my ($name) = @_;
302  
303 # Create a request
304 my $req = new HTTP::Request GET => "$RemoteURI/getBaselineRaw.phtml?name=$name";
305 $req->content_type('application/x-www-form-urlencoded');
306  
307 # Pass request to the user agent and get a response back
308 my $res = $ua->request($req);
309  
310 # Check the reply
311 if(! $res->is_success)
312 {
313 return undef;
314 }
315  
316 # Return the resulting text
317 ($res->content);
318 }
319  
320 #-------------------------------------------------------------------------------
321 # Function: getSyncFileObj
322 # Description: Retrieve a file object from the server, which will be used to
323 # sync the corresponding file on the client side
324 # Input Parms: fileName
325 # Output Parms: File Object
326 #-------------------------------------------------------------------------------
327 sub getSyncFileObj
328 {
329 my $obj = shift;
330 my $ua = $obj->{ua};
331 my $RemoteURI = $obj->{RemoteURI};
332  
333 my ($fileName) = @_;
334  
335 # Create a request
336 my $req = new HTTP::Request GET => "$RemoteURI/getSyncFileObj.phtml?fileName=$fileName";
337 $req->content_type('application/x-www-form-urlencoded');
338  
339 # Pass request to the user agent and get a response back
340 my $res = $ua->request($req);
341  
342 # Check the reply
343 if(! $res->is_success)
344 {
345 return undef;
346 }
347  
348 my $result = $res->content || return undef;
349
350 my $fileObj = &thaw($result);
351
352 ($fileObj);
353 }
354  
355 #-------------------------------------------------------------------------------
356 # Function: getSyncList
357 # Description: Retrieve a list of file objects which are in the sync list
358 # Input Parms: None
359 # Output Parms: File Object Sync List
360 #-------------------------------------------------------------------------------
361 sub getSyncList
362 {
363 my $obj = shift;
364 my $ua = $obj->{ua};
365 my $RemoteURI = $obj->{RemoteURI};
366  
367 # Create a request
368 my $req = new HTTP::Request GET => "$RemoteURI/getSyncList.phtml";
369 $req->content_type('application/x-www-form-urlencoded');
370  
371 # Pass request to the user agent and get a response back
372 my $res = $ua->request($req);
373  
374 # Check the reply
375 if(! $res->is_success)
376 {
377 return undef;
378 }
379  
380 my $result = $res->content || return undef;
381 my $struct = &thaw($result) || return undef;
382  
383 # Return an array containing file objects to be synced. The first element
384 # of the array is the value of VBHOME on the remote server.
385 @{$struct};
386 }
387  
388 1;
389 __END__