Difference between revisions of "Perl POE"
From Teknologisk videncenter
m (→Links) |
m (→Timing the lifespan of a session) |
||
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=POE - portable multitasking and networking framework for Perl= | =POE - portable multitasking and networking framework for Perl= | ||
− | == A simple multitasking TCP Server == | + | = POE Networking = |
− | <source lang="perl"> | + | ==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''' | ||
+ | |||
+ | {| | ||
+ | <source lang="perl" line> | ||
#!/usr/bin/perl | #!/usr/bin/perl | ||
− | |||
use warnings; | use warnings; | ||
use strict; | use strict; | ||
use POE qw(Component::Server::TCP); | 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( | 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"); | |
− | }, | + | } |
+ | $_[HEAP]{client}->put($client_input); | ||
+ | }, | ||
+ | ClientDisconnected => sub { | ||
+ | print "client from $_[HEAP]{remote_ip} disconnected\n"; | ||
+ | }, | ||
+ | ); | ||
+ | POE::Kernel->run; | ||
+ | exit; | ||
+ | </source> | ||
+ | |} | ||
+ | |||
+ | == A simple TCP Client == | ||
+ | <source lang="perl"> | ||
+ | #!/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; | + | POE::Kernel->run(); |
− | exit; | + | exit; |
+ | </source> | ||
+ | |||
+ | = Timing the lifespan of a session = | ||
+ | <source lang="perl"> | ||
+ | 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"; | ||
+ | } | ||
</source> | </source> | ||
− | + | ||
− | [http://search.cpan.org/~rcaputo/POE-1.003/lib/POE.pm CPAN POE] | + | =Links= |
− | [http://search.cpan.org/~rcaputo/POE-1.003/lib/POE/Component/Server/TCP.pm CPAN POE::Component::Server::TCP]] | + | *[http://search.cpan.org/~rcaputo/POE-1.003/lib/POE.pm CPAN POE] |
+ | *[http://search.cpan.org/~rcaputo/POE-1.003/lib/POE/Component/Server/TCP.pm CPAN POE::Component::Server::TCP]] | ||
[[Category:Perl]] | [[Category:Perl]] |
Latest revision as of 12:42, 28 June 2009
Contents
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";
}