Sprocket/AIO.pm

filter: Statementstmt 39.5% 32 / 81 Branchbran 15.0% 3 / 20 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 47.3% 9 / 19 Podpod 0.0% 0 / 5 totaltotal 33.3% 45 / 135 CC 13 SCAR 17.7
CC
13
Cov
34%
new
20.0
import
6.5
_start
6.0
CRAP 5.9
« Sprocket/ChannelManager.pm ↑ Summary Sprocket/Plugin.pm »
1 package Sprocket::AIO;
2  
3 8 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our $sprocket_aio;
11
12 8 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.5
13 0 ▶ eval "use IO::AIO qw( poll_fileno poll_cb 2 )";
Subroutine BEGIN: called CC=1 SCAR=1.2
14 8 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 8 0 8
15 8 eval 'sub HAS_AIO () { 0 }';
16 } else {
17 0 eval 'sub HAS_AIO () { 1 }';
18 }
19 }
20  
21 13 ▶sub import {
Subroutine import: called CC=5 SCAR=18.7
22 13 my ( $class, $args ) = @_;
23 13 my $package = caller();
24
25 13 ▶ croak "Sprocket::AIO expects its arguments in a hash ref"
Branch: 1 branch
BranchTrueFalseTotal
if $args and ref $args ne "HASH" 0 13 13
Condition33%1/3
$args and ref $args ne "HASH"
!ll&&!rl&&r
1300
MC/DC0%0/2
$args and ref $args ne "HASH"
$args ref $args ne "HASH"
Truth table33%1/3
A and B
A= $argsB= ref $args ne "HASH"
ABresult
0X0
100
111
26 if ( $args && ref( $args ) ne 'HASH' );
27  
28 13 ▶ unless ( delete $args->{no_auto_export} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (delete $args->{"no_auto_export"}) 13 0 13
29 {
30 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 13 *{ $package . '::sprocket_aio' } = \$sprocket_aio;
32 }
33 }
34  
35 13 return if ( !HAS_AIO || delete $args->{no_auto_bootstrap} );
36  
37 # bootstrap
38 # XXX I don't like this, let's find another way
39 0 eval( qq|
40 package $package;
41 use IO::AIO 2;
42 sub plugin_start_aio {
43 Sprocket::AIO->new( parent_id => shift->parent_id );
44 }
45 | );
46 0 ▶ croak "could not import AIO into $package : $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
47 if ( $@ );
48
49 0 return;
50 }
51  
52 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
53 0 my $class = shift;
54 0 ▶ return $sprocket_aio if ( $sprocket_aio );
Branch: 1 branch
BranchTrueFalseTotal
if $Sprocket::AIO::sprocket_aio 0 0 0
55
56 0 return undef unless ( HAS_AIO );
57  
58 0 ▶ my $self = $sprocket_aio = bless({
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
59 session_id => undef,
60 watch_fork_delay => 2,
61 @_,
62 pid => $$,
63 }, ref $class || $class );
64  
65 0 ▶ return $self unless ( $self->{parent_id} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"parent_id"} 0 0 0
66  
67 0 POE::Session->create(
68 object_states => [
69 $self => {
70 _start => '_start',
71 _stop => '_stop',
72 poll_cb => 'poll_cb',
73 watch_aio => 'watch_aio',
74 watch_fork => 'watch_fork',
75 shutdown => '_shutdown',
76 restart => '_restart',
77 },
78 ],
79 );
80  
81 0 return $self;
82 }
83  
84 0 ▶sub _start {
Subroutine _start: not called CC=2 SCAR=17.9
85 0 my ( $self, $kernel, $session ) = @_[ OBJECT, KERNEL, SESSION ];
86
87 0 $self->{session_id} = $session->ID();
88 0 $kernel->alias_set( "$self" );
89 0 $kernel->call( $session => 'watch_aio' );
90
91 $kernel->delay_set( watch_fork => $self->{watch_fork_delay} )
92 0 ▶ if ( $self->{watch_for_fork} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"watch_for_fork"} 0 0 0
93
94 0 $self->_log( v => 2, msg => 'AIO support module started' );
95
96 0 return;
97 }
98  
99 0 ▶sub watch_aio {
Subroutine watch_aio: not called CC=1 SCAR=6.9
Pod undocumented
100 0 my ( $self, $kernel ) = @_[ OBJECT, KERNEL ];
101
102 # eval here because poll_fileno isn't imported when IO::AIO isn't installed
103 0 open( my $fh, "<&=".eval "poll_fileno()" );
104 #or die "error during open in watch_aio $!";
105 0 $kernel->select_read( $fh, 'poll_cb' );
106 0 $self->{fh} = $fh;
107  
108 # save our pid for watch_fork
109 0 $self->{pid} = $$;
110
111 0 return;
112 }
113  
114 0 ▶sub watch_fork {
Subroutine watch_fork: not called CC=2 SCAR=17.9
Pod undocumented
115 0 my ( $self, $kernel ) = @_[ OBJECT, KERNEL ];
116  
117 0 ▶ if ( $self->{pid} != $$ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pid"} != $$) 0 0 0
118 0 $self->_log( v => 4, msg => 'fork detected, restarting aio' );
119 0 $kernel->call( $_[SESSION] => 'restart' );
120 }
121  
122 0 $kernel->delay_set( watch_fork => $self->{watch_fork_delay} );
123 }
124  
125 0 ▶sub _stop {
Subroutine _stop: not called CC=1 SCAR=6.9
126 0 $_[ OBJECT ]->_log(v => 2, msg => 'stopped');
127 }
128  
129 0 ▶sub _log {
Subroutine _log: not called CC=1 SCAR=6.9
130 # TODO replace with $sprocket->log
131 0 $poe_kernel->call( shift->{parent_id} => _log => ( call => ( caller(1) )[ 3 ], @_ ) );
132 }
133  
134 0 ▶sub shutdown {
Subroutine shutdown: not called CC=2 SCAR=17.9
Pod undocumented
135 0 my $self = shift;
136 0 ▶ return $self->{session_id} ? $poe_kernel->call( $self->{session_id} => shutdown => @_ ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'session_id'} ? : 0 0 0
137 }
138  
139 0 ▶sub _shutdown {
Subroutine _shutdown: not called CC=1 SCAR=6.9
140 0 my ( $self, $kernel ) = @_[ OBJECT, KERNEL ];
141  
142 0 $kernel->alias_remove( "$self" );
143 0 $kernel->alarm_remove_all();
144 0 $kernel->select_read( delete $self->{fh} );
145 0 $sprocket_aio = undef;
146  
147 0 return;
148 }
149  
150 0 ▶sub restart {
Subroutine restart: not called CC=2 SCAR=17.9
Pod undocumented
151 0 my $self = shift;
152 0 ▶ return unless ( $self->{session_id} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"session_id"} 0 0 0
153 0 return $poe_kernel->call( $self->{session_id} => restart => @_ );
154 }
155  
156 0 ▶sub _restart {
Subroutine _restart: not called CC=1 SCAR=6.9
157 0 my ( $self, $kernel, $session ) = @_[ OBJECT, KERNEL, SESSION ];
158
159 0 $kernel->select_read( delete $self->{fh} );
160 0 $kernel->call( $session, 'watch_aio' );
161  
162 0 $self->_log( v => 2, msg => 'AIO support module restarted' );
163
164 0 return;
165 }
166  
167 8 1;
168  
169 __END__
« Sprocket/ChannelManager.pm ↑ Summary Sprocket/Plugin.pm »