ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-DBus/DBus.pm
(Generate patch)

Comparing AnyEvent-DBus/DBus.pm (file contents):
Revision 1.1 by root, Sun Jun 20 23:52:13 2010 UTC vs.
Revision 1.6 by root, Mon Jun 21 19:43:42 2010 UTC

1=head1 NAME 1=head1 NAME
2 2
3AnyEvent::AIO - truly asynchronous file and directory I/O 3AnyEvent::DBus - adapt Net::DBus to AnyEvent
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use AnyEvent::DBus; 7 use AnyEvent::DBus;
8 8
9 # now use the Net::DBus API, preferably the non-blocking variants 9 # now use the Net::DBus API, preferably the non-blocking variants:
10
11 use Net::DBus::Annotation qw(:call);
12
13 $bus->get_object (...)
14 ->Method (dbus_call_async, $arg1, ...)
15 ->set_notify (sub {
16 my @data = $_[0]->get_result
17 ...
18 });
19
20 $bus->get_connection->send (...);
10 21
11=head1 DESCRIPTION 22=head1 DESCRIPTION
12 23
13This module is an L<AnyEvent> user, you need to make sure that you use and 24This module is an L<AnyEvent> user, you need to make sure that you use and
14run a supported event loop. 25run a supported event loop.
25module only implements the minimum API required to make Net::DBus work - 36module only implements the minimum API required to make Net::DBus work -
26Net::DBus unfortunately has no nice hooking API. 37Net::DBus unfortunately has no nice hooking API.
27 38
28However, unlike L<Net::DBus::Reactor>, this module should be fully 39However, unlike L<Net::DBus::Reactor>, this module should be fully
29non-blocking as long as you only use non-blocking APIs (Net::DBus::Reactor 40non-blocking as long as you only use non-blocking APIs (Net::DBus::Reactor
30blocks on writes). 41blocks on writes). It should also be faster, but Net::DBus is such a
42morass os unneeded method calls that speed won't matter much...
43
44=head2 EXAMPLE
45
46Here is a simple example. Both work with AnyEvent::DBus and do the same
47thing, but only the second is actually non-blocking.
48
49Example 1: list registered named, blocking version.
50
51 use AnyEvent::DBus;
52
53 my $conn = Net::DBus->find;
54 my $bus = $conn->get_bus_object;
55
56 for my $name (@{ $bus->ListNames }) {
57 print " $name\n";
58 }
59
60Example 1: list registered named, somewhat non-blocking version.
61
62 use AnyEvent;
63 use AnyEvent::DBus;
64 use Net::DBus::Annotation qw(:call);
65
66 my $conn = Net::DBus->find; # always blocks :/
67 my $bus = $conn->get_bus_object;
68
69 my $quit = AE::cv;
70
71 # the trick here is to prepend dbus_call_async to any method
72 # arguments and then to call the set_notify method on the
73 # returned Net::DBus::AsyncReply object
74
75 $bus->ListNames (dbus_call_async)->set_notify (sub {
76 for my $name (@{ $_[0]->get_result }) {
77 print " $name\n";
78 }
79 $quit->send;
80 });
81
82 $quit->recv;
31 83
32=cut 84=cut
33 85
34package AnyEvent::DBus; 86package AnyEvent::DBus;
35 87
37 89
38use AnyEvent (); 90use AnyEvent ();
39use Net::DBus (); 91use Net::DBus ();
40use Net::DBus::Binding::Watch (); 92use Net::DBus::Binding::Watch ();
41 93
42our $VERSION = '0.1'; 94our $VERSION = '0.3';
43 95
44# yup, Net::DBus checks by using exists on %INC... 96# yup, Net::DBus checks by using exists on %INC...
45$INC{'Net/DBus/Reactor.pm'} = undef; 97$INC{'Net/DBus/Reactor.pm'} = undef;
46 98
47# claim we are the main reactor mainloop 99# claim we are the main reactor mainloop
60 my $id = $w->get_data; 112 my $id = $w->get_data;
61 my $f = $w->get_flags; 113 my $f = $w->get_flags;
62 my $fd = $w->get_fileno; 114 my $fd = $w->get_fileno;
63 my $on = $w->is_enabled; 115 my $on = $w->is_enabled;
64 116
65 warn "io $id $on?$f\n";
66
67 $f & Net::DBus::Binding::Watch::READABLE () 117 $f & Net::DBus::Binding::Watch::READABLE ()
68 and 118 and
69 $O{$id}[0] = $on && AE::io $fd, 0, sub { 119 $O{$id}[0] = $on && AE::io $fd, 0, sub {
70 $w->handle (Net::DBus::Binding::Watch::READABLE ()); 120 $w->handle (Net::DBus::Binding::Watch::READABLE ());
71 $con->dispatch; # wtf., we tell it data is ready, but have to call dispatch ourselves??? 121 $con->dispatch;
72 }; 122 };
73 123
74 $f & Net::DBus::Binding::Watch::WRITABLE () 124 $f & Net::DBus::Binding::Watch::WRITABLE ()
75 and 125 and
76 $O{$id}[1] = $on && AE::io $fd, 1, sub { 126 $O{$id}[1] = $on && AE::io $fd, 1, sub {
77 $w->handle (Net::DBus::Binding::Watch::WRITABLE ()); 127 $w->handle (Net::DBus::Binding::Watch::WRITABLE ());
78 # calling flush, as NEt::DBus::Reactor does, is blocking :/ 128 $con->dispatch;
79 }; 129 };
80} 130}
81 131
82sub io_on { 132sub io_on {
83 my ($con, $w) = @_; 133 my ($con, $w) = @_;
92 my ($con, $w) = @_; 142 my ($con, $w) = @_;
93 143
94 my $id = $w->get_data; 144 my $id = $w->get_data;
95 my $i = $w->get_interval * 0.001; 145 my $i = $w->get_interval * 0.001;
96 146
97 $O{$id} = $w->is_enabled && AE::timer $i, $i, sub { $w->handle }; 147 $O{$id} = $w->is_enabled && AE::timer $i, $i, sub {
148 $w->handle;
149 $con->dispatch;
150 };
98} 151}
99 152
100sub timeout_on { 153sub timeout_on {
101 my ($con, $w) = @_; 154 my ($con, $w) = @_;
102 my $id = ++$I; 155 my $id = ++$I;
107 160
108sub manage { 161sub manage {
109 my (undef, $con) = @_; 162 my (undef, $con) = @_;
110 163
111 $con->set_watch_callbacks (\&io_on, \&watch_off, \&io_toggle); 164 $con->set_watch_callbacks (\&io_on, \&watch_off, \&io_toggle);
165# if $con->can ("set_watch_callbacks");
166
112 $con->set_timeout_callbacks (\&timeout_on, \&watch_off, \&timeout_toggle); 167 $con->set_timeout_callbacks (\&timeout_on, \&watch_off, \&timeout_toggle);
168# if $con->can ("set_timeout_callbacks");
113} 169}
114
115use Net::DBus::Annotation qw(:call);
116my $bus = Net::DBus->find;
117my $ob = $bus->get_bus_object;
118#my $res = $ob->ListNames (dbus_call_async, "x" x 8192000);
119my $res = $ob->ListNames (dbus_call_async);
120#Net::DBus::Binding::PendingCall
121$res->set_notify (sub {
122 warn "<@_>\n";#d#
123 for my $name (sort @{$_[0]->get_result}) {
124 print " ", $name, "\n";
125 }
126});
127AE::cv->recv;
128warn $ob;
129 170
130=head1 SEE ALSO 171=head1 SEE ALSO
131 172
132L<AnyEvent>, L<Net::DBus>. 173L<AnyEvent>, L<Net::DBus>.
133 174

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines