DBus/Client/IOAsync.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 101 CC 7 SCAR 18.3
CC
7
Cov
0%
_initialize
12.0
DESTROY
12.0
_create
6.0
CRAP 6.2
1 package Protocol::DBus::Client::IOAsync;
2  
3 use strict;
4 use warnings;
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Protocol::DBus::Client::IOAsync - D-Bus with L<IO::Async>
11  
12 =head1 SYNOPSIS
13  
14 The following creates a D-Bus connection, sends two messages,
15 waits for their responses, then ends:
16  
17 use experimental 'signatures';
18  
19 my $loop = IO::Async::Loop->new();
20  
21 my $dbus = Protocol::DBus::Client::IOAsync::system($loop)
22  
23 $dbus->initialize()->then(
24 sub ($dbus) {
25 my $a = $dbus->send_call( .. )->then( sub ($resp) {
26 # ..
27 } );
28  
29 my $b = $dbus->send_call( .. )->then( sub ($resp) {
30 # ..
31 } );
32  
33 return Promise::ES6->all( [$a, $b] );
34 },
35 )->finally( sub { $loop->stop() } );
36  
37 $loop->run();
38  
39 =head1 DESCRIPTION
40  
41 This module provides an L<IO::Async> interface on top of
42 L<Protocol::DBus::Client>. It subclasses L<Protocol::DBus::Client::EventBase>.
43  
44 =cut
45  
46 our @ISA; # checked explicitly in DESTROY
47  
48 use parent qw( Protocol::DBus::Client::EventBase );
49  
50 use IO::Async::Handle ();
51  
52 #----------------------------------------------------------------------
53  
54 =head1 STATIC FUNCTIONS
55  
56 This module offers C<system()> and C<login_session()> functions that
57 offer similar functionality to their analogues in
58 L<Protocol::DBus::Client>, but they return instances of this class.
59  
60 Additionally, both functions require an L<IO::Async::Loop> to be passed.
61  
62 =cut
63  
64 sub system {
65 return __PACKAGE__->_create_system( $_[0] );
66 }
67  
68 sub login_session {
69 return __PACKAGE__->_create_login_session( $_[0] );
70 }
71  
72 #----------------------------------------------------------------------
73  
74 sub _create {
75 my ($class, $dbus, $loop) = @_;
76  
77 die 'need loop!' if !$loop;
78  
79 open my $s, '+>&=' . $dbus->fileno() or die "failed to dupe filehandle: $!";
80  
81 return $class->SUPER::_create($dbus, loop => $loop, socket => $s);
82 }
83  
84 sub _initialize {
85 my ($self, $y, $n) = @_;
86  
87 my $dbus = $self->{'db'};
88 my $loop = $self->{'loop'};
89 my $s = $self->{'socket'};
90  
91 my $watch;
92 my $watch_sr = \$watch;
93  
94 my $each_time = sub {
95  
96 $n->($@) if !eval {
97 if ( $dbus->initialize() ) {
98 $loop->remove($$watch_sr);
99 $$watch_sr = undef;
100 $y->();
101 }
102 else {
103 my $write_yn = $dbus->init_pending_send();
104 $$watch_sr->configure(
105 want_readready => !$write_yn,
106 want_writeready => $write_yn,
107 );
108 }
109  
110 1;
111 };
112 };
113  
114 $watch = IO::Async::Handle->new(
115 handle => $s,
116  
117 on_read_ready => $each_time,
118 on_write_ready => $each_time,
119  
120 # It does work to set the read watcher right away, but only
121 # because poll(POLLIN) gives POLLHUP, which IO::Async interprets
122 # as read-ready. Let’s not depend on that.
123 want_readready => 0,
124 want_writeready => 0,
125 );
126  
127 $loop->add($watch);
128  
129 # Defer in order to give the caller time to set up promises, etc.
130 $loop->later($each_time);
131  
132 return;
133 }
134  
135 sub _set_watches_and_create_messenger {
136 my ($self) = @_;
137  
138 my $dbus = $self->{'db'};
139 my $socket = $self->{'socket'};
140  
141 my $watch_sr;
142  
143 my $watch = IO::Async::Handle->new(
144 handle => $socket,
145  
146 on_read_ready => $self->_create_get_message_callback(),
147  
148 on_write_ready => sub {
149 $$watch_sr->want_writeready(0) if $dbus->flush_write_queue();
150 },
151 );
152  
153 $self->{'loop'}->add($watch);
154  
155 $self->{'watch_sr'} = $watch_sr = \$watch;
156  
157 my $loop = $self->{'loop'};
158  
159 $self->{'_give_up_cr'} = sub {
160 $loop->remove($$watch_sr);
161 $$watch_sr = undef;
162 };
163  
164 return sub {
165 $watch->want_writeready( $dbus->pending_send() );
166 };
167 }
168  
169 sub _pause {
170 ${ $_[0]->{'watch_sr'} }->want_readready(0);
171 }
172  
173 sub _resume {
174 ${ $_[0]->{'watch_sr'} }->want_readready(1);
175 }
176  
177 sub DESTROY {
178 my $watch_sr = delete $_[0]{'watch_sr'};
179  
180 if ($watch_sr && $$watch_sr) {
181 $_[0]{'loop'}->remove($$watch_sr);
182 $$watch_sr = undef;
183 }
184  
185 $_[0]->SUPER::DESTROY() if $ISA[0]->can('DESTROY');
186  
187 return;
188 }
189  
190 1;