On Wed, 2002-02-27 at 21:14, [EMAIL PROTECTED] wrote:
> > 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.

> 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.

> HTH, 
> 
> -Philip
> 
> 
> --------------THE CODE---------------------------
[snip]

OK, I now have a system that works as a simple CGI script:
http://lucas.ucs.ed.ac.uk/cgi-bin/gallery.cgi

I am now attempting to move to a sessional system, and have some code
that works mostly fine, based on Philips origional code.

The problem I now have is reading the parameters that are passed back to
the server.

The code snippit I'm using is:
            # find session ID in $req, put in $sessionID
            my @params = split /&/, $req->content;
            my %param;
            foreach $item (@params) {
              my ($key, $value) = split /=/, $item;
              $param{$key} = $value;
            }
            my $sessionID = $param{'sessionID'};
however this seems to work intermittently.

Can someone offer a better solution?

Below is my complete code (the server is behind my firewall, sorry):
--------- my code -----------------------
#!/home/cpan/bin/perl

use POE::Component::Server::HTTP;
use POE;
use POE::Session;

use Apache::Session::Generate::MD5;
use CGI;
use CGI::Pretty;

use Data::Dumper;

POE::Component::Server::HTTP->new(
    Port           => 65502,
    ContentHandler => {
        '/' => sub {
            $poe_kernel->post(MyHandler => 'handler', @_);
            return RC_WAIT;
          } ## end sub
    }
    );

POE::Session->create(
    inline_states => {

        _start => sub {
            $_[KERNEL]->alias_set('MyHandler');
            $_[HEAP] = {};
        },

        handler => sub {
            my ($kernel, $heap, $req, $resp) = @_[KERNEL, HEAP, ARG0,
ARG1];

            warn "image.server.perl: handler =========> \n";
            warn "image.server.perl: \$req\n";
            warn Dumper($req);
           # warn "image.server.perl: \$resp\n";
           # warn Dumper($resp);
            warn "image.server.perl: handler =========> \n";

            # find session ID in $req, put in $sessionID
            my @params = split /&/, $req->content;
            my %param;
            foreach $item (@params) {
              my ($key, $value) = split /=/, $item;
              $param{$key} = $value;
            }
            my $sessionID = $param{'sessionID'};


            warn "image.server.perl:  Parsing Request:\n";
            warn "       >>>>> content: @params\n";

            warn "image.server.perl: -----------------\n";
            warn "image.server.perl:  Query SessionID:\n";
            warn "       >>>>> content: $sessionID\n";
            if ($sessionID) {
              warn "image.server.perl:  Query Heap->{SessionID}:\n";
              warn "       >>>>> content: ".$heap->{$sessionID}."\n";
            };

            unless ($sessionID) {

                # generate a new sessionID somehow
                $id = Apache::Session::Generate::MD5::generate();
                $param{'sessionID'} = $id;
                @params = %param;

                warn "image.server.perl:  New Session: $id.\n";
                warn "       >>>>> Using the parameters: @params\n";

                # indocate the the server response-code is "OK".
                $resp->code(RC_OK);
                $resp->content_type('text/html');
                $resp->add_content(join '', &make_page(@params));

                # save some data for next request
                my $some_data = localtime;
                $heap->{$id} = $some_data;
                warn "       >>>>> Create Heap->{$id}: ".$heap->{$id}."\n";
            } elsif (not $heap->{$sessionID}) {


                warn "image.server.perl:  Session $$sessionID
Expired.\n";
                warn "       >>>>> Using the parameters: @params\n";

                # this is a new session, return image via $resp, 
                $resp->code(RC_OK);
                $resp->content_type('text/html');
                $resp->add_content(join '', &make_page(@params));

                # save some data for next request
                my $some_data = localtime;
                $heap->{$sessionID} = $some_data;
                warn "       >>>>> Updated Heap->{$sessionID}: ".
                  $heap->{$sessionID}."\n";
            } else {


                warn "image.server.perl:  Welcom back $sessionID.\n";
                warn "       >>>>> Using the parameters: @params\n";

                # this is a repeat request
                $some_data = $heap->{$sessionID};

                # use $some_data and $req to generate image
                $resp->code(RC_OK);
                $resp->content_type('text/html');
                $resp->add_content(join '', &make_page(@params));

                # save some data for next request
                my $some_data = localtime;
                $heap->{$sessionID} = $some_data;
                warn "       >>>>> Updated Heap->{$sessionID}:
".$heap->{$sessionID}."\n";
            } ## end else

            # this causes the response to finaly be sent to the client
            $resp->continue();
          } ## end sub
    }
    );

$poe_kernel->run();

exit;

#################################

sub make_page {
    my %param = @_;


    # get the handlers
    my $cgi = CGI->new;
    while (($key, $value) = each %param) {
        $cgi->param(-name => $key, -value => $value);
    }

    # get the parameters (deleting them all, so they don't get passed in
    # the re-call.
    my $query_in = $cgi->query_string;

    my $sessionID  = $cgi->param('sessionID');
    my $name  = $cgi->param('name');
 
    # and now clear all the remaining parameters
    $cgi->delete_all;


    # Either ask for a name, or display the one we were given
    my $name_text;
    if (defined $name) {
      $name_text = $cgi->p("Hello $name");
      $name_text .= $cgi->hidden(-name  => 'name',
                                 -value => $name);
    } else {
      $name_text = $cgi->p("What is your name?");
      $name_text .= $cgi->textarea(-name  => 'name',
                                   -columns => '10',
                                   -rows => '1',
                                  )
    }


    my @page;
    push @page, $cgi->h2('test sessional web browser');

    push @page, $cgi->p("Your session ID is: $sessionID");
    push @page,
      $cgi->p("I was passed the following pseudo:query_string:
$query_in");

    push @page,
      $cgi->start_form(
                       -method => 'GET',
                       -action => '/',
                       );
    push @page,
      $cgi->hidden(
                   -name  => 'sessionID',
                   -value => $sessionID
                  ),
                    $name_text;
    push @page, $cgi->submit(-name => "submit");

    push @page, $cgi->end_form;

    return @page;

} ## end sub make_page
--------------- end code ------------------------
-- 
--==++
Ian Stuart: Edinburgh University Data Library.

Information is not knowledge
Knowledge is not wisdom
Wisdom is not truth
Truth is not beauty
Beauty is not love
Love is not music
              -- Mary.

 Personal web site: http://lucas.ucs.ed.ac.uk/ 

Reply via email to