DBus/Client.pm

filter: Statementstmt 71.5% 68 / 95 Branchbran 36.8% 14 / 38 Conditioncond 29.4% 5 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 89.4% 17 / 19 Podpod 87.5% 7 / 8 totaltotal 59.0% 111 / 188 CC 26 SCAR 25.0
CC
26
Cov
58%
init_pending_send
30.0
_connect
28.5
initialize
10.4
CRAP 12.2
« DBus/Client/EventBase.pm ↑ Summary DBus/Path.pm »
1 package Protocol::DBus::Client;
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::Client
11  
12 =head1 SYNOPSIS
13  
14 my $dbus = Protocol::DBus::Client::system();
15  
16 $dbus->initialize();
17  
18 =head1 DESCRIPTION
19  
20 This is the end class for use in DBus client applications. It subclasses
21 L<Protocol::DBus::Peer>.
22  
23 B<NOTE:> This module will automatically send a “Hello” message after
24 authentication completes. That message’s response will be processed
25 automatically. Because this is part of the protocol’s handshake
26 logic rather than something useful for callers, it is abstracted away from
27 the caller. It is neither necessary nor productive for callers to send a
28 “Hello” message.
29  
30 =cut
31  
32 6 ▶use parent 'Protocol::DBus::Peer';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 6 ▶use Protocol::DBus::Authn;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 6 ▶use Protocol::DBus::Connect;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 6 ▶use Protocol::DBus::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 =head1 STATIC FUNCTIONS
39  
40 =head2 system()
41  
42 Creates an instance of this class that includes a connection to the
43 system’s message bus.
44  
45 This does not do authentication; you’ll need to do that via the class’s
46 methods.
47  
48 =cut
49  
50 1 ▶sub system {
Subroutine system: called CC=1 SCAR=0.0
Pod documented
51 1 my @addrs = Protocol::DBus::Path::system_message_bus();
52  
53 1 return _create_local(@addrs);
54 }
55  
56 =head2 login_session()
57  
58 Like C<system()> but for the login session’s message bus.
59  
60 =cut
61  
62 1 ▶sub login_session {
Subroutine login_session: called CC=2 SCAR=14.6
Pod documented
63 1 my @addrs = Protocol::DBus::Path::login_session_message_bus();
64  
65 0 ▶ if (!@addrs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@addrs) 0 0 0
66 0 die "Failed to identify login system message bus!";
67 }
68  
69 0 return _create_local(@addrs);
70 }
71  
72 1 ▶sub _create_local {
Subroutine _create_local: called CC=1 SCAR=0.0
73 1 my ($addr) = @_;
74 1 my ($socket, $bin_addr) = Protocol::DBus::Connect::create_socket($addr);
75  
76 1 return __PACKAGE__->new(
77 socket => $socket,
78 address => $bin_addr,
79 human_address => $addr->to_string(),
80 authn_mechanism => 'EXTERNAL',
81 );
82 }
83  
84 #----------------------------------------------------------------------
85  
86 =head1 METHODS
87  
88 =head2 $done_yn = I<OBJ>->initialize()
89  
90 This returns truthy once the connection is ready to use and falsy until then.
91 In blocking I/O contexts the call will block.
92  
93 Note that this automatically handles D-Bus’s initial C<Hello> message and
94 its response.
95  
96 Previously this function was called C<do_authn()> and did not wait for
97 the C<Hello> message’s response. The older name is retained
98 as an alias for backward compatibility.
99  
100 =cut
101  
102 3 ▶sub initialize {
Subroutine initialize: called CC=8 SCAR=23.4
Pod documented
103 3 my ($self) = @_;
104  
105 3 ▶ if ($self->_connect() && $self->{'_authn'}->go()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_connect and $self->{"_authn"}->go) 2 0 2
Condition33%1/3
$self->_connect and $self->{"_authn"}->go
!ll&&!rl&&r
002
MC/DC0%0/2
$self->_connect and $self->{"_authn"}->go
$self->_connect $self->{"_authn"}->go
Truth table33%1/3
A and B
A= $self->_connectB= $self->{"_authn"}->go
ABresult
0X0
100
111
106 2 ▶ $self->{'_sent_hello'} ||= do {
Condition33%1/3
$self->{"_sent_hello"} ||= do { my $connection_name_sr = \$self->{"_connection_name"}; $self->send_call("path", "/org/freedesktop/DBus", "interface", "org.freedesktop.DBus", "destination", "org.freedesktop.DBus", "member", "Hello")->then(sub { $$connection_name_sr = $_[0]->get_body->[0]; } ) }
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"_sent_hello"} ||= do { my $connection_name_sr = \$self->{"_connection_name"}; $self->send_call("path", "/org/freedesktop/DBus", "interface", "org.freedesktop.DBus", "destination", "org.freedesktop.DBus", "member", "Hello")->then(sub { $$connection_name_sr = $_[0]->get_body->[0]; } ) }
$self->{"_sent_hello"} do { my $connection_name_sr = \$self->{"_connection_name"}; $self->send_call("path", "/org/freedesktop/DBus", "interface", "org.freedesktop.DBus", "destination", "org.freedesktop.DBus", "member", "Hello")->then(sub { $$connection_name_sr = $_[0]->get_body->[0]; } ) }
Truth table33%1/3
A ||= B
A= $self->{"_sent_hello"}B= do { my $connection_name_sr = \$self->{"_connection_name"}; $self->send_call("path", "/org/freedesktop/DBus", "interface", "org.freedesktop.DBus", "destination", "org.freedesktop.DBus", "member", "Hello")->then(sub { $$connection_name_sr = $_[0]->get_body->[0]; } ) }
ABresult
1X1
011
000
107 2 my $connection_name_sr = \$self->{'_connection_name'};
108  
109 $self->send_call(
110 path => '/org/freedesktop/DBus',
111 interface => 'org.freedesktop.DBus',
112 destination => 'org.freedesktop.DBus',
113 member => 'Hello',
114 2 ▶ )->then( sub { $$connection_name_sr = $_[0]->get_body()->[0]; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
115 };
116  
117 2 ▶ if (!$self->{'_connection_name'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_connection_name"}) 2 0 2
118 GET_MESSAGE: {
119 2 ▶ if (my $msg = $self->SUPER::get_message()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $self->SUPER::get_message) 4 0 4
120 4 ▶ return 1 if $self->{'_connection_name'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_connection_name"} 2 2 4
121  
122 2 push @{ $self->{'_pending_received_messages'} }, $msg;
123  
124 2 redo GET_MESSAGE;
125 }
126 }
127 }
128 }
129  
130 0 ▶ return $self->{'_connection_name'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_connection_name'} ? : 0 0 0
131 }
132  
133 3 ▶sub _connect {
Subroutine _connect: called CC=9 SCAR=33.5
134 3 my ($self) = @_;
135  
136 3 local $!;
137  
138 3 ▶ if (!$self->{'_connected'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_connected"}) 1 2 3
139 1 ▶ $self->{'_sent_connect'} ||= do {
Condition33%1/3
$self->{"_sent_connect"} ||= do { if (connect $self->{"_socket"}, $self->{"_address"}) { $self->{"_connected"} = 1; } elsif (not $!{"EINPROGRESS"}) { die qq[connect($self->{"_human_address"}): $!]; } }
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"_sent_connect"} ||= do { if (connect $self->{"_socket"}, $self->{"_address"}) { $self->{"_connected"} = 1; } elsif (not $!{"EINPROGRESS"}) { die qq[connect($self->{"_human_address"}): $!]; } }
$self->{"_sent_connect"} do { if (connect $self->{"_socket"}, $self->{"_address"}) { $self->{"_connected"} = 1; } elsif (not $!{"EINPROGRESS"}) { die qq[connect($self->{"_human_address"}): $!]; } }
Truth table33%1/3
A ||= B
A= $self->{"_sent_connect"}B= do { if (connect $self->{"_socket"}, $self->{"_address"}) { $self->{"_connected"} = 1; } elsif (not $!{"EINPROGRESS"}) { die qq[connect($self->{"_human_address"}): $!]; } }
ABresult
1X1
011
000
140 1 ▶ if ( connect $self->{'_socket'}, $self->{'_address'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (connect $self->{'_socket'}, $self->{'_address'}) { } 0 1 1
elsif (not $!{'EINPROGRESS'}) { } 1 0 1
141 0 $self->{'_connected'} = 1;
142 }
143 elsif (!$!{'EINPROGRESS'}) {
144 1 die "connect($self->{'_human_address'}): $!";
145 }
146 };
147 }
148  
149 2 ▶ if (!$self->{'_connected'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_connected"}) 0 2 2
150  
151 # This non-blocking connect logic will ordinarily be unneeded
152 # since even in non-blocking mode a UNIX socket connect() doesn’t
153 # normally block. Where such a connect() *will* have to wait is
154 # when the server has no more space for a new connection.
155  
156 0 my $mask = q<>;
157 0 vec( $mask, fileno $self->{'_socket'}, 1 ) = 1;
158  
159 0 my $got = select undef, $mask, undef, 0;
160  
161 0 ▶ if ($got > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got > 0) 0 0 0
162 0 my $errno = getsockopt( $self->{'_socket'}, Socket::SOL_SOCKET(), Socket::SO_ERROR() );
163 0 ▶ if (!defined $errno) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $errno) 0 0 0
164 0 die "getsockopt(SOL_SOCKET, SO_ERROR): $!";
165 }
166  
167 0 local $! = unpack 'I', $errno;
168  
169 0 ▶ if (0 + $!) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 + $!) { } 0 0 0
170 0 die "connect($self->{'_human_address'}): $!";
171 }
172 else {
173 0 $self->{'_connected'} = 1;
174 }
175 }
176 }
177  
178 2 return $self->{'_connected'};
179 }
180  
181 6 *do_authn = *initialize;
182  
183 #----------------------------------------------------------------------
184  
185 =head2 $yn = I<OBJ>->init_pending_send()
186  
187 This indicates whether there is data queued up to send for the initialization.
188 Only useful with non-blocking I/O.
189  
190 This function was previously called C<authn_pending_send()>; the former
191 name is retained for backward compatibility.
192  
193 =cut
194  
195 0 ▶sub init_pending_send {
Subroutine init_pending_send: not called CC=5 SCAR=34.0
Pod documented
196 0 my ($self) = @_;
197  
198 0 ▶ if ($self->{'_connection_name'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_connection_name"}) 0 0 0
199 0 die "Don’t call this after initialize() is done!";
200 }
201  
202 0 ▶ return 1 if $self->{'_sent_connect'} && !$self->{'_connected'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_sent_connect"} and not $self->{"_connected"} 0 0 0
Condition0%0/3
$self->{"_sent_connect"} and not $self->{"_connected"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_sent_connect"} and not $self->{"_connected"}
$self->{"_sent_connect"} not $self->{"_connected"}
Truth table0%0/3
A and not(B)
A= $self->{"_sent_connect"}B= not $self->{"_connected"}
ABresult
0X0
100
111
203  
204 0 ▶ if ($self->{'_sent_hello'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_sent_hello"}) 0 0 0
205 0 return $self->pending_send();
206 }
207  
208 0 return $self->{'_authn'}->pending_send();
209 }
210  
211 6 *authn_pending_send = \*init_pending_send;
212  
213 #----------------------------------------------------------------------
214  
215 =head2 $yn = I<OBJ>->supports_unix_fd()
216  
217 Boolean that indicates whether this client supports UNIX FD passing.
218  
219 (See the main L<Protocol::DBus> documentation for details about
220 support for UNIX FD passing.)
221  
222 =cut
223  
224 0 ▶sub supports_unix_fd {
Subroutine supports_unix_fd: not called CC=1 SCAR=6.9
Pod documented
225 0 my ($self) = @_;
226  
227 0 return $self->{'_authn'}->negotiated_unix_fd();
228 }
229  
230 #----------------------------------------------------------------------
231  
232 =head2 $msg = I<OBJ>->get_message()
233  
234 Same as in the base class, but for clients the initial “Hello” message and
235 its response are abstracted
236  
237 =cut
238  
239 2 ▶sub get_message {
Subroutine get_message: called CC=4 SCAR=15.2
Pod documented
240 2 my ($self) = @_;
241  
242 2 ▶ die "initialize() is not finished!" if !$self->{'_connection_name'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_connection_name"} 0 2 2
243  
244 2 ▶ if ($self->{'_pending_received_messages'} && @{ $self->{'_pending_received_messages'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_pending_received_messages"} and @{$self->{"_pending_received_messages"};}) 2 0 2
Condition33%1/3
$self->{"_pending_received_messages"} and @{$self->{"_pending_received_messages"};}
!ll&&!rl&&r
002
MC/DC0%0/2
$self->{"_pending_received_messages"} and @{$self->{"_pending_received_messages"};}
$self->{"_pending_received_messages"} @{$self->{"_pending_received_messages"};}
Truth table33%1/3
A and B
A= $self->{"_pending_received_messages"}B= @{$self->{"_pending_received_messages"};}
ABresult
0X0
100
111
245 2 return shift @{ $self->{'_pending_received_messages'} };
246 }
247  
248 6 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
249 0 *get_message = Protocol::DBus::Peer->can('get_message');
250  
251 0 return $_[0]->get_message();
252 }
253  
254 =head2 $name = I<OBJ>->get_unique_bus_name()
255  
256 Returns the connection’s unique bus name.
257  
258 C<get_connection_name()> is a historical alias for this method.
259  
260 =cut
261  
262 2 ▶sub get_unique_bus_name {
Subroutine get_unique_bus_name: called CC=2 SCAR=7.6
Pod documented
263 2 ▶ return $_[0]->{'_connection_name'} || die 'No connection name known yet!';
Condition50%1/2
$_[0]{'_connection_name'} || die('No connection name known yet!')
l!l
20
MC/DC0%0/1
$_[0]{'_connection_name'} || die('No connection name known yet!')
$_[0]{'_connection_name'}
Truth table50%1/2
A
A= $_[0]{'_connection_name'}
Aresult
11
00
264 }
265  
266 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
267 6 *get_connection_name = *get_unique_bus_name;
268 }
269  
270 # undocumented for now
271 3 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
272 3 my ($class, %opts) = @_;
273  
274 my $authn = Protocol::DBus::Authn->new(
275 socket => $opts{'socket'},
276 3 mechanism => $opts{'authn_mechanism'},
277 );
278  
279 3 my $self = $class->SUPER::new( $opts{'socket'} );
280  
281 3 $self->{'_authn'} = $authn;
282  
283 3 ▶ if (my $address = $opts{'address'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $address = $opts{'address'}) { } 1 2 3
284 1 $self->{'_address'} = $address;
285 1 $self->{'_human_address'} = $opts{'human_address'};
286 }
287 else {
288 2 $self->{'_connected'} = 1;
289 }
290  
291 3 return $self;
292 }
293  
294 #sub DESTROY {
295 # print "DESTROYED: [$_[0]]\n";
296 #}
297  
298 6 1;
« DBus/Client/EventBase.pm ↑ Summary DBus/Path.pm »