RPC/pClient.pm

filter: Statementstmt 66.8% 107 / 160 Branchbran 45.0% 36 / 80 Conditioncond 41.6% 5 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 93.7% 15 / 16 Podpod 80.0% 4 / 5 totaltotal 59.7% 168 / 281 CC 45 SCAR 29.9
CC
45
Cov
59%
new
40.9
_Retrieve
23.2
Encrypt
20.0
CRAP 19.8
« RPC/pServer.pm ↑ Summary Bundle/pRPC-modules.pm »
1 # -*- perl -*-
2 #
3 #
4 # pRPC - Perl RPC, package for writing simple, RPC like clients and
5 # servers
6 #
7 # RPC::pClient.pm is the module for writing the pRPC client.
8 #
9 #
10 # Copyright (c) 1997 Jochen Wiedmann
11 #
12 # You may distribute under the terms of either the GNU General Public
13 # License or the Artistic License, as specified in the Perl README file.
14 #
15 # Author: Jochen Wiedmann
16 # Am Eisteich 9
17 # 72555 Metzingen
18 # Germany
19 #
20 # Email: wiedmann@neckar-alb.de
21 # Phone: +49 7123 14881
22 #
23 #
24 # $Id: pClient.pm,v 0.1001 1997/09/14 22:53:27 joe Exp $
25 #
26 package RPC::pClient;
27  
28 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 7 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 7 require Exporter;
33 7 require DynaLoader;
34  
35 7 @ISA = qw(Exporter DynaLoader);
36 # Items to export into callers namespace by default. Note: do not export
37 # names by default without a very good reason. Use EXPORT_OK instead.
38 # Do not simply export all your public functions/methods/constants.
39 7 @EXPORT = qw(
40
41 );
42 7 $VERSION = '0.1002';
43  
44 7 ▶use POSIX();
Subroutine BEGIN: called CC=1 SCAR=0.0
45 7 ▶use Sys::Syslog();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 7 ▶use IO::Socket();
Subroutine BEGIN: called CC=1 SCAR=0.0
47 7 ▶use Storable();
Subroutine BEGIN: called CC=1 SCAR=0.0
48 7 ▶use Socket();
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50  
51 52 ▶sub error ($) { my $self = shift; $self->{'error'}; }
Subroutine error: called CC=1 SCAR=0.0
Pod undocumented
52  
53  
54 ############################################################################
55 #
56 # Name: new
57 #
58 # Purpose: Constructor of the pRPC::Client module
59 #
60 # Inputs: Hash list of attributes; see pRPC::Client(3)
61 #
62 # Returns: connection object or error message
63 #
64 ############################################################################
65  
66 2 ▶sub new ($@) {
Subroutine new: called CC=14 SCAR=37.1
Pod documented
67 2 my ($proto) = shift;
68 2 ▶ my ($class) = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
69 2 my ($self) = {@_};
70  
71 2 bless($self, $class);
72  
73 2 ▶ if (!defined($self->{'application'}) || !defined($self->{'version'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"application"} and defined $self->{"version"}) 0 2 2
Condition33%1/3
defined $self->{"application"} and defined $self->{"version"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self->{"application"} and defined $self->{"version"}
defined $self->{"application"} defined $self->{"version"}
Truth table33%1/3
A and B
A= defined $self->{"application"}B= defined $self->{"version"}
ABresult
0X0
100
111
74 0 return "Required attributes 'application' or 'version' missing.";
75 }
76  
77 #
78 # Create Storable objects and send the login message.
79 #
80 2 ▶ if ($self->_HaveOoStorable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_HaveOoStorable) { } 0 2 2
81 0 $self->{'io'} = Storable->new('file' => *{$self->{'sock'}}{IO},
82 0 'crypt' => $self->{'cipher'},
83 'netorder' => 1,
84 'forgive_me' => 1);
85 0 ▶ if (!defined($self->{'io'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"io"}) 0 0 0
86 0 return "Cannot create Storable object: $!";
87 }
88 } else {
89 2 $self->{'file'} = $self->{'sock'};
90 }
91  
92 2 ▶ if ($self->{'debug'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 2 0 2
93 Sys::Syslog::syslog('debug', "Sending login message: %s %s %s",
94 $self->{'application'}, $self->{'version'},
95 2 $self->{'user'});
96 }
97 2 ▶ if (!$self->_Store([$self->{'application'},
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_Store([$self->{"application"}, $self->{"version"}, $self->{"user"}, $self->{"password"}])) 0 2 2
98 $self->{'version'},
99 $self->{'user'},
100 $self->{'password'}])) {
101 0 return "Cannot send login message: " . $self->{'error'};
102 }
103
104 2 ▶ if ($self->{'debug'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 2 0 2
105 2 Sys::Syslog::syslog('debug', "Waiting for server's response ...");
106 }
107 2 my ($msg) = $self->_Retrieve();
108 2 ▶ if (!$msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg) 0 2 2
109 0 $msg = "Error while reading server reply: " . $self->{'error'};
110 0 Sys::Syslog::syslog('debug', $msg);
111 0 return $msg;
112 }
113 2 ▶ if (ref($msg) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg ne "ARRAY") 0 2 2
114 0 $msg = "Error while reading server reply: Expected array";
115 0 Sys::Syslog::syslog('debug', $msg);
116 0 return $msg;
117 }
118  
119 2 ▶ if (!$$msg[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg->[0]) 0 2 2
120 0 ▶ $msg = "Refused by server: "
Branch: 1 branch
BranchTrueFalseTotal
defined $msg->[1] ? : 0 0 0
121 . (defined($$msg[1]) ? $$msg[1] : "No cause");
122 0 Sys::Syslog::syslog('debug', $msg);
123 0 return $msg;
124 }
125  
126 2 ▶ Sys::Syslog::syslog('debug', "Logged in, server replies %s",
Branch: 1 branch
BranchTrueFalseTotal
defined $msg->[1] ? : 0 2 2
127 defined($$msg[1]) ? $$msg[1] : "undef");
128 2 $self->{'error'} = '';
129 2 $self;
130 }
131  
132  
133 ############################################################################
134 #
135 # Name: Call, CallInt
136 #
137 # Purpose: coerce method located on the server
138 #
139 # Inputs: $con - connection attributes
140 # $method - method name
141 # @args - method attributes
142 #
143 # Returns: method results; you *must* check $con->error for potential
144 # error conditions
145 #
146 ############################################################################
147  
148 24 ▶sub CallInt ($@) {
Subroutine CallInt: called CC=11 SCAR=26.3
Pod documented
149 24 my($self) = shift;
150 24 my($error, $msg, @result);
151  
152 24 ▶ if (!$self->_Store([@_])) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_Store([@_])) { } 0 24 24
153 0 $error = $self->{'error'};
154 } else {
155 24 $msg = $self->_Retrieve();
156 24 ▶ if (!$msg) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $msg) { } 0 24 24
elsif (ref $msg ne 'ARRAY') { } 0 24 24
elsif (not defined $msg->[0] && $msg->[0]) { } 4 20 24
157 0 $error = $self->{'error'};
158 } elsif (ref($msg) ne 'ARRAY') {
159 0 $error = "Error while reading server reply: Expected array";
160 ▶ } elsif (!defined($$msg[0]) || !$$msg[0]) {
Condition66%2/3
defined $msg->[0] && $msg->[0]
!ll&&!rl&&r
0420
MC/DC50%1/2
defined $msg->[0] && $msg->[0]
defined $msg->[0] $msg->[0]
Truth table66%2/3
A && B
A= defined $msg->[0]B= $msg->[0]
ABresult
0X0
100
111
161 4 ▶ if (defined($$msg[1]) && $$msg[1] ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msg->[1] and $msg->[1] ne '') { } 4 0 4
Condition33%1/3
defined $msg->[1] and $msg->[1] ne ""
!ll&&!rl&&r
004
MC/DC0%0/2
defined $msg->[1] and $msg->[1] ne ""
defined $msg->[1] $msg->[1] ne ""
Truth table33%1/3
A and B
A= defined $msg->[1]B= $msg->[1] ne ""
ABresult
0X0
100
111
162 4 $error = $$msg[1];
163 } else {
164 0 $error = "No error message";
165 }
166 } else {
167 20 $error = '';
168 20 @result = @$msg;
169 }
170 }
171  
172 24 ▶ if ($self->{'error'} = $error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"error"} = $error) 4 20 24
173 4 @result = (0, $error);
174 }
175  
176 24 ▶ if ($self->{'debug'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 24 0 24
177 24 ▶ if ($self->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error) { } 4 20 24
178 4 Sys::Syslog::syslog('err', "Calling method %s -> error %s",
179 $_[0], $self->error);
180 } else {
181 20 Sys::Syslog::syslog('debug', "Calling method %s -> ok",
182 $_[0]);
183 }
184 }
185  
186 24 @result;
187 }
188  
189 24 ▶sub Call ($@) {
Subroutine Call: called CC=2 SCAR=6.9
Pod documented
190 24 my($self) = shift;
191 24 my(@result) = $self->CallInt(@_);
192  
193 24 ▶ if (!shift @result) {
Branch: 1 branch
BranchTrueFalseTotal
unless (shift @result) 4 20 24
194 4 @result = ();
195 }
196  
197 24 @result;
198 }
199  
200  
201 ############################################################################
202 #
203 # Name: Encrypt
204 #
205 # Purpose: Get or set the current encryption mode
206 #
207 # Inputs: $self - client object
208 # $crypt - encryption object
209 #
210 # Returns: current encryption object; 'undef' for no encryption
211 #
212 ############################################################################
213  
214 0 ▶sub Encrypt ($;$) {
Subroutine Encrypt: not called CC=4 SCAR=30.0
Pod documented
215 0 my ($self, $crypt) = @_;
216 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
217 0 ▶ if ($self->_HaveOoStorable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_HaveOoStorable) { } 0 0 0
218 0 $self->{'io'}->{'crypt'} = $crypt;
219 } else {
220 0 $self->{'cipher'} = $crypt;
221 }
222 }
223 0 ▶ $self->_HaveOoStorable ? $self->{'io'}->{'crypt'} : $self->{'cipher'};
Branch: 1 branch
BranchTrueFalseTotal
$self->_HaveOoStorable ? : 0 0 0
224 }
225  
226  
227 ############################################################################
228 #
229 # Name: _Store, _Retrieve
230 #
231 # Purpose: Preliminary replacements for Storable->Store and
232 # Storable->Retrieve as long as Raphael hasn't integrated
233 # my suggestion for an OO API.
234 #
235 # Inputs: $self - server object
236 # $msg - message being sent (_Store only)
237 #
238 # Returns: _Retrieve returns a message in case of success. Both
239 # methods return FALSE in case of error, $self->{'error'}
240 # will be set in that case.
241 #
242 ############################################################################
243  
244 7 $RPC::pClient::haveOoStorable = undef;
245 108 ▶sub _HaveOoStorable () {
Subroutine _HaveOoStorable: called CC=3 SCAR=11.0
246 108 ▶ if (!defined($RPC::pClient::haveOoStorable)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $RPC::pClient::haveOoStorable) 2 106 108
247 2 $@ = '';
248 2 eval "Storable->new()";
249 2 ▶ $RPC::pClient::haveOoStorable = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 2 0 2
250 }
251 108 $RPC::pClient::haveOoStorable;
252 }
253  
254 52 ▶sub _Retrieve($) {
Subroutine _Retrieve: called CC=11 SCAR=31.4
255 52 my($self) = @_;
256 52 my($result);
257  
258 52 ▶ if ($self->_HaveOoStorable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_HaveOoStorable) 0 52 52
259 0 ▶ if (!($result = $self->{'io'}->Retrieve())) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result = $self->{"io"}->Retrieve) 0 0 0
260 0 $self->{'error'} = $self->{'io'}->errstr;
261 }
262 0 return $result;
263 }
264  
265 52 my($encodedSize, $readSize, $blockSize);
266 52 $readSize = 4;
267 52 $encodedSize = '';
268 52 ▶ while ($readSize > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($readSize > 0) 52 52 104
269 52 my $result = $self->{'file'}->read($encodedSize, $readSize,
270 length($encodedSize));
271 52 ▶ if ($result < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result < 0) 0 52 52
272 0 $self->{'error'} = "Error while reading: $!";
273 0 return undef;
274 }
275 52 $readSize -= $result;
276 }
277 52 $encodedSize = unpack("N", $encodedSize);
278 52 $readSize = $encodedSize;
279 52 ▶ if ($self->{'cipher'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cipher"}) 0 52 52
280 0 $blockSize = $self->{'cipher'}->blocksize;
281 0 ▶ if (my $addSize = ($encodedSize % $blockSize)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $addSize = $encodedSize % $blockSize) 0 0 0
282 0 $readSize += ($blockSize - $addSize);
283 }
284 }
285 52 my $msg = '';
286 52 my $rs = $readSize;
287 52 ▶ while ($rs > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rs > 0) 52 52 104
288 52 my $result = read($self->{'file'}, $msg, $rs, length($msg));
289 52 ▶ if ($result < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result < 0) 0 52 52
290 0 $self->{'error'} = "Error while reading: $!";
291 0 return undef;
292 }
293 52 $rs -= $result;
294 }
295 52 ▶ if ($self->{'cipher'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cipher"}) 0 52 52
296 0 my $cipher = $self->{'cipher'};
297 0 my $encodedMsg = $msg;
298 0 $msg = '';
299 0 ▶ for (my $i = 0; $i < $readSize; $i += $blockSize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $readSize) 0 0 0
300 0 $msg .= $cipher->decrypt(substr($encodedMsg, $i, $blockSize));
301 }
302 0 $msg = substr($msg, 0, $encodedSize);
303 }
304 52 my $ref = Storable::thaw($msg);
305 52 $ref;
306 }
307 52 ▶sub _Store($$) {
Subroutine _Store: called CC=6 SCAR=28.8
308 52 my($self, $msg) = @_;
309  
310 52 ▶ if ($self->_HaveOoStorable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_HaveOoStorable) 0 52 52
311 0 ▶ if (!$self->{'io'}->Store($msg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"io"}->Store($msg)) 0 0 0
312 0 $self->{'error'} = $self->{'io'}->errstr;
313 0 return undef;
314 }
315 0 return 1;
316 }
317  
318 52 my($encodedMsg) = Storable::nfreeze($msg);
319 52 my($encodedSize) = length($encodedMsg);
320 52 ▶ if ($self->{'cipher'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cipher"}) 0 52 52
321 0 my $cipher = $self->{'cipher'};
322 0 my $size = $cipher->blocksize;
323 0 ▶ if (my $addSize = length($encodedMsg) % $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $addSize = length($encodedMsg) % $size) 0 0 0
324 0 $encodedMsg .= chr(0) x ($size - $addSize);
325 }
326 0 $msg = $encodedMsg;
327 0 $encodedMsg = '';
328 0 ▶ for (my $i = 0; $i < length($msg); $i += $size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $msg) 0 0 0
329 0 $encodedMsg .= $cipher->encrypt(substr($msg, $i, $size));
330 }
331 }
332 52 $self->{'file'}->print(pack("N", $encodedSize) . $encodedMsg);
333 52 $self->{'file'}->flush();
334 }
335  
336  
337 # Autoload methods go after =cut, and are processed by the autosplit program.
338  
339 7 1;
340 __END__
« RPC/pServer.pm ↑ Summary Bundle/pRPC-modules.pm »