POE-Component-Server-Bayeux

 view release on metacpan or  search on metacpan

lib/POE/Component/Server/Bayeux/Request.pm  view on Meta::CPAN

    my %args = validate(@_, {
        request => 1,
        response => 1,
        server_heap => 1,
        ip => 0,
    });

    my %self = (
        http_request => $args{request},
        http_response => $args{response},
        heap => $args{server_heap},
        messages => [],
        responses => [],
        id => $uuid->create_str,
        ip => $args{ip},
    );

    my $self = bless \%self, $class;
    $self->init();
    return $self;
}

## Object Methods, Public ###

=head1 OBJECT METHODS

=head2 handle ()

=over 4

Call after creating the request.  Calls the pre_handle(), handle()
methods on each message, possibly completing the request.

=back

=cut

sub handle {
    my ($self) = @_;

    my $heap = $self->heap;

    # Some messages (/meta/connect, for example) need to be handled in a specific
    # order.  Allow each message to affect the queueing.
    foreach my $message (@{ $self->messages }) {
        $message->pre_handle();
    }

    # Starting at the beginning of the message array, process each message in
    # turn.  Messages will interact with the Request $self object, adding responses
    # and in some cases affecting other messages still in the stack.

    while (my $message = shift @{ $self->messages }) {
        $message->handle();
    }

    if ($self->delay) {
        $poe_kernel->post($heap->{manager}, 'delay_request', $self->id, $self->delay);
        $self->delay(0);
        $self->is_complete(0);
        $self->http_response->streaming(1);
    }
    else {
        $self->complete();
    }
}

=head2 complete ()

=over 4

Completes the request, calling the post_handle() method on the messages
that need it.

=back

=cut

sub complete {
    my ($self) = @_;

    $self->form_response( @{ $self->responses } );
    $self->is_complete(1);
    if ($self->http_response->streaming) {
        $self->http_response->send( $self->http_response );
        $self->http_response->close();
    }

    # Ensure no KeepAlive
    $self->http_request->header(Connection => 'close');

    if ($self->post_handle) {
        while (my $message = shift @{ $self->post_handle }) {
            $message->post_handle();
        }
    }
}

## Object Methods, Private ###

=head1 PRIVATE METHODS

=over 4

These methods are mainly called by messages during their handle() phase.

=back

=head2 client ($id)

=over 4

Returns a L<POE::Component::Server::Bayeux::Client> object with the given id.

=back

=cut

sub client {
    my ($self, $id) = @_;

    return POE::Component::Server::Bayeux::Client->new(
        request => $self,
        id => $id,
        server_heap => $self->heap,
    );
}

=head2 add_response ($response)

=over 4

Adds a message response onto the stack of responses.

=back

=cut

sub add_response {
    my ($self, $response) = @_;

    push @{ $self->responses }, $response;
}



( run in 1.524 second using v1.01-cache-2.11-cpan-5837b0d9d2c )