line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package TestServer; |
2
|
|
|
|
|
|
|
|
3
|
14
|
|
|
14
|
|
10153
|
use warnings; |
|
14
|
|
|
|
|
27
|
|
|
14
|
|
|
|
|
724
|
|
4
|
14
|
|
|
14
|
|
70
|
use strict; |
|
14
|
|
|
|
|
22
|
|
|
14
|
|
|
|
|
445
|
|
5
|
|
|
|
|
|
|
|
6
|
14
|
|
|
14
|
|
67
|
use POE; |
|
14
|
|
|
|
|
24
|
|
|
14
|
|
|
|
|
319
|
|
7
|
14
|
|
|
14
|
|
27171
|
use POE::Component::Server::TCP; |
|
14
|
|
|
|
|
88919
|
|
|
14
|
|
|
|
|
571
|
|
8
|
14
|
|
|
14
|
|
166
|
use Socket qw(sockaddr_in); |
|
14
|
|
|
|
|
32
|
|
|
14
|
|
|
|
|
7881
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
my %clients; |
11
|
|
|
|
|
|
|
my %servers; |
12
|
|
|
|
|
|
|
my $seq = 0; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub spawn { |
15
|
16
|
|
|
16
|
0
|
506
|
my ($class, $port) = @_; |
16
|
|
|
|
|
|
|
|
17
|
16
|
|
|
|
|
65
|
my $alias = 'TestServer_' . ++$seq; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
POE::Component::Server::TCP->new( |
20
|
|
|
|
|
|
|
Alias => $alias, |
21
|
|
|
|
|
|
|
Port => $port, # Random one if 0. |
22
|
|
|
|
|
|
|
Address => "127.0.0.1", |
23
|
|
|
|
|
|
|
ClientInput => \&discard_client_input, |
24
|
|
|
|
|
|
|
ClientConnected => \®ister_client, |
25
|
|
|
|
|
|
|
ClientDisconnected => \&unregister_client, |
26
|
|
|
|
|
|
|
Started => sub { |
27
|
|
|
|
|
|
|
# Switch $port to the port this server actually bound on. |
28
|
16
|
|
|
16
|
|
22283
|
my $listener_sockname = $_[HEAP]{listener}->getsockname(); |
29
|
16
|
50
|
|
|
|
305
|
if (defined $listener_sockname) { |
30
|
16
|
|
|
|
|
103
|
($port, undef) = sockaddr_in($listener_sockname); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
else { |
33
|
0
|
|
|
|
|
0
|
$port = undef; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
}, |
36
|
16
|
|
|
|
|
478
|
InlineStates => { |
37
|
|
|
|
|
|
|
send_something => \&internal_send_something, |
38
|
|
|
|
|
|
|
}, |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
16
|
|
|
|
|
3641
|
$servers{$port} = $alias; |
42
|
|
|
|
|
|
|
|
43
|
16
|
|
|
|
|
80
|
return $port; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub register_client { |
47
|
24
|
|
|
24
|
0
|
57462
|
$clients{$_[SESSION]->ID} = 1; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub unregister_client { |
51
|
24
|
|
|
24
|
0
|
37080
|
delete $clients{$_[SESSION]->ID}; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
0
|
0
|
0
|
sub discard_client_input { |
55
|
|
|
|
|
|
|
# Do nothing. |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub send_something { |
59
|
3
|
|
|
3
|
0
|
22658
|
foreach my $client (keys %clients) { |
60
|
3
|
|
|
|
|
19
|
$poe_kernel->call($client, "send_something"); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub internal_send_something { |
65
|
3
|
|
|
3
|
0
|
1554
|
$_[HEAP]->{client}->put(scalar localtime); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub shutdown { |
69
|
14
|
|
|
14
|
0
|
1997751
|
foreach my $alias (values(%servers), keys(%clients)) { |
70
|
28
|
|
|
|
|
2408
|
$poe_kernel->post($alias => "shutdown"); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub shutdown_clients { |
75
|
2
|
|
|
2
|
0
|
6555
|
foreach my $session (keys(%clients)) { |
76
|
2
|
|
|
|
|
15
|
$poe_kernel->call($session => "shutdown"); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
1; |