El Módulo IO::Event

pp2@nereida:~/src/perl/testing$ cat -n ioevents.pl
 1  use strict;
 2  use IO::Events;
 3
 4  my $loop = IO::Events::Loop-> new();
 5
 6  my $stdin_alive = 1;
 7  my $calculator = IO::Events::Process::ReadWrite-> new(
 8          owner    => $loop,
 9          process  => 'bc -l',
10          on_read  => sub {
11                  while ( my $line = $_[0]-> readline) {
12                          print "bc says: $line";
13                  }
14          },
15          on_close => sub {
16                  exit 1 if $stdin_alive; # fork/exec error
17          }
18  );
19
20  my $stdin = IO::Events::stdin-> new(
21          owner => $loop,
22          on_read => sub {
23          $calculator-> write( $_[0]-> read );
24          },
25          on_close => sub {
26                  $stdin_alive = 0;
27                  exit;
28          },
29  );
30
31  $loop-> yield while 1;

pp2@nereida:~/src/perl/testing$ perl ioevents.pl
4*2
bc says: 8
3-5
bc says: -2
quit

Ejemplo: TCP

pp2@nereida:~/src/perl/testing$ cat -n ioeventstcpport.pl
 1  use strict;
 2  use IO::Events;
 3
 4  my $loop = IO::Events::Loop-> new();
 5  IO::Events::Socket::TCP-> new(
 6          owner    => $loop,
 7          listen   => 1,
 8          port     => 10000,
 9          on_read => sub {
10                  my $new = shift-> accept(
11                          read   => 1,
12                          on_read => sub {
13                                  while ( my $line = $_[0]-> readline) {
14                                          print "client says: $line\n";
15                                          exit;
16                                  }
17                          }
18                  );
19                  print "connect from $new->{remote_addr}:$new->{remote_port}\n";
20          },
21  );
22
23  IO::Events::Socket::TCP-> new(
24          owner   => $loop,
25          connect => 'localhost',
26          port    => 10000,
27  )-> write("hello, tcp socket!\n");
28
29  $loop->yield while 1;

pp2@nereida:~/src/perl/testing$ perl ioeventstcpport.pl
connect from 127.0.0.1:39294
client says: hello, tcp socket!

UDP

 1  use strict;
 2  use Socket;
 3  use IO::Events;
 4
 5  my $loop = IO::Events::Loop-> new();
 6  IO::Events::Socket::UDP-> new(
 7          owner    => $loop,
 8          port     => 10000,
 9          on_read => sub {
10                  my $self = $_[0];
11                  my $data = $self-> recv;
12                  print "$self->{remote_host}:$self->{remote_port} says: $data";
13                  exit;
14          },
15  );
16
17  IO::Events::Socket::UDP-> new(
18          owner   => $loop,
19  )-> send( 'localhost', 10000, "hello, udp socket!\n");
20
21  $loop->yield while 1;

pp2@nereida:~/src/perl/testing$ perl ioeventsudpport.pl
localhost:32835 says: hello, udp socket!
pp2@nereida:~/src/perl/testing$ cat -n ioeventsudpport.pl



Subsecciones
Casiano Rodríguez León
Licencia de Creative Commons
Programación Distribuida y Mejora del Rendimiento
por Casiano Rodríguez León is licensed under a Creative Commons Reconocimiento 3.0 Unported License.

Permissions beyond the scope of this license may be available at http://campusvirtual.ull.es/ocw/course/view.php?id=44.
2012-06-19