lib/Alvis/Pipeline/Read.pm

filter: Statementstmt 65.9% 89 / 135 Branchbran 36.1% 26 / 72 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 12 / 15 Podpod 100% 3 / 3 totaltotal 57.7% 130 / 225 CC 37 SCAR 35.4
CC
37
Cov
56%
_read
132.0
_start_server
20.0
_store_file
20.0
CRAP 34.6
↑ Summary lib/Alvis/Logger.pm »
1 # $Id: Read.pm,v 1.12 2006/08/14 17:24:04 mike Exp $
2  
3 package Alvis::Pipeline::Read;
4 5 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 @ISA = qw(Alvis::Pipeline);
6  
7 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 5 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod documented
15 5 my $class = shift();
16 5 my(%opts) = @_;
17  
18 5 my $this = bless {}, $class;
19 $this->{spooldir} = delete $opts{spooldir}
20 5 ▶ or die "new($class) with no spooldir";
Branch: 1 branch
BranchTrueFalseTotal
$this->{"spooldir"} = delete $opts{"spooldir"} or die "new($class) with no spooldir" 4 1 5
21 $this->{port} = delete $opts{port}
22 4 ▶ or die "new($class) with no port";
Branch: 1 branch
BranchTrueFalseTotal
$this->{"port"} = delete $opts{"port"} or die "new($class) with no port" 3 1 4
23  
24 3 $this->_setopts(sleep => 10, %opts);
25  
26 # Asynchronicity: server process accepts pushes and stores them
27 3 $this->log(1, "forking");
28 3 my $pid = fork();
29 3 ▶ die "couldn't fork: $!" if !defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 3 3
30 3 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 3 3
31 # Child process
32 0 $this->_start_server();
33 0 die "_start_server() returned! It should never do that";
34 }
35  
36 # Automatic reaper prevents the child going zombie when we kill
37 # it. (Yes, "IGNORE" has a special-case meaning for SIGCHLD.)
38 3 $SIG{CHLD} = 'IGNORE';
39  
40 3 $this->{pid} = $pid;
41 3 $this->log(1, "parent $$ spawned harvesting child, pid=$pid");
42 3 return $this;
43 }
44  
45  
46 18 ▶sub read {
Subroutine read: called CC=4 SCAR=13.9
Pod documented
47 18 my $this = shift();
48 18 my($block) = @_;
49  
50 18 $this->log(2, "parent reading from spooldir");
51 18 my $dir = $this->{spooldir};
52 18 my($fh, $lastread, $lastwrite) = $this->_lock_and_read($dir);
53 18 ▶ while ($lastread == $lastwrite) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastread == $lastwrite) 5 16 21
54 5 $this->_write_and_unlock($dir, $fh, $lastread, $lastwrite);
55 5 ▶ return undef if !$block;
Branch: 1 branch
BranchTrueFalseTotal
unless $block 2 3 5
56 3 $this->log(2, "no document yet, sleeping");
57 3 sleep $this->option("sleep");
58 3 ($fh, $lastread, $lastwrite) = $this->_lock_and_read($dir);
59 }
60  
61 16 $lastread++;
62 16 my $filename = "$dir/$lastread";
63 16 ▶ my $f2 = new IO::File("<$filename")
Branch: 1 branch
BranchTrueFalseTotal
my $f2 = "IO::File"->new("<$filename") or die "can't read file '${filename}': $!" 16 0 16
64 or die "can't read file '$filename': $!";
65 16 binmode $f2, ":utf8";
66 16 my $doc = join("", <$f2>);
67 16 $f2->close();
68 16 unlink($filename);
69  
70 16 $this->_write_and_unlock($dir, $fh, $lastread, $lastwrite);
71 16 return $doc;
72 }
73  
74  
75 3 ▶sub close {
Subroutine close: called CC=3 SCAR=13.1
Pod documented
76 3 my $this = shift();
77  
78 # We need to kill the child process that is running the OAI server
79 # so that its Internet port is cleared for subsequent invocations.
80 # Also so that the parent can exit cleanly.
81 3 my $pid = $this->{pid};
82  
83 3 kill 15, $pid;
84 3 sleep 1;
85  
86 3 ▶ if (kill 0, $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid) 0 3 3
87 0 warn "kill -15 failed; killing $pid with rude signal 9";
88 0 kill 9, $pid;
89 0 sleep 1;
90 }
91  
92 3 ▶ if (kill 0, $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid) 0 3 3
93 0 die "can't kill child server with pid $pid";
94 }
95 }
96  
97  
98 0 ▶sub _start_server {
Subroutine _start_server: not called CC=4 SCAR=30.0
99 0 my $this = shift();
100  
101 0 $this->log(1, "opening listener on port ", $this->{port});
102 my $listener = new IO::Socket::INET(Listen => 1,
103 LocalPort => $this->{port},
104 Proto => "tcp",
105 ReuseAddr => 1)
106 0 ▶ or die("can't listen on port '" . $this->{port} . "': $!");
Branch: 1 branch
BranchTrueFalseTotal
my $listener = "IO::Socket::INET"->new("Listen", 1, "LocalPort", $this->{"port"}, "Proto", "tcp", "ReuseAddr", 1) or die q[can't listen on port '] . $this->{"port"} . "': $!" 0 0 0
107  
108 0 while (1) {
109 0 $this->log(1, "accepting connection");
110 0 ▶ $this->{socket} = $listener->accept()
Branch: 1 branch
BranchTrueFalseTotal
$this->{"socket"} = $listener->accept or die "can't accept connection: $!" 0 0 0
111 or die "can't accept connection: $!";
112  
113 0 binmode $this->{socket}, ":utf8";
114 0 $this->log(1, "started background process, pid $$");
115 0 while (1) {
116 0 my $doc = $this->_read();
117 0 ▶ last if !defined $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc 0 0 0
118 0 $this->_store_file($doc);
119 }
120 }
121 }
122  
123  
124 0 ▶sub _read {
Subroutine _read: not called CC=11 SCAR=48.8
125 0 my $this = shift();
126  
127 my $s = $this->{socket}
128 0 ▶ or die "$this reading from non-existent socket";
Branch: 1 branch
BranchTrueFalseTotal
my $s = $this->{"socket"} or die "$this reading from non-existent socket" 0 0 0
129  
130 0 my $magic = $s->getline();
131 0 ▶ return undef if !defined $magic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $magic 0 0 0
132 0 ▶ $magic eq "Alvis::Pipeline\n" or die "incorrect magic '$magic'";
Branch: 1 branch
BranchTrueFalseTotal
$magic eq "Alvis::Pipeline\n" or die "incorrect magic '${magic}'" 0 0 0
133 0 ▶ my $version = $s->getline() or die "can't get protocol version: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $version = $s->getline or die "can't get protocol version: $!" 0 0 0
134 0 ▶ $version == 1 or die "unsupported protocol version '$version'";
Branch: 1 branch
BranchTrueFalseTotal
$version == 1 or die "unsupported protocol version '${version}'" 0 0 0
135 0 ▶ my $count = $s->getline() or die "can't get object-length byte-count: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $count = $s->getline or die "can't get object-length byte-count: $!" 0 0 0
136 0 chomp($count);
137 0 my $buf;
138 0 my $nread = $s->read($buf, $count); ### multiple reads may be necessary
139 0 ▶ die "can't read document: $!" if !defined $nread;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nread 0 0 0
140 0 ▶ die "document was short: $nread of $count bytes" if $nread != $count;
Branch: 1 branch
BranchTrueFalseTotal
if $nread != $count 0 0 0
141 0 ▶ my $term = $s->getline() or die "can't get terminator: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $term = $s->getline or die "can't get terminator: $!" 0 0 0
142 0 ▶ $term eq "--end--\n" or die "incorrect terminator '$term'";
Branch: 1 branch
BranchTrueFalseTotal
$term eq "--end--\n" or die "incorrect terminator '${term}'" 0 0 0
143  
144 0 return $buf;
145 }
146  
147  
148 0 ▶sub _store_file {
Subroutine _store_file: not called CC=4 SCAR=30.0
149 0 my $this = shift();
150 0 my($doc) = @_;
151  
152 0 $this->log(2, "child writing to spooldir");
153 0 my $dir = $this->{spooldir};
154 0 my($fh, $lastread, $lastwrite) = $this->_lock_and_read($dir);
155  
156 0 $lastwrite++;
157 0 my $filename = "$dir/$lastwrite";
158 0 ▶ my $f2 = new IO::File(">$filename")
Branch: 1 branch
BranchTrueFalseTotal
my $f2 = "IO::File"->new(">$filename") or die "can't create new file '${filename}': $!" 0 0 0
159 or die "can't create new file '$filename': $!";
160 0 binmode $f2, ":utf8";
161 0 ▶ $f2->print($doc) or die "can't write '$filename': $!";
Branch: 1 branch
BranchTrueFalseTotal
$f2->print($doc) or die "can't write '${filename}': $!" 0 0 0
162 0 ▶ $f2->close() or die "can't close '$filename': $!";
Branch: 1 branch
BranchTrueFalseTotal
$f2->close or die "can't close '${filename}': $!" 0 0 0
163  
164 0 $this->_write_and_unlock($dir, $fh, $lastread, $lastwrite);
165 }
166  
167  
168 # A sequence file called "seq" is maintained in the spool directory,
169 # and is always locked when read and rewritten. The invariant it
170 # preserves between lock-read-write operations is that it contains two
171 # numbers, space-serarate, followed by a newline. The first number is
172 # that of the last document read from the spool directory. The second
173 # number is that of the last document written to the spool directory,
174 # or zero if no document has yet been written. If the two numbers are
175 # equal, there are no documents available to be read.
176 #
177 # _lock_and_read() and _write_and_unlock() together implement safe
178 # maintenance of the sequence file. The former returns a filehandle,
179 # locked; it is the caller's responsibility to unlock the returned
180 # filehandle using _write_and_unlock(), like this:
181 # ($fh, $lastread, $lastwrite) = $this->_lock_and_read($dir);
182 # # Do some stuff
183 # $this->_write_and_unlock($dir, $fh, $lastread, $lastwrite);
184 #
185 21 ▶sub _lock_and_read {
Subroutine _lock_and_read: called CC=8 SCAR=21.1
186 21 my $this = shift();
187 21 my($dir) = @_;
188  
189 21 my $seqfile = "$dir/seq";
190 21 my $fh;
191 21 ▶ if (! -d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 2 19 21
192 2 ▶ mkdir($dir, 0777)
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir, 511 or die "can't create directory '${dir}': $!" 2 0 2
193 or die "can't create directory '$dir': $!";
194 2 ▶ my $f = new IO::File(">$seqfile")
Branch: 1 branch
BranchTrueFalseTotal
my $f = "IO::File"->new(">$seqfile") or die "can't create initial '${seqfile}': $!" 2 0 2
195 or die "can't create initial '$seqfile': $!";
196 2 $f->close();
197 }
198  
199 21 ▶ $fh = new IO::File("+<$seqfile")
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new("+<$seqfile") or die "can't read '${seqfile}': $!" 21 0 21
200 or die "can't read '$seqfile': $!";
201  
202 21 ▶ flock($fh, LOCK_EX) or die "can't lock '$seqfile': $!";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or die "can't lock '${seqfile}': $!" 21 0 21
203 21 ▶ seek($fh, 0, SEEK_SET) or die "can't seek to start of '$seqfile': $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or die "can't seek to start of '${seqfile}': $!" 21 0 21
204 21 my($lastread, $lastwrite);
205 21 my $line = $fh->getline();
206 21 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) { } 19 2 21
207 19 ($lastread, $lastwrite) = ($line =~ /(\d+) (\d+)/);
208 } else {
209 # File is empty: must have just been created
210 2 $lastread = $lastwrite = 0;
211 }
212  
213 21 $this->log(3, "got lastread='$lastread', lastwrite='$lastwrite'");
214 21 return ($fh, $lastread, $lastwrite);
215 }
216  
217  
218 21 ▶sub _write_and_unlock {
Subroutine _write_and_unlock: called CC=5 SCAR=16.6
219 21 my $this = shift();
220 21 my($dir, $fh, $lastread, $lastwrite) = @_;
221  
222 21 my $seqfile = "$dir/seq";
223 5 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
224 21 ▶ seek($fh, 0, SEEK_SET) or confess "can't seek to start of '$seqfile': $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or confess("can't seek to start of '${seqfile}': $!") 21 0 21
225 21 ▶ $fh->print("$lastread $lastwrite\n") or die "can't rewrite '$seqfile': $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->print("$lastread $lastwrite\n") or die "can't rewrite '${seqfile}': $!" 21 0 21
226 21 ▶ flock($fh, LOCK_UN) or die "can't unlock '$seqfile': $!";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 8 or die "can't unlock '${seqfile}': $!" 21 0 21
227 21 ▶ $fh->close() or die "Truly unbelievable";
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or die "Truly unbelievable" 21 0 21
228 21 $this->log(3, "put lastread='$lastread', lastwrite='$lastwrite'");
229 }
230  
231  
232 # Test harness follows
233 # my $p = bless {
234 # spooldir => "/tmp/ap",
235 # }, "Alvis::Pipeline::Read";
236 #
237 # if (@ARGV) {
238 # $p->_store_file(join("", @ARGV));
239 # } else {
240 # my $doc = $p->read();
241 # die "no document queued" if !defined $doc;
242 # print $doc;
243 # }
244  
245 5 1;
↑ Summary lib/Alvis/Logger.pm »