… | |
… | |
29 | hacking L<Net::DBus::Reactor> so that all dbus connections created after |
29 | hacking L<Net::DBus::Reactor> so that all dbus connections created after |
30 | loading this module will automatically be managed by this module. |
30 | loading this module will automatically be managed by this module. |
31 | |
31 | |
32 | Note that a) a lot inside Net::DBus is still blocking b) if you call a |
32 | Note that a) a lot inside Net::DBus is still blocking b) if you call a |
33 | method that blocks, you again block your process (basically anything |
33 | method that blocks, you again block your process (basically anything |
34 | but calls to the Net::DBus::Binding::Connection objects block, but |
34 | but calls to the Net::DBus::Binding::Connection objects block, but see |
35 | see Net::DBus::Annoation, specifically dbus_call_async) and c) this |
35 | Net::DBus::Annoation, specifically dbus_call_async) c) the underlying |
|
|
36 | libdbus is often blocking itself, even with infinite timeouts and d) this |
36 | module only implements the minimum API required to make Net::DBus work - |
37 | module only implements the minimum API required to make Net::DBus work - |
37 | Net::DBus unfortunately has no nice hooking API. |
38 | Net::DBus unfortunately has no nice hooking API. |
38 | |
39 | |
39 | However, unlike L<Net::DBus::Reactor>, this module should be fully |
40 | However, unlike L<Net::DBus::Reactor>, this module should be fully |
40 | non-blocking as long as you only use non-blocking APIs (Net::DBus::Reactor |
41 | non-blocking as long as you only use non-blocking APIs (Net::DBus::Reactor |
41 | blocks on writes). It should also be faster, but Net::DBus is such a |
42 | blocks on writes). It should also be faster, but Net::DBus is such a |
42 | morass os unneeded method calls that speed won't matter much... |
43 | morass so unneeded method calls that speed won't matter much... |
43 | |
44 | |
44 | =head2 EXAMPLE |
45 | =head2 EXAMPLE |
45 | |
46 | |
46 | Here is a simple example. Both work with AnyEvent::DBus and do the same |
47 | Here is a simple example. Both work with AnyEvent::DBus and do the same |
47 | thing, but only the second is actually non-blocking. |
48 | thing, but only the second is actually non-blocking. |
… | |
… | |
89 | |
90 | |
90 | use AnyEvent (); |
91 | use AnyEvent (); |
91 | use Net::DBus (); |
92 | use Net::DBus (); |
92 | use Net::DBus::Binding::Watch (); |
93 | use Net::DBus::Binding::Watch (); |
93 | |
94 | |
94 | our $VERSION = '0.3'; |
95 | our $VERSION = '0.31'; |
95 | |
96 | |
96 | # yup, Net::DBus checks by using exists on %INC... |
97 | # yup, Net::DBus checks by using exists on %INC... |
97 | $INC{'Net/DBus/Reactor.pm'} = undef; |
98 | $INC{'Net/DBus/Reactor.pm'} = undef; |
98 | |
99 | |
99 | # claim we are the main reactor mainloop |
100 | # claim we are the main reactor mainloop |
… | |
… | |
116 | |
117 | |
117 | $f & Net::DBus::Binding::Watch::READABLE () |
118 | $f & Net::DBus::Binding::Watch::READABLE () |
118 | and |
119 | and |
119 | $O{$id}[0] = $on && AE::io $fd, 0, sub { |
120 | $O{$id}[0] = $on && AE::io $fd, 0, sub { |
120 | $w->handle (Net::DBus::Binding::Watch::READABLE ()); |
121 | $w->handle (Net::DBus::Binding::Watch::READABLE ()); |
121 | $con->dispatch; # wtf., we tell it data is ready, but have to call dispatch ourselves??? |
122 | $con->dispatch; |
122 | }; |
123 | }; |
123 | |
124 | |
124 | $f & Net::DBus::Binding::Watch::WRITABLE () |
125 | $f & Net::DBus::Binding::Watch::WRITABLE () |
125 | and |
126 | and |
126 | $O{$id}[1] = $on && AE::io $fd, 1, sub { |
127 | $O{$id}[1] = $on && AE::io $fd, 1, sub { |
127 | $w->handle (Net::DBus::Binding::Watch::WRITABLE ()); |
128 | $w->handle (Net::DBus::Binding::Watch::WRITABLE ()); |
128 | # calling flush, as NEt::DBus::Reactor does, is blocking :/ |
129 | $con->dispatch; |
129 | }; |
130 | }; |
130 | } |
131 | } |
131 | |
132 | |
132 | sub io_on { |
133 | sub io_on { |
133 | my ($con, $w) = @_; |
134 | my ($con, $w) = @_; |
… | |
… | |
142 | my ($con, $w) = @_; |
143 | my ($con, $w) = @_; |
143 | |
144 | |
144 | my $id = $w->get_data; |
145 | my $id = $w->get_data; |
145 | my $i = $w->get_interval * 0.001; |
146 | my $i = $w->get_interval * 0.001; |
146 | |
147 | |
147 | $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 | }; |
148 | } |
152 | } |
149 | |
153 | |
150 | sub timeout_on { |
154 | sub timeout_on { |
151 | my ($con, $w) = @_; |
155 | my ($con, $w) = @_; |
152 | my $id = ++$I; |
156 | my $id = ++$I; |
… | |
… | |
161 | $con->set_watch_callbacks (\&io_on, \&watch_off, \&io_toggle); |
165 | $con->set_watch_callbacks (\&io_on, \&watch_off, \&io_toggle); |
162 | # if $con->can ("set_watch_callbacks"); |
166 | # if $con->can ("set_watch_callbacks"); |
163 | |
167 | |
164 | $con->set_timeout_callbacks (\&timeout_on, \&watch_off, \&timeout_toggle); |
168 | $con->set_timeout_callbacks (\&timeout_on, \&watch_off, \&timeout_toggle); |
165 | # if $con->can ("set_timeout_callbacks"); |
169 | # if $con->can ("set_timeout_callbacks"); |
|
|
170 | |
|
|
171 | $con->dispatch; # for good measure |
166 | } |
172 | } |
167 | |
173 | |
168 | =head1 SEE ALSO |
174 | =head1 SEE ALSO |
169 | |
175 | |
170 | L<AnyEvent>, L<Net::DBus>. |
176 | L<AnyEvent>, L<Net::DBus>. |