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.21 by root, Sat Jun 6 12:04:30 2009 UTC vs.
Revision 1.22 by root, Mon Jun 29 20:55:58 2009 UTC

108 ); 108 );
109 109
110 # do something else here 110 # do something else here
111 111
112Looks more complicated, and surely is, but the advantage of using events 112Looks more complicated, and surely is, but the advantage of using events
113is 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
114input. Waiting as in the first example is also called "blocking" because 118Waiting as done in the first example is also called "blocking" the process
115you "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.
116 121
117The second example avoids blocking, by only registering interest in a read 122The second example avoids blocking by only registering interest in a read
118event, 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
119is 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
120the data. 125the data.
121 126
122The "interest" is represented by an object returned by C<< AnyEvent->io 127The "interest" is represented by an object returned by C<< AnyEvent->io
123>> called a "watcher" object - called like that because it "watches" your 128>> called a "watcher" object - called like that because it "watches" your
124file 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.
125 130
126In 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<<
127AnyEvent->io >> method. Disinterest in some event is simply expressed by 132AnyEvent->io >> method. Disinterest in some event is simply expressed
128forgetting 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
129is 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
130longer 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
131them anywhere. 136longer use them anywhere.
132 137
133=head3 A short note on callbacks 138=head3 A short note on callbacks
134 139
135A common issue that hits people is the problem of passing parameters 140A common issue that hits people is the problem of passing parameters
136to callbacks. Programmers used to languages such as C or C++ are often 141to callbacks. Programmers used to languages such as C or C++ are often
153is also an abstraction penalty to pay as one has to I<name> the callback, 158is also an abstraction penalty to pay as one has to I<name> the callback,
154which often is unnecessary and leads to nonsensical or duplicated names. 159which often is unnecessary and leads to nonsensical or duplicated names.
155 160
156In Perl, one can specify behaviour much more directly by using 161In Perl, one can specify behaviour much more directly by using
157I<closures>. Closures are code blocks that take a reference to the 162I<closures>. Closures are code blocks that take a reference to the
158enclosing scope(s) when they are created. This means lexical variables in scope at the time 163enclosing scope(s) when they are created. This means lexical variables in
159of creating the closure can simply be used inside the closure: 164scope at the time of creating the closure can simply be used inside the
165closure:
160 166
161 my $arg = ...; 167 my $arg = ...;
162 168
163 call_me_back_later sub { $arg->method }; 169 call_me_back_later sub { $arg->method };
164 170
165Under most circumstances, closures are faster, use less resources and 171Under most circumstances, closures are faster, use fewer resources and
166result in much clearer code then the traditional approach. Faster, 172result in much clearer code then the traditional approach. Faster,
167because parameter passing and storing them in local variables in Perl 173because parameter passing and storing them in local variables in Perl
168is relatively slow. Less resources, because closures take references to 174is relatively slow. Fewer resources, because closures take references
169existing variables without having to create new ones, and clearer code 175to existing variables without having to create new ones, and clearer
170because it is immediately obvious that the second example calls the 176code because it is immediately obvious that the second example calls the
171C<method> method when the callback is invoked. 177C<method> method when the callback is invoked.
172 178
173Apart from these, the strongest argument for using closures with AnyEvent 179Apart from these, the strongest argument for using closures with AnyEvent
174is that AnyEvent does not allow passing parameters to the callback, so 180is that AnyEvent does not allow passing parameters to the callback, so
175closures are the only way to achieve that in most cases :-> 181closures are the only way to achieve that in most cases :->
176 182
177 183
178=head3 A hint on debugging 184=head3 A hint on debugging
179 185
180AnyEvent does, by default, not do any argument checking. This can lead to 186AnyEvent does, by default, not do any argument checking. This can lead to
181strange and unexpected results especially if you are trying to learn yur 187strange and unexpected results especially if you are trying to learn your
182ways with AnyEvent. 188ways with AnyEvent.
183 189
184AnyEvent supports a special "strict" mode, off by default, which does very 190AnyEvent supports a special "strict" mode, off by default, which does very
185strict argument checking, at the expense of being somewhat slower. When 191strict argument checking, at the expense of being somewhat slower. During
186developing, however, this mode is very useful. 192development, however, this mode is very useful.
187 193
188You can enable this strict mode either by having an environment variable 194You can enable this strict mode either by having an environment variable
189C<PERL_ANYEVENT_STRICT> with a true value in your environment: 195C<PERL_ANYEVENT_STRICT> with a true value in your environment:
190 196
191 PERL_ANYEVENT_STRICT=1 perl test.pl 197 PERL_ANYEVENT_STRICT=1 perl test.pl
194same effect (do not do this in production, however). 200same effect (do not do this in production, however).
195 201
196 202
197=head2 Condition Variables 203=head2 Condition Variables
198 204
199Back to the I/O watcher example: The code not yet a fully working program, 205Back to the I/O watcher example: The code is not yet a fully working
200and will not work as-is. The reason is that your callback will not be 206program, and will not work as-is. The reason is that your callback will
201invoked out of the blue, you have to run the event loop. Also, event-based 207not be invoked out of the blue, you have to run the event loop. Also,
202programs sometimes have to block, too, as when there simply is nothing 208event-based programs sometimes have to block, too, as when there simply is
203else to do and everything waits for some events, it needs to block the 209nothing else to do and everything waits for some events, it needs to block
204process as well. 210the process as well until new events arrive.
205 211
206In AnyEvent, this is done using condition variables. Condition variables 212In AnyEvent, this is done using condition variables. Condition variables
207are named "condition variables" because they represent a condition that is 213are named "condition variables" because they represent a condition that is
208initially false and needs to be fulfilled. 214initially false and needs to be fulfilled.
209 215
246 print "your name is $name\n"; 252 print "your name is $name\n";
247 253
248This program creates an AnyEvent condvar by calling the C<< 254This program creates an AnyEvent condvar by calling the C<<
249AnyEvent->condvar >> method. It then creates a watcher as usual, but 255AnyEvent->condvar >> method. It then creates a watcher as usual, but
250inside 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,
251which causes anybody waiting on it to continue. 257which causes whoever is waiting on it to continue.
252 258
253The "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<<
254$name_ready->recv >>: The producer calls C<send>, the consumer calls 260$name_ready->recv >>: The producer calls C<send>, the consumer calls
255C<recv>. 261C<recv>.
256 262
257If 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<<
258$name_ready->recv >> will halt your program until the condition becomes 264$name_ready->recv >> will halt your program until the condition becomes
340This 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
341to 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
342worked. 348worked.
343 349
344Admittedly, 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
345form standard input in a Gtk+ application. But imagine that instead of 351from standard input in a Gtk+ application. But imagine that instead of
346doing 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
347it'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
348http-requests in parallel in your program and still provide feedback to 354http-requests in parallel in your program and still provide feedback to
349the user and stay interactive. 355the user and stay interactive.
350 356
351In 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
352HTTP request, on our own, with the utility modules AnyEvent comes with. 358HTTP request, on our own, with the utility modules AnyEvent comes with.
353 359
354Before 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
355program with using only AnyEvent, without ever calling some other event 361program with using only AnyEvent, without ever calling some other event
356loop's run function. 362loop's run function.
357 363
358In the example using condition variables, we used that, and in fact, this 364In the example using condition variables, we used those to start waiting
359is the solution: 365for events, and in fact, condition variables are the solution:
360 366
361 my $quit_program = AnyEvent->condvar; 367 my $quit_program = AnyEvent->condvar;
362 368
363 # create AnyEvent watchers (or not) here 369 # create AnyEvent watchers (or not) here
364 370
365 $quit_program->recv; 371 $quit_program->recv;
366 372
367If 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<<
368C<< $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
369simply 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.
370in a long-running daemon program). 377in a long-running daemon program).
371 378
372In 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:
373 381
374 AnyEvent->condvar->recv; 382 AnyEvent->condvar->recv;
375 383
376And 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
377AnyEvent offers. 385AnyEvent offers.
409 417
410 # now wait till our time has come 418 # now wait till our time has come
411 $cv->recv; 419 $cv->recv;
412 420
413Unlike 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
414they 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,
415invoke your callback. 423AnyEvent will invoke your callback.
416 424
417Unlike 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
418is data available, timers are one-shot: after they have "fired" once and 426is data available, timers are normally one-shot: after they have "fired"
419invoked your callback, they are dead and no longer do anything. 427once and invoked your callback, they are dead and no longer do anything.
420 428
421To 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,
422you have to recreate it: 430you can specify an C<interval> parameter:
423 431
424 use AnyEvent; 432 my $once_per_second = AnyEvent->timer (
425 433 after => 0, # first invoke ASAP
426 my $time_watcher; 434 interval => 1, # then invoke every second
427 435 cb => sub { # the callback to invoke
428 sub once_per_second { 436 $cv->send;
429 print "tick\n";
430 437 },
431 # (re-)create the watcher
432 $time_watcher = AnyEvent->timer (
433 after => 1,
434 cb => \&once_per_second,
435 ); 438 );
436 }
437
438 # now start the timer
439 once_per_second;
440
441Having to recreate your timer is a restriction put on AnyEvent that is
442present in most event libraries it uses. It is so annoying that some
443future version might work around this limitation, but right now, it's the
444only way to do repeating timers.
445
446Fortunately most timers aren't really repeating but specify timeouts of
447some sort.
448 439
449=head3 More esoteric sources 440=head3 More esoteric sources
450 441
451AnyEvent also has some other, more esoteric event sources you can tap 442AnyEvent also has some other, more esoteric event sources you can tap
452into: signal and child watchers. 443into: signal, child and idle watchers.
453 444
454Signal watchers can be used to wait for "signal events", which simply 445Signal watchers can be used to wait for "signal events", which simply
455means 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>).
456 447
457Process 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
458you 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
459wait for that by blocking. 450do not wait for that by blocking.
460 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
461Both 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>
462page. 459manual page.
463 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.
464 467
465=head1 Network programming and AnyEvent 468=head1 Network programming and AnyEvent
466 469
467So 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.
468 471
469This 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
470all 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
471buffering again and again becomes tedious, not to mention that it attracts 474I/O buffering again and again becomes tedious, not to mention that it
472errors. 475attracts errors.
473 476
474While the core L<AnyEvent> module is still small and self-contained, 477While the core L<AnyEvent> module is still small and self-contained,
475the distribution comes with some very useful utility modules such as 478the distribution comes with some very useful utility modules such as
476L<AnyEvent::Handle>, L<AnyEvent::DNS> and L<AnyEvent::Socket>. These can 479L<AnyEvent::Handle>, L<AnyEvent::DNS> and L<AnyEvent::Socket>. These can
477make your life as non-blocking network programmer a lot easier. 480make your life as non-blocking network programmer a lot easier.
485a 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
486IP addresses for log files. 489IP addresses for log files.
487 490
488=head2 L<AnyEvent::Handle> 491=head2 L<AnyEvent::Handle>
489 492
490This module handles non-blocking IO on file handles in an event based 493This module handles non-blocking IO on (socket-, pipe- etc.) file handles
491manner. It provides a wrapper object around your file handle that provides 494in an event based manner. It provides a wrapper object around your file
492queueing and buffering of incoming and outgoing data for you. 495handle that provides queueing and buffering of incoming and outgoing data
496for you.
493 497
494It 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
495fixed and variable-width data blocks. 499fixed and variable-width data blocks.
496 500
497=head2 L<AnyEvent::Socket> 501=head2 L<AnyEvent::Socket>
525It 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
526single 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
527specified by that user. OK, RFC 1288 specifies a vastly more complex 531specified by that user. OK, RFC 1288 specifies a vastly more complex
528protocol, but it basically boils down to this: 532protocol, but it basically boils down to this:
529 533
530 # telnet idsoftware.com finger 534 # telnet kernel.org finger
531 Trying 192.246.40.37... 535 Trying 204.152.191.37...
532 Connected to idsoftware.com (192.246.40.37). 536 Connected to kernel.org (204.152.191.37).
533 Escape character is '^]'. 537 Escape character is '^]'.
534 johnc 538
535 Welcome to id Software's Finger Service V1.5! 539 The latest stable version of the Linux kernel is: [...]
536
537 [...]
538 Now on the web:
539 [...]
540
541 Connection closed by foreign host. 540 Connection closed by foreign host.
542 541
543"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:
544daemon still works, so let's write a little AnyEvent function that makes a
545finger request:
546 543
547 use AnyEvent; 544 use AnyEvent;
548 use AnyEvent::Socket; 545 use AnyEvent::Socket;
549 546
550 sub finger($$) { 547 sub finger($$) {
615socket handle as first argument, otherwise, nothing will be passed to our 612socket handle as first argument, otherwise, nothing will be passed to our
616callback. 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
617the outcome of the TCP connect is known. 614the outcome of the TCP connect is known.
618 615
619This style of programming is also called "continuation style": the 616This style of programming is also called "continuation style": the
620"continuation" is simply the way the program continues - normally, a 617"continuation" is simply the way the program continues - normally at the
621program continues at the next line after some statement (the exception 618next line after some statement (the exception is loops or things like
622is loops or things like C<return>). When we are interested in events, 619C<return>). When we are interested in events, however, we instead specify
623however, we instead specify the "continuation" of our program by passing a 620the "continuation" of our program by passing a closure, which makes that
624closure, which makes that closure the "continuation" of the program. The 621closure the "continuation" of the program.
622
625C<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
626established or it failed, continue there". 624connection is established or it failed, continue there".
627 625
628Now let's look at the callback/closure in more detail: 626Now let's look at the callback/closure in more detail:
629 627
630 # the callback receives the socket handle - or nothing 628 # the callback receives the socket handle - or nothing
631 my ($fh) = @_ 629 my ($fh) = @_
643report 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>
644function, and most event loops continue even after a C<die>! 642function, and most event loops continue even after a C<die>!
645 643
646This 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
647any arguments to signal to the condvar consumer that something bad has 645any arguments to signal to the condvar consumer that something bad has
648happened. The return value of C<< $cv->send >> is irrelevant, as is the 646happened. The return value of C<< $cv->send >> is irrelevant, as is
649return value of our callback. The return statement is simply used for the 647the return value of our callback. The C<return> statement is simply
650side effect of, well, returning immediately from the callback. Checking 648used for the side effect of, well, returning immediately from the
651for 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,
652compact idiom is so handy. 650which is why this compact idiom is so handy.
653 651
654As 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
655finger 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):
656 656
657 syswrite $fh, "$user\015\012"; 657 syswrite $fh, "$user\015\012";
658 658
659Note that this isn't 100% clean socket programming - the socket could, 659Note that this isn't 100% clean socket programming - the socket could,
660for whatever reasons, not accept our data. When writing a small amount 660for whatever reasons, not accept our data. When writing a small amount
678variable, 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
679destroy the variable and its contents, which would in turn unregister our 679destroy the variable and its contents, which would in turn unregister our
680watcher. 680watcher.
681 681
682To 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
683means that, when the C<tcp_connect> callback returns, that perl thinks 683means that, when the C<tcp_connect> callback returns, perl thinks (quite
684(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,
685callback. 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).
686 688
687The trick, however, is that instead of: 689The trick, however, is that instead of:
688 690
689 my $read_watcher = AnyEvent->io (... 691 my $read_watcher = AnyEvent->io (...
690 692
709 my $len = sysread $fh, $response, 1024, length $response; 711 my $len = sysread $fh, $response, 1024, length $response;
710 712
711 if ($len <= 0) { 713 if ($len <= 0) {
712 714
713Note 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,
714by 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
715example. 717example.
716 718
717When C<sysread> indicates we are done, the callback C<undef>ines 719When C<sysread> indicates we are done, the callback C<undef>ines
718the 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
719variable. All this has the following effects: 721variable. All this has the following effects:
733But 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
734the background, we even can run multiple sessions in parallel, like this: 736the background, we even can run multiple sessions in parallel, like this:
735 737
736 my $f1 = finger "trouble", "noc.dfn.de"; # check for trouble tickets 738 my $f1 = finger "trouble", "noc.dfn.de"; # check for trouble tickets
737 my $f2 = finger "1736" , "noc.dfn.de"; # fetch ticket 1736 739 my $f2 = finger "1736" , "noc.dfn.de"; # fetch ticket 1736
738 my $f3 = finger "johnc", "idsoftware.com"; # finger john 740 my $f3 = finger "hpa" , "kernel.org"; # finger hpa
739 741
740 print "trouble tickets:\n", $f1->recv, "\n"; 742 print "trouble tickets:\n" , $f1->recv, "\n";
741 print "trouble ticket #1736:\n", $f2->recv, "\n"; 743 print "trouble ticket #1736:\n", $f2->recv, "\n";
742 print "john carmacks finger file: ", $f3->recv, "\n"; 744 print "kernel release info: " , $f3->recv, "\n";
743 745
744It 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
745parallel. 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
746that 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>
747call 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
775How 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
776be 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
777a 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
778it "synchronously" often (for example, a simple http-get script would not 780it "synchronously" often (for example, a simple http-get script would not
779really 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
780tell them "simply ->recv the data". 782tell them "simply C<< ->recv >> the data".
781 783
782=head3 Problems with the implementation and how to fix them 784=head3 Problems with the implementation and how to fix them
783 785
784To 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
785some write buffering (for the paranoid), but we would also have to handle 787some write buffering (for the paranoid, or maybe denial-of-service aware
786timeouts and maybe protocol errors. 788security expert), but we would also have to handle timeouts and maybe
789protocol errors.
787 790
788Doing this quickly gets unwieldy, which is why we introduce 791Doing this quickly gets unwieldy, which is why we introduce
789L<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
790details for you and let's you concentrate on the actual protocol. 793details for you and let's you concentrate on the actual protocol.
791 794
792 795
793=head2 Implementing simple HTTP and HTTPS GET requests with AnyEvent::Handle 796=head2 Implementing simple HTTP and HTTPS GET requests with AnyEvent::Handle
794 797
795The 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
796see what it really offers. 799so far, so let's see what it really offers.
797 800
798As 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
799complicated: HTTP/1.0. 802complicated: HTTP/1.0.
800 803
801An 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
952of the headers to the server. 955of the headers to the server.
953 956
954The 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>
955and 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
956data without blocking, and to do this, AnyEvent::Handle needs some write 959data without blocking, and to do this, AnyEvent::Handle needs some write
957queue 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
958that 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.
959 963
960The 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
961be 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
962C<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
963functions have some symmetry in their name. 967all those functions have some symmetry in their name.
964 968
965If 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
966do 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
967some 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
968to the write queue: 972to the write queue:
970 $handle->push_write (json => [1, 2, 3]); 974 $handle->push_write (json => [1, 2, 3]);
971 975
972Apart from that, this pretty much summarises the write queue, there is 976Apart from that, this pretty much summarises the write queue, there is
973little else to it. 977little else to it.
974 978
975Reading the response if far more interesting: 979Reading the response is far more interesting, because it involves the more
980powerful and complex I<read queue>:
976 981
977=head3 The read queue 982=head3 The read queue
978 983
979The response consists of three parts: a single line of response status, a 984The response consists of three parts: a single line with the response
980single 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
981which is simply the remaining data on that connection. 986request body, which is simply the remaining data on that connection.
982 987
983For 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:
984 989
985 # now fetch response status line 990 # now fetch response status line
986 $handle->push_read (line => sub { 991 $handle->push_read (line => sub {
992 $handle->push_read (line => "\015\012\015\012", sub { 997 $handle->push_read (line => "\015\012\015\012", sub {
993 my ($handle, $line) = @_; 998 my ($handle, $line) = @_;
994 $header = $line; 999 $header = $line;
995 }); 1000 });
996 1001
997While 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
998really 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
999read 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
1000standard end-of-line marker in internet protocols). 1005line, ended by C<\015\012> (the standard end-of-line marker in internet
1006protocols).
1001 1007
1002The second "line" is actually a single paragraph - instead of reading it 1008The second "line" is actually a single paragraph - instead of reading it
1003line 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
1004C<\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
1005header 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
1023header 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
1024specified when constructing the object, but doing it this way preserves 1030specified when constructing the object, but doing it this way preserves
1025logical ordering. 1031logical ordering.
1026 1032
1027The 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>
1028variable and, most importantly, I<empties> it by assign the empty string 1034variable and, most importantly, I<empties> the buffer by assigning the
1029to it. 1035empty string to it.
1030 1036
1031After AnyEvent::Handle has been so instructed, it will now handle incoming 1037After AnyEvent::Handle has been so instructed, it will handle incoming
1032data according to these instructions - if all goes well, the callback will 1038data according to these instructions - if all goes well, the callback will
1033be 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.
1034 1040
1035In general, you get pipelining very easy with AnyEvent::Handle: If 1041In general, you can implement pipelining (a semi-advanced feature of many
1036you have a protocol with a request/response structure, your request 1042protocols) very easy with AnyEvent::Handle: If you have a protocol with a
1037methods/functions will all look like this (simplified): 1043request/response structure, your request methods/functions will all look
1044like this (simplified):
1038 1045
1039 sub request { 1046 sub request {
1040 1047
1041 # send the request to the server 1048 # send the request to the server
1042 $handle->push_write (...); 1049 $handle->push_write (...);
1043 1050
1044 # push some response handlers 1051 # push some response handlers
1045 $handle->push_read (...); 1052 $handle->push_read (...);
1046 } 1053 }
1047 1054
1048=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.
1049 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
1050And here is how you would use it: 1069Finally, here is how you would use C<http_get>:
1051 1070
1052 http_get "www.google.com", "/", sub { 1071 http_get "www.google.com", "/", sub {
1053 my ($response, $header, $body) = @_; 1072 my ($response, $header, $body) = @_;
1054 1073
1055 print 1074 print
1066correctly, 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
1067speaks HTTPS instead. 1086speaks HTTPS instead.
1068 1087
1069HTTPS 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
1070B<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>)
1071that contains standard HTTP protocol exchanges. The other difference to 1090that contains standard HTTP protocol exchanges. The only other difference
1072HTTP 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>.
1073 1092
1074To 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
1075we replace C<http> by C<https>): 1094C<tcp_connect> call we replace C<http> by C<https>):
1076 1095
1077 tcp_connect $host, "https", sub { ... 1096 tcp_connect $host, "https", sub { ...
1078 1097
1079The other change deals with TLS, which is something L<AnyEvent::Handle> 1098The other change deals with TLS, which is something L<AnyEvent::Handle>
1080does 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
1081around. 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
1082C<tls> parameter to the call to C<AnyEvent::Handle::new>: 1101additional C<tls> parameter to the call to C<AnyEvent::Handle::new>:
1083 1102
1084 tls => "connect", 1103 tls => "connect",
1085 1104
1086Specifying C<tls> enables TLS, and the argument specifies whether 1105Specifying C<tls> enables TLS, and the argument specifies whether
1087AnyEvent::Handle is the server side ("accept") or the client side 1106AnyEvent::Handle is the server side ("accept") or the client side
1088("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
1089server/client relationship in TLS. 1108server/client relationship in TLS.
1090 1109
1091That's all. 1110That's all.
1092 1111
1093Of course, all this should be handled transparently by C<http_get> after 1112Of course, all this should be handled transparently by C<http_get>
1094parsing the URL. See the part about exercising your inspiration earlier in 1113after parsing the URL. If you need this, see the part about exercising
1095this 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.
1096 1117
1097=head3 The read queue - revisited 1118=head3 The read queue - revisited
1098 1119
1099HTTP always uses the same structure in its responses, but many protocols 1120HTTP always uses the same structure in its responses, but many protocols
1100require parsing responses different depending on the response itself. 1121require parsing responses differently depending on the response itself.
1101 1122
1102For example, in SMTP, you normally get a single response line: 1123For example, in SMTP, you normally get a single response line:
1103 1124
1104 220 mail.example.net Neverusesendmail 8.8.8 <mailme@example.net> 1125 220 mail.example.net Neverusesendmail 8.8.8 <mailme@example.net>
1105 1126
1108 220-mail.example.net Neverusesendmail 8.8.8 <mailme@example.net> 1129 220-mail.example.net Neverusesendmail 8.8.8 <mailme@example.net>
1109 220-hey guys 1130 220-hey guys
1110 220 my response is longer than yours 1131 220 my response is longer than yours
1111 1132
1112To 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,
1113C<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
1114queue, but instead it will prepend it to the queue. 1135queue, but instead it will prepend it to the queue.
1115 1136
1116This 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
1117request 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
1118the 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
1119like this: 1140callback if required, like this:
1120 1141
1121 my $response; # response lines end up in here 1142 my $response; # response lines end up in here
1122 1143
1123 my $read_response; $read_response = sub { 1144 my $read_response; $read_response = sub {
1124 my ($handle, $line) = @_; 1145 my ($handle, $line) = @_;
1170 } 1191 }
1171 1192
1172=head3 Your own read queue handler 1193=head3 Your own read queue handler
1173 1194
1174Sometimes, your protocol doesn't play nice and uses lines or chunks of 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
1175data, in which case you have to implement your own read parser. 1197this case you have to implement your own read parser.
1176 1198
1177To make up a contorted example, imagine you are looking for an even 1199To make up a contorted example, imagine you are looking for an even
1178number of characters followed by a colon (":"). Also imagine that 1200number of characters followed by a colon (":"). Also imagine that
1179AnyEvent::Handle had no C<regex> read type which could be used, so you'd 1201AnyEvent::Handle had no C<regex> read type which could be used, so you'd
1180had to do it manually. 1202had to do it manually.
1181 1203
1182To implement this, you would C<push_read> (or C<unshift_read>) just a 1204To implement a read handler for this, you would C<push_read> (or
1183single code reference. 1205C<unshift_read>) just a single code reference.
1184 1206
1185This code reference will then be called each time there is (new) data 1207This code reference will then be called each time there is (new) data
1186available in the read buffer, and is expected to either eat/consume some 1208available in the read buffer, and is expected to either successfully
1187of that data (and return true) or to return false to indicate that it 1209eat/consume some of that data (and return true) or to return false to
1188wants to be called again. 1210indicate that it wants to be called again.
1189 1211
1190If the code reference returns true, then it will be removed from the read 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
1191queue, otherwise it stays in front of it. 1214consume), otherwise it stays in the front of it.
1192 1215
1193The example above could be coded like this: 1216The example above could be coded like this:
1194 1217
1195 $handle->push_read (sub { 1218 $handle->push_read (sub {
1196 my ($handle) = @_; 1219 my ($handle) = @_;
1207 1230
1208 # and return true to indicate we are done 1231 # and return true to indicate we are done
1209 1 1232 1
1210 }); 1233 });
1211 1234
1235This concludes our little tutorial.
1236
1237=head1 Where to go from here?
1238
1239This introduction should have explained the key concepts between
1240L<AnyEvent>, namely event watchers and condition variables,
1241L<AnyEvent::Socket>, for your basic networking needs, and
1242L<AnyEvent::Handle>, a nice wrapper around handles.
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).
1212 1258
1213=head1 Authors 1259=head1 Authors
1214 1260
1215Robin 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>.
1216 1262

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines