blib/lib/POE/Component/AIO.pm

filter: Statementstmt 83.4% 91 / 109 Branchbran 42.8% 12 / 28 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 95.6% 22 / 23 Podpod 80.0% 4 / 5 totaltotal 73.3% 132 / 180 CC 18 SCAR 12.5
CC
18
Cov
73%
_start
9.2
import
6.3
postback
3.8
CRAP 3.5
↑ Summary
1 package POE::Component::AIO;
2  
3 1 ▶use IO::AIO qw( poll_fileno poll_cb );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our %callback_ids;
11 1 our $VERSION = '1.00';
12  
13 1 ▶use vars qw( $poco_aio );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶sub import {
Subroutine import: called CC=6 SCAR=18.5
16 2 my ( $class, $args ) = @_;
17 2 my $package = caller();
18  
19 2 ▶ croak "PoCo::AIO expects its arguments in a hash ref"
Branch: 1 branch
BranchTrueFalseTotal
if $args and ref $args ne "HASH" 0 2 2
Condition33%1/3
$args and ref $args ne "HASH"
!ll&&!rl&&r
200
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
20 if ( $args && ref( $args ) ne 'HASH' );
21  
22 2 ▶ unless ( delete $args->{no_auto_export} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (delete $args->{"no_auto_export"}) 2 0 2
23 {
24 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 *{ $package . '::poco_aio' } = \$poco_aio;
26 }
27  
28 1 ▶ eval( "package $package; use IO::AIO qw( 2 );" );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
30 0 croak "could not export IO::AIO into $package (is it installed?)";
31 }
32 }
33  
34 2 ▶ return if ( $args->{no_auto_bootstrap} );
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"no_auto_bootstrap"} 0 2 2
35  
36 # bootstrap
37 2 POE::Component::AIO->new( %$args );
38
39 2 return;
40 }
41  
42 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
43 2 my $class = shift;
44 2 ▶ return $poco_aio if ( $poco_aio );
Branch: 1 branch
BranchTrueFalseTotal
if $POE::Component::AIO::poco_aio 1 1 2
45  
46 1 ▶ my $self = $poco_aio = bless({
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
47 session_id => undef,
48 postback => undef,
49 @_
50 }, ref $class || $class );
51  
52 1 POE::Session->create(
53 object_states => [
54 $self => [qw(
55 _start
56 _stop
57 poll_cb
58 do_postback
59 _shutdown
60 )]
61 ],
62 );
63  
64 1 return $self;
65 }
66  
67 1 ▶sub _start {
Subroutine _start: called CC=5 SCAR=22.1
68 1 my ( $self, $kernel ) = @_[ OBJECT, KERNEL ];
69
70 1 $self->{session_id} = $_[ SESSION ]->ID();
71
72 1 $kernel->alias_set( "$self" );
73
74 1 ▶ open( my $fh, "<&=".poll_fileno() ) or die "$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, '<&=' . &poll_fileno() or die "$!" 1 0 1
75 1 $kernel->select_read( $fh, 'poll_cb' );
76 1 $self->{_fh} = $fh;
77
78 1 ▶ return unless( $self->{postback} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"postback"} 1 0 1
79
80 # XXX undocumented
81 0 ▶ if ( ref( $self->{postback} ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->{'postback'} eq 'ARRAY') { } 0 0 0
elsif (ref $self->{'postback'} eq 'CODE') { } 0 0 0
82 0 $kernel->post( @{$self->{postback}}, $self->{session_id}, "$self" );
83 } elsif ( ref( $self->{postback} ) eq 'CODE' ) {
84 0 $kernel->yield( 'do_postback' );
85 }
86
87 0 return;
88 }
89  
90 0 ▶sub do_postback {
Subroutine do_postback: not called CC=1 SCAR=6.9
Pod undocumented
91 0 my $self = $_[ OBJECT ];
92
93 0 $self->{postback}->( $self->{session_id}, "$self" );
94
95 0 return;
96 }
97  
98 1 ▶sub _stop { }
Subroutine _stop: called CC=1 SCAR=0.0
99  
100 1 ▶sub callback {
Subroutine callback: called CC=3 SCAR=11.6
Pod documented
101 1 my ($self, $event, @etc) = @_;
102  
103 1 my $id;
104 1 ▶ if ( ref( $event ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $event eq 'ARRAY') { } 0 1 1
105 0 ( $id, $event ) = @$event;
106 } else {
107 1 my $ses = $poe_kernel->get_active_session();
108 1 ▶ if ( $ses ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ses) { } 1 0 1
109 1 $id = $ses->ID();
110 } else {
111 0 warn 'no active session in call to PoCo::AIO::callback';
112 0 return undef;
113 }
114 }
115  
116 1 ▶ my $callback = POE::Component::AIO::AnonCallback->new(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
117 1 $poe_kernel->call( $id => $event => @etc => @_ );
118 1 });
119  
120 1 $callback_ids{$callback} = $id;
121  
122 1 $poe_kernel->refcount_increment( $id, 'anon_event' );
123  
124 1 return $callback;
125 }
126  
127 1 ▶sub postback {
Subroutine postback: called CC=3 SCAR=13.3
Pod documented
128 1 my ($self, $event, @etc) = @_;
129  
130 1 my $id;
131 1 ▶ if ( ref( $event ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $event eq 'ARRAY') { } 1 0 1
132 1 ( $id, $event ) = @$event;
133 } else {
134 0 my $ses = $poe_kernel->get_active_session();
135 0 ▶ if ( $ses ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ses) { } 0 0 0
136 0 $id = $ses->ID();
137 } else {
138 0 warn 'no active session in call to PoCo::AIO::callback';
139 0 return undef;
140 }
141 }
142
143 1 ▶ my $postback = POE::Component::AIO::AnonCallback->new(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 1 $poe_kernel->post( $id => $event => @etc => @_ );
145 1 });
146  
147 1 $callback_ids{$postback} = $id;
148  
149 1 $poe_kernel->refcount_increment( $id, 'anon_event' );
150  
151 1 return $postback;
152 }
153  
154 1 ▶sub shutdown {
Subroutine shutdown: called CC=1 SCAR=0.0
Pod documented
155 1 $poe_kernel->call( shift->{session_id} => '_shutdown' );
156 }
157  
158 1 ▶sub _shutdown {
Subroutine _shutdown: called CC=1 SCAR=0.0
159 1 my ( $self, $kernel ) = @_[ OBJECT, KERNEL ];
160  
161 1 $kernel->alias_remove( "$self" );
162 1 $kernel->select_read( delete $self->{_fh} );
163  
164 1 $poco_aio = undef;
165  
166 1 return;
167 }
168  
169 1 1;
170  
171 =pod
172  
173 =head1 NAME
174  
175 POE::Component::AIO - Asynchronous Input/Output for POE
176  
177 =head1 SYNOPSIS
178  
179 use POE qw( Component::AIO );
180  
181 ...
182  
183 aio_read( $fh, 0, 1024, $buffer, 0, $poco_aio->callback( 'open_done' ) );
184
185 aio_read( $fh, 0, 1024, $buffer, 0, sub {
186 ...
187 } );
188  
189 =head1 DESCRIPTION
190  
191 This component adds support for L<IO::AIO> use in POE
192  
193 =head2 EXAMPLE
194  
195 use POE;
196  
197 Foo->new();
198  
199 $poe_kernel->run();
200  
201 package Foo;
202  
203 use POE qw( Component::AIO );
204 use Fcntl;
205  
206 use strict;
207 use warnings;
208  
209 sub new {
210 my $class = shift;
211  
212 my $self = bless( {}, $class );
213  
214 POE::Session->create(
215 object_states => [
216 $self => [qw(
217 _start
218 _stop
219  
220 open_done
221 read_done
222 )]
223 ]
224 );
225
226 return $self;
227 }
228  
229 sub _start {
230 my $file = '/etc/passwd';
231
232 aio_open( $file, O_RDONLY, 0, $poco_aio->callback( 'open_done', $file ) );
233 }
234
235 sub open_done {
236 my ( $self, $session, $file, $fh ) = @_[ OBJECT, SESSION, ARG0, ARG1 ];
237
238 unless ( defined $fh ) {
239 die "aio open failed on $file: $!";
240 }
241
242 my $buffer = '';
243 # read 1024 bytes from $fh
244 aio_read( $fh, 0, 1024, $buffer, 0, $poco_aio->postback( 'read_done', \$buffer ) );
245 }
246
247 sub read_done {
248 my ( $self, $buffer, $bytes ) = @_[ OBJECT, ARG0, ARG1 ];
249
250 unless( $bytes > 0 ) {
251 die "aio read failed: $!";
252 }
253
254 print $$buffer;
255 }
256
257 sub _stop {
258 $poco_aio->shutdown();
259 }
260  
261 =head1 NOTES
262  
263 This module automaticly bootstraps itself on use(). $poco_aio is imported into your
264 namespace for easy use. Just like $poe_kernel when using L<POE>. There are two
265 import options available: no_auto_bootstrap and no_auto_export.
266  
267 Example:
268  
269 use POE::Component::AIO { no_auto_bootstrap => 1, no_auto_export => 1 };
270  
271 Also, use of this modules' callback and postback methods are completely optional.
272 They are included for convenience, but note that they don't work the same as the
273 postback and callback methods from L<POE::Session>.
274  
275 =head1 METHODS
276  
277 =over 4
278  
279 =item new()
280  
281 Call this to get the singleton object, which is the same as $poco_aio. See the notes
282 above. You do not need to call this unless you have disabled auto bootstrapping.
283  
284 =item shutdown()
285  
286 Stop the session used by this module.
287  
288 =item callback( $event [, $params ] )
289  
290 Returns a callback. Params are optional and are stacked before params passed to the callback
291 at call time. This differs from L<POE::Session>'s callback because the params are not wrapped
292 in array references. It uses the current session to latch the callback to. If you want to
293 use another session, you can pass an array ref of the session id and event name as the event
294 param.
295  
296 Examples:
297  
298 $cb = $poco_aio->callback( 'foo' );
299 $cb = $poco_aio->callback( 'foo', $bar );
300 $cb = $poco_aio->callback( [ $session->ID(), 'foo' ] );
301 $cb = $poco_aio->callback( [ $session->ID(), 'foo' ], $bar );
302  
303 =item postback( $event [, $params ] );
304  
305 See the callback method. The only difference is that it uses a post instead of call
306  
307 =head1 SEE ALSO
308  
309 L<IO::AIO>, L<POE>
310  
311 =head1 AUTHOR
312  
313 David Davis <xantus@cpan.org>
314 L<http://xantus.org/>
315  
316 =head1 LICENSE
317  
318 Artistic License
319  
320 =head1 COPYRIGHT AND LICENSE
321  
322 Copyright 2007 David Davis, and The Dojo Foundation.
323 Code was shared from the Cometd project L<http://cometd.com/>
324  
325 =cut
326  
327 package POE::Component::AIO::AnonCallback;
328  
329 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
330  
331 2 ▶sub new {
Subroutine new: called CC=2 SCAR=8.1
332 2 my $class = shift;
333  
334 2 ▶ bless( shift, ref $class || $class );
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
335 }
336  
337 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.2
338 2 my $self = shift;
339 2 my $session_id = delete $POE::Component::AIO::callback_ids{"$self"};
340  
341 2 ▶ if ( defined $session_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $session_id) { } 2 0 2
342 2 $poe_kernel->refcount_decrement( $session_id, 'anon_event' );
343 } else {
344 0 warn "connection callback DESTROY without session_id to refcount_decrement";
345 }
346  
347 2 return;
348 }
349  
350  
351 1 1;
352  
↑ Summary