1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent - provide framework for multiple event loops |
3 | AnyEvent - provide framework for multiple event loops |
4 | |
4 | |
5 | Event, Coro, Glib, Tk - various supported event loops |
5 | Event, Coro, Glib, Tk, Perl - various supported event loops |
6 | |
6 | |
7 | =head1 SYNOPSIS |
7 | =head1 SYNOPSIS |
8 | |
8 | |
9 | use AnyEvent; |
9 | use AnyEvent; |
10 | |
10 | |
11 | my $w = AnyEvent->timer (fh => ..., poll => "[rw]+", cb => sub { |
11 | my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { |
12 | my ($poll_got) = @_; |
|
|
13 | ... |
12 | ... |
14 | }); |
13 | }); |
|
|
14 | |
15 | my $w = AnyEvent->io (after => $seconds, cb => sub { |
15 | my $w = AnyEvent->timer (after => $seconds, cb => sub { |
16 | ... |
16 | ... |
17 | }); |
17 | }); |
18 | |
18 | |
19 | # watchers get canceled whenever $w is destroyed |
19 | my $w = AnyEvent->condvar; # stores wether a condition was flagged |
20 | # only one watcher per $fh and $poll type is allowed |
|
|
21 | # (i.e. on a socket you cna have one r + one w or one rw |
|
|
22 | # watcher, not any more. |
|
|
23 | # timers can only be used once |
|
|
24 | |
|
|
25 | my $w = AnyEvent->condvar; # kind of main loop replacement |
|
|
26 | # can only be used once |
|
|
27 | $w->wait; # enters main loop till $condvar gets ->send |
20 | $w->wait; # enters "main loop" till $condvar gets ->broadcast |
28 | $w->broadcast; # wake up waiting and future wait's |
21 | $w->broadcast; # wake up current and all future wait's |
29 | |
22 | |
30 | =head1 DESCRIPTION |
23 | =head1 DESCRIPTION |
31 | |
24 | |
32 | L<AnyEvent> provides an identical interface to multiple event loops. This |
25 | L<AnyEvent> provides an identical interface to multiple event loops. This |
33 | allows module authors to utilizy an event loop without forcing module |
26 | allows module authors to utilise an event loop without forcing module |
34 | users to use the same event loop (as only a single event loop can coexist |
27 | users to use the same event loop (as only a single event loop can coexist |
35 | peacefully at any one time). |
28 | peacefully at any one time). |
36 | |
29 | |
37 | The interface itself is vaguely similar but not identical to the Event |
30 | The interface itself is vaguely similar but not identical to the Event |
38 | module. |
31 | module. |
… | |
… | |
40 | On the first call of any method, the module tries to detect the currently |
33 | On the first call of any method, the module tries to detect the currently |
41 | loaded event loop by probing wether any of the following modules is |
34 | loaded event loop by probing wether any of the following modules is |
42 | loaded: L<Coro::Event>, L<Event>, L<Glib>, L<Tk>. The first one found is |
35 | loaded: L<Coro::Event>, L<Event>, L<Glib>, L<Tk>. The first one found is |
43 | used. If none is found, the module tries to load these modules in the |
36 | used. If none is found, the module tries to load these modules in the |
44 | order given. The first one that could be successfully loaded will be |
37 | order given. The first one that could be successfully loaded will be |
45 | used. If still none could be found, it will issue an error. |
38 | used. If still none could be found, AnyEvent will fall back to a pure-perl |
|
|
39 | event loop, which is also not very efficient. |
|
|
40 | |
|
|
41 | Because AnyEvent first checks for modules that are already loaded, loading |
|
|
42 | an Event model explicitly before first using AnyEvent will likely make |
|
|
43 | that model the default. For example: |
|
|
44 | |
|
|
45 | use Tk; |
|
|
46 | use AnyEvent; |
|
|
47 | |
|
|
48 | # .. AnyEvent will likely default to Tk |
|
|
49 | |
|
|
50 | The pure-perl implementation of AnyEvent is called |
|
|
51 | C<AnyEvent::Impl::Perl>. Like other event modules you can load it |
|
|
52 | explicitly. |
|
|
53 | |
|
|
54 | =head1 WATCHERS |
|
|
55 | |
|
|
56 | AnyEvent has the central concept of a I<watcher>, which is an object that |
|
|
57 | stores relevant data for each kind of event you are waiting for, such as |
|
|
58 | the callback to call, the filehandle to watch, etc. |
|
|
59 | |
|
|
60 | These watchers are normal Perl objects with normal Perl lifetime. After |
|
|
61 | creating a watcher it will immediately "watch" for events and invoke |
|
|
62 | the callback. To disable the watcher you have to destroy it (e.g. by |
|
|
63 | setting the variable that stores it to C<undef> or otherwise deleting all |
|
|
64 | references to it). |
|
|
65 | |
|
|
66 | All watchers are created by calling a method on the C<AnyEvent> class. |
|
|
67 | |
|
|
68 | =head2 IO WATCHERS |
|
|
69 | |
|
|
70 | You can create I/O watcher by calling the C<< AnyEvent->io >> method with |
|
|
71 | the following mandatory arguments: |
|
|
72 | |
|
|
73 | C<fh> the Perl I<filehandle> (not filedescriptor) to watch for |
|
|
74 | events. C<poll> must be a string that is either C<r> or C<w>, that creates |
|
|
75 | a watcher waiting for "r"eadable or "w"ritable events. C<cb> teh callback |
|
|
76 | to invoke everytime the filehandle becomes ready. |
|
|
77 | |
|
|
78 | Only one io watcher per C<fh> and C<poll> combination is allowed (i.e. on |
|
|
79 | a socket you can have one r + one w, not any more (limitation comes from |
|
|
80 | Tk - if you are sure you are not using Tk this limitation is gone). |
|
|
81 | |
|
|
82 | Filehandles will be kept alive, so as long as the watcher exists, the |
|
|
83 | filehandle exists, too. |
|
|
84 | |
|
|
85 | Example: |
|
|
86 | |
|
|
87 | # wait for readability of STDIN, then read a line and disable the watcher |
|
|
88 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
|
|
89 | chomp (my $input = <STDIN>); |
|
|
90 | warn "read: $input\n"; |
|
|
91 | undef $w; |
|
|
92 | }); |
|
|
93 | |
|
|
94 | =head2 TIME WATCHERS |
|
|
95 | |
|
|
96 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
|
|
97 | method with the following mandatory arguments: |
|
|
98 | |
|
|
99 | C<after> after how many seconds (fractions are supported) should the timer |
|
|
100 | activate. C<cb> the callback to invoke. |
|
|
101 | |
|
|
102 | The timer callback will be invoked at most once: if you want a repeating |
|
|
103 | timer you have to create a new watcher (this is a limitation by both Tk |
|
|
104 | and Glib). |
|
|
105 | |
|
|
106 | Example: |
|
|
107 | |
|
|
108 | # fire an event after 7.7 seconds |
|
|
109 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
|
|
110 | warn "timeout\n"; |
|
|
111 | }); |
|
|
112 | |
|
|
113 | # to cancel the timer: |
|
|
114 | undef $w |
|
|
115 | |
|
|
116 | =head2 CONDITION WATCHERS |
|
|
117 | |
|
|
118 | Condition watchers can be created by calling the C<< AnyEvent->condvar >> |
|
|
119 | method without any arguments. |
|
|
120 | |
|
|
121 | A condition watcher watches for a condition - precisely that the C<< |
|
|
122 | ->broadcast >> method has been called. |
|
|
123 | |
|
|
124 | The watcher has only two methods: |
46 | |
125 | |
47 | =over 4 |
126 | =over 4 |
48 | |
127 | |
|
|
128 | =item $cv->wait |
|
|
129 | |
|
|
130 | Wait (blocking if necessary) until the C<< ->broadcast >> method has been |
|
|
131 | called on c<$cv>, while servicing other watchers normally. |
|
|
132 | |
|
|
133 | Not all event models support a blocking wait - some die in that case, so |
|
|
134 | if you are using this from a module, never require a blocking wait, but |
|
|
135 | let the caller decide wether the call will block or not (for example, |
|
|
136 | by coupling condition variables with some kind of request results and |
|
|
137 | supporting callbacks so the caller knows that getting the result will not |
|
|
138 | block, while still suppporting blockign waits if the caller so desires). |
|
|
139 | |
|
|
140 | You can only wait once on a condition - additional calls will return |
|
|
141 | immediately. |
|
|
142 | |
|
|
143 | =item $cv->broadcast |
|
|
144 | |
|
|
145 | Flag the condition as ready - a running C<< ->wait >> and all further |
|
|
146 | calls to C<wait> will return after this method has been called. If nobody |
|
|
147 | is waiting the broadcast will be remembered.. |
|
|
148 | |
|
|
149 | Example: |
|
|
150 | |
|
|
151 | # wait till the result is ready |
|
|
152 | my $result_ready = AnyEvent->condvar; |
|
|
153 | |
|
|
154 | # do something such as adding a timer |
|
|
155 | # or socket watcher the calls $result_ready->broadcast |
|
|
156 | # when the "result" is ready. |
|
|
157 | |
|
|
158 | $result_ready->wait; |
|
|
159 | |
|
|
160 | =back |
|
|
161 | |
|
|
162 | =head2 SIGNAL WATCHERS |
|
|
163 | |
|
|
164 | You can listen for signals using a signal watcher, C<signal> is the signal |
|
|
165 | I<name> without any C<SIG> prefix. |
|
|
166 | |
|
|
167 | These watchers might use C<%SIG>, so programs overwriting those signals |
|
|
168 | directly will likely not work correctly. |
|
|
169 | |
|
|
170 | Example: exit on SIGINT |
|
|
171 | |
|
|
172 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
|
|
173 | |
|
|
174 | =head1 GLOBALS |
|
|
175 | |
|
|
176 | =over 4 |
|
|
177 | |
|
|
178 | =item $AnyEvent::MODEL |
|
|
179 | |
|
|
180 | Contains C<undef> until the first watcher is being created. Then it |
|
|
181 | contains the event model that is being used, which is the name of the |
|
|
182 | Perl class implementing the model. This class is usually one of the |
|
|
183 | C<AnyEvent::Impl:xxx> modules, but can be any other class in the case |
|
|
184 | AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). |
|
|
185 | |
|
|
186 | The known classes so far are: |
|
|
187 | |
|
|
188 | AnyEvent::Impl::Coro based on Coro::Event, best choise. |
|
|
189 | AnyEvent::Impl::Event based on Event, also best choice :) |
|
|
190 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
|
|
191 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
|
|
192 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
|
|
193 | |
|
|
194 | =item AnyEvent::detect |
|
|
195 | |
|
|
196 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model if |
|
|
197 | necessary. You should only call this function right before you would have |
|
|
198 | created an AnyEvent watcher anyway, that is, very late at runtime. |
|
|
199 | |
|
|
200 | =back |
|
|
201 | |
|
|
202 | =head1 WHAT TO DO IN A MODULE |
|
|
203 | |
|
|
204 | As a module author, you should "use AnyEvent" and call AnyEvent methods |
|
|
205 | freely, but you should not load a specific event module or rely on it. |
|
|
206 | |
|
|
207 | Be careful when you create watchers in the module body - Anyevent will |
|
|
208 | decide which event module to use as soon as the first method is called, so |
|
|
209 | by calling AnyEvent in your module body you force the user of your module |
|
|
210 | to load the event module first. |
|
|
211 | |
|
|
212 | =head1 WHAT TO DO IN THE MAIN PROGRAM |
|
|
213 | |
|
|
214 | There will always be a single main program - the only place that should |
|
|
215 | dictate which event model to use. |
|
|
216 | |
|
|
217 | If it doesn't care, it can just "use AnyEvent" and use it itself, or not |
|
|
218 | do anything special and let AnyEvent decide which implementation to chose. |
|
|
219 | |
|
|
220 | If the main program relies on a specific event model (for example, in Gtk2 |
|
|
221 | programs you have to rely on either Glib or Glib::Event), you should load |
|
|
222 | it before loading AnyEvent or any module that uses it, generally, as early |
|
|
223 | as possible. The reason is that modules might create watchers when they |
|
|
224 | are loaded, and AnyEvent will decide on the event model to use as soon as |
|
|
225 | it creates watchers, and it might chose the wrong one unless you load the |
|
|
226 | correct one yourself. |
|
|
227 | |
|
|
228 | You can chose to use a rather inefficient pure-perl implementation by |
|
|
229 | loading the C<AnyEvent::Impl::Perl> module, but letting AnyEvent chose is |
|
|
230 | generally better. |
|
|
231 | |
49 | =cut |
232 | =cut |
50 | |
233 | |
51 | package AnyEvent; |
234 | package AnyEvent; |
52 | |
235 | |
53 | no warnings; |
236 | no warnings; |
54 | use strict 'vars'; |
237 | use strict; |
55 | use Carp; |
238 | use Carp; |
56 | |
239 | |
57 | our $VERSION = 0.2; |
240 | our $VERSION = '2.5'; |
58 | our $MODEL; |
241 | our $MODEL; |
59 | |
242 | |
60 | our $AUTOLOAD; |
243 | our $AUTOLOAD; |
61 | our @ISA; |
244 | our @ISA; |
62 | |
245 | |
|
|
246 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
|
|
247 | |
|
|
248 | our @REGISTRY; |
|
|
249 | |
63 | my @models = ( |
250 | my @models = ( |
64 | [Coro => Coro::Event::], |
251 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
65 | [Event => Event::], |
252 | [Event:: => AnyEvent::Impl::Event::], |
66 | [Glib => Glib::], |
253 | [Glib:: => AnyEvent::Impl::Glib::], |
67 | [Tk => Tk::], |
254 | [Tk:: => AnyEvent::Impl::Tk::], |
|
|
255 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
68 | ); |
256 | ); |
69 | |
257 | |
70 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait cancel DESTROY); |
258 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY); |
71 | |
259 | |
72 | sub AUTOLOAD { |
260 | sub detect() { |
73 | $AUTOLOAD =~ s/.*://; |
|
|
74 | |
|
|
75 | $method{$AUTOLOAD} |
|
|
76 | or croak "$AUTOLOAD: not a valid method for AnyEvent objects"; |
|
|
77 | |
|
|
78 | unless ($MODEL) { |
261 | unless ($MODEL) { |
|
|
262 | no strict 'refs'; |
|
|
263 | |
79 | # check for already loaded models |
264 | # check for already loaded models |
80 | for (@models) { |
265 | for (@REGISTRY, @models) { |
81 | my ($model, $package) = @$_; |
266 | my ($package, $model) = @$_; |
82 | if (scalar keys %{ *{"$package\::"} }) { |
267 | if (${"$package\::VERSION"} > 0) { |
83 | eval "require AnyEvent::Impl::$model"; |
268 | if (eval "require $model") { |
84 | last if $MODEL; |
269 | $MODEL = $model; |
|
|
270 | warn "AnyEvent: found model '$model', using it.\n" if $verbose > 1; |
|
|
271 | last; |
|
|
272 | } |
85 | } |
273 | } |
86 | } |
274 | } |
87 | |
275 | |
88 | unless ($MODEL) { |
276 | unless ($MODEL) { |
89 | # try to load a model |
277 | # try to load a model |
90 | |
278 | |
91 | for (@models) { |
279 | for (@REGISTRY, @models) { |
92 | my ($model, $package) = @$_; |
280 | my ($package, $model) = @$_; |
93 | eval "require AnyEvent::Impl::$model"; |
281 | if (eval "require $model") { |
94 | last if $MODEL; |
282 | $MODEL = $model; |
|
|
283 | warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; |
|
|
284 | last; |
|
|
285 | } |
95 | } |
286 | } |
96 | |
287 | |
97 | $MODEL |
288 | $MODEL |
98 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; |
289 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Event (or Coro+Event), Glib or Tk."; |
99 | } |
290 | } |
|
|
291 | |
|
|
292 | unshift @ISA, $MODEL; |
|
|
293 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
100 | } |
294 | } |
101 | |
295 | |
102 | @ISA = $MODEL; |
296 | $MODEL |
|
|
297 | } |
|
|
298 | |
|
|
299 | sub AUTOLOAD { |
|
|
300 | (my $func = $AUTOLOAD) =~ s/.*://; |
|
|
301 | |
|
|
302 | $method{$func} |
|
|
303 | or croak "$func: not a valid method for AnyEvent objects"; |
|
|
304 | |
|
|
305 | detect unless $MODEL; |
103 | |
306 | |
104 | my $class = shift; |
307 | my $class = shift; |
105 | $class->$AUTOLOAD (@_); |
308 | $class->$func (@_); |
106 | } |
309 | } |
107 | |
310 | |
108 | =back |
311 | package AnyEvent::Base; |
|
|
312 | |
|
|
313 | # default implementation for signal |
|
|
314 | |
|
|
315 | our %SIG_CB; |
|
|
316 | |
|
|
317 | sub signal { |
|
|
318 | my (undef, %arg) = @_; |
|
|
319 | |
|
|
320 | my $signal = uc $arg{signal} |
|
|
321 | or Carp::croak "required option 'signal' is missing"; |
|
|
322 | |
|
|
323 | my $w = bless [$signal, $arg{cb}], "AnyEvent::Base::Signal"; |
|
|
324 | |
|
|
325 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
|
|
326 | $SIG{$signal} ||= sub { |
|
|
327 | $_->() for values %{ $SIG_CB{$signal} }; |
|
|
328 | }; |
|
|
329 | |
|
|
330 | $w |
|
|
331 | } |
|
|
332 | |
|
|
333 | sub AnyEvent::Base::Signal::DESTROY { |
|
|
334 | my ($signal, $cb) = @{$_[0]}; |
|
|
335 | |
|
|
336 | delete $SIG_CB{$signal}{$cb}; |
|
|
337 | |
|
|
338 | $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; |
|
|
339 | } |
|
|
340 | |
|
|
341 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
|
|
342 | |
|
|
343 | If you need to support another event library which isn't directly |
|
|
344 | supported by AnyEvent, you can supply your own interface to it by |
|
|
345 | pushing, before the first watcher gets created, the package name of |
|
|
346 | the event module and the package name of the interface to use onto |
|
|
347 | C<@AnyEvent::REGISTRY>. You can do that before and even without loading |
|
|
348 | AnyEvent. |
|
|
349 | |
|
|
350 | Example: |
|
|
351 | |
|
|
352 | push @AnyEvent::REGISTRY, [urxvt => urxvt::anyevent::]; |
|
|
353 | |
|
|
354 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
|
|
355 | package/class when it finds the C<urxvt> package/module is loaded. When |
|
|
356 | AnyEvent is loaded and asked to find a suitable event model, it will |
|
|
357 | first check for the presence of urxvt. |
|
|
358 | |
|
|
359 | The class should provide implementations for all watcher types (see |
|
|
360 | L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
|
|
361 | (Source code) and so on for actual examples, use C<perldoc -m |
|
|
362 | AnyEvent::Impl::Glib> to see the sources). |
|
|
363 | |
|
|
364 | The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) |
|
|
365 | uses the above line as-is. An interface isn't included in AnyEvent |
|
|
366 | because it doesn't make sense outside the embedded interpreter inside |
|
|
367 | I<rxvt-unicode>, and it is updated and maintained as part of the |
|
|
368 | I<rxvt-unicode> distribution. |
|
|
369 | |
|
|
370 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
|
|
371 | condition variables: code blocking while waiting for a condition will |
|
|
372 | C<die>. This still works with most modules/usages, and blocking calls must |
|
|
373 | not be in an interactive appliation, so it makes sense. |
|
|
374 | |
|
|
375 | =head1 ENVIRONMENT VARIABLES |
|
|
376 | |
|
|
377 | The following environment variables are used by this module: |
|
|
378 | |
|
|
379 | C<PERL_ANYEVENT_VERBOSE> when set to C<2> or higher, reports which event |
|
|
380 | model gets used. |
109 | |
381 | |
110 | =head1 EXAMPLE |
382 | =head1 EXAMPLE |
111 | |
383 | |
112 | The following program uses an io watcher to read data from stdin, a timer |
384 | The following program uses an io watcher to read data from stdin, a timer |
113 | to display a message once per second, and a condvar to exit the program |
385 | to display a message once per second, and a condvar to exit the program |
… | |
… | |
135 | |
407 | |
136 | new_timer; # create first timer |
408 | new_timer; # create first timer |
137 | |
409 | |
138 | $cv->wait; # wait until user enters /^q/i |
410 | $cv->wait; # wait until user enters /^q/i |
139 | |
411 | |
|
|
412 | =head1 REAL-WORLD EXAMPLE |
|
|
413 | |
|
|
414 | Consider the L<Net::FCP> module. It features (among others) the following |
|
|
415 | API calls, which are to freenet what HTTP GET requests are to http: |
|
|
416 | |
|
|
417 | my $data = $fcp->client_get ($url); # blocks |
|
|
418 | |
|
|
419 | my $transaction = $fcp->txn_client_get ($url); # does not block |
|
|
420 | $transaction->cb ( sub { ... } ); # set optional result callback |
|
|
421 | my $data = $transaction->result; # possibly blocks |
|
|
422 | |
|
|
423 | The C<client_get> method works like C<LWP::Simple::get>: it requests the |
|
|
424 | given URL and waits till the data has arrived. It is defined to be: |
|
|
425 | |
|
|
426 | sub client_get { $_[0]->txn_client_get ($_[1])->result } |
|
|
427 | |
|
|
428 | And in fact is automatically generated. This is the blocking API of |
|
|
429 | L<Net::FCP>, and it works as simple as in any other, similar, module. |
|
|
430 | |
|
|
431 | More complicated is C<txn_client_get>: It only creates a transaction |
|
|
432 | (completion, result, ...) object and initiates the transaction. |
|
|
433 | |
|
|
434 | my $txn = bless { }, Net::FCP::Txn::; |
|
|
435 | |
|
|
436 | It also creates a condition variable that is used to signal the completion |
|
|
437 | of the request: |
|
|
438 | |
|
|
439 | $txn->{finished} = AnyAvent->condvar; |
|
|
440 | |
|
|
441 | It then creates a socket in non-blocking mode. |
|
|
442 | |
|
|
443 | socket $txn->{fh}, ...; |
|
|
444 | fcntl $txn->{fh}, F_SETFL, O_NONBLOCK; |
|
|
445 | connect $txn->{fh}, ... |
|
|
446 | and !$!{EWOULDBLOCK} |
|
|
447 | and !$!{EINPROGRESS} |
|
|
448 | and Carp::croak "unable to connect: $!\n"; |
|
|
449 | |
|
|
450 | Then it creates a write-watcher which gets called whenever an error occurs |
|
|
451 | or the connection succeeds: |
|
|
452 | |
|
|
453 | $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'w', cb => sub { $txn->fh_ready_w }); |
|
|
454 | |
|
|
455 | And returns this transaction object. The C<fh_ready_w> callback gets |
|
|
456 | called as soon as the event loop detects that the socket is ready for |
|
|
457 | writing. |
|
|
458 | |
|
|
459 | The C<fh_ready_w> method makes the socket blocking again, writes the |
|
|
460 | request data and replaces the watcher by a read watcher (waiting for reply |
|
|
461 | data). The actual code is more complicated, but that doesn't matter for |
|
|
462 | this example: |
|
|
463 | |
|
|
464 | fcntl $txn->{fh}, F_SETFL, 0; |
|
|
465 | syswrite $txn->{fh}, $txn->{request} |
|
|
466 | or die "connection or write error"; |
|
|
467 | $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r }); |
|
|
468 | |
|
|
469 | Again, C<fh_ready_r> waits till all data has arrived, and then stores the |
|
|
470 | result and signals any possible waiters that the request ahs finished: |
|
|
471 | |
|
|
472 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
|
|
473 | |
|
|
474 | if (end-of-file or data complete) { |
|
|
475 | $txn->{result} = $txn->{buf}; |
|
|
476 | $txn->{finished}->broadcast; |
|
|
477 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
|
|
478 | } |
|
|
479 | |
|
|
480 | The C<result> method, finally, just waits for the finished signal (if the |
|
|
481 | request was already finished, it doesn't wait, of course, and returns the |
|
|
482 | data: |
|
|
483 | |
|
|
484 | $txn->{finished}->wait; |
|
|
485 | return $txn->{result}; |
|
|
486 | |
|
|
487 | The actual code goes further and collects all errors (C<die>s, exceptions) |
|
|
488 | that occured during request processing. The C<result> method detects |
|
|
489 | wether an exception as thrown (it is stored inside the $txn object) |
|
|
490 | and just throws the exception, which means connection errors and other |
|
|
491 | problems get reported tot he code that tries to use the result, not in a |
|
|
492 | random callback. |
|
|
493 | |
|
|
494 | All of this enables the following usage styles: |
|
|
495 | |
|
|
496 | 1. Blocking: |
|
|
497 | |
|
|
498 | my $data = $fcp->client_get ($url); |
|
|
499 | |
|
|
500 | 2. Blocking, but parallelizing: |
|
|
501 | |
|
|
502 | my @datas = map $_->result, |
|
|
503 | map $fcp->txn_client_get ($_), |
|
|
504 | @urls; |
|
|
505 | |
|
|
506 | Both blocking examples work without the module user having to know |
|
|
507 | anything about events. |
|
|
508 | |
|
|
509 | 3a. Event-based in a main program, using any support Event module: |
|
|
510 | |
|
|
511 | use Event; |
|
|
512 | |
|
|
513 | $fcp->txn_client_get ($url)->cb (sub { |
|
|
514 | my $txn = shift; |
|
|
515 | my $data = $txn->result; |
|
|
516 | ... |
|
|
517 | }); |
|
|
518 | |
|
|
519 | Event::loop; |
|
|
520 | |
|
|
521 | 3b. The module user could use AnyEvent, too: |
|
|
522 | |
|
|
523 | use AnyEvent; |
|
|
524 | |
|
|
525 | my $quit = AnyEvent->condvar; |
|
|
526 | |
|
|
527 | $fcp->txn_client_get ($url)->cb (sub { |
|
|
528 | ... |
|
|
529 | $quit->broadcast; |
|
|
530 | }); |
|
|
531 | |
|
|
532 | $quit->wait; |
|
|
533 | |
140 | =head1 SEE ALSO |
534 | =head1 SEE ALSO |
141 | |
535 | |
142 | L<Coro::Event>, L<Coro>, L<Event>, L<Glib::Event>, L<Glib>, |
536 | Event modules: L<Coro::Event>, L<Coro>, L<Event>, L<Glib::Event>, L<Glib>. |
143 | L<AnyEvent::Impl::Coro>, |
537 | |
144 | L<AnyEvent::Impl::Event>, |
538 | Implementations: L<AnyEvent::Impl::Coro>, L<AnyEvent::Impl::Event>, L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>. |
145 | L<AnyEvent::Impl::Glib>, |
539 | |
146 | L<AnyEvent::Impl::Tk>. |
540 | Nontrivial usage example: L<Net::FCP>. |
147 | |
541 | |
148 | =head1 |
542 | =head1 |
149 | |
543 | |
150 | =cut |
544 | =cut |
151 | |
545 | |