DBus/Client/AnyEvent.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 111 CC 7 SCAR 21.4
CC
7
Cov
0%
_initialize
20.0
_flush_send_queue
12.0
_set_watches_and_create_messenger
6.0
CRAP 8.5
1 package Protocol::DBus::Client::AnyEvent;
2  
3 use strict;
4 use warnings;
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Protocol::DBus::Client::AnyEvent - D-Bus with L<AnyEvent>
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 $dbus = Protocol::DBus::Client::AnyEvent::system();
20  
21 my $cv = AnyEvent->condvar();
22  
23 $dbus->initialize()->then(
24 sub ($msgr) {
25 my $a = $msgr->send_call( .. )->then( sub ($resp) {
26 # ..
27 } );
28  
29 my $b = $msgr->send_call( .. )->then( sub ($resp) {
30 # ..
31 } );
32  
33 return Promise::ES6->all( [$a, $b] );
34 },
35 )->finally($cv);
36  
37 $cv->recv();
38  
39 =head1 DESCRIPTION
40  
41 This module provides an L<AnyEvent> interface on top of
42 L<Protocol::DBus::Client>. It subclasses L<Protocol::DBus::Client::EventBase>.
43  
44 =cut
45  
46 use parent qw( Protocol::DBus::Client::EventBase );
47  
48 use AnyEvent ();
49  
50 #----------------------------------------------------------------------
51  
52 =head1 STATIC FUNCTIONS
53  
54 This module provides C<system()> and C<login_session()> functions
55 that parallel their equivalents in L<Protocol::DBus::Client> but return
56 an instance of this class instead.
57  
58 =cut
59  
60 sub system {
61 return __PACKAGE__->_create_system();
62 }
63  
64 sub login_session {
65 return __PACKAGE__->_create_login_session();
66 }
67  
68 #----------------------------------------------------------------------
69  
70 =head1 SEE ALSO
71  
72 L<AnyEvent::DBus> is an AnyEvent wrapper for L<Net::DBus>.
73  
74 =cut
75  
76 #----------------------------------------------------------------------
77  
78 sub _initialize {
79 my ($self, $y, $n) = @_;
80  
81 my $dbus = $self->{'db'};
82  
83 my $fileno = $dbus->fileno();
84  
85 my $read_watch_r = \do { $self->{'_read_watch'} = undef };
86 my $write_watch_r = \do { $self->{'_write_watch'} = undef };
87  
88 my $cb_r;
89  
90 my $clear_watchers_cr = sub {
91 undef $$cb_r;
92 undef $$read_watch_r;
93 undef $$write_watch_r;
94 };
95  
96 my $cb = sub {
97 my $ok = eval {
98 if ( $dbus->initialize() ) {
99 $clear_watchers_cr->();
100 $y->();
101 }
102 else {
103 my $current_sub = $$cb_r;
104  
105 if ($dbus->init_pending_send()) {
106 $$write_watch_r ||= AnyEvent->io(
107 fh => $fileno,
108 poll => 'w',
109 cb => $current_sub,
110 );
111 }
112 else {
113 undef $$write_watch_r;
114  
115 $$read_watch_r ||= AnyEvent->io(
116 fh => $fileno,
117 poll => 'r',
118 cb => $current_sub,
119 );
120 }
121 }
122  
123 1;
124 };
125  
126 if (!$ok) {
127 $clear_watchers_cr->();
128 $n->($@);
129 }
130 };
131  
132 $cb_r = \$cb;
133  
134 # Postpone in order to give the caller time to set up promises, etc.
135 AnyEvent::postpone( sub { $cb->() } );
136  
137 return;
138 }
139  
140 sub _flush_send_queue {
141 my ($dbus, $fileno, $watch_sr) = @_;
142  
143 if ($dbus->pending_send()) {
144 $$watch_sr = AnyEvent->io(
145 fh => $fileno,
146 poll => 'w',
147 cb => sub { $$watch_sr = undef if $dbus->flush_write_queue() },
148 );
149 }
150  
151 return;
152 }
153  
154 sub _set_watches_and_create_messenger {
155 my ($self) = @_;
156  
157 my $dbus = $self->{'db'};
158  
159 my $fileno = $dbus->fileno();
160  
161 my $write_watch_sr = \$self->{'_write_watch'};
162  
163 my $flush_cr = sub {
164 _flush_send_queue( $dbus, $fileno, $write_watch_sr );
165 };
166  
167 if (!$self->{'_read_watch'}) {
168  
169 $flush_cr->();
170  
171 $self->{'_read_callback'} = $self->_create_get_message_callback();
172  
173 $self->_resume();
174  
175 my $read_watch_sr = \$self->{'_read_watch'};
176  
177 $self->{'_give_up_cr'} = sub {
178 $$read_watch_sr = undef;
179 };
180 }
181  
182 return $flush_cr;
183 }
184  
185 sub _pause {
186 $_[0]->{'_read_watch'} = undef;
187 }
188  
189 sub _resume {
190 my ($self) = @_;
191  
192 $self->{'_read_watch'} ||= AnyEvent->io(
193 fh => $self->{'db'}->fileno(),
194 poll => 'r',
195 cb => $self->{'_read_callback'},
196 );
197 }
198  
199 1;