Difference between revisions of "Perl POE"

From Teknologisk videncenter
Jump to: navigation, search
m
m (A simple multitasking TCP Server)
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 =
 +
==A simple multitasking TCP Server ==
 
<source lang="perl">
 
<source lang="perl">
 
#!/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 => 12345,
+
    Port => 1234,
        ClientConnected => sub {
+
    ClientConnected => sub {
            print "got a connection from $_[HEAP]{remote_ip}\n";
+
      print "Connection from client from ",$_[HEAP]{remote_ip},"\n";
            $_[HEAP]{client}->put("Smile from the server.");
+
      $_[HEAP]{client}->put("Greetings from LMS");
        },
+
    },
        ClientInput => sub {
+
    ClientInput => sub {
            my $client_input = $_[ARG0];
+
      my $client_input = $_[ARG0];
            $client_input =~ tr[a-zA-Z][A-Za-z];
+
      if ($client_input =~ /Get orders/) {
            $_[HEAP]{client}->put($client_input);
+
      $_[HEAP]{client}->put("Your orders: ",%clientorder);
        },
+
      } else {
        ClientDisconnected => sub {
+
      print "Unknown Command received from Client\n";
            print "client from $_[HEAP]{remote_ip} disconnected\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;
 +
</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",
 +
  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;
 
</source>
 
</source>
 +
 
= Timing the lifespan of a session =
 
= Timing the lifespan of a session =
 
<source lang="perl">
 
<source lang="perl">

Revision as of 09:28, 22 March 2009

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";
  }

Links