… | |
… | |
754 | The C<GET ...> and the empty line were entered manually, the rest of the |
754 | The C<GET ...> and the empty line were entered manually, the rest of the |
755 | telnet output is google's response, in which case a C<404 not found> one. |
755 | telnet output is google's response, in which case a C<404 not found> one. |
756 | |
756 | |
757 | So, here is how you would do it with C<AnyEvent::Handle>: |
757 | So, here is how you would do it with C<AnyEvent::Handle>: |
758 | |
758 | |
759 | ###TODO |
759 | sub http_get { |
|
|
760 | my ($host, $uri, $cb) = @_; |
|
|
761 | |
|
|
762 | AnyEvent::Socket::tcp_connect $host, "http", sub { |
|
|
763 | my ($fh) = @_ |
|
|
764 | or $cb->("HTTP/1.0 500 $!"); |
|
|
765 | |
|
|
766 | # store results here |
|
|
767 | my ($response, $header, $body); |
|
|
768 | |
|
|
769 | my $handle; $handle = new AnyEvent::Handle |
|
|
770 | fh => $fh, |
|
|
771 | on_error => sub { |
|
|
772 | undef $handle; |
|
|
773 | $cb->("HTTP/1.0 500 $!"); |
|
|
774 | }, |
|
|
775 | on_eof => sub { |
|
|
776 | undef $handle; # keep it alive till eof |
|
|
777 | $cb->($response, $header, $body); |
|
|
778 | }; |
|
|
779 | |
|
|
780 | $handle->push_write ("GET $uri HTTP/1.0\015\012\015\012"); |
|
|
781 | |
|
|
782 | # now fetch response status line |
|
|
783 | $handle->push_read (line => sub { |
|
|
784 | my ($handle, $line) = @_; |
|
|
785 | $response = $line; |
|
|
786 | }); |
|
|
787 | |
|
|
788 | # then the headers |
|
|
789 | $handle->push_read (line => "\015\012\015\012", sub { |
|
|
790 | my ($handle, $line) = @_; |
|
|
791 | $header = $line; |
|
|
792 | }); |
|
|
793 | |
|
|
794 | # and finally handle any remaining data as body |
|
|
795 | $handle->on_read (sub { |
|
|
796 | $body .= $_[0]->rbuf; |
|
|
797 | $_[0]->rbuf = ""; |
|
|
798 | }); |
|
|
799 | }; |
|
|
800 | } |
760 | |
801 | |
761 | And now let's go through it step by step. First, as usual, the overall |
802 | And now let's go through it step by step. First, as usual, the overall |
762 | C<http_get> function structure: |
803 | C<http_get> function structure: |
763 | |
804 | |
764 | sub http_get { |
805 | sub http_get { |
… | |
… | |
860 | |
901 | |
861 | Reading the response if far more interesting: |
902 | Reading the response if far more interesting: |
862 | |
903 | |
863 | =head3 The read queue |
904 | =head3 The read queue |
864 | |
905 | |
865 | the response consists of three parts: a single line of response status, a |
906 | The response consists of three parts: a single line of response status, a |
866 | single paragraph of headers ended by an empty line, and the request body, |
907 | single paragraph of headers ended by an empty line, and the request body, |
867 | which is simply the remaining data on that connection. |
908 | which is simply the remaining data on that connection. |
868 | |
909 | |
869 | For the first two, we push two read requests onto the read queue: |
910 | For the first two, we push two read requests onto the read queue: |
870 | |
911 | |
… | |
… | |
904 | $_[0]->rbuf = ""; |
945 | $_[0]->rbuf = ""; |
905 | }); |
946 | }); |
906 | |
947 | |
907 | This callback is invoked every time data arrives and the read queue is |
948 | This callback is invoked every time data arrives and the read queue is |
908 | empty - which in this example will only be the case when both response and |
949 | empty - which in this example will only be the case when both response and |
909 | header have been read. |
950 | header have been read. The C<on_read> callback could actually have been |
|
|
951 | specified when constructing the object, but doing it this way preserves |
|
|
952 | logical ordering. |
910 | |
953 | |
|
|
954 | The read callback simply adds the current read buffer to it's C<$body> |
|
|
955 | variable and, most importantly, I<empties> it by assign the empty string |
|
|
956 | to it. |
911 | |
957 | |
912 | ############################################################################# |
958 | After AnyEvent::Handle has been so instructed, it will now handle incoming |
|
|
959 | data according to these instructions - if all goes well, the callback will |
|
|
960 | be invoked with the response data, if not, it will get an error. |
913 | |
961 | |
914 | Now let's start with something simple: a program that reads from standard |
962 | =head3 Using it |
915 | input in a non-blocking way, that is, in a way that lets your program do |
|
|
916 | other things while it is waiting for input. |
|
|
917 | |
963 | |
918 | First, the full program listing: |
964 | And here is how you would use it: |
919 | |
965 | |
920 | #!/usr/bin/perl |
966 | http_get "www.google.com", "/", sub { |
|
|
967 | my ($response, $header, $body) = @_; |
921 | |
968 | |
922 | use AnyEvent; |
969 | print |
923 | use AnyEvent::Handle; |
970 | $response, "\n", |
924 | |
971 | $body; |
925 | my $end_prog = AnyEvent->condvar; |
|
|
926 | |
|
|
927 | my $handle = |
|
|
928 | AnyEvent::Handle->new ( |
|
|
929 | fh => \*STDIN, |
|
|
930 | on_eof => sub { |
|
|
931 | print "received EOF, exiting...\n"; |
|
|
932 | $end_prog->broadcast; |
|
|
933 | }, |
|
|
934 | on_error => sub { |
|
|
935 | print "error while reading from STDIN: $!\n"; |
|
|
936 | $end_prog->broadcast; |
|
|
937 | } |
|
|
938 | ); |
|
|
939 | |
|
|
940 | $handle->push_read (sub { |
|
|
941 | my ($handle) = @_; |
|
|
942 | |
|
|
943 | if ($handle->rbuf =~ s/^.*?\bend\b.*$//s) { |
|
|
944 | print "got 'end', existing...\n"; |
|
|
945 | $end_prog->broadcast; |
|
|
946 | return 1 |
|
|
947 | } |
|
|
948 | |
|
|
949 | 0 |
|
|
950 | }); |
972 | }; |
951 | |
973 | |
952 | $end_prog->recv; |
974 | And of course, you can run as many of these requests in parallel as you |
|
|
975 | want (and your memory supports). |
953 | |
976 | |
954 | That's a mouthful, so let's go through it step by step: |
977 | =head3 The read queue - revisited |
955 | |
978 | |
956 | #!/usr/bin/perl |
979 | ###TODO |
957 | |
980 | |
958 | use AnyEvent; |
|
|
959 | use AnyEvent::Handle; |
|
|
960 | |
|
|
961 | Nothing unexpected here, just load AnyEvent for the event functionality |
|
|
962 | and AnyEvent::Handle for your file handling needs. |
|
|
963 | |
|
|
964 | my $end_prog = AnyEvent->condvar; |
|
|
965 | |
|
|
966 | Here the program creates a so-called 'condition variable': Condition |
|
|
967 | variables are a great way to signal the completion of some event, or to |
|
|
968 | state that some condition became true (thus the name). |
|
|
969 | |
|
|
970 | This condition variable represents the condition that the program wants to |
|
|
971 | terminate. Later in the program, we will 'recv' that condition (call the |
|
|
972 | C<recv> method on it), which will wait until the condition gets signalled |
|
|
973 | (which is done by calling the C<send> method on it). |
|
|
974 | |
|
|
975 | The next step is to create the handle object: |
|
|
976 | |
|
|
977 | my $handle = |
|
|
978 | AnyEvent::Handle->new ( |
|
|
979 | fh => \*STDIN, |
|
|
980 | on_eof => sub { |
|
|
981 | print "received EOF, exiting...\n"; |
|
|
982 | $end_prog->broadcast; |
|
|
983 | }, |
|
|
984 | |
|
|
985 | This handle object will read from standard input. Setting the C<on_eof> |
|
|
986 | callback should be done for every file handle, as that is a condition that |
|
|
987 | we always need to check for when working with file handles, to prevent |
|
|
988 | reading or writing to a closed file handle, or getting stuck indefinitely |
|
|
989 | in case of an error. |
|
|
990 | |
|
|
991 | Speaking of errors: |
|
|
992 | |
|
|
993 | on_error => sub { |
|
|
994 | print "error while reading from STDIN: $!\n"; |
|
|
995 | $end_prog->broadcast; |
|
|
996 | } |
|
|
997 | ); |
|
|
998 | |
|
|
999 | The C<on_error> callback is also not required, but we set it here in case |
|
|
1000 | any error happens when we read from the file handle. It is usually a good |
|
|
1001 | idea to set this callback and at least print some diagnostic message: Even |
|
|
1002 | in our small example an error can happen. More on this later... |
|
|
1003 | |
|
|
1004 | $handle->push_read (sub { |
|
|
1005 | |
|
|
1006 | Next we push a general read callback on the read queue, which |
|
|
1007 | will wait until we have received all the data we wanted to |
|
|
1008 | receive. L<AnyEvent::Handle> has two queues per file handle, a read and a |
|
|
1009 | write queue. The write queue queues pending data that waits to be written |
|
|
1010 | to the file handle. And the read queue queues reading callbacks. For more |
|
|
1011 | details see the documentation L<AnyEvent::Handle> about the READ QUEUE and |
|
|
1012 | WRITE QUEUE. |
|
|
1013 | |
|
|
1014 | my ($handle) = @_; |
|
|
1015 | |
|
|
1016 | if ($handle->rbuf =~ s/^.*?\bend\b.*$//s) { |
|
|
1017 | print "got 'end', existing...\n"; |
|
|
1018 | $end_prog->broadcast; |
|
|
1019 | return 1 |
|
|
1020 | } |
|
|
1021 | |
|
|
1022 | 0 |
|
|
1023 | }); |
|
|
1024 | |
|
|
1025 | The actual callback waits until the word 'end' has been seen in the data |
|
|
1026 | received on standard input. Once we encounter the stop word 'end' we |
|
|
1027 | remove everything from the read buffer and call the condition variable |
|
|
1028 | we setup earlier, that signals our 'end of program' condition. And the |
|
|
1029 | callback returns with a true value, that signals we are done with reading |
|
|
1030 | all the data we were interested in (all data until the word 'end' has been |
|
|
1031 | seen). |
|
|
1032 | |
|
|
1033 | In all other cases, when the stop word has not been seen yet, we just |
|
|
1034 | return a false value, to indicate that we are not finished yet. |
|
|
1035 | |
|
|
1036 | The C<rbuf> method returns our read buffer, that we can directly modify as |
|
|
1037 | lvalue. Alternatively we also could have written: |
|
|
1038 | |
|
|
1039 | if ($handle->{rbuf} =~ s/^.*?\bend\b.*$//s) { |
|
|
1040 | |
|
|
1041 | The last line will wait for the condition that our program wants to exit: |
|
|
1042 | |
|
|
1043 | $end_prog->recv; |
|
|
1044 | |
|
|
1045 | The call to C<recv> will setup an event loop for us and wait for IO, timer |
|
|
1046 | or signal events and will handle them until the condition gets sent (by |
|
|
1047 | calling its C<send> method). |
|
|
1048 | |
|
|
1049 | The key points to learn from this example are: |
|
|
1050 | |
|
|
1051 | =over 4 |
|
|
1052 | |
|
|
1053 | =item * Condition variables are used to start an event loop. |
|
|
1054 | |
|
|
1055 | =item * How to registering some basic callbacks on AnyEvent::Handle's. |
|
|
1056 | |
|
|
1057 | =item * How to process data in the read buffer. |
|
|
1058 | |
|
|
1059 | =back |
|
|
1060 | |
981 | |
1061 | =head1 AUTHORS |
982 | =head1 AUTHORS |
1062 | |
983 | |
1063 | Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. |
984 | Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. |
1064 | |
985 | |