$num_of_exclamation_marks = $no_of_part
title thing, but this subject is perhaps the hardest thing to understand when approaching POE. If you heard about event-based programming, and you figure "okay that's simple enough, I get it" then you probably don't! falling_off_the_chair
happens, you provide a callback (AKA code reference) to sub { print "hahaha\n"; }
.sub incoming_client { my $client = shift; print_to_client( $client, "yo yo yo. what page will you be askingz for?" ); }Now this won't help us much if no one is going to be waiting for incoming connections, right? So we need to set up a TCP server, that accepts connections and forwards it according to the event. Obviously, the TCP server will have to understand what each event means. For example, if a client sends (using HTTP) a code that requests a file, our TCP server will need to say "oh oh, it's trying to get a file, I'll forward it to the correct subroutine". So, we'll use the fictional TCP server Sawyers::TCP::Server::That::Understands::HTTP. Yes, I like lots of namespaces. Dont::Event::Get::Me::Started.
sub asked_for_page { my ( $client, $request ) = @_; my $page = get_page($request); give_client_the_page( $client, $page ); }
sub accept_thankyou { my $client = shift; print_to_client( $client, "no, thank you!\n" ); }
use Sawyers::TCP::Server::That::Understands::HTTP;Now, the trick here is that any code that comes after the
my $server = Sawyers::TCP::Server::That::Understands::HTTP; $server->set_up_events( { incoming_connection => \&incoming_client, incoming_request => \&asked_for_page, closing_connection => \&accept_thankyou, echo_hello => sub { print "hi!\n"; }, } );
$server->start;
print "yay!\n";
$server->start
won't be executed. Why? Simple, because $server
will be running forever. Keeps an open connection and keeps waiting for events. So, there is really very little point waiting for the "yay" line, because it won't be there. Unless we set up an event that closes $server
and when it will be closed, the remaining lines of code will run.2 fold answer
xsawyerx on 2009-01-20T08:14:41
1. Fair enough. Perhaps HTTP server wasn't the best example for multi-events, since it also doesn't have a lot of events.
2. MY HTTPD WILL HAVE A BANNER, albeit I might have to write a new HTTP protocol...
BTW, thanks so much for all the help on #poe a while ago, this is what encouraged me to write these short articles and helped me understand POE altogether!Re:HTTP servers just wait
Alias on 2009-01-20T12:56:51
The event is probably worth still having through, because once the connection is created, the server might need to do some internal accounting related to the connection. For example, firing off a new child process if we're getting close to running out of available children to handle connections (although in this case it's async so you wouldn't do that anyways).
But you get what I mean.
Re:HTTP servers just wait
xsawyerx on 2009-01-20T13:30:59
It could check the incoming client's IP against an ACL, and decide to refuse it before even getting the request. Sort of like ACL blocking pre-HTTP but post-iptables/kernel-socket-connection.