Difference between revisions of "Perl POE"

From Teknologisk videncenter
Jump to: navigation, search
m
m (Timing the lifespan of a session)
 
(One intermediate revision by the same user not shown)
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'''
{| class="collapsible collapsed" width=200px
 
! Click Here for Contents
 
|-
 
|Hej
 
|}
 
  
 
{|   
 
{|   
|<source lang="perl" line>
+
<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";
+
print "LMS";
|my %clientorder = ( "LCnumber" => 7,
+
my %clientorder = ( "LCnumber" => 7,
|          "LCsessions" => 10,
+
          "LCsessions" => 10,
|                        "loadServer" => "sun.tekkom.dk",
+
                        "loadServer" => "sun.tekkom.dk",
|                        "loadPort" => 80);
+
                        "loadPort" => 80);
|
+
 
|POE::Component::Server::TCP->new(
+
POE::Component::Server::TCP->new(
|    Port => 1234,
+
    Port => 1234,
|    ClientConnected => sub {
+
    ClientConnected => sub {
|      print "Connection from client from ",$_[HEAP]{remote_ip},"\n";
+
      print "Connection from client from ",$_[HEAP]{remote_ip},"\n";
|      $_[HEAP]{client}->put("Greetings from LMS");
+
      $_[HEAP]{client}->put("Greetings from LMS");
|    },
+
    },
|    ClientInput => sub {
+
    ClientInput => sub {
|      my $client_input = $_[ARG0];
+
      my $client_input = $_[ARG0];
|      if ($client_input =~ /Get orders/) {
+
      if ($client_input =~ /Get orders/) {
|      $_[HEAP]{client}->put("Your orders: ",%clientorder);
+
      $_[HEAP]{client}->put("Your orders: ",%clientorder);
|      } else {
+
      } else {
|      print "Unknown Command received from Client\n";
+
      print "Unknown Command received from Client\n";
|        $_[HEAP]{client}->put("501 Unknown Command received from Client");
+
        $_[HEAP]{client}->put("501 Unknown Command received from Client");
|      }
+
      }
|      $_[HEAP]{client}->put($client_input);
+
      $_[HEAP]{client}->put($client_input);
|    },
+
    },
|    ClientDisconnected => sub {
+
    ClientDisconnected => sub {
|      print "client from $_[HEAP]{remote_ip} disconnected\n";
+
      print "client from $_[HEAP]{remote_ip} disconnected\n";
|    },
+
    },
);
+
  );
POE::Kernel->run;
+
  POE::Kernel->run;
exit;
+
  exit;
|</source>
+
</source>
 
|}
 
|}
  
Line 78: Line 73:
 
sub _start_handler {
 
sub _start_handler {
 
     $_[HEAP]{ts_start} = time();
 
     $_[HEAP]{ts_start} = time();
  }
+
}
  sub _stop_handler {
+
 
 +
sub _stop_handler {
 
     my $time_elapsed = time() - $_[HEAP]{ts_start};
 
     my $time_elapsed = time() - $_[HEAP]{ts_start};
 
     print "Session ", $_[SESSION]->ID, " elapsed seconds: $elapsed\n";
 
     print "Session ", $_[SESSION]->ID, " elapsed seconds: $elapsed\n";
  }
+
}
 
</source>
 
</source>
  

Latest revision as of 12:42, 28 June 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

 1 #!/usr/bin/perl
 2 use warnings;
 3 use strict;
 4 use POE qw(Component::Server::TCP);
 5 print "LMS";
 6 my %clientorder = (	"LCnumber" => 7,
 7            		"LCsessions" => 10,
 8                         "loadServer" => "sun.tekkom.dk",
 9                         "loadPort" => 80);
10 
11 POE::Component::Server::TCP->new(
12     Port => 1234,
13     ClientConnected => sub {
14       print "Connection from client from ",$_[HEAP]{remote_ip},"\n";
15       $_[HEAP]{client}->put("Greetings from LMS");
16     },
17     ClientInput => sub {
18       my $client_input = $_[ARG0];
19       if ($client_input =~ /Get orders/) {
20       	$_[HEAP]{client}->put("Your orders: ",%clientorder);
21       } else {
22       	print "Unknown Command received from Client\n";
23         	$_[HEAP]{client}->put("501 Unknown Command received from Client");
24       }
25       $_[HEAP]{client}->put($client_input);
26     },
27     ClientDisconnected => sub {
28       print "client from $_[HEAP]{remote_ip} disconnected\n";
29     },
30   );
31   POE::Kernel->run;
32   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",
  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