File: 04_listen.t

package info (click to toggle)
libanyevent-perl 7.010-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,528 kB
  • sloc: perl: 6,349; sh: 108; makefile: 10
file content (61 lines) | stat: -rw-r--r-- 1,204 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/opt/perl/bin/perl

use strict;

use AnyEvent;
BEGIN { require AnyEvent::Impl::Perl unless $ENV{PERL_ANYEVENT_MODEL} }
use AnyEvent::Handle;
use AnyEvent::Socket;

my $lbytes;
my $rbytes;

print "1..2\n";

my $cv = AnyEvent->condvar;

my $hdl;
my $port;

my $w = tcp_server undef, undef,
   sub {
      my ($fh, $host, $port) = @_;

      $hdl = AnyEvent::Handle->new (fh => $fh, on_eof => sub { $cv->broadcast });

      $hdl->push_read (chunk => 6, sub {
         my ($hdl, $data) = @_;

         if ($data eq "TEST\015\012") {
            print "ok 1 - server received client data\n";
         } else {
            print "not ok 1 - server received bad client data\n";
         }

         $hdl->push_write ("BLABLABLA\015\012");
      });
   }, sub {
      $port = $_[2];

      0
   };

my $clhdl; $clhdl = AnyEvent::Handle->new (
   connect => [localhost => $port],
   on_eof => sub { $cv->broadcast },
);

$clhdl->push_write ("TEST\015\012");
$clhdl->push_read (line => sub {
   my ($clhdl, $line) = @_;

   if ($line eq 'BLABLABLA') {
      print "ok 2 - client received response\n";
   } else {
      print "not ok 2 - client received bad response\n";
   }

   $cv->broadcast;
});

$cv->wait;