Upfiles/Tie/Handle/Throttle.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 132 SCAR 47.0
CC
10
Cov
0%
TIEHANDLE
12.0
OPEN
12.0
throttle
12.0
CRAP 110.0
1 # Copyright 2012, 2013, 2014, 2015, 2017, 2020 Kevin Ryde
2  
3 # This file is part of Upfiles.
4 #
5 # Upfiles is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Upfiles is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Upfiles. If not, see <http://www.gnu.org/licenses/>.
17  
18 package App::Upfiles::Tie::Handle::Throttle;
19 use 5.004;
20 use strict;
21 use Time::HiRes;
22 use List::Util 'min';
23  
24 use vars '$VERSION';
25 $VERSION = 15;
26  
27 # uncomment this to run the ### lines
28 #use Smart::Comments;
29  
30  
31 sub TIEHANDLE {
32 my $class = shift;
33 ### TIEHANDLE(): @_
34 my $self = bless { blocksize => 4096,
35 upto => 0,
36 last_time => Time::HiRes::time(),
37 @_,
38 }, $class;
39  
40 my $bytes_per_second;
41 if (defined ($bytes_per_second = $self->{'bytes_per_second'})) {
42 } elsif (defined (my $bits_per_second = $self->{'bits_per_second'})) {
43 $bytes_per_second = $bits_per_second/8;
44 } else {
45 $bytes_per_second = 9600/8;
46 }
47 $self->{'period'} = $self->{'blocksize'} / $bytes_per_second;
48  
49 ### blocksize: $self->{'blocksize'}
50 ### period: $self->{'period'}
51  
52 return $self;
53 }
54  
55 sub OPEN {
56 my ($self) = @_;
57 ### OPEN() ...
58  
59 if ($self->{'fh'}) {
60 $self->CLOSE;
61 }
62 # if (! defined $self->{'fh'}) {
63 # require Symbol;
64 # $self->{'fh'} = Symbol::gensym();
65 # }
66 return (@_ == 2
67 ? open($self->{'fh'}, $_[1])
68 : open($self->{'fh'}, $_[1], $_[2]));
69 }
70 sub CLOSE {
71 my ($self) = @_;
72 return close ($self->{'fh'});
73 }
74 sub EOF {
75 my ($self) = @_;
76 return eof($self->{'fh'});
77 }
78 sub TELL {
79 my ($self) = @_;
80 return tell($self->{'fh'});
81 }
82 sub FILENO {
83 my ($self) = @_;
84 return fileno($self->{'fh'});
85 }
86 sub SEEK {
87 my ($self) = @_;
88 return seek($self->{'fh'},$_[1],$_[2]);
89 }
90 sub BINMODE {
91 my ($self) = @_;
92 return binmode($self->{'fh'});
93 }
94  
95 sub READ {
96 ### READ(): $_[2]
97  
98 my $self = $_[0];
99 my $len = $_[2];
100 $self->throttle;
101 my $remaining = $self->{'blocksize'} - $self->{'upto'};
102 $len = min ($len, $remaining);
103 ### $len
104  
105 my $ret = read($self->{'fh'},$_[1],$len);
106 if (defined $ret) {
107 $self->{'upto'} += $ret;
108 }
109 return $ret;
110 }
111 sub READLINE {
112 my ($self) = @_;
113 ### READLINE() ...
114  
115 $self->throttle;
116 my $fh = $self->{'fh'};
117 my $ret = <$fh>;
118 if (defined $ret) {
119 $self->{'upto'} += length($ret);
120 }
121 return $ret;
122 }
123 sub GETC {
124 my ($self) = @_;
125 ### GETC() ...
126  
127 $self->throttle;
128 my $ret = getc($self->{'fh'});
129 if (defined $ret) {
130 $self->{'upto'}++;
131 }
132 return $ret;
133 }
134  
135 sub throttle {
136 my ($self) = @_;
137 my $remaining = $self->{'blocksize'} - $self->{'upto'};
138 if ($remaining <= 0) {
139 my $now = Time::HiRes::time();
140 my $sleep = $self->{'last_time'} + $self->{'period'} - $now;
141 ### $sleep
142 if ($sleep > 0 && $sleep < 5) {
143 Time::HiRes::sleep ($sleep);
144 }
145 $self->{'upto'} = 0;
146 $self->{'last_time'} = $now;
147 }
148 }
149  
150 sub WRITE {
151 die;
152 }
153 sub PRINT {
154 die;
155 }
156 sub PRINTF {
157 die;
158 }
159  
160 1;
161 __END__