DBus/Client.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 84 SCAR 24.8
CC
3
Cov
0%
do_authn
6.0
get_message
6.0
system
2.0
CRAP 12.0
1 package Protocol::DBus::Client;
2  
3 use strict;
4 use warnings;
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->do_authn();
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 use parent 'Protocol::DBus::Peer';
33  
34 use Protocol::DBus::Authn;
35 use Protocol::DBus::Connect;
36 use Protocol::DBus::Path;
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 sub system {
51 my $addr = Protocol::DBus::Path::system_message_bus();
52  
53 return _create_local($addr);
54 }
55  
56 =head2 login_session()
57  
58 Like C<system()> but for the login session’s message bus.
59  
60 =cut
61  
62 sub login_session {
63 my $addr = Protocol::DBus::Path::login_session_message_bus() or do {
64 die "Failed to identify login system message bus!";
65 };
66  
67 return _create_local($addr);
68 }
69  
70 sub _create_local {
71 my ($addr) = @_;
72 my $socket = Protocol::DBus::Connect::create_socket($addr);
73  
74 return __PACKAGE__->new(
75 socket => $socket,
76 authn_mechanism => 'EXTERNAL',
77 );
78 }
79  
80 #----------------------------------------------------------------------
81  
82 =head1 METHODS
83  
84 =head2 $done_yn = I<OBJ>->do_authn()
85  
86 This returns truthy once the authn is complete and falsy until then.
87 In blocking I/O contexts the call will block until authn is complete.
88  
89 =cut
90  
91 sub do_authn {
92 my ($self) = @_;
93  
94 if ($self->{'_authn'}->go()) {
95 $self->{'_sent_hello'} ||= do {
96 $self->send_call(
97 path => '/org/freedesktop/DBus',
98 interface => 'org.freedesktop.DBus',
99 destination => 'org.freedesktop.DBus',
100 member => 'Hello',
101 on_return => sub {
102 $self->{'_connection_name'} = $_[0]->get_body()->[0];
103 },
104 );
105 };
106  
107 return 1;
108 }
109  
110 return 0;
111 }
112  
113 #----------------------------------------------------------------------
114  
115 =head2 $yn = I<OBJ>->authn_pending_send()
116  
117 This indicates whether there is data queued up to send for the authn.
118 Only useful with non-blocking I/O.
119  
120 =cut
121  
122 sub authn_pending_send {
123 my ($self) = @_;
124  
125 return $self->{'_authn'}->pending_send();
126 }
127  
128 #----------------------------------------------------------------------
129  
130 =head2 $msg = I<OBJ>->get_message()
131  
132 Same as in the base class, but for clients the initial “Hello” message and
133 its response are abstracted
134  
135 =cut
136  
137 sub get_message {
138 if ( my $msg = $_[0]->SUPER::get_message() ) {
139  
140 no warnings 'redefine';
141 *get_message = Protocol::DBus::Peer->can('get_message');
142  
143 return $_[0]->get_message();
144 }
145  
146 return undef;
147 }
148  
149 =head2 $name = I<OBJ>->get_connection_name()
150  
151 Returns the name of the connection. This must only be called after at least
152 one message is received; if it is called before then, an exception is thrown.
153  
154 =cut
155  
156 sub get_connection_name {
157 return $_[0]->{'_connection_name'} || die 'No connection name known yet!';
158 }
159  
160 # undocumented
161 sub new {
162 my ($class, %opts) = @_;
163  
164 my $authn = Protocol::DBus::Authn->new(
165 socket => $opts{'socket'},
166 mechanism => $opts{'authn_mechanism'},
167 );
168  
169 my $self = bless { _socket => $opts{'socket'}, _authn => $authn }, $class;
170  
171 $self->_set_up_peer_io( $opts{'socket'} );
172  
173 return $self;
174 }
175  
176 1;