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.4 by root, Mon Jun 21 00:12:52 2010 UTC vs.
Revision 1.9 by root, Sun Aug 14 13:22:52 2022 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.
18hacking L<Net::DBus::Reactor> so that all dbus connections created after 29hacking L<Net::DBus::Reactor> so that all dbus connections created after
19loading this module will automatically be managed by this module. 30loading this module will automatically be managed by this module.
20 31
21Note that a) a lot inside Net::DBus is still blocking b) if you call a 32Note that a) a lot inside Net::DBus is still blocking b) if you call a
22method that blocks, you again block your process (basically anything 33method that blocks, you again block your process (basically anything
23but calls to the Net::DBus::Binding::Connection objects block, but 34but calls to the Net::DBus::Binding::Connection objects block, but see
24see Net::DBus::Annoation, specifically dbus_call_async) and c) this 35Net::DBus::Annoation, specifically dbus_call_async) c) the underlying
36libdbus is often blocking itself, even with infinite timeouts and d) this
25module only implements the minimum API required to make Net::DBus work - 37module only implements the minimum API required to make Net::DBus work -
26Net::DBus unfortunately has no nice hooking API. 38Net::DBus unfortunately has no nice hooking API.
27 39
28However, unlike L<Net::DBus::Reactor>, this module should be fully 40However, unlike L<Net::DBus::Reactor>, this module should be fully
29non-blocking as long as you only use non-blocking APIs (Net::DBus::Reactor 41non-blocking as long as you only use non-blocking APIs (Net::DBus::Reactor
30blocks on writes). It should also be faster, but Net::DBus is such a 42blocks on writes). It should also be faster, but Net::DBus is such a
31morass os unneeded method calls that speed won't matter much... 43morass of unneeded method calls that speed won't matter much...
32 44
33=head2 EXAMPLE 45=head2 EXAMPLE
34 46
35Here is a simple example. Both work with AnyEvent::DBus and do the same 47Here is a simple example. Both work with AnyEvent::DBus and do the same
36thing, but only the second is actually non-blocking. 48thing, but only the second is actually non-blocking.
78 90
79use AnyEvent (); 91use AnyEvent ();
80use Net::DBus (); 92use Net::DBus ();
81use Net::DBus::Binding::Watch (); 93use Net::DBus::Binding::Watch ();
82 94
83our $VERSION = '0.1'; 95our $VERSION = '0.31';
84 96
85# yup, Net::DBus checks by using exists on %INC... 97# yup, Net::DBus checks by using exists on %INC...
86$INC{'Net/DBus/Reactor.pm'} = undef; 98$INC{'Net/DBus/Reactor.pm'} = undef;
87 99
88# claim we are the main reactor mainloop 100# claim we are the main reactor mainloop
105 117
106 $f & Net::DBus::Binding::Watch::READABLE () 118 $f & Net::DBus::Binding::Watch::READABLE ()
107 and 119 and
108 $O{$id}[0] = $on && AE::io $fd, 0, sub { 120 $O{$id}[0] = $on && AE::io $fd, 0, sub {
109 $w->handle (Net::DBus::Binding::Watch::READABLE ()); 121 $w->handle (Net::DBus::Binding::Watch::READABLE ());
110 $con->dispatch; # wtf., we tell it data is ready, but have to call dispatch ourselves??? 122 $con->dispatch;
111 }; 123 };
112 124
113 $f & Net::DBus::Binding::Watch::WRITABLE () 125 $f & Net::DBus::Binding::Watch::WRITABLE ()
114 and 126 and
115 $O{$id}[1] = $on && AE::io $fd, 1, sub { 127 $O{$id}[1] = $on && AE::io $fd, 1, sub {
116 $w->handle (Net::DBus::Binding::Watch::WRITABLE ()); 128 $w->handle (Net::DBus::Binding::Watch::WRITABLE ());
117 # calling flush, as NEt::DBus::Reactor does, is blocking :/ 129 $con->dispatch;
118 }; 130 };
119} 131}
120 132
121sub io_on { 133sub io_on {
122 my ($con, $w) = @_; 134 my ($con, $w) = @_;
131 my ($con, $w) = @_; 143 my ($con, $w) = @_;
132 144
133 my $id = $w->get_data; 145 my $id = $w->get_data;
134 my $i = $w->get_interval * 0.001; 146 my $i = $w->get_interval * 0.001;
135 147
136 $O{$id} = $w->is_enabled && AE::timer $i, $i, sub { $w->handle }; 148 $O{$id} = $w->is_enabled && AE::timer $i, $i, sub {
149 $w->handle;
150 $con->dispatch;
151 };
137} 152}
138 153
139sub timeout_on { 154sub timeout_on {
140 my ($con, $w) = @_; 155 my ($con, $w) = @_;
141 my $id = ++$I; 156 my $id = ++$I;
150 $con->set_watch_callbacks (\&io_on, \&watch_off, \&io_toggle); 165 $con->set_watch_callbacks (\&io_on, \&watch_off, \&io_toggle);
151# if $con->can ("set_watch_callbacks"); 166# if $con->can ("set_watch_callbacks");
152 167
153 $con->set_timeout_callbacks (\&timeout_on, \&watch_off, \&timeout_toggle); 168 $con->set_timeout_callbacks (\&timeout_on, \&watch_off, \&timeout_toggle);
154# if $con->can ("set_timeout_callbacks"); 169# if $con->can ("set_timeout_callbacks");
170
171 $con->dispatch; # for good measure
155} 172}
156 173
157=head1 SEE ALSO 174=head1 SEE ALSO
158 175
159L<AnyEvent>, L<Net::DBus>. 176L<AnyEvent>, L<Net::DBus>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines