line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DelayLine; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# $Id: DelayLine.pm,v 1.4 2000/07/22 11:53:48 lth Exp $ |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
8554
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
39
|
|
6
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
78
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
389
|
|
9
|
|
|
|
|
|
|
$VERSION = '0.02'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my %fields = ( |
12
|
|
|
|
|
|
|
_LINE => [], |
13
|
|
|
|
|
|
|
_BORN => undef, |
14
|
|
|
|
|
|
|
DEBUG => 0, |
15
|
|
|
|
|
|
|
DELAY => 0, |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
10
|
|
|
10
|
1
|
131
|
my ($proto, %args) = @_; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# new() can be used as both class and object method |
22
|
10
|
|
66
|
|
|
35
|
my $class = ref($proto) || $proto; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# build object |
25
|
10
|
|
|
|
|
40
|
my $self = bless { |
26
|
|
|
|
|
|
|
%fields, |
27
|
|
|
|
|
|
|
}, $class; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# set creation time |
30
|
10
|
|
|
|
|
28
|
$self->{_BORN} = time(); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# parse arguments |
33
|
10
|
|
|
|
|
19
|
foreach my $arg (keys %args) { |
34
|
8
|
|
|
|
|
17
|
foreach my $attrib (grep {!/^_/} keys %fields) { |
|
32
|
|
|
|
|
76
|
|
35
|
16
|
100
|
|
|
|
258
|
if ($arg =~ /^-?$attrib$/i) { |
36
|
5
|
|
|
|
|
25
|
$self->{$attrib} = delete $args{$arg}; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# complain about unknown arguments |
42
|
10
|
100
|
|
|
|
28
|
if (my @unknown = keys %args) { |
43
|
3
|
|
|
|
|
372
|
croak __PACKAGE__, ": Unknown argument", |
44
|
|
|
|
|
|
|
(@unknown == 1 ? ' ' : 's '), |
45
|
2
|
100
|
|
|
|
8
|
join(', ', map {"'$_'"} @unknown); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
8
|
50
|
|
|
|
21
|
print STDERR "new\n" |
49
|
|
|
|
|
|
|
if $self->{DEBUG}; |
50
|
|
|
|
|
|
|
|
51
|
8
|
|
|
|
|
37
|
$self; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# clone standard attribute accessor methods |
55
|
|
|
|
|
|
|
for my $attrib (grep {!/^_/} keys %fields) { |
56
|
1
|
|
|
1
|
|
7
|
no strict "refs"; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
466
|
|
57
|
|
|
|
|
|
|
*{lc $attrib} = sub { |
58
|
2
|
|
|
2
|
|
101
|
my $self = shift; |
59
|
2
|
|
|
|
|
6
|
my $prev = $self->{$attrib}; |
60
|
2
|
50
|
|
|
|
7
|
$self->{$attrib} = shift if @_; |
61
|
2
|
|
|
|
|
9
|
$prev; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub in { |
66
|
5
|
|
|
5
|
1
|
11
|
my ($self, $obj, $delay) = @_; |
67
|
|
|
|
|
|
|
|
68
|
5
|
|
|
|
|
39
|
my $now = time(); |
69
|
5
|
50
|
|
|
|
14
|
$delay = $self->delay unless defined $delay; |
70
|
|
|
|
|
|
|
|
71
|
5
|
|
|
|
|
28
|
my %entry = ( |
72
|
|
|
|
|
|
|
'OBJ' => $obj, |
73
|
|
|
|
|
|
|
'DELAY' => $delay, |
74
|
|
|
|
|
|
|
'INTIME' => $now, |
75
|
|
|
|
|
|
|
'OUTTIME' => $now + $delay, |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
5
|
50
|
|
|
|
16
|
print STDERR __PACKAGE__, "::in obj='$obj' delay=$entry{DELAY} outtime=$entry{OUTTIME}\n" |
79
|
|
|
|
|
|
|
if $self->{DEBUG}; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# push new object onto delayline |
82
|
5
|
|
|
|
|
8
|
push @{$self->{_LINE}}, \%entry; |
|
5
|
|
|
|
|
24
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# re-sort delayline according to outtime |
85
|
5
|
|
|
|
|
21
|
@{$self->{_LINE}} = sort { |
|
2
|
|
|
|
|
8
|
|
86
|
5
|
|
|
|
|
18
|
$a->{OUTTIME} <=> $b->{OUTTIME} |
87
|
5
|
|
|
|
|
8
|
} @{$self->{_LINE}}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub out { |
91
|
9
|
|
|
9
|
1
|
3001257
|
my ($self) = @_; |
92
|
9
|
|
|
|
|
23
|
my $now = time(); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# return immediately if the DelayLine is empty |
95
|
9
|
50
|
|
|
|
14
|
unless (@{$self->{_LINE}}) { |
|
9
|
|
|
|
|
67
|
|
96
|
0
|
0
|
|
|
|
0
|
print STDERR __PACKAGE__, "::out now=$now empty\n" |
97
|
|
|
|
|
|
|
if $self->{DEBUG}; |
98
|
0
|
|
|
|
|
0
|
return; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# return overdue object |
102
|
9
|
100
|
|
|
|
57
|
if ($self->{_LINE}->[0]->{OUTTIME} <= $now) { |
103
|
5
|
|
|
|
|
9
|
my $obj = (shift @{$self->{_LINE}})->{OBJ}; |
|
5
|
|
|
|
|
18
|
|
104
|
5
|
50
|
|
|
|
29
|
print STDERR __PACKAGE__, "::out now=$now obj='$obj'\n" |
105
|
|
|
|
|
|
|
if $self->{DEBUG}; |
106
|
5
|
|
|
|
|
56
|
return $obj; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# nothing ready yet |
110
|
4
|
50
|
|
|
|
15
|
print STDERR __PACKAGE__, "::out now=$now next=$self->{_LINE}->[0]->{OUTTIME}\n" |
111
|
|
|
|
|
|
|
if $self->{DEBUG}; |
112
|
4
|
|
|
|
|
17
|
return; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |