Simple/Server.pm

filter: Statementstmt 14.1% 28 / 198 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 31.8% 7 / 22 Podpod 73.3% 11 / 15 totaltotal 13.9% 46 / 331 CC 49 SCAR 49.3
CC
49
Cov
10%
readClient
306.0
mainLoop
182.0
new
156.0
CRAP 138.1
↑ Summary Simple/Factory.pm »
1 package RPC::Simple::Server;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @ISA @EXPORT %pidTab %deadChildren %fhTab $verbose
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 @buddies);
6  
7 # %fhTab is a hash of fileno of file descriptors opened for reading the
8 # STDOUT of children. If contains the ref of the process objects controlling
9 # this child.
10  
11 1 ▶use Fcntl ;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use IO::Socket ;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use IO::Select ;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use RPC::Simple::ObjectHandler ;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 require Exporter;
19  
20 1 @ISA = qw(Exporter);
21 # Items to export into callers namespace by default. Note: do not export
22 # names by default without a very good reason. Use EXPORT_OK instead.
23 # Do not simply export all your public functions/methods/constants.
24 1 @EXPORT = qw(mainLoop chilDeath goodGuy registerChild unregisterChild);
25  
26 1 ( $VERSION ) = '$Revision: 1.8 $ ' =~ /\$Revision:\s+([^\s]+)/;
27  
28 # Preloaded methods go here.
29  
30 # Autoload methods go after =cut, and are processed by the autosplit program.
31  
32 1 @buddies = ('127.0.0.1') ; # I am a good guy
33 1 our $verbose = 0;
34  
35 sub childDeath
36 0 ▶ {
Subroutine childDeath: not called CC=3 SCAR=24.8
Pod documented
37 # not an object method
38 # DO NOT call Tk code in signal handler or in called functions
39 0 my $pid = wait ;
40 0 ▶ if (defined $pidTab{$pid})
Branch: 2 branches
BranchTrueFalseTotal
if (defined $RPC::Simple::Server::pidTab{$pid}) { } 0 0 0
elsif (exists $RPC::Simple::Server::pidTab{$pid}) { } 0 0 0
41 {
42 0 print "child pid $pid died ($?)\n";
43 0 $deadChildren{$pid} = [$pidTab{$pid}, $?] ;
44 0 delete $pidTab{$pid} ;
45 }
46 elsif (exists $pidTab{$pid})
47 {
48 0 print "old news: child died ($pid)\n" ;
49 }
50 else
51 {
52 0 print "Unknown child died ($pid)\n" ;
53 }
54 # may not be needed anymore according to Tom C TBD
55 }
56  
57 0 ▶sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
Subroutine logmsg: not called CC=1 SCAR=6.9
Pod undocumented
58  
59 sub mainLoop
60 0 ▶ {
Subroutine mainLoop: not called CC=13 SCAR=52.0
Pod documented
61 0 ▶ my $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
62 0 ▶ $verbose = 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
63  
64 0 my $clientOpen = 0 ;
65
66 #create listening socket
67 0 my $server = IO::Socket::INET -> new (Listen => 5,
68 LocalAddr => 'localhost',
69 LocalPort => $port,
70 Proto => 'tcp'
71 ) ;
72
73 0 ▶ die "Can't create listening socket $!\n" unless defined $server ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server 0 0 0
74  
75 0 my $serverNb = $server -> fileno ;
76  
77 0 logmsg "server started on port $port";
78
79 # my $sclient = register_io_client
80 # ([],'rw', SERVER ,
81 # \&acceptSocket,\&acceptSocket,\&acceptSocket )
82 # || die "socket server not registered\n";
83
84 # set_maximum_inactive_server_time(6000) ; # need a handler TBD
85
86 # print "listening to socket registered\n";
87
88 # register_interval_client([],5,sub{ print ".";}) ;
89 # start_server() ;
90  
91 # create select object
92 0 my $s = IO::Select -> new() ;
93 0 $s -> add ($server) ; # add listening socket
94
95 0 while (1)
96 {
97 0 my ($toRead,$dummy,$shutThem) = IO::Select ->
98 select ($s ,undef, $s, 2) ;
99  
100 0 foreach my $fh (@$shutThem)
101 {
102 # close fh on errors (usually dead children, or closed client)
103 0 ▶ if ($serverNb == $fh->fileno)
Branch: 1 branch
BranchTrueFalseTotal
if ($serverNb == $fh->fileno) 0 0 0
104 {
105 0 my $nb = $fh->fileno ;
106 0 ▶ print "closing fno $nb (on error)\n" if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::Simple::Server::verbose 0 0 0
107 0 my ($theObj,$theMeth) = @{$fhTab{$nb}} ;
108 0 $theObj-> close(1) ;
109 0 delete $fhTab{$nb} ;
110 }
111 }
112  
113 0 foreach my $fh (@$toRead)
114 {
115 0 ▶ if ($serverNb == $fh->fileno)
Branch: 1 branch
BranchTrueFalseTotal
if ($serverNb == $fh->fileno) { } 0 0 0
116 {
117 # reading server socket
118 0 my $ref = RPC::Simple::Server -> new($server,$s) ;
119 0 ▶ next unless defined $ref ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref 0 0 0
120 0 my $nb = $ref->getFileno ;
121 0 $fhTab{$nb} = [ $ref , 'readClient' ] ;
122 }
123 else
124 {
125 0 my $nb = $fh->fileno ;
126 0 ▶ print "reading fno $nb\n" if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::Simple::Server::verbose 0 0 0
127 0 my ($theObj,$theMeth) = @{$fhTab{$nb}} ;
128 0 ▶ unless ($theObj-> $theMeth(1) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($theObj->$theMeth(1)) 0 0 0
129 {
130 0 ▶ print "closing fno $nb (error after reading)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::Simple::Server::verbose 0 0 0
131 if $verbose ;
132 0 my ($theObj,$theMeth) = @{$fhTab{$nb}} ;
133 0 $theObj-> close() ;
134 0 delete $fhTab{$nb} ;
135 }
136 }
137 }
138  
139  
140 0 &checkDead ;
141 }
142 }
143  
144 sub registerChild
145 0 ▶ {
Subroutine registerChild: not called CC=1 SCAR=6.9
Pod documented
146 0 my $object=shift ;
147 0 my $pid = shift ;
148 0 $pidTab{$pid}=$object;
149 }
150  
151 sub unregisterChild
152 0 ▶ {
Subroutine unregisterChild: not called CC=1 SCAR=6.9
Pod documented
153 0 my $pid = shift ;
154 0 print "Child $pid unregistered\n";
155 0 undef $pidTab{$pid};
156 0 delete $deadChildren{$pid} ;
157 }
158  
159  
160 sub close
161 0 ▶ {
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
162 0 my $self= shift ;
163  
164 0 print "closing connection\n";
165 0 $self->{selector}->remove($self->{mySocket}) ;
166 #$self->{mySocket}->close ;
167 0 shutdown($self->{mySocket},2) ;
168 }
169  
170 sub readClient
171 0 ▶ {
Subroutine readClient: not called CC=17 SCAR=57.2
Pod documented
172 0 my $self= shift ;
173  
174 # my ($obj,$key,$handle) = @_ ;
175 0 ▶ print "readClient called\n" if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::Simple::Server::verbose 0 0 0
176  
177 0 ▶ return 0 if ($self->{mySocket}->eof) ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mySocket"}->eof 0 0 0
178  
179 0 my @codeTab = () ;
180  
181 0 my $code = '' ;
182 0 my $line ;
183 0 my $codeEnd = 1 ;
184  
185 0 ▶ while ( $line = $self->{mySocket}->getline or not $codeEnd )
Branch: 1 branch
BranchTrueFalseTotal
if ($line = $self->{"mySocket"}->getline or not $codeEnd) 0 0 0
Condition0%0/3
$line = $self->{"mySocket"}->getline or not $codeEnd
l!l&&r!l&&!r
000
MC/DC0%0/2
$line = $self->{"mySocket"}->getline or not $codeEnd
$line = $self->{"mySocket"}->getline not $codeEnd
Truth table0%0/3
A or not(B)
A= $line = $self->{"mySocket"}->getlineB= not $codeEnd
ABresult
1X1
011
000
186 {
187 0 ▶ next unless defined $line ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
188
189 0 ▶ print "-> ",$line if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::Simple::Server::verbose 0 0 0
190 0 $code .= $line ;
191 0 ▶ if ($line =~ /#end$/
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /#end$/) 0 0 0
192 )
193 {
194 0 push @codeTab, $code ;
195 0 $code = '' ;
196 0 $codeEnd = 1 ;
197 }
198 0 ▶ if ($line =~ /#begin$/
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /#begin$/) 0 0 0
199 )
200 {
201 0 $codeEnd = 0 ;
202 }
203 }
204  
205 0 foreach $code (@codeTab)
206 {
207 0 my ($args,$method,$reqId,$handle,$objectName) ;
208 # untaint $code and place it in the safe
209  
210 0 ▶ if ($code =~ m/(.+)/s )
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /(.+)/s) 0 0 0
211 {
212 0 $code = $1 ;
213 0 ▶ print "code is laundered\n" if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::Simple::Server::verbose 0 0 0
214 }
215  
216 0 eval($code) ;
217  
218 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
219 {
220 0 print "failed eval ($@) of :\n",$code,"end evaled code\n" ;
221 }
222 else
223 {
224 0 ▶ print "Call $method \n" if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::Simple::Server::verbose 0 0 0
225  
226 0 ▶ if ($method eq 'new')
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'new') { } 0 0 0
elsif ($method eq 'destroy') { } 0 0 0
227 {
228 # create new object, call-back always required
229 0 $self->{handleTab}{$handle} = RPC::Simple::ObjectHandler
230 -> new ($self,$objectName, $handle, $args, $reqId) ;
231 }
232 elsif ($method eq 'destroy')
233 {
234 0 $self->{handleTab}{$handle}->destroy ;
235 0 delete $self->{handleTab}{$handle} ;
236 }
237 else
238 {
239 0 $self->{handleTab}{$handle} ->
240 remoteCall($reqId,$method,$args) ;
241 }
242 }
243 }
244 0 ▶ print "readClient finished\n" if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::Simple::Server::verbose 0 0 0
245 0 return 1 ;
246 }
247  
248 0 ▶sub dummy { print "Dummy function called\n"; }
Subroutine dummy: not called CC=1 SCAR=6.9
Pod undocumented
249  
250 sub writeSock
251 0 ▶ {
Subroutine writeSock: not called CC=5 SCAR=31.5
Pod documented
252 0 my $self=shift;
253  
254 0 my $handle = shift ; # index of RpcClient
255 0 my $method = shift ;
256 0 my $reqId = shift ;
257 0 my $param = shift ; # usually an array ref
258 0 my $objectName = shift ; # optionnal
259
260 0 my $refs = [$param,$method,$reqId, $handle ] ;
261 0 my $names = ['args','method','reqId','handle',] ;
262
263 0 ▶ if (defined $objectName)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $objectName) 0 0 0
264 {
265 0 push @$refs, $objectName ;
266 0 push @$names, 'objectName' ;
267 }
268
269 0 my $d = Data::Dumper->new ( $refs, $names ) ;
270 0 my $paramStr = "#begin\n".$d->Dumpxs."#end\n" ;
271 #my $str = sprintf("%6d",length($paramStr)) . $paramStr ;
272 0 my $str = $paramStr ;
273 0 ▶ print "$paramStr\n" if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::Simple::Server::verbose 0 0 0
274 1 ▶ no strict 'refs' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
275 0 my $val;
276 eval
277 0 {
278 0 $val = $self->{mySocket}->send($str,0) ;
279 };
280 0 ▶ warn "send failed $!\n" unless defined $val ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
281 0 ▶ print "$val bytes sent\n" if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::Simple::Server::verbose 0 0 0
282 }
283  
284 sub new
285 0 ▶ {
Subroutine new: not called CC=12 SCAR=50.5
Pod undocumented
286 0 my $type = shift ;
287 0 my $server = shift ;
288 0 my $selector = shift ;
289 # Optional parameters which can be used to tell server not
290 # to accept the new connection but let the calling routine
291 # do that for us. If these parameters are used, you may
292 # need to override the mainLoop subroutine.
293 0 ▶ my $socket = shift || undef;
Condition0%0/2
shift() || undef
l!l
00
MC/DC0%0/1
shift() || undef
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
294 0 ▶ my $manual_accept = 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
295 0 my $self = {} ;
296  
297 0 $self->{'server'} = $server ;
298 0 $self->{'selector'} = $selector ;
299  
300 0 bless $self, $type;
301  
302 0 ▶ if ($manual_accept && not defined $socket)
Branch: 1 branch
BranchTrueFalseTotal
if ($manual_accept and not defined $socket) 0 0 0
Condition0%0/3
$manual_accept and not defined $socket
!ll&&!rl&&r
000
MC/DC0%0/2
$manual_accept and not defined $socket
$manual_accept not defined $socket
Truth table0%0/3
A and not(B)
A= $manual_acceptB= not defined $socket
ABresult
0X0
100
111
303 {
304 0 print "socket required for manual accept mode\n" ;
305 0 undef $self ;
306 0 return undef ;
307 }
308  
309  
310 0 my $iaddr;
311 0 ▶ unless ($manual_accept)
Branch: 1 branch
BranchTrueFalseTotal
unless ($manual_accept) 0 0 0
312 {
313 0 print "Accepting connection\n" ;
314 0 ($socket, $iaddr) = $server -> accept() ; # blocking call
315 }
316  
317 0 ▶ unless (defined $socket)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket) 0 0 0
318 {
319 0 print "accept failed $!\n" ;
320 0 undef $self ;
321 0 return undef ;
322 }
323  
324 0 print "Connection accepted\n";
325  
326 0 my $name = gethostbyaddr($socket->peeraddr,AF_INET) ;
327 0 my $ipadr = $socket -> peerhost ;
328 0 my $ok = 0 ;
329 0 foreach (@buddies)
330 {
331 0 print "Comparing $ipadr with $_\n";
332 0 ▶ if ($ipadr eq $_)
Branch: 1 branch
BranchTrueFalseTotal
if ($ipadr eq $_) 0 0 0
333 {
334 0 $ok = 1 ;
335 0 last;
336 }
337 }
338  
339 0 ▶ unless ($ok)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
340 {
341 0 logmsg "connection from $name refused [ $ipadr ]";
342 0 $socket->close ;
343 0 undef $self ;
344 0 return undef ;
345 }
346  
347 0 $self->{mySocket} = $socket ;
348 0 ▶ $selector->add($socket) unless($manual_accept) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $manual_accept 0 0 0
349  
350 # put the socket in non-blocking mode
351 0 ▶ fcntl($socket,F_SETFL, O_NDELAY) || die "fcntl failed $!\n";
Branch: 1 branch
BranchTrueFalseTotal
fcntl $socket, 4, 2048 or die "fcntl failed $!\n" 0 0 0
352  
353 0 logmsg "connection from $name [ $ipadr ] ";
354 0 return $self ;
355 }
356  
357  
358 # register an object/method to call
359 sub setMask
360 0 ▶ {
Subroutine setMask: not called CC=1 SCAR=6.9
Pod documented
361 0 my $obj = shift ;
362 0 my $method = shift ;
363 0 my $nb = shift ;
364 0 $fhTab{$nb} = [ $obj , $method ] ;
365 }
366  
367 sub resetMask
368 0 ▶ {
Subroutine resetMask: not called CC=1 SCAR=6.9
Pod documented
369 0 my $nb = shift ;
370 0 delete $fhTab{$nb} ;
371 }
372  
373 sub checkDead
374 0 ▶ {
Subroutine checkDead: not called CC=3 SCAR=24.8
Pod undocumented
375 0 ▶ if (scalar %deadChildren )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar %RPC::Simple::Server::deadChildren) 0 0 0
376 {
377 0 my $pid ;
378 0 foreach $pid (keys %deadChildren)
379 {
380 0 my ($ref,$out) = @{$deadChildren{$pid}};
381 0 $ref->processOver($out) ;
382 0 delete $deadChildren{$pid} ;
383 }
384 }
385 }
386  
387 sub getFileno
388 0 ▶ {
Subroutine getFileno: not called CC=1 SCAR=6.9
Pod documented
389 0 my $self = shift ;
390 0 return $self->{mySocket}->fileno ;
391 }
392  
393 sub goodGuy
394 0 ▶ {
Subroutine goodGuy: not called CC=2 SCAR=17.9
Pod documented
395 0 my $good = shift ;
396  
397 0 ▶ if ($good =~ /^[\d\.]+$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($good =~ /^[\d\.]+$/) { } 0 0 0
398 {
399 0 push @buddies , $good ;
400 }
401 else
402 {
403 0 my (@addrs) = (gethostbyname($good))[4] ;
404 0 my $addr = join(".", unpack('C4', $addrs[0])) ;
405 0 push @buddies, $addr ;
406 }
407 }
408  
409 1 1;
410 __END__
↑ Summary Simple/Factory.pm »