On Sun, Mar 13, 2005 at 06:28:24AM -0500, Ciro The Spider-Man wrote:
> This is probably a really dumb question, but I've dug through the docs
> and messed around with the code for hours, and I still can't solve
> this one problem. Any help would be appreciated:
>
> The project at hand is an IRC bot with web connectivity. The desired
> end at this point is to visit http://hostname:port/whois/username, and
> have the web browser output the data from the irc_whois event. I tried
> throwing the event from within the ContentHandler, and when that
> didn't work, I moved it all the way up to the transhandler, but it
> still doesn't seem to want to cooperate. It seems to insist on going
> ALL the way through the ContentHandler, THEN processing the
> $irc->yield('whois', $nick); command, so effectively the web browser
> is always 1 query behind. Is there any way I can make
> POE::Component::Server::HTTP branch immediately and wait for the
> result?
>
> Here's the relevant (I think) code.
>
>
> my $session = POE::Session->create(...);
> my ($irc) = POE::Component::IRC->spawn('alias' => 'irc_client');
> #Standard IRC events here.
>
> sub irc_whois
> {
> my $info=parseWhois($_[ARG0]);
> print "DEBUG: Whois Event Triggered\n";
> $_[HEAP]->{'whoisinfo'} = $info;
> if ($info ne '') {return 0;}
> }
>
> #more junk
>
> my $httpd = POE::Component::Server::HTTP->new(
> Port => 8080,
> ContentHandler=> {
> '/' => \&defaultHandler, # Just prints a generic message
> '/whois/' => \&whoisHandler,
> },
> TransHandler => [ \&trans ],
> );
>
> sub trans
> {
> my ($request, $response) = @_;
> my $nick = (split('/', $request->uri))[4];
> $irc->yield('whois'=>nick);
> }
>
> sub whoisHandler
> {
> my ($request, $response) = @_;
> my $heap= $session->get_heap();
> my $output=$heap->{'whoisinfo'};
> print "DEBUG: Outputting Page\n";
> $response->content("$Output");
> }
>
> Yet the console output always comes out like so:
>
> DEBUG: Outputting Page
> DEBUG: Whois Event Triggered
>
> So of course, when the next query comes around, the first query is
> still sitting on the heap, so it gets used, then overwritten, etc...
>
> What fundamental fact am I overlooking?
You're not allowing time for the whois event to execute; POE is event-driven
so things happen asynchronously. Try storing the request and response
objects in the heap along with the query, with a unique key to access them,
then in the irc_whois sub pull the reponse object and populate it there.
--
Matt S Trout Brag sheet: http://trout.me.uk/services.html
LAMP, Infrastructure Contact: [EMAIL PROTECTED]
and Automation
specialist Do it once. Do it right.