On 27-Feb-2002 Ian Stuart wrote:
> Hi folks.
>
> I've spent a few years using Perl, and have a coupla stable services
> under my belt. I now have a new task, and I'm getting lost....
>
> SO, I know what I want to do, and I've worked out that POE is the way to
> do it, but I'm reading all this documentation, with lots of almost
> solutions, and I'm starting to lose site of the wood for the trees!
>
> Basically what I want to set up is a server (a POE-based script) that
> accepts connections, recieves a sessional identity, possibly with some
> instructions, and returns an image. (I can build in a timeout easily
> enough :)
>
> Thus, a seperate series of (lets say, CGI) scripts can connect to the
> server, specifying an image file - and the script recieves that file. It
> can reconnect (with it's sessional ID), asking for a version (maybe
> zoomed in, possibly with a new center-point), and the server will
> already know the image to use.
>
> I am making the server operate in this (slightly strange) way because I
> want to demonstrate that the same technology can be used as an
> intermediary between a multi-user (stateless) web server and multiple
> (state-requiring) Z30.50 databases.
>
> I *think* I want to base it on the objsessions.pl sample, but I'm
> getting bogged down with PoCo::HTTPD interfaces, wheels, Filters and
> recogising existing sessions...
>
> Can someone give me a nudge in the right direction here?
Here's a rough stab. It keeps session data in the heap, and has a worker
session that handles the request. RC_WAIT causes the response to not be
sent until $resp->continue() is called.
Note that I wouldn't implement this with inline_states (i prefer object or
package states). Also, this isn't forking, so if genrating an image
takes a long time, no other request will be processed until it is done.
Finaly, expiring session data from the heap is left as an excercise to the
reader.
HTH,
-Philip
--------------THE CODE---------------------------
use POE::Component::Server::HTTP;
use POE;
use POE::Session;
POE::Component::Server::HTTP->new(
Port => 8000,
ContentHandler => { '/' => sub {
$poe_kernel->post(MyHandler=>'handler', @_)
return RC_WAIT;
},
);
POE::Session->create(
inline_states=>{
_start=>sub {
$_[KERNEL]->alias_set('MyHandler');
$_[HEAP]={};
},
handler=>sub {
my($kernel, $heap, $req, $resp)=@_[KERNEL, HEAP, ARG0, ARG1];
# find session ID in $req, put in $sessionID
unless($sessionID) {
# generate a new sessionID somehow
# redirect the client to the new url that has sessionID in
# NOTE : this isn't the way to do it really
$resp->code(RC_REDIRECT);
$resp->url($resp->url."/$sessionID");
}
elsif(not $heap->{$sessionID}) {
# this is a new session, return image via $resp,
$resp->code(...);
$resp->content($the_image);
# save some data for next request
$heap->{$sessionID}=$some_data;
}
else {
# this is a repeat request
$some_data=$heap->{$sessionID}
# use $some_data and $req to generate image
$resp->code(...);
$resp->content($newer_image);
# save some data for next request
$heap->{$sessionID}=$some_data;
}
# this causes the response to finaly be sent to the client
$resp->continue();
}
}
);
$poe_kernel->run();