Difference between revisions of "Perl POE"

From Teknologisk videncenter
Jump to: navigation, search
m (A simple TCP Client)
m (A simple multitasking TCP Server)
Line 2: Line 2:
 
= POE Networking =
 
= POE Networking =
 
==A simple multitasking TCP Server ==
 
==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'''  
+
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>
+
{| class="wikitable collapsible collapsed" width=200px
#!/usr/bin/perl
+
! Click Here for Contents
use warnings;
+
|-
use strict;
+
|   
use POE qw(Component::Server::TCP);
+
|<source lang="perl" line>
 
+
|#!/usr/bin/perl
print "LMS";
+
|use warnings;
my %clientorder = ( "LCnumber" => 7,
+
|use strict;
          "LCsessions" => 10,
+
|use POE qw(Component::Server::TCP);
                        "loadServer" => "sun.tekkom.dk",
+
|print "LMS";
                        "loadPort" => 80);
+
|my %clientorder = ( "LCnumber" => 7,
 
+
|          "LCsessions" => 10,
POE::Component::Server::TCP->new(
+
|                        "loadServer" => "sun.tekkom.dk",
    Port => 1234,
+
|                        "loadPort" => 80);
    ClientConnected => sub {
+
|
      print "Connection from client from ",$_[HEAP]{remote_ip},"\n";
+
|POE::Component::Server::TCP->new(
      $_[HEAP]{client}->put("Greetings from LMS");
+
|    Port => 1234,
    },
+
|    ClientConnected => sub {
    ClientInput => sub {
+
|      print "Connection from client from ",$_[HEAP]{remote_ip},"\n";
      my $client_input = $_[ARG0];
+
|      $_[HEAP]{client}->put("Greetings from LMS");
      if ($client_input =~ /Get orders/) {
+
|    },
      $_[HEAP]{client}->put("Your orders: ",%clientorder);
+
|    ClientInput => sub {
      } else {
+
|      my $client_input = $_[ARG0];
      print "Unknown Command received from Client\n";
+
|      if ($client_input =~ /Get orders/) {
        $_[HEAP]{client}->put("501 Unknown Command received from Client");
+
|      $_[HEAP]{client}->put("Your orders: ",%clientorder);
      }
+
|      } else {
      $_[HEAP]{client}->put($client_input);
+
|      print "Unknown Command received from Client\n";
    },
+
|        $_[HEAP]{client}->put("501 Unknown Command received from Client");
    ClientDisconnected => sub {
+
|      }
      print "client from $_[HEAP]{remote_ip} disconnected\n";
+
|      $_[HEAP]{client}->put($client_input);
    },
+
|    },
  );
+
|    ClientDisconnected => sub {
  POE::Kernel->run;
+
|      print "client from $_[HEAP]{remote_ip} disconnected\n";
  exit;
+
|    },
</source>
+
);
 +
POE::Kernel->run;
 +
exit;
 +
|</source>
 +
|}
  
 
== A simple TCP Client ==
 
== A simple TCP Client ==

Revision as of 08:56, 28 March 2009

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