ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Intro.pod
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Intro.pod (file contents):
Revision 1.13 by root, Mon Jun 2 09:33:43 2008 UTC vs.
Revision 1.23 by root, Mon Jun 29 20:59:08 2009 UTC

1=head1 NAME
2
3AnyEvent::Intro - an introductory tutorial to AnyEvent
4
1=head1 Introduction to AnyEvent 5=head1 Introduction to AnyEvent
2 6
3This is a tutorial that will introduce you to the features of AnyEvent. 7This is a tutorial that will introduce you to the features of AnyEvent.
4 8
5The first part introduces the core AnyEvent module (after swamping you a 9The first part introduces the core AnyEvent module (after swamping you a
6bit in evangelism), which might already provide all you ever need. 10bit in evangelism), which might already provide all you ever need. If you
11are only interested in AnyEvent's event handling capabilities, read no
12further.
7 13
8The second part focuses on network programming using sockets, for which 14The second part focuses on network programming using sockets, for which
9AnyEvent offers a lot of support you can use. 15AnyEvent offers a lot of support you can use, and a lot of workarounds
16around portability quirks.
10 17
11 18
12=head1 What is AnyEvent? 19=head1 What is AnyEvent?
13 20
14If you don't care for the whys and want to see code, skip this section! 21If you don't care for the whys and want to see code, skip this section!
101 ); 108 );
102 109
103 # do something else here 110 # do something else here
104 111
105Looks more complicated, and surely is, but the advantage of using events 112Looks more complicated, and surely is, but the advantage of using events
106is that your program can do something else instead of waiting for 113is that your program can do something else instead of waiting for input
114(side note: combining AnyEvent with a thread package such as Coro can
115recoup much of the simplicity, effectively getting the best of two
116worlds).
117
107input. Waiting as in the first example is also called "blocking" because 118Waiting as done in the first example is also called "blocking" the process
108you "block" your process from executing anything else while you do so. 119because you "block"/keep your process from executing anything else while
120you do so.
109 121
110The second example avoids blocking, by only registering interest in a read 122The second example avoids blocking by only registering interest in a read
111event, which is fast and doesn't block your process. Only when read data 123event, which is fast and doesn't block your process. Only when read data
112is available will the callback be called, which can then proceed to read 124is available will the callback be called, which can then proceed to read
113the data. 125the data.
114 126
115The "interest" is represented by an object returned by C<< AnyEvent->io 127The "interest" is represented by an object returned by C<< AnyEvent->io
116>> called a "watcher" object - called like that because it "watches" your 128>> called a "watcher" object - called like that because it "watches" your
117file handle (or other event sources) for the event you are interested in. 129file handle (or other event sources) for the event you are interested in.
118 130
119In the example above, we create an I/O watcher by calling the C<< 131In the example above, we create an I/O watcher by calling the C<<
120AnyEvent->io >> method. Disinterest in some event is simply expressed by 132AnyEvent->io >> method. Disinterest in some event is simply expressed
121forgetting about the watcher, for example, by C<undef>'ing the variable it 133by forgetting about the watcher, for example, by C<undef>'ing the only
122is stored in. AnyEvent will automatically clean up the watcher if it is no 134variable it is stored in. AnyEvent will automatically clean up the watcher
123longer used, much like Perl closes your file handles if you no longer use 135if it is no longer used, much like Perl closes your file handles if you no
124them anywhere. 136longer use them anywhere.
137
138=head3 A short note on callbacks
139
140A common issue that hits people is the problem of passing parameters
141to callbacks. Programmers used to languages such as C or C++ are often
142used to a style where one passes the address of a function (a function
143reference) and some data value, e.g.:
144
145 sub callback {
146 my ($arg) = @_;
147
148 $arg->method;
149 }
150
151 my $arg = ...;
152
153 call_me_back_later \&callback, $arg;
154
155This is clumsy, as the place where behaviour is specified (when the
156callback is registered) is often far away from the place where behaviour
157is implemented. It also doesn't use Perl syntax to invoke the code. There
158is also an abstraction penalty to pay as one has to I<name> the callback,
159which often is unnecessary and leads to nonsensical or duplicated names.
160
161In Perl, one can specify behaviour much more directly by using
162I<closures>. Closures are code blocks that take a reference to the
163enclosing scope(s) when they are created. This means lexical variables in
164scope at the time of creating the closure can simply be used inside the
165closure:
166
167 my $arg = ...;
168
169 call_me_back_later sub { $arg->method };
170
171Under most circumstances, closures are faster, use fewer resources and
172result in much clearer code then the traditional approach. Faster,
173because parameter passing and storing them in local variables in Perl
174is relatively slow. Fewer resources, because closures take references
175to existing variables without having to create new ones, and clearer
176code because it is immediately obvious that the second example calls the
177C<method> method when the callback is invoked.
178
179Apart from these, the strongest argument for using closures with AnyEvent
180is that AnyEvent does not allow passing parameters to the callback, so
181closures are the only way to achieve that in most cases :->
182
183
184=head3 A hint on debugging
185
186AnyEvent does, by default, not do any argument checking. This can lead to
187strange and unexpected results especially if you are trying to learn your
188ways with AnyEvent.
189
190AnyEvent supports a special "strict" mode, off by default, which does very
191strict argument checking, at the expense of being somewhat slower. During
192development, however, this mode is very useful.
193
194You can enable this strict mode either by having an environment variable
195C<PERL_ANYEVENT_STRICT> with a true value in your environment:
196
197 PERL_ANYEVENT_STRICT=1 perl test.pl
198
199Or you can write C<use AnyEvent::Strict> in your program, which has the
200same effect (do not do this in production, however).
201
125 202
126=head2 Condition Variables 203=head2 Condition Variables
127 204
128However, the above is not a fully working program, and will not work 205Back to the I/O watcher example: The code is not yet a fully working
129as-is. The reason is that your callback will not be invoked out of the 206program, and will not work as-is. The reason is that your callback will
130blue, you have to run the event loop. Also, event-based programs sometimes 207not be invoked out of the blue, you have to run the event loop. Also,
131have to block, too, as when there simply is nothing else to do and 208event-based programs sometimes have to block, too, as when there simply is
132everything waits for some events, it needs to block the process as well. 209nothing else to do and everything waits for some events, it needs to block
210the process as well until new events arrive.
133 211
134In AnyEvent, this is done using condition variables. Condition variables 212In AnyEvent, this is done using condition variables. Condition variables
135are named "condition variables" because they represent a condition that is 213are named "condition variables" because they represent a condition that is
136initially false and needs to be fulfilled. 214initially false and needs to be fulfilled.
137 215
139or even callbacks and many other things (and they are often called like 217or even callbacks and many other things (and they are often called like
140this in other frameworks). The important point is that you can create them 218this in other frameworks). The important point is that you can create them
141freely and later wait for them to become true. 219freely and later wait for them to become true.
142 220
143Condition variables have two sides - one side is the "producer" of the 221Condition variables have two sides - one side is the "producer" of the
144condition (whatever code detects the condition), the other side is the 222condition (whatever code detects and flags the condition), the other side
145"consumer" (the code that waits for that condition). 223is the "consumer" (the code that waits for that condition).
146 224
147In our example in the previous section, the producer is the event callback 225In our example in the previous section, the producer is the event callback
148and there is no consumer yet - let's change that now: 226and there is no consumer yet - let's change that right now:
149 227
150 use AnyEvent; 228 use AnyEvent;
151 229
152 $| = 1; print "enter your name> "; 230 $| = 1; print "enter your name> ";
153 231
174 print "your name is $name\n"; 252 print "your name is $name\n";
175 253
176This program creates an AnyEvent condvar by calling the C<< 254This program creates an AnyEvent condvar by calling the C<<
177AnyEvent->condvar >> method. It then creates a watcher as usual, but 255AnyEvent->condvar >> method. It then creates a watcher as usual, but
178inside the callback it C<send>'s the C<$name_ready> condition variable, 256inside the callback it C<send>'s the C<$name_ready> condition variable,
179which causes anybody waiting on it to continue. 257which causes whoever is waiting on it to continue.
180 258
181The "anybody" in this case is the code that follows, which calls C<< 259The "whoever" in this case is the code that follows, which calls C<<
182$name_ready->recv >>: The producer calls C<send>, the consumer calls 260$name_ready->recv >>: The producer calls C<send>, the consumer calls
183C<recv>. 261C<recv>.
184 262
185If there is no C<$name> available yet, then the call to C<< 263If there is no C<$name> available yet, then the call to C<<
186$name_ready->recv >> will halt your program until the condition becomes 264$name_ready->recv >> will halt your program until the condition becomes
196 274
197 my $name_ready = AnyEvent->condvar; 275 my $name_ready = AnyEvent->condvar;
198 276
199 my $wait_for_input = AnyEvent->io ( 277 my $wait_for_input = AnyEvent->io (
200 fh => \*STDIN, poll => "r", 278 fh => \*STDIN, poll => "r",
201 cb => sub { $name_ready->send (scalar = <STDIN>) } 279 cb => sub { $name_ready->send (scalar <STDIN>) }
202 ); 280 );
203 281
204 # do something else here 282 # do something else here
205 283
206 # now wait and fetch the name 284 # now wait and fetch the name
268This also shows that AnyEvent is quite flexible - you didn't have anything 346This also shows that AnyEvent is quite flexible - you didn't have anything
269to do to make the AnyEvent watcher use Gtk2 (actually Glib) - it just 347to do to make the AnyEvent watcher use Gtk2 (actually Glib) - it just
270worked. 348worked.
271 349
272Admittedly, the example is a bit silly - who would want to read names 350Admittedly, the example is a bit silly - who would want to read names
273form standard input in a Gtk+ application. But imagine that instead of 351from standard input in a Gtk+ application. But imagine that instead of
274doing that, you would make a HTTP request in the background and display 352doing that, you would make a HTTP request in the background and display
275it's results. In fact, with event-based programming you can make many 353it's results. In fact, with event-based programming you can make many
276http-requests in parallel in your program and still provide feedback to 354http-requests in parallel in your program and still provide feedback to
277the user and stay interactive. 355the user and stay interactive.
278 356
279In the next part you will see how to do just that - by implementing an 357And in the next part you will see how to do just that - by implementing an
280HTTP request, on our own, with the utility modules AnyEvent comes with. 358HTTP request, on our own, with the utility modules AnyEvent comes with.
281 359
282Before that, however, let's briefly look at how you would write your 360Before that, however, let's briefly look at how you would write your
283program with using only AnyEvent, without ever calling some other event 361program with using only AnyEvent, without ever calling some other event
284loop's run function. 362loop's run function.
285 363
286In the example using condition variables, we used that, and in fact, this 364In the example using condition variables, we used those to start waiting
287is the solution: 365for events, and in fact, condition variables are the solution:
288 366
289 my $quit_program = AnyEvent->condvar; 367 my $quit_program = AnyEvent->condvar;
290 368
291 # create AnyEvent watchers (or not) here 369 # create AnyEvent watchers (or not) here
292 370
293 $quit_program->recv; 371 $quit_program->recv;
294 372
295If any of your watcher callbacks decide to quit, they can simply call 373If any of your watcher callbacks decide to quit (this is often
374called an "unloop" in other frameworks), they can simply call C<<
296C<< $quit_program->send >>. Of course, they could also decide not to and 375$quit_program->send >>. Of course, they could also decide not to and
297simply call C<exit> instead, or they could decide not to quit, ever (e.g. 376simply call C<exit> instead, or they could decide not to quit, ever (e.g.
298in a long-running daemon program). 377in a long-running daemon program).
299 378
300In that case, you can simply use: 379If you don't need some clean quit functionality and just want to run the
380event loop, you can simply do this:
301 381
302 AnyEvent->condvar->recv; 382 AnyEvent->condvar->recv;
303 383
304And this is, in fact, closest to the idea of a main loop run function that 384And this is, in fact, closest to the idea of a main loop run function that
305AnyEvent offers. 385AnyEvent offers.
337 417
338 # now wait till our time has come 418 # now wait till our time has come
339 $cv->recv; 419 $cv->recv;
340 420
341Unlike I/O watchers, timers are only interested in the amount of seconds 421Unlike I/O watchers, timers are only interested in the amount of seconds
342they have to wait. When that amount of time has passed, AnyEvent will 422they have to wait. When (at least) that amount of time has passed,
343invoke your callback. 423AnyEvent will invoke your callback.
344 424
345Unlike I/O watchers, which will call your callback as many times as there 425Unlike I/O watchers, which will call your callback as many times as there
346is data available, timers are one-shot: after they have "fired" once and 426is data available, timers are normally one-shot: after they have "fired"
347invoked your callback, they are dead and no longer do anything. 427once and invoked your callback, they are dead and no longer do anything.
348 428
349To get a repeating timer, such as a timer firing roughly once per second, 429To get a repeating timer, such as a timer firing roughly once per second,
350you have to recreate it: 430you can specify an C<interval> parameter:
351 431
352 use AnyEvent; 432 my $once_per_second = AnyEvent->timer (
353 433 after => 0, # first invoke ASAP
354 my $time_watcher; 434 interval => 1, # then invoke every second
355 435 cb => sub { # the callback to invoke
356 sub once_per_second { 436 $cv->send;
357 print "tick\n";
358 437 },
359 # (re-)create the watcher
360 $time_watcher = AnyEvent->timer (
361 after => 1,
362 cb => \&once_per_second,
363 ); 438 );
364 }
365
366 # now start the timer
367 once_per_second;
368
369Having to recreate your timer is a restriction put on AnyEvent that is
370present in most event libraries it uses. It is so annoying that some
371future version might work around this limitation, but right now, it's the
372only way to do repeating timers.
373
374Fortunately most timers aren't really repeating but specify timeouts of
375some sort.
376 439
377=head3 More esoteric sources 440=head3 More esoteric sources
378 441
379AnyEvent also has some other, more esoteric event sources you can tap 442AnyEvent also has some other, more esoteric event sources you can tap
380into: signal and child watchers. 443into: signal, child and idle watchers.
381 444
382Signal watchers can be used to wait for "signal events", which simply 445Signal watchers can be used to wait for "signal events", which simply
383means your process got send a signal (such as C<SIGTERM> or C<SIGUSR1>). 446means your process got send a signal (such as C<SIGTERM> or C<SIGUSR1>).
384 447
385Process watchers wait for a child process to exit. They are useful when 448Child-process watchers wait for a child process to exit. They are useful
386you fork a separate process and need to know when it exits, but you do not 449when you fork a separate process and need to know when it exits, but you
387wait for that by blocking. 450do not wait for that by blocking.
388 451
452Idle watchers invoke their callback when the event loop has handled all
453outstanding events, polled for new events and didn't find any, i.e., when
454your process is otherwise idle. They are useful if you want to do some
455non-trivial data processing that can be done when your program doesn't
456have anything better to do.
457
389Both watcher types are described in detail in the main L<AnyEvent> manual 458All these watcher types are described in detail in the main L<AnyEvent>
390page. 459manual page.
391 460
461Sometimes you also need to know what the current time is: C<<
462AnyEvent->now >> returns the time the event toolkit uses to schedule
463relative timers, and is usually what you want. It is often cached (which
464means it can be a bit outdated). In that case, you can use the more costly
465C<< AnyEvent->time >> method which will ask your operating system for the
466current time, which is slower, but also more up to date.
392 467
393=head1 Network programming and AnyEvent 468=head1 Network programming and AnyEvent
394 469
395So far you have seen how to register event watchers and handle events. 470So far you have seen how to register event watchers and handle events.
396 471
397This is a great foundation to write network clients and servers, and might be 472This is a great foundation to write network clients and servers, and might
398all that your module (or program) ever requires, but writing your own I/O 473be all that your module (or program) ever requires, but writing your own
399buffering again and again becomes tedious, not to mention that it attracts 474I/O buffering again and again becomes tedious, not to mention that it
400errors. 475attracts errors.
401 476
402While the core L<AnyEvent> module is still small and self-contained, 477While the core L<AnyEvent> module is still small and self-contained,
403the distribution comes with some very useful utility modules such as 478the distribution comes with some very useful utility modules such as
404L<AnyEvent::Handle>, L<AnyEvent::DNS> and L<AnyEvent::Socket>. These can 479L<AnyEvent::Handle>, L<AnyEvent::DNS> and L<AnyEvent::Socket>. These can
405make your life as non-blocking network programmer a lot easier. 480make your life as non-blocking network programmer a lot easier.
413a great way to do other DNS resolution tasks, such as reverse lookups of 488a great way to do other DNS resolution tasks, such as reverse lookups of
414IP addresses for log files. 489IP addresses for log files.
415 490
416=head2 L<AnyEvent::Handle> 491=head2 L<AnyEvent::Handle>
417 492
418This module handles non-blocking IO on file handles in an event based 493This module handles non-blocking IO on (socket-, pipe- etc.) file handles
419manner. It provides a wrapper object around your file handle that provides 494in an event based manner. It provides a wrapper object around your file
420queueing and buffering of incoming and outgoing data for you. 495handle that provides queueing and buffering of incoming and outgoing data
496for you.
421 497
422It also implements the most common data formats, such as text lines, or 498It also implements the most common data formats, such as text lines, or
423fixed and variable-width data blocks. 499fixed and variable-width data blocks.
424 500
425=head2 L<AnyEvent::Socket> 501=head2 L<AnyEvent::Socket>
443to your program? That C<WSAEINPROGRESS> means your C<connect> call was 519to your program? That C<WSAEINPROGRESS> means your C<connect> call was
444ignored instead of being in progress? AnyEvent::Socket works around all of 520ignored instead of being in progress? AnyEvent::Socket works around all of
445these Windows/Perl bugs for you). 521these Windows/Perl bugs for you).
446 522
447=head2 Implementing a parallel finger client with non-blocking connects 523=head2 Implementing a parallel finger client with non-blocking connects
524and AnyEvent::Socket
448 525
449The finger protocol is one of the simplest protocols in use on the 526The finger protocol is one of the simplest protocols in use on the
450internet. Or in use in the past, as almost nobody uses it anymore. 527internet. Or in use in the past, as almost nobody uses it anymore.
451 528
452It works by connecting to the finger port on another host, writing a 529It works by connecting to the finger port on another host, writing a
453single line with a user name and then reading the finger response, as 530single line with a user name and then reading the finger response, as
454specified by that user. OK, RFC 1288 specifies a vastly more complex 531specified by that user. OK, RFC 1288 specifies a vastly more complex
455protocol, but it basically boils down to this: 532protocol, but it basically boils down to this:
456 533
457 # telnet idsoftware.com finger 534 # telnet kernel.org finger
458 Trying 192.246.40.37... 535 Trying 204.152.191.37...
459 Connected to idsoftware.com (192.246.40.37). 536 Connected to kernel.org (204.152.191.37).
460 Escape character is '^]'. 537 Escape character is '^]'.
461 johnc 538
462 Welcome to id Software's Finger Service V1.5! 539 The latest stable version of the Linux kernel is: [...]
463
464 [...]
465 Now on the web:
466 [...]
467
468 Connection closed by foreign host. 540 Connection closed by foreign host.
469 541
470"Now on the web..." yeah, I<was> used indeed, but at least the finger 542So let's write a little AnyEvent function that makes a finger request:
471daemon still works, so let's write a little AnyEvent function that makes a
472finger request:
473 543
474 use AnyEvent; 544 use AnyEvent;
475 use AnyEvent::Socket; 545 use AnyEvent::Socket;
476 546
477 sub finger($$) { 547 sub finger($$) {
542socket handle as first argument, otherwise, nothing will be passed to our 612socket handle as first argument, otherwise, nothing will be passed to our
543callback. The important point is that it will always be called as soon as 613callback. The important point is that it will always be called as soon as
544the outcome of the TCP connect is known. 614the outcome of the TCP connect is known.
545 615
546This style of programming is also called "continuation style": the 616This style of programming is also called "continuation style": the
547"continuation" is simply the way the program continues - normally, a 617"continuation" is simply the way the program continues - normally at the
548program continues at the next line after some statement (the exception 618next line after some statement (the exception is loops or things like
549is loops or things like C<return>). When we are interested in events, 619C<return>). When we are interested in events, however, we instead specify
550however, we instead specify the "continuation" of our program by passing a 620the "continuation" of our program by passing a closure, which makes that
551closure, which makes that closure the "continuation" of the program. The 621closure the "continuation" of the program.
622
552C<tcp_connect> call is like saying "return now, and when the connection is 623The C<tcp_connect> call is like saying "return now, and when the
553established or it failed, continue there". 624connection is established or it failed, continue there".
554 625
555Now let's look at the callback/closure in more detail: 626Now let's look at the callback/closure in more detail:
556 627
557 # the callback receives the socket handle - or nothing 628 # the callback receives the socket handle - or nothing
558 my ($fh) = @_ 629 my ($fh) = @_
570report the results to anybody, certainly not the caller of our C<finger> 641report the results to anybody, certainly not the caller of our C<finger>
571function, and most event loops continue even after a C<die>! 642function, and most event loops continue even after a C<die>!
572 643
573This is why we instead C<return>, but also call C<< $cv->send >> without 644This is why we instead C<return>, but also call C<< $cv->send >> without
574any arguments to signal to the condvar consumer that something bad has 645any arguments to signal to the condvar consumer that something bad has
575happened. The return value of C<< $cv->send >> is irrelevant, as is the 646happened. The return value of C<< $cv->send >> is irrelevant, as is
576return value of our callback. The return statement is simply used for the 647the return value of our callback. The C<return> statement is simply
577side effect of, well, returning immediately from the callback. Checking 648used for the side effect of, well, returning immediately from the
578for errors and handling them this way is very common, which is why this 649callback. Checking for errors and handling them this way is very common,
579compact idiom is so handy. 650which is why this compact idiom is so handy.
580 651
581As the next step in the finger protocol, we send the username to the 652As the next step in the finger protocol, we send the username to the
582finger daemon on the other side of our connection: 653finger daemon on the other side of our connection (the kernel.org finger
654service doesn't actually wait for a username, but the net is running out
655of finger servers fast):
583 656
584 syswrite $fh, "$user\015\012"; 657 syswrite $fh, "$user\015\012";
585 658
586Note that this isn't 100% clean socket programming - the socket could, 659Note that this isn't 100% clean socket programming - the socket could,
587for whatever reasons, not accept our data. When writing a small amount 660for whatever reasons, not accept our data. When writing a small amount
605variable, but in a local one - if the callback returns, it would normally 678variable, but in a local one - if the callback returns, it would normally
606destroy the variable and its contents, which would in turn unregister our 679destroy the variable and its contents, which would in turn unregister our
607watcher. 680watcher.
608 681
609To avoid that, we C<undef>ine the variable in the watcher callback. This 682To avoid that, we C<undef>ine the variable in the watcher callback. This
610means that, when the C<tcp_connect> callback returns, that perl thinks 683means that, when the C<tcp_connect> callback returns, perl thinks (quite
611(quite correctly) that the read watcher is still in use - namely in the 684correctly) that the read watcher is still in use - namely in the callback,
612callback. 685and thus keeps it alive even if nothing else in the program refers to it
686anymore (it is much like Baron Münchhausen keeping himself from dying by
687pulling himself out of a swamp).
613 688
614The trick, however, is that instead of: 689The trick, however, is that instead of:
615 690
616 my $read_watcher = AnyEvent->io (... 691 my $read_watcher = AnyEvent->io (...
617 692
636 my $len = sysread $fh, $response, 1024, length $response; 711 my $len = sysread $fh, $response, 1024, length $response;
637 712
638 if ($len <= 0) { 713 if ($len <= 0) {
639 714
640Note that C<sysread> has the ability to append data it reads to a scalar, 715Note that C<sysread> has the ability to append data it reads to a scalar,
641by specifying an offset, which is what we make good use of in this 716by specifying an offset, a feature of which we make good use of in this
642example. 717example.
643 718
644When C<sysread> indicates we are done, the callback C<undef>ines 719When C<sysread> indicates we are done, the callback C<undef>ines
645the watcher and then C<send>'s the response data to the condition 720the watcher and then C<send>'s the response data to the condition
646variable. All this has the following effects: 721variable. All this has the following effects:
660But the main advantage is that we can not only run this finger function in 735But the main advantage is that we can not only run this finger function in
661the background, we even can run multiple sessions in parallel, like this: 736the background, we even can run multiple sessions in parallel, like this:
662 737
663 my $f1 = finger "trouble", "noc.dfn.de"; # check for trouble tickets 738 my $f1 = finger "trouble", "noc.dfn.de"; # check for trouble tickets
664 my $f2 = finger "1736" , "noc.dfn.de"; # fetch ticket 1736 739 my $f2 = finger "1736" , "noc.dfn.de"; # fetch ticket 1736
665 my $f3 = finger "johnc", "idsoftware.com"; # finger john 740 my $f3 = finger "hpa" , "kernel.org"; # finger hpa
666 741
667 print "trouble tickets:\n", $f1->recv, "\n"; 742 print "trouble tickets:\n" , $f1->recv, "\n";
668 print "trouble ticket #1736:\n", $f2->recv, "\n"; 743 print "trouble ticket #1736:\n", $f2->recv, "\n";
669 print "john carmacks finger file: ", $f3->recv, "\n"; 744 print "kernel release info: " , $f3->recv, "\n";
670 745
671It doesn't look like it, but in fact all three requests run in 746It doesn't look like it, but in fact all three requests run in
672parallel. The code waits for the first finger request to finish first, but 747parallel. The code waits for the first finger request to finish first, but
673that doesn't keep it from executing them parallel: when the first C<recv> 748that doesn't keep it from executing them parallel: when the first C<recv>
674call sees that the data isn't ready yet, it serves events for all three 749call sees that the data isn't ready yet, it serves events for all three
702How you implement it is a matter of taste - if you expect your function to 777How you implement it is a matter of taste - if you expect your function to
703be used mainly in an event-based program you would normally prefer to pass 778be used mainly in an event-based program you would normally prefer to pass
704a callback directly. If you write a module and expect your users to use 779a callback directly. If you write a module and expect your users to use
705it "synchronously" often (for example, a simple http-get script would not 780it "synchronously" often (for example, a simple http-get script would not
706really care much for events), then you would use a condition variable and 781really care much for events), then you would use a condition variable and
707tell them "simply ->recv the data". 782tell them "simply C<< ->recv >> the data".
708 783
709=head3 Problems with the implementation and how to fix them 784=head3 Problems with the implementation and how to fix them
710 785
711To make this example more real-world-ready, we would not only implement 786To make this example more real-world-ready, we would not only implement
712some write buffering (for the paranoid), but we would also have to handle 787some write buffering (for the paranoid, or maybe denial-of-service aware
713timeouts and maybe protocol errors. 788security expert), but we would also have to handle timeouts and maybe
789protocol errors.
714 790
715Doing this quickly gets unwieldy, which is why we introduce 791Doing this quickly gets unwieldy, which is why we introduce
716L<AnyEvent::Handle> in the next section, which takes care of all these 792L<AnyEvent::Handle> in the next section, which takes care of all these
717details for you and let's you concentrate on the actual protocol. 793details for you and let's you concentrate on the actual protocol.
718 794
719 795
720=head2 Implementing simple HTTP and HTTPS GET requests with AnyEvent::Handle 796=head2 Implementing simple HTTP and HTTPS GET requests with AnyEvent::Handle
721 797
722The L<AnyEvent::Handle> module has been hyped quite a bit so far, so let's 798The L<AnyEvent::Handle> module has been hyped quite a bit in this document
723see what it really offers. 799so far, so let's see what it really offers.
724 800
725As finger is such a simple protocol, let's try something slightly more 801As finger is such a simple protocol, let's try something slightly more
726complicated: HTTP/1.0. 802complicated: HTTP/1.0.
727 803
728An HTTP GET request works by sending a single request line that indicates 804An HTTP GET request works by sending a single request line that indicates
879of the headers to the server. 955of the headers to the server.
880 956
881The more interesting question is why the method is called C<push_write> 957The more interesting question is why the method is called C<push_write>
882and not just write. The reason is that you can I<always> add some write 958and not just write. The reason is that you can I<always> add some write
883data without blocking, and to do this, AnyEvent::Handle needs some write 959data without blocking, and to do this, AnyEvent::Handle needs some write
884queue internally - and C<push_write> simply pushes some data at the end of 960queue internally - and C<push_write> simply pushes some data onto the end
885that queue, just like Perl's C<push> pushes data at the end of an array. 961of that queue, just like Perl's C<push> pushes data onto the end of an
962array.
886 963
887The deeper reason is that at some point in the future, there might 964The deeper reason is that at some point in the future, there might
888be C<unshift_write> as well, and in any case, we will shortly meet 965be C<unshift_write> as well, and in any case, we will shortly meet
889C<push_read> and C<unshift_read>, and it's usually easiest if all those 966C<push_read> and C<unshift_read>, and it's usually easiest to remember if
890functions have some symmetry in their name. 967all those functions have some symmetry in their name.
891 968
892If C<push_write> is called with more than one argument, then you can even 969If C<push_write> is called with more than one argument, then you can even
893do I<formatted> I/O, which simply means your data will be transformed in 970do I<formatted> I/O, which simply means your data will be transformed in
894some ways. For example, this would JSON-encode your data before pushing it 971some ways. For example, this would JSON-encode your data before pushing it
895to the write queue: 972to the write queue:
897 $handle->push_write (json => [1, 2, 3]); 974 $handle->push_write (json => [1, 2, 3]);
898 975
899Apart from that, this pretty much summarises the write queue, there is 976Apart from that, this pretty much summarises the write queue, there is
900little else to it. 977little else to it.
901 978
902Reading the response if far more interesting: 979Reading the response is far more interesting, because it involves the more
980powerful and complex I<read queue>:
903 981
904=head3 The read queue 982=head3 The read queue
905 983
906The response consists of three parts: a single line of response status, a 984The response consists of three parts: a single line with the response
907single paragraph of headers ended by an empty line, and the request body, 985status, a single paragraph of headers ended by an empty line, and the
908which is simply the remaining data on that connection. 986request body, which is simply the remaining data on that connection.
909 987
910For the first two, we push two read requests onto the read queue: 988For the first two, we push two read requests onto the read queue:
911 989
912 # now fetch response status line 990 # now fetch response status line
913 $handle->push_read (line => sub { 991 $handle->push_read (line => sub {
919 $handle->push_read (line => "\015\012\015\012", sub { 997 $handle->push_read (line => "\015\012\015\012", sub {
920 my ($handle, $line) = @_; 998 my ($handle, $line) = @_;
921 $header = $line; 999 $header = $line;
922 }); 1000 });
923 1001
924While one can simply push a single callback to the queue, I<formatted> I/O 1002While one can simply push a single callback to parse the data the
925really comes to out advantage here, as there is a ready-made "read line" 1003queue, I<formatted> I/O really comes to our advantage here, as there
926read type. The first read expects a single line, ended by C<\015\012> (the 1004is a ready-made "read line" read type. The first read expects a single
927standard end-of-line marker in internet protocols). 1005line, ended by C<\015\012> (the standard end-of-line marker in internet
1006protocols).
928 1007
929The second "line" is actually a single paragraph - instead of reading it 1008The second "line" is actually a single paragraph - instead of reading it
930line by line we tell C<push_read> that the end-of-line marker is really 1009line by line we tell C<push_read> that the end-of-line marker is really
931C<\015\012\015\012>, which is an empty line. The result is that the whole 1010C<\015\012\015\012>, which is an empty line. The result is that the whole
932header paragraph will be treated as a single line and read. The word 1011header paragraph will be treated as a single line and read. The word
950header have been read. The C<on_read> callback could actually have been 1029header have been read. The C<on_read> callback could actually have been
951specified when constructing the object, but doing it this way preserves 1030specified when constructing the object, but doing it this way preserves
952logical ordering. 1031logical ordering.
953 1032
954The read callback simply adds the current read buffer to it's C<$body> 1033The read callback simply adds the current read buffer to it's C<$body>
955variable and, most importantly, I<empties> it by assign the empty string 1034variable and, most importantly, I<empties> the buffer by assigning the
956to it. 1035empty string to it.
957 1036
958After AnyEvent::Handle has been so instructed, it will now handle incoming 1037After AnyEvent::Handle has been so instructed, it will handle incoming
959data according to these instructions - if all goes well, the callback will 1038data according to these instructions - if all goes well, the callback will
960be invoked with the response data, if not, it will get an error. 1039be invoked with the response data, if not, it will get an error.
961 1040
962In general, you get pipelining very easy with AnyEvent::Handle: If 1041In general, you can implement pipelining (a semi-advanced feature of many
963you have a protocol with a request/response structure, your request 1042protocols) very easy with AnyEvent::Handle: If you have a protocol with a
964methods/functions will all look like this (simplified): 1043request/response structure, your request methods/functions will all look
1044like this (simplified):
965 1045
966 sub request { 1046 sub request {
967 1047
968 # send the request to the server 1048 # send the request to the server
969 $handle->push_write (...); 1049 $handle->push_write (...);
970 1050
971 # push some response handlers 1051 # push some response handlers
972 $handle->push_read (...); 1052 $handle->push_read (...);
973 } 1053 }
974 1054
975=head3 Using it 1055This means you can queue as many requests as you want, and while
1056AnyEvent::Handle goes through its read queue to handle the response data,
1057the other side can work on the next request - queueing the request just
1058appends some data to the write queue and installs a handler to be called
1059later.
976 1060
1061You might ask yourself how to handle decisions you can only make I<after>
1062you have received some data (such as handling a short error response or a
1063long and differently-formatted response). The answer to this problem is
1064C<unshift_read>, which we will introduce together with an example in the
1065coming sections.
1066
1067=head3 Using C<http_get>
1068
977And here is how you would use it: 1069Finally, here is how you would use C<http_get>:
978 1070
979 http_get "www.google.com", "/", sub { 1071 http_get "www.google.com", "/", sub {
980 my ($response, $header, $body) = @_; 1072 my ($response, $header, $body) = @_;
981 1073
982 print 1074 print
993correctly, let's change our C<http_get> function into a function that 1085correctly, let's change our C<http_get> function into a function that
994speaks HTTPS instead. 1086speaks HTTPS instead.
995 1087
996HTTPS is, quite simply, a standard TLS connection (B<T>ransport B<L>ayer 1088HTTPS is, quite simply, a standard TLS connection (B<T>ransport B<L>ayer
997B<S>ecurity is the official name for what most people refer to as C<SSL>) 1089B<S>ecurity is the official name for what most people refer to as C<SSL>)
998that contains standard HTTP protocol exchanges. The other difference to 1090that contains standard HTTP protocol exchanges. The only other difference
999HTTP is that it uses port C<443> instead of port C<80>. 1091to HTTP is that by default it uses port C<443> instead of port C<80>.
1000 1092
1001To implement these two differences we need two tiny changes, first, in the C<tcp_connect> call 1093To implement these two differences we need two tiny changes, first, in the
1002we replace C<http> by C<https>): 1094C<tcp_connect> call we replace C<http> by C<https>):
1003 1095
1004 tcp_connect $host, "https", sub { ... 1096 tcp_connect $host, "https", sub { ...
1005 1097
1006The other change deals with TLS, which is something L<AnyEvent::Handle> 1098The other change deals with TLS, which is something L<AnyEvent::Handle>
1007does for us, as long as I<you> made sure that the L<Net::SSLeay> module is 1099does for us, as long as I<you> made sure that the L<Net::SSLeay> module
1008around. To enable TLS with L<AnyEvent::Handle>, we simply pass an addition 1100is around. To enable TLS with L<AnyEvent::Handle>, we simply pass an
1009C<tls> parameter to the call to C<AnyEvent::Handle::new>: 1101additional C<tls> parameter to the call to C<AnyEvent::Handle::new>:
1010 1102
1011 tls => "connect", 1103 tls => "connect",
1012 1104
1013Specifying C<tls> enables TLS, and the argument specifies whether 1105Specifying C<tls> enables TLS, and the argument specifies whether
1014AnyEvent::Handle is the server side ("accept") or the client side 1106AnyEvent::Handle is the server side ("accept") or the client side
1015("connect") for the TLS connection, as unlike TCP, there is a clear 1107("connect") for the TLS connection, as unlike TCP, there is a clear
1016server/client relationship in TLS. 1108server/client relationship in TLS.
1017 1109
1110That's all.
1111
1018That's all. Of course, all this should be handled transparently by 1112Of course, all this should be handled transparently by C<http_get>
1019C<http_get> after parsing the URL. See the part about exercising your 1113after parsing the URL. If you need this, see the part about exercising
1020inspiration earlier in this document. 1114your inspiration earlier in this document. You could also use the
1115L<AnyEvent::HTTP> module from CPAN, which implements all this and works
1116around a lot of quirks for you, too.
1021 1117
1022=head3 The read queue - revisited 1118=head3 The read queue - revisited
1023 1119
1024HTTP always uses the same structure in its responses, but many protocols 1120HTTP always uses the same structure in its responses, but many protocols
1025require parsing responses different depending on the response itself. 1121require parsing responses differently depending on the response itself.
1026 1122
1027For example, in SMTP, you normally get a single response line: 1123For example, in SMTP, you normally get a single response line:
1028 1124
1029 220 mail.example.net Neverusesendmail 8.8.8 <mailme@example.net> 1125 220 mail.example.net Neverusesendmail 8.8.8 <mailme@example.net>
1030 1126
1033 220-mail.example.net Neverusesendmail 8.8.8 <mailme@example.net> 1129 220-mail.example.net Neverusesendmail 8.8.8 <mailme@example.net>
1034 220-hey guys 1130 220-hey guys
1035 220 my response is longer than yours 1131 220 my response is longer than yours
1036 1132
1037To handle this, we need C<unshift_read>. As the name (hopefully) implies, 1133To handle this, we need C<unshift_read>. As the name (hopefully) implies,
1038C<unshift_read> will not append your read request tot he end of the read 1134C<unshift_read> will not append your read request to the end of the read
1039queue, but instead it will prepend it to the queue. 1135queue, but instead it will prepend it to the queue.
1040 1136
1041This is useful for this this situation: You push your response-line read 1137This is useful in the situation above: Just push your response-line read
1042request when sending the SMTP command, and when handling it, you look at 1138request when sending the SMTP command, and when handling it, you look at
1043the line to see if more is to come, and C<unshift_read> another reader, 1139the line to see if more is to come, and C<unshift_read> another reader
1044like this: 1140callback if required, like this:
1045 1141
1046 my $response; # response lines end up in here 1142 my $response; # response lines end up in here
1047 1143
1048 my $read_response; $read_response = sub { 1144 my $read_response; $read_response = sub {
1049 my ($handle, $line) = @_; 1145 my ($handle, $line) = @_;
1067 1163
1068 $handle->push_read (line => $read_response); 1164 $handle->push_read (line => $read_response);
1069 1165
1070This recipe can be used for all similar parsing problems, for example in 1166This recipe can be used for all similar parsing problems, for example in
1071NNTP, the response code to some commands indicates that more data will be 1167NNTP, the response code to some commands indicates that more data will be
1072sent. 1168sent:
1073 1169
1074=head1 AUTHORS 1170 $handle->push_write ("article 42");
1171
1172 # read response line
1173 $handle->push_read (line => sub {
1174 my ($handle, $status) = @_;
1175
1176 # article data following?
1177 if ($status =~ /^2/) {
1178 # yes, read article body
1179
1180 $handle->unshift_read (line => "\012.\015\012", sub {
1181 my ($handle, $body) = @_;
1182
1183 $finish->($status, $body);
1184 });
1185
1186 } else {
1187 # some error occured, no article data
1188
1189 $finish->($status);
1190 }
1191 }
1192
1193=head3 Your own read queue handler
1194
1195Sometimes, your protocol doesn't play nice and uses lines or chunks of
1196data not formatted in a way handled by AnyEvent::Handle out of the box. In
1197this case you have to implement your own read parser.
1198
1199To make up a contorted example, imagine you are looking for an even
1200number of characters followed by a colon (":"). Also imagine that
1201AnyEvent::Handle had no C<regex> read type which could be used, so you'd
1202had to do it manually.
1203
1204To implement a read handler for this, you would C<push_read> (or
1205C<unshift_read>) just a single code reference.
1206
1207This code reference will then be called each time there is (new) data
1208available in the read buffer, and is expected to either successfully
1209eat/consume some of that data (and return true) or to return false to
1210indicate that it wants to be called again.
1211
1212If the code reference returns true, then it will be removed from the
1213read queue (because it has parsed/consumed whatever it was supposed to
1214consume), otherwise it stays in the front of it.
1215
1216The example above could be coded like this:
1217
1218 $handle->push_read (sub {
1219 my ($handle) = @_;
1220
1221 # check for even number of characters + ":"
1222 # and remove the data if a match is found.
1223 # if not, return false (actually nothing)
1224
1225 $handle->{rbuf} =~ s/^( (?:..)* ) ://x
1226 or return;
1227
1228 # we got some data in $1, pass it to whoever wants it
1229 $finish->($1);
1230
1231 # and return true to indicate we are done
1232 1
1233 });
1234
1235This concludes our little tutorial.
1236
1237=head1 Where to go from here?
1238
1239This introduction should have explained the key concepts of L<AnyEvent>
1240- event watchers and condition variables, L<AnyEvent::Socket> - basic
1241networking utilities, and L<AnyEvent::Handle> - a nice wrapper around
1242handles.
1243
1244You could either start coding stuff right away, look at those manual
1245pages for the gory details, or roam CPAN for other AnyEvent modules (such
1246as L<AnyEvent::IRC> or L<AnyEvent::HTTP>) to see more code examples (or
1247simply to use them).
1248
1249If you need a protocol that doesn't have an implementation using AnyEvent,
1250remember that you can mix AnyEvent with one other event framework, such as
1251L<POE>, so you can always use AnyEvent for your own tasks plus modules of
1252one other event framework to fill any gaps.
1253
1254And last not least, you could also look at L<Coro>, especially
1255L<Coro::AnyEvent>, to see how you can turn event-based programming from
1256callback style back to the usual imperative style (also called "inversion
1257of control" - AnyEvent calls I<you>, but Coro lets I<you> call AnyEvent).
1258
1259=head1 Authors
1075 1260
1076Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. 1261Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>.
1077 1262

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines