DBus/Client/EventBase.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 132 CC 7 SCAR 26.3
CC
7
Cov
0%
_create_get_message_callback
42.0
DESTROY
6.0
initialize
2.0
CRAP 13.8
1 package Protocol::DBus::Client::EventBase;
2  
3 use strict;
4 use warnings;
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Protocol::DBus::Client::EventBase - Base class for event-driven L<Protocol::DBus>
11  
12 =head1 DESCRIPTION
13  
14 This base class encapsulates the fundamentals of an event-loop-aware
15 L<Protocol::DBus::Client>. If you use D-Bus in a standard event loop
16 (i.e., an event loop from CPAN), you probably want to use a subclass of
17 this module.
18  
19 The module you’ll actually use will be an end class like
20 L<Protocol::DBus::Client::IOAsync> or
21 L<Protocol::DBus::Client::AnyEvent>.
22  
23 =head1 SUBCLASS INTERFACE
24  
25 Currently the subclass interface is not documented for public consumption.
26 Contact me if you’d like to change that (and are willing to put in some
27 effort toward such end).
28  
29 =head1 TODO
30  
31 There aren’t tests written against this module or its subclasses.
32 It would be great to rectify that.
33  
34 =cut
35  
36 #----------------------------------------------------------------------
37  
38 use Protocol::DBus::Client ();
39 use Protocol::DBus::Client::EventMessenger ();
40  
41 use Scalar::Util ();
42  
43 #----------------------------------------------------------------------
44  
45 =head1 INSTANCE METHODS
46  
47 =head2 $promise = I<OBJ>->initialize()
48  
49 Returns a promise that resolves to a
50 L<Protocol::DBus::Client::EventMessenger> instance. That object, not
51 this one, is what you’ll use to send and receive messages.
52  
53 =cut
54  
55 sub initialize {
56 my ($self) = @_;
57  
58 my $paused_sr = \$self->{'_paused'};
59  
60 my $dbus = $self->{'db'};
61  
62 my $weak_self = $self;
63 Scalar::Util::weaken( $weak_self );
64  
65 return $self->{'_initialize_promise'} ||= $self->{'db'}->_get_promise_class()->new( sub {
66 $self->_initialize(@_);
67 } )->then( sub {
68 my $post_send_cr = $weak_self->_set_watches_and_create_messenger();
69  
70 return Protocol::DBus::Client::EventMessenger->new(
71 $dbus,
72 $post_send_cr,
73 sub {
74 $weak_self->_pause();
75 $$paused_sr = 1;
76 },
77 sub {
78 $weak_self->_resume();
79 $$paused_sr = 0;
80 },
81 );
82 } );
83 }
84  
85 =head2 $obj = I<OBJ>->on_signal( $HANDLER_CR )
86  
87 Installs a handler for D-Bus signals. Whenever I<OBJ> receives such a
88 message, an instance of L<Protocol::DBus::Message> that represents the
89 message will be passed to $HANDLER_CR.
90  
91 Pass undef to disable a previously-set handler.
92  
93 Returns I<OBJ>.
94  
95 =cut
96  
97 sub on_signal {
98 my ($self, $cb) = @_;
99  
100 $self->{'_on_signal_r'} = \$cb;
101  
102 return $self;
103 }
104  
105 =head2 $obj = I<OBJ>->on_message( $HANDLER_CR )
106  
107 Like C<on_signal()> but for all received D-Bus messages, not just signals.
108 This is useful for monitoring … and not much else?
109  
110 =cut
111  
112 sub on_message {
113 my ($self, $cb) = @_;
114  
115 $self->{'_on_message_r'} = \$cb;
116  
117 return $self;
118 }
119  
120 =head2 $obj = I<OBJ>->on_failure( $HANDLER_CR )
121  
122 Set this to receive a copy of whatever error kills the connection.
123 If not set, such an error will be warn()ed.
124  
125 =cut
126  
127 sub on_failure {
128 my ($self, $cb) = @_;
129  
130 $self->{'_on_failure'} = $cb;
131  
132 return $self;
133 }
134  
135 #----------------------------------------------------------------------
136  
137 sub _on_failure {
138 my ($self, $cb) = @_;
139  
140 $self->{'_on_failure'} = $cb;
141  
142 return $self;
143 }
144  
145 sub _create_system {
146 return $_[0]->_create(
147 Protocol::DBus::Client::system(),
148 @_[ 1 .. $#_ ],
149 );
150 }
151  
152 sub _create_login_session {
153 return $_[0]->_create(
154 Protocol::DBus::Client::login_session(),
155 @_[ 1 .. $#_ ],
156 );
157 }
158  
159 sub _create {
160  
161 my ($class, $dbus, %opts) = @_;
162  
163 $opts{'db'} = $dbus;
164  
165 $dbus->blocking(0);
166  
167 return bless \%opts, $class;
168 }
169  
170 sub _create_get_message_callback {
171 my ($self) = @_;
172  
173 my $dbus = $self->{'db'};
174  
175 my $on_message_cr_r = $self->{'_on_message_r'} ||= \do { my $v = undef };
176 my $on_signal_cr_r = $self->{'_on_signal_r'} ||= \do { my $v = undef };
177  
178 my $on_failure_cr_r = \$self->{'_on_failure'};
179 my $_give_up_cr_r = \$self->{'_give_up_cr'};
180  
181 my $paused_r = \$self->{'_paused'};
182  
183 return sub {
184 my $ok = eval {
185 while (my $msg = $dbus->get_message()) {
186 if ($$on_message_cr_r) {
187 $$on_message_cr_r->($msg);
188 }
189  
190 if ($$on_signal_cr_r && $msg->type_is('SIGNAL')) {
191 $$on_signal_cr_r->($msg);
192 }
193  
194 # In testing D-Bus was fast enough to send back
195 # a self-directed signal while this loop was happening,
196 # which caused receipt of messages even after pause()
197 # had been called.
198 last if $$paused_r;
199 }
200  
201 1;
202 };
203  
204 if (!$ok) {
205 my $err = $@;
206  
207 if (my $cr = $$on_failure_cr_r) {
208 $cr->($err);
209 }
210 else {
211 warn $err;
212 }
213  
214 $$_give_up_cr_r->();
215  
216 $dbus->do_armageddon($err);
217 }
218 };
219 }
220  
221 sub DESTROY {
222 if (defined(${^GLOBAL_PHASE}) && 'DESTRUCT' eq ${^GLOBAL_PHASE}) {
223 warn "$_[0] lasted until global destruction!";
224 }
225 }
226  
227 1;