DBus/Peer.pm

filter: Statementstmt 75.3% 58 / 77 Branchbran 37.5% 6 / 16 Conditioncond 70.0% 7 / 10 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 65.0% 13 / 20 Podpod 66.6% 8 / 12 totaltotal 67.1% 94 / 140 CC 13 SCAR 13.7
CC
13
Cov
69%
big_endian
12.0
_send_msg
6.2
flush_write_queue
6.0
CRAP 3.9
« DBus/Client.pm ↑ Summary DBus/Authn/Mechanism/DBUS_COOKIE_SHA1/Pieces.pm »
1 package Protocol::DBus::Peer;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶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 4 ▶use Call::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 4 ▶use Protocol::DBus::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 ▶use Protocol::DBus::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 ▶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 to the server.
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 sub send_return {
139 1 ▶ my ($self, $orig_msg, @opts_kv) = @_;
Subroutine send_return: called CC=1 SCAR=0.0
Pod undocumented
140  
141 1 return $self->_send_msg(
142 @opts_kv,
143 reply_serial => $orig_msg->get_serial(),
144 type => 'METHOD_RETURN',
145 );
146 }
147  
148 sub send_error {
149 0 ▶ my ($self, $orig_msg, @opts_kv) = @_;
Subroutine send_error: not called CC=1 SCAR=6.9
Pod undocumented
150  
151 0 return $self->_send_msg(
152 @opts_kv,
153 reply_serial => $orig_msg->get_serial(),
154 type => 'ERROR',
155 );
156 }
157  
158 sub send_signal {
159 1 ▶ my ($self, @opts_kv) = @_;
Subroutine send_signal: called CC=1 SCAR=0.0
Pod undocumented
160  
161 1 return $self->_send_msg(
162 @opts_kv,
163 type => 'SIGNAL',
164 );
165 }
166  
167 #----------------------------------------------------------------------
168  
169 =head2 I<OBJ>->big_endian()
170  
171 Same interface as C<blocking()>, but this sets/gets/toggles whether to send
172 big-endian messages instead of little-endian.
173  
174 (I’m not sure why it would matter?)
175  
176 =cut
177  
178 sub big_endian {
179 0 ▶ my ($self) = @_;
Subroutine big_endian: not called CC=3 SCAR=24.8
Pod documented
180  
181 0 ▶ if (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 0 0 0
182 0 my $old = $self->{'_big_endian'};
183 0 $self->{'_big_endian'} = !!$_[1];
184  
185 0 ▶ $self->{'_to_str_fn'} = 'to_string_' . ($_[1] ? 'be' : 'le');
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
186  
187 0 return $self->{'_big_endian'};
188 }
189  
190 0 return !!$self->{'_big_endian'};
191 }
192  
193 #----------------------------------------------------------------------
194  
195 =head2 I<OBJ>->preserve_variant_signatures()
196  
197 Same interface as C<blocking()>, but when this is enabled
198 variants are given as two-member array references ([ signature => value ]),
199 blessed as C<Protocol::DBus::Type::Variant> instances.
200  
201 For most Perl applications this is probably counterproductive.
202  
203 =cut
204  
205 sub preserve_variant_signatures {
206 0 ▶ my $self = shift;
Subroutine preserve_variant_signatures: not called CC=1 SCAR=6.9
Pod documented
207  
208 0 return $self->{'_parser'}->preserve_variant_signatures(@_);
209 }
210  
211 #----------------------------------------------------------------------
212  
213 =head2 I<OBJ>->blocking()
214  
215 Same interface as L<IO::Handle>’s method of the same name.
216  
217 =cut
218  
219 sub blocking {
220 0 ▶ my $self = shift;
Subroutine blocking: not called CC=1 SCAR=6.9
Pod documented
221  
222 0 return $self->{'_socket'}->blocking(@_);
223 }
224  
225 #----------------------------------------------------------------------
226  
227 =head2 I<OBJ>->fileno()
228  
229 Returns the connection socket’s file descriptor.
230  
231 =cut
232  
233 sub fileno {
234 0 ▶ return fileno $_[0]->{'_socket'};
Subroutine fileno: not called CC=1 SCAR=6.9
Pod documented
235 }
236  
237 #----------------------------------------------------------------------
238  
239 =head2 I<OBJ>->pending_send()
240  
241 Returns a boolean that indicates whether there is data queued up to send
242 to the server.
243  
244 =cut
245  
246 sub pending_send {
247 0 ▶ return !!$_[0]->{'_io'}->get_write_queue_count();
Subroutine pending_send: not called CC=1 SCAR=6.9
Pod documented
248 }
249  
250 #----------------------------------------------------------------------
251  
252 # undocumented
253 sub new {
254 2 ▶ my ($class, $socket) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
255  
256 2 my $self = bless { _socket => $socket }, $class;
257  
258 2 $self->_set_up_peer_io( $socket );
259  
260 2 return $self;
261 }
262  
263 #----------------------------------------------------------------------
264  
265 sub _set_up_peer_io {
266 2 ▶ my ($self, $socket) = @_;
Subroutine _set_up_peer_io: called CC=1 SCAR=0.0
267  
268 2 $self->{'_io'} = Protocol::DBus::WriteMsg->new( $socket )->enable_write_queue();
269 2 $self->{'_parser'} = Protocol::DBus::Parser->new( $socket );
270  
271 2 return;
272 }
273  
274 sub _send_msg {
275 3 ▶ my ($self, %opts) = @_;
Subroutine _send_msg: called CC=6 SCAR=18.3
276  
277 3 my ($type, $body_ar, $flags) = delete @opts{'type', 'body', 'flags'};
278  
279 my @hargs = map {
280 3 my $k = $_;
281 14 $k =~ tr<a-z><A-Z>;
282 14 ( $k => $opts{$_} );
283 } keys %opts;
284  
285 3 my $serial = ++$self->{'_last_sent_serial'};
286  
287 3 my $msg = Protocol::DBus::Message->new(
288 type => $type,
289 hfields => \@hargs,
290 flags => $flags,
291 body => $body_ar,
292 serial => $serial,
293 );
294  
295 3 ▶ $self->{'_endian'} ||= '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
296 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
297  
298 3 my ($buf_sr, $fds_ar) = $msg->can($self->{'_to_str_fn'})->($msg);
299  
300 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
301 0 die "Cannot send file descriptors without UNIX FD support!";
302 }
303  
304 3 $self->{'_io'}->enqueue_message( $buf_sr, $fds_ar );
305  
306 3 return $self->{'_io'}->flush_write_queue();
307 }
308  
309 4 1;
« DBus/Client.pm ↑ Summary DBus/Authn/Mechanism/DBUS_COOKIE_SHA1/Pieces.pm »