line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
###################################################################### |
2
|
|
|
|
|
|
|
# Listen for UDP upload status packets |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Copyright 2005-2007, Six Apart, Ltd. |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package Perlbal::UploadListener; |
8
|
22
|
|
|
22
|
|
149
|
use strict; |
|
22
|
|
|
|
|
49
|
|
|
22
|
|
|
|
|
907
|
|
9
|
22
|
|
|
22
|
|
333
|
use warnings; |
|
22
|
|
|
|
|
43
|
|
|
22
|
|
|
|
|
679
|
|
10
|
22
|
|
|
22
|
|
114
|
no warnings qw(deprecated); |
|
22
|
|
|
|
|
47
|
|
|
22
|
|
|
|
|
916
|
|
11
|
|
|
|
|
|
|
|
12
|
22
|
|
|
22
|
|
615
|
use base "Perlbal::Socket"; |
|
22
|
|
|
|
|
258
|
|
|
22
|
|
|
|
|
4420
|
|
13
|
22
|
|
|
22
|
|
129
|
use fields qw(service hostport); |
|
22
|
|
|
|
|
43
|
|
|
22
|
|
|
|
|
236
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# TCPListener |
16
|
|
|
|
|
|
|
sub new { |
17
|
0
|
|
|
0
|
1
|
|
my ($class, $hostport, $service) = @_; |
18
|
|
|
|
|
|
|
|
19
|
0
|
|
|
|
|
|
my $sock = |
20
|
|
|
|
|
|
|
IO::Socket::INET->new( |
21
|
|
|
|
|
|
|
LocalAddr => $hostport, |
22
|
|
|
|
|
|
|
Proto => "udp", |
23
|
|
|
|
|
|
|
ReuseAddr => 1, |
24
|
|
|
|
|
|
|
Blocking => 0, |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
0
|
0
|
0
|
|
|
|
return Perlbal::error("Error creating listening socket: " . ($@ || $!)) |
28
|
|
|
|
|
|
|
unless $sock; |
29
|
0
|
|
|
|
|
|
my $self = fields::new($class); |
30
|
0
|
|
|
|
|
|
$self->SUPER::new($sock); |
31
|
0
|
|
|
|
|
|
$self->{service} = $service; |
32
|
0
|
|
|
|
|
|
$self->{hostport} = $hostport; |
33
|
0
|
|
|
|
|
|
$self->watch_read(1); |
34
|
0
|
|
|
|
|
|
return $self; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my %status; |
38
|
|
|
|
|
|
|
my @todelete; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub get_status { |
41
|
0
|
|
|
0
|
0
|
|
my $ses = shift; |
42
|
0
|
|
|
|
|
|
return $status{$ses}; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# TCPListener: accepts a new client connection |
46
|
|
|
|
|
|
|
sub event_read { |
47
|
0
|
|
|
0
|
1
|
|
my Perlbal::TCPListener $self = shift; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my $buf; |
50
|
0
|
|
|
|
|
|
$self->{sock}->recv($buf, 500); |
51
|
0
|
0
|
|
|
|
|
return unless $buf =~ /^UPLOAD:(\w{5,50}):(\d+):(\d+):(\d+):(\d+)$/; |
52
|
0
|
|
|
|
|
|
my ($ses, $done, $total, $starttime, $nowtime) = ($1, $2, $3, $4, $5); |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
my $now = time(); |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
$status{$ses} = { |
57
|
|
|
|
|
|
|
done => $done, |
58
|
|
|
|
|
|
|
total => $total, |
59
|
|
|
|
|
|
|
starttime => $starttime, |
60
|
|
|
|
|
|
|
lasttouch => $now, |
61
|
|
|
|
|
|
|
}; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# keep a history of touched records, then we'll clean 'em |
64
|
|
|
|
|
|
|
# after 30 seconds. |
65
|
0
|
|
|
|
|
|
push @todelete, [$now, $ses]; |
66
|
0
|
|
|
|
|
|
my $too_old = $now - 4; |
67
|
0
|
|
0
|
|
|
|
while (@todelete && $todelete[0][0] < $too_old) { |
68
|
0
|
|
|
|
|
|
my $rec = shift @todelete; |
69
|
0
|
|
|
|
|
|
my $to_kill = $rec->[1]; |
70
|
0
|
0
|
|
|
|
|
if (my $krec = $status{$to_kill}) { |
71
|
0
|
|
|
|
|
|
my $last_touch = $krec->{lasttouch}; |
72
|
0
|
0
|
|
|
|
|
delete $status{$to_kill} if $last_touch < $too_old; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub as_string { |
78
|
0
|
|
|
0
|
1
|
|
my Perlbal::TCPListener $self = shift; |
79
|
0
|
|
|
|
|
|
my $ret = $self->SUPER::as_string; |
80
|
0
|
|
|
|
|
|
my Perlbal::Service $svc = $self->{service}; |
81
|
0
|
|
|
|
|
|
$ret .= ": listening on $self->{hostport} for service '$svc->{name}'"; |
82
|
0
|
|
|
|
|
|
return $ret; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub as_string_html { |
86
|
0
|
|
|
0
|
0
|
|
my Perlbal::TCPListener $self = shift; |
87
|
0
|
|
|
|
|
|
my $ret = $self->SUPER::as_string_html; |
88
|
0
|
|
|
|
|
|
my Perlbal::Service $svc = $self->{service}; |
89
|
0
|
|
|
|
|
|
$ret .= ": listening on $self->{hostport} for service $svc->{name}"; |
90
|
0
|
|
|
|
|
|
return $ret; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub die_gracefully { |
94
|
|
|
|
|
|
|
# die off so we stop waiting for new connections |
95
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
96
|
0
|
|
|
|
|
|
$self->close('graceful_death'); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Local Variables: |
104
|
|
|
|
|
|
|
# mode: perl |
105
|
|
|
|
|
|
|
# c-basic-indent: 4 |
106
|
|
|
|
|
|
|
# indent-tabs-mode: nil |
107
|
|
|
|
|
|
|
# End: |