1 |
#!/opt/bin/perl |
2 |
|
3 |
use AnyEvent; |
4 |
use AnyEvent::MP; |
5 |
|
6 |
initialise_node "127.0.0.1:1299"; |
7 |
|
8 |
print "initialized server\n"; |
9 |
|
10 |
my $chatter_port = port; |
11 |
|
12 |
reg $chatter_port, "chatter"; |
13 |
|
14 |
my %client_ports; |
15 |
|
16 |
rcv $chatter_port, |
17 |
join => sub { |
18 |
my ($tag, $client_port) = @_; |
19 |
|
20 |
print "got new client port: $client_port\n"; |
21 |
$client_ports{$client_port} = 1; |
22 |
|
23 |
mon $client_port, sub { |
24 |
my (@reason) = @_; |
25 |
print "client disconnected: " . join (', ', @reason) . "\n"; |
26 |
delete $client_ports{$client_port} |
27 |
}; |
28 |
|
29 |
0 |
30 |
}, |
31 |
message => sub { |
32 |
my ($tag, $msg) = @_; |
33 |
|
34 |
print "message> $msg\n"; |
35 |
|
36 |
snd $_, message => $msg |
37 |
for keys %client_ports; |
38 |
|
39 |
0 |
40 |
}; |
41 |
|
42 |
AnyEvent->condvar->recv; |