DBus/Peer.pm

filter: Statementstmt 72.9% 97 / 133 Branchbran 37.5% 9 / 24 Conditioncond 56.5% 13 / 23 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 72.7% 24 / 33 Podpod 84.6% 11 / 13 totaltotal 65.2% 156 / 239 CC 23 SCAR 13.1
CC
23
Cov
66%
big_endian
12.0
_send_msg
6.2
flush_write_queue
6.0
CRAP 3.7
« DBus/WriteMsg.pm ↑ Summary DBus/Authn/Mechanism/DBUS_COOKIE_SHA1/Pieces.pm »
1 package Protocol::DBus::Peer;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Protocol::DBus::Peer - base class for a D-Bus peer
11  
12 =head1 SYNOPSIS
13  
14 $dbus->send_call(
15 interface => 'org.freedesktop.DBus.Properties',
16 member => 'GetAll',
17 signature => 's',
18 path => '/org/freedesktop/DBus',
19 destination => 'org.freedesktop.DBus',
20 body => [ 'org.freedesktop.DBus' ],
21 )->then( sub { .. } );
22  
23 my $msg = $dbus->get_message();
24  
25 # Same pattern as the IO::Handle method.
26 $dbus->blocking(0);
27  
28 my $fileno = $dbus->fileno();
29  
30 $dbus->flush_write_queue() if $dbus->pending_send();
31  
32 # I’m not sure why you’d want to do this, but …
33 $dbus->big_endian();
34  
35 =head1 DESCRIPTION
36  
37 This class contains D-Bus logic that is useful in both client and
38 server contexts. (Currently this distribution does not include a server
39 implementation.)
40  
41 =cut
42  
43 6 ▶use Call::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 6 ▶use Protocol::DBus::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 6 ▶use Protocol::DBus::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 6 ▶use Protocol::DBus::WriteMsg;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 6 ▶use constant _PROMISE_CLASS => 'Promise::ES6';
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 #----------------------------------------------------------------------
52  
53 =head1 METHODS
54  
55 =head2 $msg = I<OBJ>->get_message()
56  
57 This returns a single instace of L<Protocol::DBus::Message>, or undef if
58 no message is available. It will also fire the appropriate “on_return”
59 method on METHOD_RETURN or ERROR messages.
60  
61 The backend I/O logic reads data in chunks; thus, if there is a message
62 already available in the read buffer, no I/O is done. If you’re doing
63 non-blocking I/O then it is thus B<vital> that, every time the DBus socket
64 is readable, you call this function until undef is returned.
65  
66 =cut
67  
68 6 ▶sub get_message {
Subroutine get_message: called CC=4 SCAR=14.0
Pod documented
69 6 my $msg = $_[0]->{'_parser'}->get_message();
70  
71 6 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 6 0 6
72 6 ▶ if (my $serial = $msg->get_header('REPLY_SERIAL')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $serial = $msg->get_header("REPLY_SERIAL")) 2 4 6
73 2 delete $_[0]->{'_on_armageddon'}{$serial};
74  
75 2 ▶ if (my $cb = delete $_[0]->{'_on_return'}{$serial}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = delete $_[0]{"_on_return"}{$serial}) 2 0 2
76 2 $cb->($msg);
77 }
78 }
79 }
80  
81 6 return $msg;
82 }
83  
84 #----------------------------------------------------------------------
85  
86 =head2 I<OBJ>->flush_write_queue()
87  
88 Same as L<IO::Framed::Write>’s method of the same name.
89  
90 =cut
91  
92 0 ▶sub flush_write_queue {
Subroutine flush_write_queue: not called CC=2 SCAR=17.9
Pod documented
93 0 ▶ if ($_[0]->{'_io'}->get_write_queue_count()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"_io"}->get_write_queue_count) 0 0 0
94 0 return $_[0]->{'_io'}->flush_write_queue();
95 }
96  
97 0 return 1;
98 }
99  
100 #----------------------------------------------------------------------
101  
102 =head2 $promise = I<OBJ>->send_call( %OPTS )
103  
104 Send a METHOD_CALL message.
105  
106 %OPTS are C<path>, C<interface>, C<member>, C<destination>, C<signature>,
107 and C<body>. These do as you’d expect, but note that C<body>, if given,
108 must be an array reference.
109  
110 C<flags> may be given as an array reference of strings, e.g.,
111 C<NO_REPLY_EXPECTED>. See the D-Bus Specification for all possible values.
112  
113 The return value is an instance of L<Promise::ES6>. Normally this promise
114 resolves when a METHOD_RETURN arrives in response. The resolution value is a
115 a L<Protocol::DBus::Message> instance that represents the response. If,
116 however, C<flags> is given and contains C<NO_REPLY_EXPECTED>, the promise
117 resolves as soon as the message is sent.
118  
119 If an ERROR arrives in response instead, the promise will instead reject
120 with a L<Protocol::DBus::Message> instance that represents that ERROR.
121 The promise will also reject if some other error happens (e.g., an I/O
122 error while sending the initial METHOD_CALL).
123  
124 =cut
125  
126 6 ▶use constant _METHOD_RETURN_NUM => Protocol::DBus::Message::Header::MESSAGE_TYPE()->{'METHOD_RETURN'};
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 6 ▶sub _get_promise_class {
Subroutine _get_promise_class: called CC=2 SCAR=7.0
129 6 my ($self) = @_;
130  
131 6 ▶ $self->{'_loaded_promise'} ||= do {
Condition66%2/3
$self->{"_loaded_promise"} ||= do { local($!, $@); my $path = $self->_PROMISE_CLASS . '.pm'; $path =~ s[::][/]g; require $path }
l!l&&r!l&&!r
240
MC/DC0%0/2
$self->{"_loaded_promise"} ||= do { local($!, $@); my $path = $self->_PROMISE_CLASS . '.pm'; $path =~ s[::][/]g; require $path }
$self->{"_loaded_promise"} do { local($!, $@); my $path = $self->_PROMISE_CLASS . '.pm'; $path =~ s[::][/]g; require $path }
Truth table66%2/3
A ||= B
A= $self->{"_loaded_promise"}B= do { local($!, $@); my $path = $self->_PROMISE_CLASS . '.pm'; $path =~ s[::][/]g; require $path }
ABresult
1X1
011
000
132 4 local ($!, $@);
133 4 my $path = $self->_PROMISE_CLASS() . '.pm';
134 4 $path =~ s[::][/]g;
135  
136 4 require $path;
137 };
138  
139 6 return $self->_PROMISE_CLASS();
140 }
141  
142 2 ▶sub send_call {
Subroutine send_call: called CC=5 SCAR=17.3
Pod documented
143 2 my ($self, %opts) = @_;
144  
145 2 my ($res, $rej, $response_expected);
146  
147 2 my $ok;
148  
149 2 my $promise_class = $self->_get_promise_class();
150  
151 2 my $serial;
152  
153 2 ▶ my $on_armageddon_hr = $self->{'_on_armageddon'} ||= {};
Condition50%1/2
$self->{"_on_armageddon"} ||= {}
l!l
02
MC/DC0%0/1
$self->{"_on_armageddon"} ||= {}
$self->{"_on_armageddon"}
Truth table50%1/2
A
A= $self->{"_on_armageddon"}
Aresult
11
00
154  
155 2 ▶ my $promise = $promise_class->new( sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
156 2 ($res, $rej) = @_;
157  
158 0 ▶ if ($opts{'flags'} && grep { $_ eq 'NO_REPLY_EXPECTED' } @{ $opts{'flags'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'flags'} and grep {$_ eq 'NO_REPLY_EXPECTED';} @{$opts{'flags'};}) { } 0 2 2
Condition33%1/3
$opts{"flags"} and grep {$_ eq "NO_REPLY_EXPECTED";} @{$opts{"flags"};}
!ll&&!rl&&r
200
MC/DC0%0/2
$opts{"flags"} and grep {$_ eq "NO_REPLY_EXPECTED";} @{$opts{"flags"};}
$opts{"flags"} grep {$_ eq "NO_REPLY_EXPECTED";} @{$opts{"flags"};}
Truth table33%1/3
A and B
A= $opts{"flags"}B= grep {$_ eq "NO_REPLY_EXPECTED";} @{$opts{"flags"};}
ABresult
0X0
100
111
159 0 $response_expected = 0;
160 }
161 else {
162 2 $response_expected = 1;
163 }
164  
165 2 $self->_send_msg(
166 $res,
167 %opts,
168 type => 'METHOD_CALL',
169 );
170  
171 2 $serial = $self->{'_last_sent_serial'};
172  
173 2 $on_armageddon_hr->{$serial} = $rej;
174  
175 2 $ok = 1;
176 2 ▶ } )->finally( sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
177 2 ▶ delete $on_armageddon_hr->{$serial} if $serial;
Branch: 1 branch
BranchTrueFalseTotal
if $serial 2 0 2
178 2 } );
179  
180 2 ▶ if ($ok && $response_expected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok and $response_expected) 2 0 2
Condition33%1/3
$ok and $response_expected
!ll&&!rl&&r
002
MC/DC0%0/2
$ok and $response_expected
$ok $response_expected
Truth table33%1/3
A and B
A= $okB= $response_expected
ABresult
0X0
100
111
181 # Keep references to $self out of the callback
182 # in order to avoid memory leaks.
183 2 ▶ my $on_return_hr = $self->{'_on_return'} ||= {};
Condition50%1/2
$self->{"_on_return"} ||= {}
l!l
02
MC/DC0%0/1
$self->{"_on_return"} ||= {}
$self->{"_on_return"}
Truth table50%1/2
A
A= $self->{"_on_return"}
Aresult
11
00
184  
185 2 my $orig_promise = $promise;
186  
187 2 ▶ $promise = $promise->then( sub {
Subroutine __ANON__: called CC=1 SCAR=0.1
188  
189 2 ▶ return $promise_class->new( sub {
Subroutine __ANON__: called CC=1 SCAR=0.2
190 2 my ($res, $rej) = @_;
191  
192 2 $on_armageddon_hr->{$serial} = $rej;
193  
194 2 ▶ $on_return_hr->{$serial} = sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
195 2 ▶ if ($_[0]->get_type() == _METHOD_RETURN_NUM()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->get_type == 2) { } 2 0 2
196 2 $res->($_[0]);
197 }
198 else {
199 0 $rej->($_[0]);
200 }
201 2 };
202 2 } );
203 2 } );
204 }
205  
206 2 return $promise;
207 }
208  
209 =head2 $promise = I<OBJ>->send_return( $ORIG_MSG, %OPTS )
210  
211 Send a METHOD_RETURN message.
212  
213 The return is a promise that resolves when the message is sent.
214  
215 Arguments are similar to C<send_call()> except for the header differences
216 that the D-Bus specification describes. Also, C<destination> is not given
217 directly but is instead inferred from the $ORIG_MSG. (Behavior is
218 undefined if this parameter is given directly.)
219  
220 =cut
221  
222 2 ▶sub send_return {
Subroutine send_return: called CC=1 SCAR=0.0
Pod documented
223 2 my ($self, $orig_msg, @opts_kv) = @_;
224  
225 2 ▶ return $self->_get_promise_class()->new( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
226 2 my ($res) = @_;
227  
228 2 $self->_send_msg(
229 $res,
230 _response_fields_from_orig_msg($orig_msg, \@opts_kv),
231 type => 'METHOD_RETURN',
232 );
233 2 } );
234 }
235  
236 =head2 $promise = I<OBJ>->send_error( $ORIG_MSG, %OPTS )
237  
238 Like C<send_return()>, but sends an error instead. The
239 C<error_name> parameter is required.
240  
241 =cut
242  
243 0 ▶sub send_error {
Subroutine send_error: not called CC=1 SCAR=6.9
Pod documented
244 0 my ($self, $orig_msg, @opts_kv) = @_;
245  
246 0 ▶ return $self->_get_promise_class()->new( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
247 0 my ($res) = @_;
248  
249 0 $self->_send_msg(
250 $res,
251 _response_fields_from_orig_msg($orig_msg, \@opts_kv),
252 type => 'ERROR',
253 );
254 0 } );
255 }
256  
257 2 ▶sub _response_fields_from_orig_msg {
Subroutine _response_fields_from_orig_msg: called CC=1 SCAR=0.0
258  
259 return (
260  
261 # This has to honor a passed “destination”
262 # so that we can implement a D-Bus server in tests.
263 destination => $_[0]->get_header('SENDER'),
264  
265 2 @{ $_[1] },
266  
267 # Reject callers’ attempts to set this one.
268 reply_serial => $_[0]->get_serial(),
269 );
270 }
271  
272 =head2 $promise = I<OBJ>->send_signal( %OPTS )
273  
274 Like C<send_call()> but sends a signal rather than a method call.
275  
276 =cut
277  
278 2 ▶sub send_signal {
Subroutine send_signal: called CC=1 SCAR=0.0
Pod documented
279 2 my ($self, @opts_kv) = @_;
280  
281 2 ▶ return $self->_get_promise_class()->new( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
282 2 my ($res) = @_;
283  
284 2 $self->_send_msg(
285 $res,
286 @opts_kv,
287 type => 'SIGNAL',
288 );
289 2 } );
290 }
291  
292 #----------------------------------------------------------------------
293  
294 =head2 I<OBJ>->big_endian()
295  
296 Same interface as C<blocking()>, but this sets/gets/toggles whether to send
297 big-endian messages instead of little-endian.
298  
299 By default this library uses the system’s native byte order, so you probably
300 have little need for this function.
301  
302 =cut
303  
304 0 ▶sub big_endian {
Subroutine big_endian: not called CC=3 SCAR=24.8
Pod documented
305 0 my ($self) = @_;
306  
307 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
308 0 my $old = $self->{'_big_endian'};
309 0 $self->{'_big_endian'} = !!$_[1];
310  
311 0 ▶ $self->{'_to_str_fn'} = 'to_string_' . ($_[1] ? 'be' : 'le');
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
312  
313 0 return $self->{'_big_endian'};
314 }
315  
316 0 return !!$self->{'_big_endian'};
317 }
318  
319 #----------------------------------------------------------------------
320  
321 =head2 I<OBJ>->preserve_variant_signatures()
322  
323 Same interface as C<blocking()>, but when this is enabled
324 variants are given as two-member array references ([ signature => value ]),
325 blessed as C<Protocol::DBus::Type::Variant> instances.
326  
327 For most Perl applications this is probably counterproductive.
328  
329 =cut
330  
331 0 ▶sub preserve_variant_signatures {
Subroutine preserve_variant_signatures: not called CC=1 SCAR=6.9
Pod documented
332 0 my $self = shift;
333  
334 0 return $self->{'_parser'}->preserve_variant_signatures(@_);
335 }
336  
337 #----------------------------------------------------------------------
338  
339 =head2 I<OBJ>->blocking()
340  
341 Same interface as L<IO::Handle>’s method of the same name.
342  
343 =cut
344  
345 0 ▶sub blocking {
Subroutine blocking: not called CC=2 SCAR=17.9
Pod documented
346 0 my $self = shift;
347  
348 # require() is needed on pre-5.14 perls:
349 0 ▶ if ($^V lt v5.14) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^V lt v5.14) 0 0 0
350 0 local ($@, $!);
351 0 require IO::File;
352 }
353  
354 0 return $self->{'_socket'}->blocking(@_);
355 }
356  
357 #----------------------------------------------------------------------
358  
359 =head2 I<OBJ>->fileno()
360  
361 Returns the connection socket’s file descriptor.
362  
363 =cut
364  
365 0 ▶sub fileno {
Subroutine fileno: not called CC=1 SCAR=6.9
Pod documented
366 0 return fileno $_[0]->{'_socket'};
367 }
368  
369 #----------------------------------------------------------------------
370  
371 =head2 I<OBJ>->pending_send()
372  
373 Returns a boolean that indicates whether there is data queued up to send
374 to the server.
375  
376 =cut
377  
378 0 ▶sub pending_send {
Subroutine pending_send: not called CC=1 SCAR=6.9
Pod documented
379 0 return !!$_[0]->{'_io'}->get_write_queue_count();
380 }
381  
382 #----------------------------------------------------------------------
383  
384 # undocumented
385 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
386 5 my ($class, $socket) = @_;
387  
388 5 my $self = bless { _socket => $socket }, $class;
389  
390 5 $self->_set_up_peer_io( $socket );
391  
392 5 return $self;
393 }
394  
395 0 ▶sub do_armageddon {
Subroutine do_armageddon: not called CC=2 SCAR=17.9
Pod undocumented
396 0 my ($self, $why) = @_;
397  
398 0 %{ $self->{'_on_return'} } = ();
399  
400 0 my $on_armageddon_hr = $self->{'_on_armageddon'};
401  
402 0 my @cbs = delete @{$on_armageddon_hr}{ keys %$on_armageddon_hr };
403  
404 0 $_->($why) for @cbs;
405  
406 0 return;
407 }
408  
409 #----------------------------------------------------------------------
410  
411 5 ▶sub _set_up_peer_io {
Subroutine _set_up_peer_io: called CC=1 SCAR=0.0
412 5 my ($self, $socket) = @_;
413  
414 5 $self->{'_io'} = Protocol::DBus::WriteMsg->new( $socket )->enable_write_queue();
415 5 $self->{'_parser'} = Protocol::DBus::Parser->new( $socket );
416  
417 5 return;
418 }
419  
420 6 ▶sub _send_msg {
Subroutine _send_msg: called CC=6 SCAR=18.3
421 6 my ($self, $on_send, %opts) = @_;
422  
423 6 my ($type, $body_ar, $flags) = delete @opts{'type', 'body', 'flags'};
424  
425 my @hargs = map {
426 6 my $k = $_;
427 28 $k =~ tr<a-z><A-Z>;
428 28 ( $k => $opts{$_} );
429 } keys %opts;
430  
431 6 my $serial = ++$self->{'_last_sent_serial'};
432  
433 6 my $msg = Protocol::DBus::Message->new(
434 type => $type,
435 hfields => \@hargs,
436 flags => $flags,
437 body => $body_ar,
438 serial => $serial,
439 );
440  
441 # Use native byte order by default.
442 6 ▶ $self->{'_endian'} ||= (pack 'n', 1) eq (pack 'l', 1) ? 'be' : 'le';
Condition100%2/2
$self->{"_endian"} ||= "le"
l!l
24
MC/DC100%1/1
$self->{"_endian"} ||= "le"
$self->{"_endian"}
Truth table100%2/2
A
A= $self->{"_endian"}
Aresult
11
00
443  
444 6 ▶ $self->{'_to_str_fn'} ||= "to_string_$self->{'_endian'}";
Condition100%2/2
$self->{"_to_str_fn"} ||= qq[to_string_$self->{"_endian"}]
l!l
24
MC/DC100%1/1
$self->{"_to_str_fn"} ||= qq[to_string_$self->{"_endian"}]
$self->{"_to_str_fn"}
Truth table100%2/2
A
A= $self->{"_to_str_fn"}
Aresult
11
00
445  
446 6 my ($buf_sr, $fds_ar) = $msg->can($self->{'_to_str_fn'})->($msg);
447  
448 6 ▶ if ($fds_ar && @$fds_ar && !$self->supports_unix_fd()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fds_ar and @$fds_ar and not $self->supports_unix_fd) 0 6 6
Condition50%3/6
$fds_ar and @$fds_ar and not $self->supports_unix_fd
!ll&&!rl&&r
600
$fds_ar and @$fds_ar
!ll&&!rl&&r
240
MC/DC0%0/3
$fds_ar and @$fds_ar and not $self->supports_unix_fd
$fds_ar @$fds_ar not $self->supports_unix_fd
Truth table50%2/4
A and B and not(C)
A= $fds_arB= @$fds_arC= not $self->supports_unix_fd
ABCresult
0XX0
10X0
1100
1111
449 0 die "Cannot send file descriptors without UNIX FD support!";
450 }
451  
452 6 $self->{'_io'}->enqueue_message( $buf_sr, $fds_ar, $on_send );
453  
454 6 return $self->{'_io'}->flush_write_queue();
455 }
456  
457 6 1;
« DBus/WriteMsg.pm ↑ Summary DBus/Authn/Mechanism/DBUS_COOKIE_SHA1/Pieces.pm »