DBus/Peer.pm

filter: Statementstmt 75.9% 60 / 79 Branchbran 37.5% 6 / 16 Conditioncond 70.0% 7 / 10 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 66.6% 14 / 21 Podpod 91.6% 11 / 12 totaltotal 69.9% 100 / 143 SCAR 28.8
CC
13
Cov
70%
big_endian
12.0
_send_msg
6.2
flush_write_queue
6.0
CRAP 17.8
« DBus/Path.pm ↑ Summary DBus/Signature.pm »
1 package Protocol::DBus::Peer;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶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 on_return => sub { my ($msg) = @_ },
22 );
23  
24 my $msg = $dbus->get_message();
25  
26 # Same pattern as the IO::Handle method.
27 $dbus->blocking(0);
28  
29 my $fileno = $dbus->fileno();
30  
31 $dbus->flush_write_queue() if $dbus->pending_send();
32  
33 # I’m not sure why you’d want to do this, but …
34 $dbus->big_endian();
35  
36 =head1 DESCRIPTION
37  
38 This class contains D-Bus logic that is useful in both client and
39 server contexts. (Currently this distribution does not include a server
40 implementation.)
41  
42 =cut
43  
44 5 ▶use Call::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 5 ▶use Protocol::DBus::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 5 ▶use Protocol::DBus::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 5 ▶use Protocol::DBus::WriteMsg;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 #----------------------------------------------------------------------
51  
52 =head1 METHODS
53  
54 =head2 $msg = I<OBJ>->get_message()
55  
56 This returns a single instace of L<Protocol::DBus::Message>, or undef if
57 no message is available. It will also fire the appropriate “on_return”
58 method on METHOD_RETURN messages.
59  
60 The backend I/O logic reads data in chunks; thus, if there is a message
61 already available in the read buffer, no I/O is done. If you’re doing
62 non-blocking I/O then it is thus B<vital> that, every time the DBus socket
63 is readable, you call this function until undef is returned.
64  
65 =cut
66  
67 sub get_message {
68 3 ▶ my $msg = $_[0]->{'_parser'}->get_message();
Subroutine get_message: called CC=4 SCAR=14.0
Pod documented
69  
70 3 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 3 0 3
71 3 ▶ if (my $serial = $msg->get_header('REPLY_SERIAL')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $serial = $msg->get_header("REPLY_SERIAL")) 1 2 3
72 1 ▶ if (my $cb = delete $_[0]->{'_on_return'}{$serial}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = delete $_[0]{"_on_return"}{$serial}) 1 0 1
73 1 $cb->($msg);
74 }
75 }
76 }
77  
78 3 return $msg;
79 }
80  
81 #----------------------------------------------------------------------
82  
83 =head2 I<OBJ>->flush_write_queue()
84  
85 Same as L<IO::Framed::Write>’s method of the same name.
86  
87 =cut
88  
89 sub flush_write_queue {
90 0 ▶ if ($_[0]->{'_io'}->get_write_queue_count()) {
Subroutine flush_write_queue: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"_io"}->get_write_queue_count) 0 0 0
91 0 return $_[0]->{'_io'}->flush_write_queue();
92 }
93  
94 0 return 1;
95 }
96  
97 #----------------------------------------------------------------------
98  
99 =head2 I<OBJ>->send_call( %OPTS )
100  
101 Send a METHOD_CALL message.
102  
103 %OPTS are C<path>, C<interface>, C<member>, C<destination>, C<signature>,
104 C<body>, and C<on_return>. These do as you’d expect, with the following
105 caveats:
106  
107 =over
108  
109 =item * C<body>, if given, must be an array reference. See
110 L<Protocol::DBus::Message> for a discussion of how to map between D-Bus and
111 Perl.
112  
113 =item * The C<on_return> callback receives the server’s response
114 message (NB: either METHOD_RETURN or ERROR) as argument.
115  
116 =back
117  
118 =cut
119  
120 sub send_call {
121 1 ▶ my ($self, %opts) = @_;
Subroutine send_call: called CC=2 SCAR=7.0
Pod documented
122  
123 1 my $cb = delete $opts{'on_return'};
124  
125 1 my $ret = $self->_send_msg(
126 %opts,
127 type => 'METHOD_CALL',
128 );
129  
130 1 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 1 0 1
131 1 my $serial = $self->{'_last_sent_serial'};
132 1 $self->{'_on_return'}{$serial} = $cb;
133 }
134  
135 1 return $ret;
136 }
137  
138 =head2 I<OBJ>->send_return( $ORIG_MSG, %OPTS )
139  
140 Send a METHOD_RETURN message.
141  
142 Arguments are similar to C<send_call()> except for the header differences
143 that the D-Bus specification describes. Also:
144  
145 =over
146  
147 =item * C<destination> is taken from the $ORIG_MSG. (Behavior is
148 undefined if this parameter is given directly.)
149  
150 =item * There is no C<on_return> parameter.
151  
152 =back
153  
154 =cut
155  
156 sub send_return {
157 1 ▶ my ($self, $orig_msg, @opts_kv) = @_;
Subroutine send_return: called CC=1 SCAR=0.0
Pod documented
158  
159 1 return $self->_send_msg(
160 _response_fields_from_orig_msg($orig_msg, \@opts_kv),
161 type => 'METHOD_RETURN',
162 );
163 }
164  
165 =head2 I<OBJ>->send_error( $ORIG_MSG, %OPTS )
166  
167 Like C<send_return()>, but sends an error instead. The
168 C<error_name> parameter is required.
169  
170 =cut
171  
172 sub send_error {
173 0 ▶ my ($self, $orig_msg, @opts_kv) = @_;
Subroutine send_error: not called CC=1 SCAR=6.9
Pod documented
174  
175 0 return $self->_send_msg(
176 _response_fields_from_orig_msg($orig_msg, \@opts_kv),
177 type => 'ERROR',
178 );
179 }
180  
181 sub _response_fields_from_orig_msg {
182  
183 return (
184  
185 # This has to honor a passed “destination”
186 # so that we can implement a D-Bus server in tests.
187 destination => $_[0]->get_header('SENDER'),
188  
189 1 ▶ @{ $_[1] },
Subroutine _response_fields_from_orig_msg: called CC=1 SCAR=0.0
190  
191 # Reject callers’ attempts to set this one.
192 reply_serial => $_[0]->get_serial(),
193 );
194 }
195  
196 =head2 I<OBJ>->send_signal( %OPTS )
197  
198 Like C<send_call()> but sends a signal rather than a method call.
199 There is no C<on_return> parameter.
200  
201 =cut
202  
203 sub send_signal {
204 1 ▶ my ($self, @opts_kv) = @_;
Subroutine send_signal: called CC=1 SCAR=0.0
Pod documented
205  
206 1 return $self->_send_msg(
207 @opts_kv,
208 type => 'SIGNAL',
209 );
210 }
211  
212 #----------------------------------------------------------------------
213  
214 =head2 I<OBJ>->big_endian()
215  
216 Same interface as C<blocking()>, but this sets/gets/toggles whether to send
217 big-endian messages instead of little-endian.
218  
219 By default this library uses the system’s native byte order, so you probably
220 have little need for this function.
221  
222 =cut
223  
224 sub big_endian {
225 0 ▶ my ($self) = @_;
Subroutine big_endian: not called CC=3 SCAR=24.8
Pod documented
226  
227 0 ▶ if (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 0 0 0
228 0 my $old = $self->{'_big_endian'};
229 0 $self->{'_big_endian'} = !!$_[1];
230  
231 0 ▶ $self->{'_to_str_fn'} = 'to_string_' . ($_[1] ? 'be' : 'le');
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
232  
233 0 return $self->{'_big_endian'};
234 }
235  
236 0 return !!$self->{'_big_endian'};
237 }
238  
239 #----------------------------------------------------------------------
240  
241 =head2 I<OBJ>->preserve_variant_signatures()
242  
243 Same interface as C<blocking()>, but when this is enabled
244 variants are given as two-member array references ([ signature => value ]),
245 blessed as C<Protocol::DBus::Type::Variant> instances.
246  
247 For most Perl applications this is probably counterproductive.
248  
249 =cut
250  
251 sub preserve_variant_signatures {
252 0 ▶ my $self = shift;
Subroutine preserve_variant_signatures: not called CC=1 SCAR=6.9
Pod documented
253  
254 0 return $self->{'_parser'}->preserve_variant_signatures(@_);
255 }
256  
257 #----------------------------------------------------------------------
258  
259 =head2 I<OBJ>->blocking()
260  
261 Same interface as L<IO::Handle>’s method of the same name.
262  
263 =cut
264  
265 sub blocking {
266 0 ▶ my $self = shift;
Subroutine blocking: not called CC=1 SCAR=6.9
Pod documented
267  
268 0 return $self->{'_socket'}->blocking(@_);
269 }
270  
271 #----------------------------------------------------------------------
272  
273 =head2 I<OBJ>->fileno()
274  
275 Returns the connection socket’s file descriptor.
276  
277 =cut
278  
279 sub fileno {
280 0 ▶ return fileno $_[0]->{'_socket'};
Subroutine fileno: not called CC=1 SCAR=6.9
Pod documented
281 }
282  
283 #----------------------------------------------------------------------
284  
285 =head2 I<OBJ>->pending_send()
286  
287 Returns a boolean that indicates whether there is data queued up to send
288 to the server.
289  
290 =cut
291  
292 sub pending_send {
293 0 ▶ return !!$_[0]->{'_io'}->get_write_queue_count();
Subroutine pending_send: not called CC=1 SCAR=6.9
Pod documented
294 }
295  
296 #----------------------------------------------------------------------
297  
298 # undocumented
299 sub new {
300 2 ▶ my ($class, $socket) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
301  
302 2 my $self = bless { _socket => $socket }, $class;
303  
304 2 $self->_set_up_peer_io( $socket );
305  
306 2 return $self;
307 }
308  
309 #----------------------------------------------------------------------
310  
311 sub _set_up_peer_io {
312 2 ▶ my ($self, $socket) = @_;
Subroutine _set_up_peer_io: called CC=1 SCAR=0.0
313  
314 2 $self->{'_io'} = Protocol::DBus::WriteMsg->new( $socket )->enable_write_queue();
315 2 $self->{'_parser'} = Protocol::DBus::Parser->new( $socket );
316  
317 2 return;
318 }
319  
320 sub _send_msg {
321 3 ▶ my ($self, %opts) = @_;
Subroutine _send_msg: called CC=6 SCAR=18.3
322  
323 3 my ($type, $body_ar, $flags) = delete @opts{'type', 'body', 'flags'};
324  
325 my @hargs = map {
326 3 my $k = $_;
327 14 $k =~ tr<a-z><A-Z>;
328 14 ( $k => $opts{$_} );
329 } keys %opts;
330  
331 3 my $serial = ++$self->{'_last_sent_serial'};
332  
333 3 my $msg = Protocol::DBus::Message->new(
334 type => $type,
335 hfields => \@hargs,
336 flags => $flags,
337 body => $body_ar,
338 serial => $serial,
339 );
340  
341 # Use native byte order by default.
342 3 ▶ $self->{'_endian'} ||= (pack 'n', 1) eq (pack 'l', 1) ? 'be' : 'le';
Condition100%2/2
$self->{"_endian"} ||= "le"
l!l
12
MC/DC100%1/1
$self->{"_endian"} ||= "le"
$self->{"_endian"}
Truth table100%2/2
A
A= $self->{"_endian"}
Aresult
11
00
343  
344 3 ▶ $self->{'_to_str_fn'} ||= "to_string_$self->{'_endian'}";
Condition100%2/2
$self->{"_to_str_fn"} ||= qq[to_string_$self->{"_endian"}]
l!l
12
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
345  
346 3 my ($buf_sr, $fds_ar) = $msg->can($self->{'_to_str_fn'})->($msg);
347  
348 3 ▶ 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 3 3
Condition50%3/6
$fds_ar and @$fds_ar and not $self->supports_unix_fd
!ll&&!rl&&r
300
$fds_ar and @$fds_ar
!ll&&!rl&&r
120
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
349 0 die "Cannot send file descriptors without UNIX FD support!";
350 }
351  
352 3 $self->{'_io'}->enqueue_message( $buf_sr, $fds_ar );
353  
354 3 return $self->{'_io'}->flush_write_queue();
355 }
356  
357 5 1;
« DBus/Path.pm ↑ Summary DBus/Signature.pm »