blib/lib/Future/IO/Impl/POE.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 83 CC 5 SCAR 19.5
CC
5
Cov
0%
ready_for_read
12.0
ready_for_write
12.0
sleep
2.0
CRAP 7.0
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2019 -- leonerd@leonerd.org.uk
5  
6 package Future::IO::Impl::POE;
7  
8 use strict;
9 use warnings;
10 use base qw( Future::IO::ImplBase );
11  
12 =head1 NAME
13  
14 C<Future::IO::Impl::POE> - implement C<Future::IO> using C<POE>
15  
16 =head1 DESCRIPTION
17  
18 This module provides an implemention for L<Future::IO> which uses L<POE>.
19  
20 There are no additional methods to use in this module; it simply has to be
21 loaded, and will provide the C<Future::IO> implementation methods.
22  
23 use Future::IO;
24 use Future::IO::Impl::POE;
25  
26 my $f = Future::IO->sleep(5);
27 ...
28  
29 =cut
30  
31 use POE;
32  
33 __PACKAGE__->APPLY;
34  
35 sub sleep
36 {
37 shift;
38 my ( $secs ) = @_;
39  
40 return POE::Future->new_delay( $secs );
41 }
42  
43 my $iosession;
44 sub _mk_iosession
45 {
46 return $iosession //= POE::Session->create(
47 inline_states => {
48 _start => sub {
49 $_[KERNEL]->alias_set( __PACKAGE__ );
50 },
51  
52 invoke => sub { $_[-1]->() },
53  
54 select_read => sub {
55 $_[KERNEL]->select_read( $_[ARG0], invoke => $_[ARG1] );
56 },
57 unselect_read => sub {
58 $_[KERNEL]->select_read( $_[ARG0] );
59 },
60 select_write => sub {
61 $_[KERNEL]->select_write( $_[ARG0], invoke => $_[ARG1] );
62 },
63 unselect_write => sub {
64 $_[KERNEL]->select_write( $_[ARG0] );
65 },
66 },
67 );
68 }
69  
70 my %watching_read_by_fileno; # {fileno} => [@futures]
71  
72 sub ready_for_read
73 {
74 shift;
75 my ( $fh ) = @_;
76  
77 my $watching = $watching_read_by_fileno{ $fh->fileno } //= [];
78 my $f = POE::Future->new;
79  
80 my $was = scalar @$watching;
81 push @$watching, $f;
82  
83 return $f if $was;
84  
85 my $session = _mk_iosession;
86 POE::Kernel->call( $session, select_read => $fh, sub {
87 $watching->[0]->done;
88 shift @$watching;
89  
90 return if scalar @$watching;
91  
92 POE::Kernel->call( $session, unselect_read => $fh );
93 delete $watching_read_by_fileno{ $fh->fileno };
94 } );
95  
96 return $f;
97 }
98  
99 my %watching_write_by_fileno; # {fileno} => [@futures]
100  
101 sub ready_for_write
102 {
103 shift;
104 my ( $fh ) = @_;
105  
106 my $watching = $watching_write_by_fileno{ $fh->fileno } //= [];
107 my $f = POE::Future->new;
108  
109 my $was = scalar @$watching;
110 push @$watching, $f;
111  
112 return $f if $was;
113  
114 my $session = _mk_iosession;
115 POE::Kernel->call( $session, select_write => $fh, sub {
116 $watching->[0]->done;
117 shift @$watching;
118  
119 return if scalar @$watching;
120  
121 POE::Kernel->call( $session, unselect_write => $fh );
122 delete $watching_write_by_fileno{ $fh->fileno };
123 } );
124  
125 return $f;
126 }
127  
128 =head1 AUTHOR
129  
130 Paul Evans <leonerd@leonerd.org.uk>
131  
132 =cut
133  
134 0x55AA;