-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestserver.pl
More file actions
69 lines (53 loc) · 1.49 KB
/
testserver.pl
File metadata and controls
69 lines (53 loc) · 1.49 KB
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
62
63
64
65
66
67
68
69
#!/usr/bin/perl -w
use strict;
use AnyEvent;
use AnyEvent::Handle;
use AnyEvent::Socket;
my $cv = AnyEvent->condvar();
my %clients = ();
warn "Numbers of Elements: ".keys(%clients);
#POSIX signal for terminating Server:
my $w = AnyEvent->signal (
signal => "TERM",
cb => sub {
warn "Received TERM Signal\n";
$cv->send; #Exit from Eventloop
});
my $time_watcher = AnyEvent->timer (after => 1, interval => 1, cb => sub {
warn "timeout\n"; # print 'timeout' at most every second
});
# a simple tcp server
tcp_server (undef, 8888, sub {
my ($fh, $host, $port) = @_;
warn "Incoming Connection";
#TODO check host and port
$clients{$fh} = new AnyEvent::Handle(
fh => $fh,
on_error => sub {
warn "error $_[2]";
$_[0]->destroy;
},
on_eof => sub {
$_[0]->destroy; # destroy handle
warn "Other Side disconnected.";
});
my @start_request; @start_request = (line => sub {
my ($hdl, $line) = @_;
warn "Something happend";
warn ($line);
warn "Clients Connected".keys(%clients);
foreach my $k (keys %clients){
warn "Key: ".$k;
if($k ne $hdl->fh()){
if(defined($clients{$k})){
$clients{$k}->push_write($line);
}
}
}
# push next request read, possibly from a nested callback
warn "Pushing new Read Request";
$hdl->push_read (@start_request);
});
$clients{$fh}->push_read (@start_request);
});
$cv->recv; #eventloop