The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

POE::Component::Client::Keepalive - manage connections, with keep-alive

VERSION

version 0.272

SYNOPSIS

use warnings;
use strict;

use POE;
use POE::Component::Client::Keepalive;

POE::Session->create(
  inline_states => {
    _start    => \&start,
    got_conn  => \&got_conn,
    got_error => \&handle_error,
    got_input => \&handle_input,
  }
);

POE::Kernel->run();
exit;

sub start {
  $_[HEAP]{ka} = POE::Component::Client::Keepalive->new();

  $_[HEAP]{ka}->allocate(
    scheme  => "http",
    addr    => "127.0.0.1",
    port    => 9999,
    event   => "got_conn",
    context => "arbitrary data (even a reference) here",
    timeout => 60,
  );

  print "Connection is in progress.\n";
}

sub got_conn {
  my ($kernel, $heap, $response) = @_[KERNEL, HEAP, ARG0];

  my $conn    = $response->{connection};
  my $context = $response->{context};

  if (defined $conn) {
    if ($response->{from_cache}) {
      print "Connection was established immediately.\n";
    }
    else {
      print "Connection was established asynchronously.\n";
    }

    $conn->start(
      InputEvent => "got_input",
      ErrorEvent => "got_error",
    );
    return;
  }

  print(
    "Connection could not be established: ",
    "$response->{function} error $response->{error_num}: ",
    "$response->{error_str}\n"
  );
}

sub handle_input {
  my $input = $_[ARG0];
  print "$input\n";
}

sub handle_error {
  my $heap = $_[HEAP];
  delete $heap->{connection};
  $heap->{ka}->shutdown();
}

DESCRIPTION

POE::Component::Client::Keepalive creates and manages connections for other components. It maintains a cache of kept-alive connections for quick reuse. It is written specifically for clients that can benefit from kept-alive connections, such as HTTP clients. Using it for one-shot connections would probably be silly.

SEE ALSO

POE POE::Component::Connection::Keepalive

LICENSE

This distribution is copyright 2004-2009 by Rocco Caputo. All rights are reserved. This distribution is free software; you may redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Rocco Caputo rcaputo@cpan.org

CONTRIBUTORS

Rob Bloodgood helped out a lot. Thank you.

Joel Bernstein solved some nasty race conditions. Portugal Telecom http://www.sapo.pt/ was kind enough to support his contributions.

BUG TRACKER

https://rt.cpan.org/Dist/Display.html?Queue=POE-Component-Client-Keepalive

REPOSITORY

http://gitorious.org/poe-component-client-keepalive http://github.com/rcaputo/poe-component-client-keepalive

OTHER RESOURCES

http://search.cpan.org/dist/POE-Component-Client-Keepalive/