Record.pm

filter: Statementstmt 15.4% 17 / 110 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 26.3% 5 / 19 Podpod 100% 14 / 14 totaltotal 15.1% 36 / 238 CC 39 SCAR 39.3
CC
39
Cov
9%
read_event
110.0
write_event
90.0
new
72.0
CRAP 50.8
↑ Summary Server.pm »
1 =head1 NAME
2  
3 Event::IO::Record - buffered asynchronous I/O, timeouts
4  
5 =head1 METHODS
6  
7 =cut
8 package Event::IO::Record;
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our $VERSION = '0.01';
12  
13 1 ▶use Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Errno qw(:POSIX);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use constant READ_SIZE => 1024; # bytes per read
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 =head2 new ( named parameters... )
21  
22 =over 4
23  
24 =item init
25  
26 If true (default), generate an init_event immediately (otherwise you must
27 call init_event later).
28  
29 =item timeout
30  
31 Default timeout; see Timeout method.
32  
33 =item irs, ors
34  
35 Input/output record separators; default irs => "\r?[\0\n]", ors => "\n".
36  
37 =item handle
38  
39 Handle for connection, should be an IO::Socket object (::INET or ::UNIX).
40  
41 =back
42  
43 =cut
44 0 ▶sub new {
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
45 0 my ($class,%param) = @_;
46 my ($init,$timeout,$irs,$ors,$handle) =
47 0 delete @param{qw[init timeout irs ors handle]};
48 0 ▶ die 'unknown parameter(s): '.(join ', ',keys %param) if keys %param;
Branch: 1 branch
BranchTrueFalseTotal
if keys %param 0 0 0
49  
50 # defaults
51 0 ▶ $init = 1 if not defined $init;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $init 0 0 0
52 0 ▶ $timeout ||= 0;
Condition0%0/2
$timeout ||= 0
l!l
00
MC/DC0%0/1
$timeout ||= 0
$timeout
Truth table0%0/2
A
A= $timeout
Aresult
11
00
53 0 ▶ $irs ||= "\r?[\0\n]";
Condition0%0/2
$irs ||= "\r?[\0\n]"
l!l
00
MC/DC0%0/1
$irs ||= "\r?[\0\n]"
$irs
Truth table0%0/2
A
A= $irs
Aresult
11
00
54 0 ▶ $ors ||= "\n";
Condition0%0/2
$ors ||= "\n"
l!l
00
MC/DC0%0/1
$ors ||= "\n"
$ors
Truth table0%0/2
A
A= $ors
Aresult
11
00
55  
56 # create object
57 0 ▶ my $self = bless { handle => $handle, in => '', out => '',
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
58 timeout => $timeout, irs => $irs, ors => $ors }, ref $class || $class;
59 0 ▶ $self->init_event() if $init;
Branch: 1 branch
BranchTrueFalseTotal
if $init 0 0 0
60  
61 0 return $self
62 }
63  
64  
65 =head2 timeout ( time )
66  
67 Time is the time in seconds; 0 disables; undef reinitializes the current value.
68 We generates a timeout_event when the timer expires.
69  
70 =cut
71 0 ▶sub timeout {
Subroutine timeout: not called CC=5 SCAR=34.0
Pod documented
72 0 my ($self,$time) = @_;
73 0 ▶ $time = $self->{timeout} unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 0 0
74  
75 0 ▶ if($self->{timer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"timer"}) 0 0 0
76 0 $self->{timer}->cancel();
77 0 delete $self->{timer};
78 }
79  
80 0 $self->{timeout} = $time;
81  
82 $self->{timer} =
83 Event->timer(after => $time, cb => [$self,'timeout_event'])
84 0 ▶ if $time and $self->{init};
Branch: 1 branch
BranchTrueFalseTotal
if $time and $self->{"init"} 0 0 0
Condition0%0/3
$time and $self->{"init"}
!ll&&!rl&&r
000
MC/DC0%0/2
$time and $self->{"init"}
$time $self->{"init"}
Truth table0%0/3
A and B
A= $timeB= $self->{"init"}
ABresult
0X0
100
111
85 }
86  
87  
88 =head2 init_event
89  
90 Initialization event, called before anything else happens.
91  
92 =cut
93 0 ▶sub init_event {
Subroutine init_event: not called CC=3 SCAR=24.8
Pod documented
94 0 my $self = shift;
95 0 ▶ warn "@{[ref $self]} initialized twice!" if $self->{init}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"init"}++ 0 0 0
96  
97 # set non-blocking
98 0 ▶ if(my $flags = $self->{handle}->fcntl(F_GETFL,pack '') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $flags = $self->{"handle"}->fcntl(3, "") >= 0) 0 0 0
99 0 $self->{handle}->fcntl(F_SETFL,$flags | O_NONBLOCK);
100 }
101  
102 # set up read/write event watchers and inactivity timeout
103 $self->{read} =
104 0 Event->io(fd => $self->{handle}, poll => 'r', cb => [$self,'read_event']);
105 $self->{write} =
106 0 Event->io(fd => $self->{handle}, poll => 'w', cb => [$self,'write_event'],
107 repeat => 0, parked => 1);
108 0 $self->timeout();
109 }
110  
111  
112 =head2 read_event
113  
114 Data is available for reading. We buffer it up and emit lines to derived
115 classes as C<line_event>s.
116  
117 =cut
118 0 ▶sub read_event {
Subroutine read_event: not called CC=10 SCAR=47.0
Pod documented
119 0 my $self = shift;
120 0 $self->timer(0);
121  
122 # buffer up input until we can't read any more
123 0 my ($data,$frag,$count) = ($self->{in},'',0);
124 0 my $close;
125 0 $self->{in} = '';
126  
127 0 ▶ do {{
Branch: 1 branch
BranchTrueFalseTotal
length $frag == 1024 and do { do { { unless (defined $self->{"handle"}->recv($frag, 1024)) { last if 11 == $! or 11 == $!; next if 4 == $!; warn "@{[ref $self];} socket read error: $!"; $close = "read error: $!"; last; } ; unless (length $frag) { $close = "remote closed socket"; last; } ; $data .= $frag; $count .= length($frag); } } } 0 0 0
128 # undef means we have an error so log it and close
129 0 ▶ unless(defined $self->{handle}->recv($frag,READ_SIZE)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"handle"}->recv($frag, 1024)) 0 0 0
130 0 ▶ last if EAGAIN == $! or EWOULDBLOCK == $!; # no data available
Branch: 1 branch
BranchTrueFalseTotal
if 11 == $! or 11 == $! 0 0 0
Condition0%0/3
11 == $! or 11 == $!
l!l&&r!l&&!r
000
MC/DC0%0/2
11 == $! or 11 == $!
11 == $! 11 == $!
Truth table0%0/3
A or B
A= 11 == $!B= 11 == $!
ABresult
1X1
011
000
131 0 ▶ next if EINTR == $!; # interrupted by signal
Branch: 1 branch
BranchTrueFalseTotal
if 4 == $! 0 0 0
132  
133 # queue up the read error until we've processed what we've read
134 0 warn "@{[ref $self]} socket read error: $!";
135 0 $close = "read error: $!";
136 0 last;
137 }
138  
139 # assume if we got 0 bytes and no error that it's time to bail
140 # if not, we get an infinite sequence of read_events....
141 # don't bail until we've sent the lines that we have, however
142 0 ▶ unless(length $frag) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $frag) 0 0 0
143 0 $close = 'remote closed socket';
144 0 last;
145 }
146  
147 # otherwise append to the existing block and read until we run out of data
148 0 $data .= $frag;
149 0 $count .= length $frag;
150 }} while length $frag == READ_SIZE;
151  
152 # send each line as an event
153 0 my $irs = $self->{irs};
154 0 ▶ while(length $data and $data =~ s/^(.*?)$irs//s) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data and $data =~ s/^(.*?)$irs//s) 0 0 0
Condition0%0/3
length $data and $data =~ s/^(.*?)$irs//s
!ll&&!rl&&r
000
MC/DC0%0/2
length $data and $data =~ s/^(.*?)$irs//s
length $data $data =~ s/^(.*?)$irs//s
Truth table0%0/3
A and B
A= length $dataB= $data =~ s/^(.*?)$irs//s
ABresult
0X0
100
111
155 0 $self->line_event($1);
156 0 $irs = $self->{irs}; # refresh in case line_event changes it
157 }
158 0 $self->{in} = $data;
159  
160 0 $self->timer(1);
161  
162 # if the socket was closed, we can now send the close event
163 0 ▶ $self->close($close) if $close;
Branch: 1 branch
BranchTrueFalseTotal
if $close 0 0 0
164 }
165  
166  
167 =head2 line_event ( line )
168  
169 Override in derived class to process incoming data.
170  
171 =cut
172 0 ▶sub line_event {
Subroutine line_event: not called CC=1 SCAR=0.0
Pod documented
173 }
174  
175  
176 =head2 write( data )
177  
178 Buffered write.
179  
180 =cut
181 0 ▶sub write {
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
182 0 my ($self,$data) = @_;
183 0 $self->{out} .= $data.$self->{ors};
184 0 $self->write_event();
185 }
186  
187  
188 =head2 write_event
189  
190 Write event - handle buffered writes.
191  
192 =cut
193 0 ▶sub write_event {
Subroutine write_event: not called CC=9 SCAR=45.0
Pod documented
194 0 my $self = shift;
195 0 my $data = $self->{out};
196  
197 # send as much as we can from the buffer
198 0 ▶ while(length $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 0 0 0
199 0 my $count = $self->{handle}->send($data);
200 0 ▶ unless(defined $count) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $count) 0 0 0
201 0 ▶ if(EAGAIN == $! or EWOULDBLOCK == $!) { # writing would block
Branch: 1 branch
BranchTrueFalseTotal
if (11 == $! or 11 == $!) 0 0 0
Condition0%0/3
11 == $! or 11 == $!
l!l&&r!l&&!r
000
MC/DC0%0/2
11 == $! or 11 == $!
11 == $! 11 == $!
Truth table0%0/3
A or B
A= 11 == $!B= 11 == $!
ABresult
1X1
011
000
202 0 $self->{write}->start();
203 0 last;
204 }
205 0 ▶ next if EINTR == $!; # interrupted by signal
Branch: 1 branch
BranchTrueFalseTotal
if 4 == $! 0 0 0
206 0 warn "@{[ref $self]} socket write error: $!";
207 0 $self->{out} = $data;
208 0 return $self->close('write error');
209 }
210 0 $data = substr($data,$count);
211 0 ▶ $self->timer(1) if $count; # reinitialize the inactivity timer
Branch: 1 branch
BranchTrueFalseTotal
if $count 0 0 0
212 }
213 0 $self->{out} = $data;
214  
215 # send an event if we've written everything in the buffer
216 0 ▶ $self->sent_event() if not length $data and $self->can('sent_event');
Branch: 1 branch
BranchTrueFalseTotal
if not length $data and $self->can("sent_event") 0 0 0
Condition0%0/3
not length $data and $self->can("sent_event")
!ll&&!rl&&r
000
MC/DC0%0/2
not length $data and $self->can("sent_event")
not length $data $self->can("sent_event")
Truth table0%0/3
not(A) and B
A= not length $dataB= $self->can("sent_event")
ABresult
0X0
100
111
217 }
218  
219  
220 =head2 timer ( enable flag )
221  
222 Disable or restart inactivity timer.
223  
224 =cut
225 0 ▶sub timer {
Subroutine timer: not called CC=3 SCAR=24.8
Pod documented
226 0 my ($self,$enable) = @_;
227 $enable ? $self->{timer}->again() : $self->{timer}->stop()
228 0 ▶ if $self->{timer};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"timer"} 0 0 0
$enable ? : 0 0 0
229 }
230  
231  
232 =head2 timeout_event
233  
234 Inactivity timeout event.
235  
236 =cut
237 0 ▶sub timeout_event {
Subroutine timeout_event: not called CC=1 SCAR=6.9
Pod documented
238 0 my $self = shift;
239 0 $self->error('closing inactive connection after '.
240 0 "@{[$self->{timeout}]} s");
241 0 $self->close('timed out');
242 }
243  
244  
245 =head2 close
246  
247 Remove event handlers, this will close the connection (as long as no other
248 outstanding references exist).
249  
250 =cut
251 0 ▶sub close {
Subroutine close: not called CC=5 SCAR=34.0
Pod documented
252 0 my $self = shift;
253 0 ▶ if($self->{read}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"read"}) 0 0 0
254 0 for my $ev(qw[read write timer]) {
255 0 ▶ (delete $self->{$ev})->cancel() if $self->{$ev};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$ev} 0 0 0
256 }
257 }
258 0 ▶ (delete $self->{handle})->close() if $self->{handle}; # close the socket
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handle"} 0 0 0
259 }
260  
261  
262 =head2 closed
263  
264 Return true iff socket is closed.
265  
266 =cut
267 0 ▶sub closed {
Subroutine closed: not called CC=1 SCAR=6.9
Pod documented
268 0 my $self = shift;
269 return not $self->{read}
270 0 }
271  
272  
273 =head2 error( message )
274  
275 Log error, subclasses may do more.
276  
277 =cut
278 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
279 0 my ($self,$err) = @_;
280 0 warn "@{[ref $self]} error: $err";
281 }
282  
283  
284 =head2 IRS( [ input record separator ] )
285  
286 Get/set input record separator.
287  
288 =cut
289 0 ▶sub IRS {
Subroutine IRS: not called CC=2 SCAR=17.9
Pod documented
290 0 my $self = shift;
291 0 ▶ $self->{irs} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
292 $self->{irs}
293 0 }
294  
295  
296 =head2 ORS( [ output record separator ] )
297  
298 Get/set output record separator.
299  
300 =cut
301 0 ▶sub ORS {
Subroutine ORS: not called CC=2 SCAR=17.9
Pod documented
302 0 my $self = shift;
303 0 ▶ $self->{ors} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
304 $self->{ors}
305 0 }
306  
307  
308 =head1 AUTHOR
309  
310 David B. Robins E<lt>dbrobins@davidrobins.netE<gt>
311  
312 =cut
313  
314  
315 1 1;
↑ Summary Server.pm »