Perl POE
From Teknologisk videncenter
Contents
POE - portable multitasking and networking framework for Perl
POE Networking
A simple multitasking TCP Server
#!/usr/bin/perl
use warnings;
use strict;
use POE qw(Component::Server::TCP);
print "LMS";
my %clientorder = ( "LCnumber" => 7,
"LCsessions" => 10,
"loadServer" => "sun.tekkom.dk",
"loadPort" => 80);
POE::Component::Server::TCP->new(
Port => 1234,
ClientConnected => sub {
print "Connection from client from ",$_[HEAP]{remote_ip},"\n";
$_[HEAP]{client}->put("Greetings from LMS");
},
ClientInput => sub {
my $client_input = $_[ARG0];
if ($client_input =~ /Get orders/) {
$_[HEAP]{client}->put("Your orders: ",%clientorder);
} else {
print "Unknown Command received from Client\n";
$_[HEAP]{client}->put("501 Unknown Command received from Client");
}
#$client_input =~ tr[a-zA-Z][n-ma-zN-MA-Z];
$_[HEAP]{client}->put($client_input);
},
ClientDisconnected => sub {
print "client from $_[HEAP]{remote_ip} disconnected\n";
},
);
POE::Kernel->run;
exit;
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",
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";
}