DBus.pm untested

filter: Statementstmt 0.0% 0 / 7 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 2 Podpod n/a totaltotal 0.0% 0 / 9 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Protocol::DBus;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.22';
7  
8 =encoding utf8
9  
10 =head1 NAME
11  
12 Protocol::DBus - D-Bus in pure Perl
13  
14 =head1 SYNOPSIS
15  
16 (NB: Examples below assume use of
17 L<subroutine signatures|perlsub/Signatures>.)
18  
19 For blocking I/O:
20  
21 my $dbus = Protcol::DBus::Client::system();
22  
23 # Authentication and “Hello” call/response:
24 $dbus->initialize();
25  
26 $dbus->send_call(
27 path => '/org/freedesktop/DBus',
28 interface => 'org.freedesktop.DBus.Properties',
29 member => 'GetAll',
30 destination => 'org.freedesktop.DBus',
31 signature => 's',
32 body => [ 'org.freedesktop.DBus' ],
33 )->then( sub ($resp_msg) { .. } );
34  
35 my $msg = $dbus->get_message();
36  
37 For non-blocking I/O, it is recommended to use an event loop.
38 This distribution includes some connectors to simplify that work:
39  
40 =over
41  
42 =item * L<Protocol::DBus::Client::IOAsync> (for L<IO::Async>)
43  
44 =item * L<Protocol::DBus::Client::Mojo> (for L<Mojolicious>)
45  
46 =item * L<Protocol::DBus::Client::AnyEvent> (for L<AnyEvent>)
47  
48 =back
49  
50 Example:
51  
52 my $loop = IO::Async::Loop->new();
53  
54 my $dbus = Protcol::DBus::Client::IOAsync::login_session($loop);
55  
56 $dbus->initialize()->then(
57 sub ($dbus) {
58 return $dbus->send_call( … ); # same arguments as above
59 },
60 )->finally( sub { $loop->stop() } );
61  
62 $loop->run();
63  
64 You can also interface with a manually-written event loop.
65 See L<the example|/EXAMPLE USING MANUALLY-WRITTEN EVENT LOOP> below.
66  
67 =head1 DESCRIPTION
68  
69 =begin html
70  
71 <a href='https://coveralls.io/github/FGasper/p5-Protocol-DBus?branch=master'><img src='https://coveralls.io/repos/github/FGasper/p5-Protocol-DBus/badge.svg?branch=master' alt='Coverage Status' /></a>
72  
73 =end html
74  
75 This is an original, pure-Perl implementation of client messaging logic for
76 L<the D-Bus protocol|https://dbus.freedesktop.org/doc/dbus-specification.html>.
77  
78 It’s not much more than an implementation of the wire protocol; it doesn’t
79 know about objects, services, or anything else besides the actual messages.
80 This is fine, of course, if all you want to do is, e.g., replace
81 an invocation of C<gdbus> or C<dbus-send> with pure Perl.
82  
83 If you want an interface that mimics D-Bus’s actual object system,
84 you’ll need to implement it yourself or use something like L<Net::DBus>.
85 (See L</DIFFERENCES FROM Net::DBus> below.)
86  
87 =head1 STATUS
88  
89 This project is in BETA status. While the API should be pretty stable now,
90 breaking changes can still happen. If you use this module
91 in your project, you B<MUST> check the changelog before deploying a new
92 version. Please file bug reports as appropriate.
93  
94 =head1 EXAMPLES
95  
96 See L<Protocol::DBus::Client> and the above samples for a starting point.
97  
98 Also see the distribution’s F<examples/> directory.
99  
100 =head1 DIFFERENCES FROM Net::DBus
101  
102 L<Net::DBus> is an XS binding to
103 L<libdbus|https://www.freedesktop.org/wiki/Software/dbus/>,
104 the reference D-Bus implementation. It is CPAN’s most mature D-Bus
105 implementation.
106  
107 There are several reasons why you might prefer this module instead,
108 though, such as:
109  
110 =over
111  
112 =item * Net::DBus discerns how to send a method call via D-Bus introspection.
113 While handy, this costs extra network overhead and requires an XML parser.
114 With Protocol::DBus you give a signature directly to send a method call.
115  
116 =item * Protocol::DBus can work smoothly with any event system you like,
117 including custom-written ones. (The distribution ships with connectors for
118 three popular ones.) Net::DBus, on the other hand, expects you to use its
119 own event loop, L<Net::DBus::Reactor>.
120  
121 =item * Protocol::DBus has a considerably lighter memory footprint.
122  
123 =item * Protocol::DBus is pure Perl, so on most OSes you can fat-pack it
124 for easy distribution.
125  
126 =item * Protocol::DBus exposes a simpler API.
127  
128 =back
129  
130 Of course, there are tradeoffs: most notably, Protocol::DBus’s API is
131 simpler because it doesn’t attempt to implement D-Bus’s object system.
132 (You never B<need> the object system, but it can be a useful abstraction.)
133 An XS-powered D-Bus library is also likely to outperform a
134 pure-Perl one, introspection overhead notwithstanding. YMMV. BYOB.
135  
136 =head1 NOTES
137  
138 =over
139  
140 =item * UNIX FD support requires that L<Socket::MsgHdr> be loaded at
141 authentication time.
142  
143 =item * Certain OSes may require L<Socket::MsgHdr> in order to authenticate
144 via a UNIX socket. (Linux, notably, does not.) It depends if your OS can
145 send local socket credentials without using L<sendmsg(2)>.
146  
147 =item * EXTERNAL and DBUS_COOKIE_SHA1 authentications are supported.
148  
149 =back
150  
151 =head1 EXAMPLE USING MANUALLY-WRITTEN EVENT LOOP
152  
153 my $dbus = Protcol::DBus::Client::system();
154  
155 $dbus->blocking(0);
156  
157 my $fileno = $dbus->fileno();
158  
159 # You can use whatever polling method you prefer;
160 # the following is just for demonstration:
161 vec( my $mask, $fileno, 1 ) = 1;
162  
163 while (!$dbus->initialize()) {
164 if ($dbus->init_pending_send()) {
165 select( undef, my $wout = $mask, undef, undef );
166 }
167 else {
168 select( my $rout = $mask, undef, undef, undef );
169 }
170 }
171  
172 $dbus->send_call( .. ); # same parameters as above
173  
174 while (1) {
175 my $wout = $dbus->pending_send() || q<>;
176 $wout &&= $mask;
177  
178 select( my $rout = $mask, $wout, undef, undef );
179  
180 if ($wout =~ tr<\0><>c) {
181 $dbus->flush_write_queue();
182 }
183  
184 if ($rout =~ tr<\0><>c) {
185  
186 # It’s critical to get_message() until undef is returned.
187 1 while $dbus->get_message();
188 }
189 }
190  
191 Life is easier if you use someone else’s event loop. :)
192  
193 =cut