Perl POE

From Teknologisk videncenter
Revision as of 08:57, 28 March 2009 by Heth (talk | contribs)
Jump to: navigation, search

POE - portable multitasking and networking framework for Perl

POE Networking

A simple multitasking TCP Server

This server is doing nothing useful, but are showing how to establish the server and control client connections. When Client connects they connect at line 14 and the server send a Greeting to the client. If the client send information to the server it is received in th ClientInput subroutine in line 18. If the Client sends Get orders it receives the content of hash %clientorder

A simple TCP Client

#!/usr/bin/perl -w
use strict;
use POE qw(Component::Client::TCP);

POE::Component::Client::TCP->new
( RemoteAddress => "192.168.1.100",
  RemotePort    => "1234",
  ConnectTimeout => 5,
  ConnectError   => sub {
    print "Reconecting..\n";
    $_[KERNEL]->delay( reconnect => 5 );
  },
  ServerInput   => sub {
    my $input = $_[ARG0];
    if ($input =~ "Greetings from LMS") {
        $_[HEAP]{server}->put("Get orders");
    } else {
        print "received: ", $input,"\n";
    }
  }
);

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

Timing the lifespan of a session

sub _start_handler {
    $_[HEAP]{ts_start} = time();
  }
  sub _stop_handler {
    my $time_elapsed = time() - $_[HEAP]{ts_start};
    print "Session ", $_[SESSION]->ID, " elapsed seconds: $elapsed\n";
  }

Links