blib/lib/IO/NonBlocking.pm

filter: Statementstmt 19.6% 41 / 209 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 29.0% 9 / 31 Podpod 86.3% 19 / 22 totaltotal 20.4% 69 / 337 CC 42 SCAR 56.8
CC
42
Cov
15%
start
756.0
new
72.0
onSheddo
20.0
CRAP 294.1
↑ Summary
1 package IO::NonBlocking;
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 POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Tie::RefHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 $VERSION = '1.035';
14  
15 1 my @now=localtime(time);
16 1 my $cronCounter=$now[0]+60*$now[1]+3600*$now[2]+3600*24*$now[3];
17 1 my %buff;
18  
19 # begin with empty buffers
20 1 my %inbuffer = ();
21 1 my %outbuffer = ();
22 1 my %ready = ();
23  
24 1 my %turn_timeout;
25 my %turn_timeout_trigger;
26 1 my $select;
27 1 my %idle;
28 1 my %timer;
29  
30 1 tie %ready, 'Tie::RefHash';
31  
32 1 my $daily=0;
33  
34 0 ▶sub new{
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
35 0 my($proto,@arg)=@_;
36 0 ▶ my $class=ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
37 0 my %hash=%{$arg[0]};
38  
39 0 my $self={};
40  
41 0 ▶ $self->{serverName}=$hash{'server_name'} || die("server_name is required");
Condition0%0/2
$hash{'server_name'} || die('server_name is required')
l!l
00
MC/DC0%0/1
$hash{'server_name'} || die('server_name is required')
$hash{'server_name'}
Truth table0%0/2
A
A= $hash{'server_name'}
Aresult
11
00
42 0 ▶ $self->{port}=$hash{'port'} || die("port is required");
Condition0%0/2
$hash{'port'} || die('port is required')
l!l
00
MC/DC0%0/1
$hash{'port'} || die('port is required')
$hash{'port'}
Truth table0%0/2
A
A= $hash{'port'}
Aresult
11
00
43 0 ▶ $self->{delimiter}=defined($hash{delimiter}) ? $hash{delimiter} : "\0";
Branch: 1 branch
BranchTrueFalseTotal
defined $hash{'delimiter'} ? : 0 0 0
44 0 ▶ $self->{string_format}=defined($hash{string_format}) ? $hash{string_format} : '.*?';
Branch: 1 branch
BranchTrueFalseTotal
defined $hash{'string_format'} ? : 0 0 0
45 0 ▶ $self->{timeout}=defined($hash{timeout}) ? $hash{timeout} : 300;
Branch: 1 branch
BranchTrueFalseTotal
defined $hash{'timeout'} ? : 0 0 0
46 0 ▶ $self->{piddir}=defined($hash{piddir}) ? $hash{piddir} : '/tmp';
Branch: 1 branch
BranchTrueFalseTotal
defined $hash{'piddir'} ? : 0 0 0
47  
48 0 bless $self,$class;
49 }
50  
51 0 ▶sub start_turn{
Subroutine start_turn: not called CC=1 SCAR=6.9
Pod documented
52 0 my $self=shift;
53 0 my $client=shift;
54 0 my $time=shift;
55  
56 0 $turn_timeout{$client}=$time;
57 0 $turn_timeout_trigger{$client}=$_[0];
58 }
59  
60 0 ▶sub stop_time {
Subroutine stop_time: not called CC=1 SCAR=6.9
Pod documented
61 0 my $self=shift;
62 0 my $client=shift;
63  
64 0 $turn_timeout{$client}=-1;
65 0 delete($turn_timeout_trigger{$client});
66 }
67  
68 0 ▶sub close_client {
Subroutine close_client: not called CC=2 SCAR=17.9
Pod documented
69 0 my $self=shift;
70 0 my $client=shift;
71  
72 0 delete $turn_timeout{$client};
73 0 delete $turn_timeout_trigger{$client};
74 0 delete $idle{$client};
75 0 delete $inbuffer{$client};
76 0 delete $outbuffer{$client};
77 0 delete $ready{$client};
78  
79 0 $select->remove($client);
80 0 ▶ close $client if $client;
Branch: 1 branch
BranchTrueFalseTotal
if $client 0 0 0
81 }
82  
83 0 ▶sub disconnect_client {
Subroutine disconnect_client: not called CC=2 SCAR=17.9
Pod documented
84 0 my $self=shift;
85 0 my $client=shift;
86  
87 0 delete $turn_timeout{$client};
88 0 delete $turn_timeout_trigger{$client};
89 0 delete $idle{$client};
90 0 delete $inbuffer{$client};
91 0 delete $outbuffer{$client};
92 0 delete $ready{$client};
93  
94 0 $self->onClientDisconnected($client);
95  
96 0 $select->remove($client);
97 0 ▶ close $client if $client;
Branch: 1 branch
BranchTrueFalseTotal
if $client 0 0 0
98 }
99  
100 0 ▶sub start{
Subroutine start: not called CC=27 SCAR=66.3
Pod documented
101 0 my $self=shift;
102 0 my $current_time=time;
103  
104 0 print "Listening on port ".$self->port."\n";
105  
106 0 ▶ my $server = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $server = "IO::Socket::INET"->new("LocalPort", $self->port, "Listen", 50, "Proto", "tcp", "Reuse", 1) or die "Can't make server socket: $@\n" 0 0 0
107 LocalPort => $self->port,
108 Listen => 50,
109 Proto => 'tcp',
110 Reuse => 1)
111 or die "Can't make server socket: $@\n";
112  
113 0 $self->nonblock($server);
114 0 $select = IO::Select->new($server);
115  
116 0 ▶ open(FILE,">".$self->piddir."/".$self->serverName) or die "Cannot write PID file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, '>' . $self->piddir . '/' . $self->serverName or die "Cannot write PID file: $!\n" 0 0 0
117 0 print FILE $$;
118 0 close(FILE);
119  
120 0 $self->onServerInit;
121  
122 0 while (1) {
123 0 my $client;
124 my $rv;
125 0 my $data;
126  
127 # cron
128 0 my $this_time=time;
129 0 ▶ if ($current_time != $this_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_time != $this_time) 0 0 0
130 0 foreach $client($select->handles) {
131 0 ▶ next if $server == $client;
Branch: 1 branch
BranchTrueFalseTotal
if $server == $client 0 0 0
132  
133 0 ▶ if ($turn_timeout{$client} != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($turn_timeout{$client} != -1) 0 0 0
134 0 ▶ if ($turn_timeout{$client} <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($turn_timeout{$client} <= 0) { } 0 0 0
135 0 &{$turn_timeout_trigger{$client}}($self);
136 0 delete $turn_timeout_trigger{$client};
137 0 $turn_timeout{$client} = -1;
138 } else {
139 0 --$turn_timeout{$client};
140 }
141 }
142 }
143  
144 0 $self->onSheddo;
145 0 $current_time=$this_time;
146 }
147  
148 #timeout the Idles
149  
150 0 ▶ if($cronCounter % ($self->{timeout}+30) == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($cronCounter % ($self->{"timeout"} + 30) == 0) 0 0 0
151 0 foreach $client ($select->handles) {
152 0 ▶ next if $server == $client;
Branch: 1 branch
BranchTrueFalseTotal
if $server == $client 0 0 0
153  
154 0 my $this_time=time;
155  
156 0 ▶ if( $this_time - $idle{$client} >= $self->{timeout} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($this_time - $idle{$client} >= $self->{"timeout"}) 0 0 0
157 0 $self->disconnect_client($client);
158 0 next;
159 }
160 }
161 }
162  
163 # check for new information on the connections we have
164  
165 # anything to read or accept?
166 0 foreach $client ($select->can_read(1)) {
167  
168 0 ▶ if ($client == $server) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client == $server) { } 0 0 0
169 # accept a new connection
170  
171 0 $client = $server->accept();
172 0 $select->add($client);
173 0 $self->nonblock($client);
174  
175 0 $self->onClientConnected($client);
176 0 $idle{$client}=time;
177 0 $turn_timeout{$client}=-1;
178 } else {
179 # read data
180  
181 0 $data = '';
182 0 $rv = $client->recv($data, POSIX::BUFSIZ, 0);
183  
184 0 ▶ unless (defined($rv) && length $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv and length $data) 0 0 0
Condition0%0/3
defined $rv and length $data
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rv and length $data
defined $rv length $data
Truth table0%0/3
A and B
A= defined $rvB= length $data
ABresult
0X0
100
111
185 # This would be the end of file, so close the client
186 0 $self->disconnect_client($client);
187 0 next;
188 }
189  
190 0 $inbuffer{$client} .= $data;
191  
192 # test whether the data in the buffer or the data we
193 # just read means there is a complete request waiting
194 # to be fulfilled. If there is, set $ready{$client}
195 # to the requests waiting to be fulfilled.
196 0 my $dm=$self->{delimiter};
197 0 my $sf=$self->{string_format};
198  
199 0 ▶ while ($inbuffer{$client} =~ s/($sf)$dm//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inbuffer{$client} =~ s/($sf)$dm//s) 0 0 0
200 0 push( @{$ready{$client}}, $1 );
201 }
202  
203 0 $idle{$client}=time;
204 }
205 }
206  
207 # Any complete requests to process?
208 0 foreach $client (keys %ready) {
209 0 $self->handle($client);
210 }
211  
212 0 my @bad_client;
213  
214 # Buffers to flush?
215 0 foreach $client ($select->can_write(1)) {
216 # Skip this client if we have nothing to say
217 0 ▶ next unless exists $outbuffer{$client};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $outbuffer{$client} 0 0 0
218  
219 0 eval{
220 0 $rv = $client->send($outbuffer{$client}, 0);
221 };
222 0 ▶ push(@bad_client,$client),next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
223  
224 0 ▶ unless (defined $rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 0 0
225 # Whine, but move on.
226  
227 0 warn "I was told I could write, but I can't.\n";
228 0 next;
229 }
230  
231 0 ▶ if ( $rv == length $outbuffer{$client} || $! == POSIX::EWOULDBLOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == length $outbuffer{$client} or $! == 11) { } 0 0 0
Condition0%0/3
$rv == length $outbuffer{$client} or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$rv == length $outbuffer{$client} or $! == 11
$rv == length $outbuffer{$client} $! == 11
Truth table0%0/3
A or B
A= $rv == length $outbuffer{$client}B= $! == 11
ABresult
1X1
011
000
232 0 substr($outbuffer{$client}, 0, $rv) = '';
233 0 ▶ delete $outbuffer{$client} unless length $outbuffer{$client};
Branch: 1 branch
BranchTrueFalseTotal
unless length $outbuffer{$client} 0 0 0
234 } else {
235 # Couldn't write all the data, and it wasn't because
236 # it would have blocked. Shutdown and move on.
237  
238 0 $self->disconnect_client($client);
239 0 next;
240 }
241 }
242  
243 0 foreach (@bad_client){
244 0 $self->disconnect_client($client);
245 }
246  
247 # Out of band data?
248 0 foreach $client ($select->has_exception(0)) {
249 # arg is timeout
250 # Deal with out-of-band data here, if you want to.
251 }
252 }
253  
254 }
255  
256 0 ▶sub nonblock {
Subroutine nonblock: not called CC=3 SCAR=24.8
Pod documented
257 0 my $self=shift;
258 0 my $socket=shift;
259 0 my $flags;
260
261 0 ▶ $flags = fcntl($socket, F_GETFL, 0)
Branch: 1 branch
BranchTrueFalseTotal
$flags = fcntl($socket, 3, 0) or die "Can't get flags for socket: $!\n" 0 0 0
262 or die "Can't get flags for socket: $!\n";
263 0 ▶ fcntl($socket, F_SETFL, $flags | O_NONBLOCK)
Branch: 1 branch
BranchTrueFalseTotal
fcntl $socket, 4, $flags | 2048 or die "Can't make socket nonblocking: $!\n" 0 0 0
264 or die "Can't make socket nonblocking: $!\n";
265 }
266  
267 0 ▶sub handle {
Subroutine handle: not called CC=2 SCAR=17.9
Pod undocumented
268 0 my $self=shift;
269 0 my $client = shift;
270 0 my $request;
271  
272 # requests are in $ready{$client}
273 # send output to $outbuffer{$client}
274  
275 0 foreach $request (@{$ready{$client}}) {
276 # $request is the text of the request
277 # put text of reply into $outbuffer{$client}
278  
279 0 $self->onReceiveMessage($client,$request);
280 }
281  
282 0 delete $ready{$client};
283 }
284  
285 #============= facility functions ============
286  
287 0 ▶sub getip {
Subroutine getip: not called CC=1 SCAR=6.9
Pod documented
288 0 my $self=shift;
289 0 $_[0]->sockhost();
290 }
291  
292 0 ▶sub getport {
Subroutine getport: not called CC=1 SCAR=6.9
Pod documented
293 0 my $self=shift;
294 0 $_[0]->sockport();
295 }
296  
297 0 ▶sub piddir{
Subroutine piddir: not called CC=1 SCAR=6.9
Pod documented
298 0 my $self=shift;
299  
300 0 return $self->{piddir};
301 }
302  
303 0 ▶sub serverName{
Subroutine serverName: not called CC=1 SCAR=6.9
Pod documented
304 0 my $self=shift;
305  
306 0 return $self->{serverName};
307 }
308  
309 0 ▶sub port{
Subroutine port: not called CC=1 SCAR=6.9
Pod documented
310 0 my $self=shift;
311  
312 0 return $self->{port};
313 }
314  
315 0 ▶sub sendmsg{
Subroutine sendmsg: not called CC=1 SCAR=6.9
Pod documented
316 0 my $self=shift;
317 0 my $client=shift;
318 0 my $msg=shift;
319  
320 0 $outbuffer{$client}.=$msg.$self->{delimiter};
321 }
322  
323 0 ▶sub onServerInit {
Subroutine onServerInit: not called CC=1 SCAR=0.0
Pod undocumented
324 }
325  
326 0 ▶sub onClientConnected{
Subroutine onClientConnected: not called CC=1 SCAR=6.9
Pod documented
327 0 my $self=shift;
328 0 my $client=shift;
329 }
330  
331 0 ▶sub onClientDisconnected{
Subroutine onClientDisconnected: not called CC=1 SCAR=6.9
Pod documented
332 0 my $self=shift;
333 0 my $client=shift;
334 }
335  
336 0 ▶sub onReceiveMessage{
Subroutine onReceiveMessage: not called CC=1 SCAR=6.9
Pod documented
337 0 my $self=shift;
338 0 my $client=shift;
339 0 my $request=shift;
340 }
341  
342 0 ▶sub onSheddo{
Subroutine onSheddo: not called CC=4 SCAR=30.0
Pod undocumented
343 0 my $self=shift;
344  
345 0 foreach (sort {$a <=> $b} keys %timer) {
346 0 ▶ unless ($cronCounter % $_) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cronCounter % $_) 0 0 0
347 0 &{$timer{$_}}($self);
348 }
349 }
350  
351 0 ▶ if ($cronCounter % 4527 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cronCounter % 4527 == 0) 0 0 0
352 #Sync time
353  
354 0 my @now=localtime(time);
355 0 $cronCounter=$now[0]+60*$now[1]+3600*$now[2]+3600*24*$now[3];
356 0 return;
357 }
358  
359 0 ++$cronCounter;
360 }
361  
362 0 ▶sub cron {
Subroutine cron: not called CC=1 SCAR=6.9
Pod documented
363 0 my $self=shift;
364  
365 0 $timer{$_[0]}=$_[1];
366 }
367  
368 0 ▶sub add_socket {
Subroutine add_socket: not called CC=1 SCAR=6.9
Pod documented
369 0 my $self=shift;
370 0 my $sock=shift;
371  
372 0 $select->add($sock);
373 0 $self->nonblock($sock);
374  
375 0 $idle{$sock}=time;
376 0 $turn_timeout{$sock}=-1;
377 }
378  
379 0 ▶sub select {
Subroutine select: not called CC=1 SCAR=6.9
Pod documented
380 0 my $self=shift;
381  
382 0 $select;
383 }
384  
385 1 1;
386 __END__
↑ Summary