DBus/Peer.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 114 SCAR 42.8
CC
8
Cov
0%
get_message
20.0
big_endian
12.0
flush_write_queue
6.0
CRAP 72.0
1 package Protocol::DBus::Peer;
2  
3 use strict;
4 use warnings;
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 use Call::Context;
45  
46 use Protocol::DBus::Message;
47 use Protocol::DBus::Parser;
48 use Protocol::DBus::WriteMsg;
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 my $msg = $_[0]->{'_parser'}->get_message();
69  
70 if ($msg) {
71 if (my $serial = $msg->get_header('REPLY_SERIAL')) {
72 if (my $cb = delete $_[0]->{'_on_return'}{$serial}) {
73 $cb->($msg);
74 }
75 }
76 }
77  
78 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 if ($_[0]->{'_io'}->get_write_queue_count()) {
91 return $_[0]->{'_io'}->flush_write_queue();
92 }
93  
94 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 my ($self, %opts) = @_;
122  
123 my $cb = delete $opts{'on_return'};
124  
125 my $ret = $self->_send_msg(
126 %opts,
127 type => 'METHOD_CALL',
128 );
129  
130 if ($cb) {
131 my $serial = $self->{'_last_sent_serial'};
132 $self->{'_on_return'}{$serial} = $cb;
133 }
134  
135 return $ret;
136 }
137  
138 sub send_signal {
139 my ($self, %opts) = @_;
140  
141 return $self->_send_msg(
142 %opts,
143 type => 'SIGNAL',
144 );
145 }
146  
147 #----------------------------------------------------------------------
148  
149 =head2 I<OBJ>->big_endian()
150  
151 Same interface as C<blocking()>, but this sets/gets/toggles whether to send
152 big-endian messages instead of little-endian.
153  
154 (I’m not sure why it would matter?)
155  
156 =cut
157  
158 sub big_endian {
159 my ($self) = @_;
160  
161 if (@_ > 0) {
162 my $old = $self->{'_big_endian'};
163 $self->{'_big_endian'} = !!$_[1];
164  
165 $self->{'_to_str_fn'} = 'to_string_' . ($_[1] ? 'be' : 'le');
166  
167 return $self->{'_big_endian'};
168 }
169  
170 return !!$self->{'_big_endian'};
171 }
172  
173 #----------------------------------------------------------------------
174  
175 =head2 I<OBJ>->preserve_variant_signatures()
176  
177 Same interface as C<blocking()>, but when this is enabled
178 variants are given as two-member array references ([ signature => value ]),
179 blessed as C<Protocol::DBus::Type::Variant> instances.
180  
181 For most Perl applications this is probably counterproductive.
182  
183 =cut
184  
185 sub preserve_variant_signatures {
186 my $self = shift;
187  
188 return $self->{'_parser'}->preserve_variant_signatures(@_);
189 }
190  
191 #----------------------------------------------------------------------
192  
193 =head2 I<OBJ>->blocking()
194  
195 Same interface as L<IO::Handle>’s method of the same name.
196  
197 =cut
198  
199 sub blocking {
200 my $self = shift;
201  
202 return $self->{'_socket'}->blocking(@_);
203 }
204  
205 #----------------------------------------------------------------------
206  
207 =head2 I<OBJ>->fileno()
208  
209 Returns the connection socket’s file descriptor.
210  
211 =cut
212  
213 sub fileno {
214 return fileno $_[0]->{'_socket'};
215 }
216  
217 #----------------------------------------------------------------------
218  
219 =head2 I<OBJ>->pending_send()
220  
221 Returns a boolean that indicates whether there is data queued up to send
222 to the server.
223  
224 =cut
225  
226 sub pending_send {
227 return !!$_[0]->{'_io'}->get_write_queue_count();
228 }
229  
230 #----------------------------------------------------------------------
231  
232 sub _set_up_peer_io {
233 my ($self, $socket) = @_;
234  
235 $self->{'_io'} = Protocol::DBus::WriteMsg->new( $socket )->enable_write_queue();
236 $self->{'_parser'} = Protocol::DBus::Parser->new( $socket );
237  
238 return;
239 }
240  
241 sub _send_msg {
242 my ($self, %opts) = @_;
243  
244 my ($type, $body_ar, $flags) = delete @opts{'type', 'body', 'flags'};
245  
246 my @hargs = map {
247 my $k = $_;
248 $k =~ tr<a-z><A-Z>;
249 ( $k => $opts{$_} );
250 } keys %opts;
251  
252 my $serial = ++$self->{'_last_sent_serial'};
253  
254 my $msg = Protocol::DBus::Message->new(
255 type => $type,
256 hfields => \@hargs,
257 flags => $flags,
258 body => $body_ar,
259 serial => $serial,
260 );
261  
262 $self->{'_endian'} ||= 'le';
263 $self->{'_to_str_fn'} ||= "to_string_$self->{'_endian'}";
264  
265 my ($buf_sr, $fds_ar) = $msg->can($self->{'_to_str_fn'})->($msg);
266  
267 $self->{'_io'}->enqueue_message( $buf_sr, $fds_ar );
268  
269 return $self->{'_io'}->flush_write_queue();
270 }
271  
272 1;