1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent::AIO - truly asynchronous file and directory I/O |
3 | AnyEvent::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 | |
13 | This module is an L<AnyEvent> user, you need to make sure that you use and |
24 | This module is an L<AnyEvent> user, you need to make sure that you use and |
14 | run a supported event loop. |
25 | run a supported event loop. |
… | |
… | |
78 | |
89 | |
79 | use AnyEvent (); |
90 | use AnyEvent (); |
80 | use Net::DBus (); |
91 | use Net::DBus (); |
81 | use Net::DBus::Binding::Watch (); |
92 | use Net::DBus::Binding::Watch (); |
82 | |
93 | |
83 | our $VERSION = '0.1'; |
94 | our $VERSION = '0.3'; |
84 | |
95 | |
85 | # yup, Net::DBus checks by using exists on %INC... |
96 | # yup, Net::DBus checks by using exists on %INC... |
86 | $INC{'Net/DBus/Reactor.pm'} = undef; |
97 | $INC{'Net/DBus/Reactor.pm'} = undef; |
87 | |
98 | |
88 | # claim we are the main reactor mainloop |
99 | # claim we are the main reactor mainloop |
… | |
… | |
105 | |
116 | |
106 | $f & Net::DBus::Binding::Watch::READABLE () |
117 | $f & Net::DBus::Binding::Watch::READABLE () |
107 | and |
118 | and |
108 | $O{$id}[0] = $on && AE::io $fd, 0, sub { |
119 | $O{$id}[0] = $on && AE::io $fd, 0, sub { |
109 | $w->handle (Net::DBus::Binding::Watch::READABLE ()); |
120 | $w->handle (Net::DBus::Binding::Watch::READABLE ()); |
110 | $con->dispatch; # wtf., we tell it data is ready, but have to call dispatch ourselves??? |
121 | $con->dispatch; |
111 | }; |
122 | }; |
112 | |
123 | |
113 | $f & Net::DBus::Binding::Watch::WRITABLE () |
124 | $f & Net::DBus::Binding::Watch::WRITABLE () |
114 | and |
125 | and |
115 | $O{$id}[1] = $on && AE::io $fd, 1, sub { |
126 | $O{$id}[1] = $on && AE::io $fd, 1, sub { |
116 | $w->handle (Net::DBus::Binding::Watch::WRITABLE ()); |
127 | $w->handle (Net::DBus::Binding::Watch::WRITABLE ()); |
117 | # calling flush, as NEt::DBus::Reactor does, is blocking :/ |
128 | $con->dispatch; |
118 | }; |
129 | }; |
119 | } |
130 | } |
120 | |
131 | |
121 | sub io_on { |
132 | sub io_on { |
122 | my ($con, $w) = @_; |
133 | my ($con, $w) = @_; |
… | |
… | |
131 | my ($con, $w) = @_; |
142 | my ($con, $w) = @_; |
132 | |
143 | |
133 | my $id = $w->get_data; |
144 | my $id = $w->get_data; |
134 | my $i = $w->get_interval * 0.001; |
145 | my $i = $w->get_interval * 0.001; |
135 | |
146 | |
136 | $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 | }; |
137 | } |
151 | } |
138 | |
152 | |
139 | sub timeout_on { |
153 | sub timeout_on { |
140 | my ($con, $w) = @_; |
154 | my ($con, $w) = @_; |
141 | my $id = ++$I; |
155 | my $id = ++$I; |
… | |
… | |
145 | } |
159 | } |
146 | |
160 | |
147 | sub manage { |
161 | sub manage { |
148 | my (undef, $con) = @_; |
162 | my (undef, $con) = @_; |
149 | |
163 | |
150 | $con->set_watch_callbacks (\&io_on, \&watch_off, \&io_toggle) |
164 | $con->set_watch_callbacks (\&io_on, \&watch_off, \&io_toggle); |
151 | if $con->can ("set_watch_callbacks"); |
165 | # if $con->can ("set_watch_callbacks"); |
152 | |
166 | |
153 | $con->set_timeout_callbacks (\&timeout_on, \&watch_off, \&timeout_toggle) |
167 | $con->set_timeout_callbacks (\&timeout_on, \&watch_off, \&timeout_toggle); |
154 | if $con->can ("set_timeout_callbacks"); |
168 | # if $con->can ("set_timeout_callbacks"); |
155 | } |
169 | } |
156 | |
170 | |
157 | =head1 SEE ALSO |
171 | =head1 SEE ALSO |
158 | |
172 | |
159 | L<AnyEvent>, L<Net::DBus>. |
173 | L<AnyEvent>, L<Net::DBus>. |