Simple/Factory.pm

filter: Statementstmt 84.0% 126 / 150 Branchbran 54.7% 23 / 42 Conditioncond 29.6% 8 / 27 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 90.4% 19 / 21 Podpod 88.8% 8 / 9 totaltotal 69.8% 185 / 265 CC 34 SCAR 24.9
CC
34
Cov
72%
new
29.4
DESTROY
20.0
readSock
10.5
CRAP 12.1
« Simple/Server.pm ↑ Summary Simple/ObjectHandler.pm »
1 package RPC::Simple::Factory;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw(@ISA @EXPORT $VERSION $serverPid);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Socket ;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Fcntl ;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::Dumper ;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 require Exporter;
12  
13 1 ( $VERSION ) = '$Revision: 1.9 $ ' =~ /\$Revision:\s+([^\s]+)/;
14  
15 1 @ISA = qw(Exporter);
16 1 @EXPORT= qw(spawn) ;
17 # Items to export into callers namespace by default. Note: do not export
18 # names by default without a very good reason. Use EXPORT_OK instead.
19 # Do not simply export all your public functions/methods/constants.
20  
21 # Preloaded methods go here.
22  
23 # As a test may control several machines, several Factory object may be created
24  
25 # opens a connection to a remote host
26 # Tk top window, remote_host, port
27 sub new
28 1 ▶ {
Subroutine new: called CC=14 SCAR=33.8
Pod documented
29 1 my $type = shift ;
30  
31 1 my ($tkTop,$verboseRef,$remote,$port,$timeout);
32  
33 1 ▶ if (ref $_[0])
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 1 1
34 {
35 # old style api
36 0 $tkTop = shift ;
37 0 $verboseRef = shift ;
38 0 ▶ $remote = shift || 'localhost';
Condition0%0/2
shift() || 'localhost'
l!l
00
MC/DC0%0/1
shift() || 'localhost'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
39 0 ▶ $port = shift || 7810;
Condition0%0/2
shift() || 7810
l!l
00
MC/DC0%0/1
shift() || 7810
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
40 0 ▶ $timeout = shift || 0 ;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
41 }
42 else
43 {
44 # new style
45 1 my %args = @_ ;
46  
47 1 $tkTop = $args{tk_top};
48 1 $verboseRef = $args{verbose_ref} ;
49 1 ▶ $remote = $args{remote_host} || 'localhost';
Condition50%1/2
$args{'remote_host'} || 'localhost'
l!l
01
MC/DC0%0/1
$args{'remote_host'} || 'localhost'
$args{'remote_host'}
Truth table50%1/2
A
A= $args{'remote_host'}
Aresult
11
00
50 1 ▶ $port = $args{remote_port} || '7810' ;
Condition50%1/2
$args{'remote_port'} || '7810'
l!l
01
MC/DC0%0/1
$args{'remote_port'} || '7810'
$args{'remote_port'}
Truth table50%1/2
A
A= $args{'remote_port'}
Aresult
11
00
51 1 ▶ $timeout = $args{timeout} || 0 ;
Condition50%1/2
$args{'timeout'} || 0
l!l
01
MC/DC0%0/1
$args{'timeout'} || 0
$args{'timeout'}
Truth table50%1/2
A
A= $args{'timeout'}
Aresult
11
00
52 }
53  
54 1 my $self = {
55 verbose => $verboseRef,
56 handleIdx => 0,
57 remoteHostName => $remote
58 };
59  
60 1 my ($iaddr, $paddr, $proto, $line);
61  
62 0 ▶ if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
Branch: 1 branch
BranchTrueFalseTotal
if ($port =~ /\D/) 0 1 1
63  
64 1 ▶ print "No port" unless $port;
Branch: 1 branch
BranchTrueFalseTotal
unless $port 0 1 1
65  
66 1 ▶ my @time_arg = $timeout ? (Timeout => $timeout) : () ;
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 0 1 1
67 1 $self->{'socket'} = IO::Socket::INET -> new (PeerAddr => $remote,
68 PeerPort => $port,
69 Proto => 'tcp',
70 @time_arg) ;
71  
72 die "Can't create a socket for $remote, port $port\n\t$!\n"
73 1 ▶ unless defined $self->{'socket'} ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"socket"} 0 1 1
74  
75 1 ▶ fcntl($self->{'socket'},F_SETFL, O_NDELAY)
Branch: 1 branch
BranchTrueFalseTotal
fcntl $self->{"socket"}, 4, 2048 or die "fcntl failed $!\n" 1 0 1
76 || die "fcntl failed $!\n";
77  
78 1 print "$type object created \n";
79  
80 # print "sleep over, closing\n";
81 # shutdown ($self->{'socket'}, 2) || print "close: $!";
82  
83 1 bless $self, $type ;
84  
85 1 ▶ if (defined $tkTop)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tkTop) 0 1 1
86 {
87 # register socket to TK's fileevent ...
88 0 $tkTop -> fileevent($self->{'socket'},
89 'readable' => [$self, 'readSock']) ;
90 0 $self->{tkTop} = $tkTop ;
91 }
92  
93 1 $self->{sockBuffer} = '' ;
94 1 return $self ;
95 }
96  
97 sub DESTROY
98 0 ▶ {
Subroutine DESTROY: not called CC=4 SCAR=30.0
99 0 my $self = shift ;
100 0 print "closing Factory socket\n";
101  
102 # de-register from Tk
103 $self->{tkTop} -> fileevent($self->{'socket'},readable => '')
104 0 ▶ if defined $self->{tkTop} ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"tkTop"} 0 0 0
105  
106 #$self->{socket}->close;
107 0 ▶ if(defined $self->{socket} && $self->{socket}->connected)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"socket"} and $self->{"socket"}->connected) 0 0 0
Condition0%0/3
defined $self->{"socket"} and $self->{"socket"}->connected
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"socket"} and $self->{"socket"}->connected
defined $self->{"socket"} $self->{"socket"}->connected
Truth table0%0/3
A and B
A= defined $self->{"socket"}B= $self->{"socket"}->connected
ABresult
0X0
100
111
108 {
109 0 shutdown($self->{socket},2) ;
110 }
111 }
112  
113 sub logmsg
114 15 ▶ {
Subroutine logmsg: called CC=3 SCAR=12.5
Pod documented
115 15 my $self = shift ;
116
117 15 ▶ print @_ if (defined $self->{verbose} and ${$self->{verbose}} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"verbose"} and ${$self->{"verbose"};} 0 15 15
Condition33%1/3
defined $self->{"verbose"} and ${$self->{"verbose"};}
!ll&&!rl&&r
0150
MC/DC0%0/2
defined $self->{"verbose"} and ${$self->{"verbose"};}
defined $self->{"verbose"} ${$self->{"verbose"};}
Truth table33%1/3
A and B
A= defined $self->{"verbose"}B= ${$self->{"verbose"};}
ABresult
0X0
100
111
118 }
119  
120 sub newRemoteObject
121 1 ▶ {
Subroutine newRemoteObject: called CC=1 SCAR=0.0
Pod documented
122 1 my $self=shift ;
123 1 my $clientRef = shift ;
124 1 my $remoteClass = shift ; #optionnal
125
126 # create an Agent tied to the client object
127 my $handle = RPC::Simple::Agent->new ($self,$clientRef,$self->{handleIdx},
128 1 $remoteClass,@_) ;
129
130 1 $self->{handleTab}{$self->{handleIdx}++} = $handle ;
131 1 return $handle ;
132 }
133  
134 sub destroyRemoteObject
135 0 ▶ {
Subroutine destroyRemoteObject: not called CC=1 SCAR=6.9
Pod undocumented
136 0 my $self=shift ;
137 0 my $idx = shift ;
138 0 $self->writeSockBuffer($idx, 'destroy' );
139 0 delete $self->{handleTab}{$idx} ;
140 }
141  
142 sub getRemoteHostName
143 1 ▶ {
Subroutine getRemoteHostName: called CC=1 SCAR=0.0
Pod documented
144 1 my $self=shift ;
145 1 return $self->{remoteHostName} ;
146 }
147  
148 sub writeSockBuffer
149 2 ▶ {
Subroutine writeSockBuffer: called CC=4 SCAR=13.9
Pod documented
150 2 my $self=shift ;
151 2 my $callingIdx = shift ; # index of Agent
152 2 my $method = shift ;
153 2 my $reqId = shift ;
154 2 my $param = shift ; # usually an array ref
155 2 my $objectName = shift ; # optionnal
156
157 2 my $refs = [$param,$method,$reqId, $callingIdx ] ;
158 2 my $names = ['args','method','reqId','handle',] ;
159
160 2 ▶ if (defined $objectName)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $objectName) 1 1 2
161 {
162 1 push @$refs, $objectName ;
163 1 push @$names, 'objectName' ;
164 }
165  
166 2 my $d = Data::Dumper->new ( $refs, $names ) ;
167 2 my $paramStr = "#begin\n".$d->Dumpxs."#end\n" ;
168 #my $str = sprintf("%6d",length($paramStr)) . $paramStr ;
169 2 my $str = $paramStr ;
170 2 $self->logmsg( "$str\n");
171  
172 2 $self->{sockBuffer} .= $str ;
173  
174 2 my $str2 = "#begin_buffer\n".$self->{sockBuffer}."#end_buffer\n" ;
175 1 ▶ no strict 'refs' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 2 my $val = send($self->{'socket'} ,$str2,0) ;
177 2 ▶ if ( defined $val and $val == length($str2))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $val == length $str2) { } 2 0 2
Condition33%1/3
defined $val and $val == length $str2
!ll&&!rl&&r
002
MC/DC0%0/2
defined $val and $val == length $str2
defined $val $val == length $str2
Truth table33%1/3
A and B
A= defined $valB= $val == length $str2
ABresult
0X0
100
111
178 {
179 2 $self->logmsg( "$val bytes sent\n");
180 }
181 else
182 {
183 0 warn "write failed for \n",$str2 ;
184 }
185  
186 2 $self->{sockBuffer} = '' ;
187 }
188  
189 sub readSock
190 1 ▶ {
Subroutine readSock: called CC=10 SCAR=23.5
Pod documented
191 1 my $self = shift ;
192  
193 1 my $fh = $self->{'socket'} ;
194
195 1 $self->logmsg( "readSock called\n");
196 1 ▶ no strict 'refs' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
197
198 1 ▶ if (eof $fh)
Branch: 1 branch
BranchTrueFalseTotal
if (eof $fh) 0 1 1
199 {
200 #print "closing connection\n";
201 #close $fh ;
202 0 return 0;
203 }
204
205 1 my $line ;
206 1 my @codeTab = () ;
207 1 my $code = '' ;
208 1 my $codeEnd = 1 ;
209 1 ▶ while ( $line = $fh->getline or not $codeEnd )
Branch: 1 branch
BranchTrueFalseTotal
if ($line = $fh->getline or not $codeEnd) 9 1 10
Condition66%2/3
$line = $fh->getline or not $codeEnd
l!l&&r!l&&!r
901
MC/DC50%1/2
$line = $fh->getline or not $codeEnd
$line = $fh->getline not $codeEnd
Truth table66%2/3
A or not(B)
A= $line = $fh->getlineB= not $codeEnd
ABresult
1X1
011
000
210 {
211 9 ▶ next unless defined $line ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 9 9
212
213 9 $self->logmsg( "->",$line );
214 9 $code .= $line ;
215
216 9 ▶ if ($line =~ /\s*#end$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\s*#end$/) 1 8 9
217 {
218 1 push @codeTab, $code ;
219 1 $code = '' ;
220 1 $codeEnd = 1 ;
221 }
222 9 ▶ if ($line =~ /\s*#begin$/
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\s*#begin$/) 1 8 9
223 )
224 {
225 1 $codeEnd = 0 ;
226 }
227 }
228
229 1 ▶ use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
230
231 1 foreach $code (@codeTab)
232 {
233 # these lexical variables are assigned in the eval
234 1 my ($args,$method,$reqId,$handle,$objectName) ;
235 1 eval $code ;
236
237 1 ▶ if ($@)
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 1 1
elsif (defined $method) { } 0 1 1
238 {
239 0 print "failed eval ($@) of :\n",$code,"end evaled code\n" ;
240 }
241 elsif (defined $method)
242 {
243 # call object method directly
244 0 $self->logmsg( "calling method $method\n");
245 0 $self->{handleTab}{$handle} -> callMethod($method , $args) ;
246 }
247 else
248 {
249 # it's a call-back
250 1 $self->logmsg( "callback for handle $handle, request $reqId\n");
251 1 $self->{handleTab}{$handle}->treatCallBack($reqId, $args);
252 # or print "eval failed: $@\n";
253 }
254 }
255 1 return 1;
256 }
257  
258 # static method. spawn a server
259 sub spawn
260 1 ▶ {
Subroutine spawn: called CC=2 SCAR=7.3
Pod documented
261 1 my $port = shift ;
262 1 my $verbose = shift ;
263  
264 1 $serverPid = fork ;
265  
266 1 ▶ if ($serverPid == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($RPC::Simple::Factory::serverPid == 0) 0 1 1
267 {
268 # I am a server now
269 0 RPC::Simple::Server::mainLoop ($port,$verbose) ;
270 0 exit ; # well I should never go there
271 }
272 1 print "spawned server pid $serverPid\n" ; # don't use verbose
273 1 sleep 2 ; # let the server start
274 1 return $serverPid ;
275 }
276  
277 sub getSocket
278 2 ▶ {
Subroutine getSocket: called CC=1 SCAR=0.0
Pod documented
279 2 my $self = shift;
280 2 return $self->{socket};
281 }
282  
283 sub END
284 1 ▶ {
Subroutine END: called CC=3 SCAR=12.5
285 1 ▶ if (defined $serverPid and $serverPid != 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $RPC::Simple::Factory::serverPid and $RPC::Simple::Factory::serverPid != 0) 1 0 1
Condition33%1/3
defined $RPC::Simple::Factory::serverPid and $RPC::Simple::Factory::serverPid != 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $RPC::Simple::Factory::serverPid and $RPC::Simple::Factory::serverPid != 0
defined $RPC::Simple::Factory::serverPid $RPC::Simple::Factory::serverPid != 0
Truth table33%1/3
A and B
A= defined $RPC::Simple::Factory::serverPidB= $RPC::Simple::Factory::serverPid != 0
ABresult
0X0
100
111
286 {
287 1 print "killing process $serverPid\n";
288 # 15 is SIGTERM signal
289 1 kill (15, $serverPid) ;
290 }
291 }
292  
293 # Autoload methods go after =cut, and are processed by the autosplit program.
294  
295 1 1;
296 __END__
« Simple/Server.pm ↑ Summary Simple/ObjectHandler.pm »