| 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
| |||||||||||
| 21 | $this->{port} = delete $opts{port} | ||||||||||
| 22 | 4 | ▶ | or die "new($class) with no port"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 30 | 3 | ▶ | if ($pid == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 54 | 5 | $this->_write_and_unlock($dir, $fh, $lastread, $lastwrite); | |||||||||
| 55 | 5 | ▶ | return undef if !$block; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 107 | |||||||||||
| 108 | 0 | while (1) { | |||||||||
| 109 | 0 | $this->log(1, "accepting connection"); | |||||||||
| 110 | 0 | ▶ | $this->{socket} = $listener->accept() | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 129 | |||||||||||
| 130 | 0 | my $magic = $s->getline(); | |||||||||
| 131 | 0 | ▶ | return undef if !defined $magic; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 0 | ▶ | $magic eq "Alvis::Pipeline\n" or die "incorrect magic '$magic'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 0 | ▶ | my $version = $s->getline() or die "can't get protocol version: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 0 | ▶ | $version == 1 or die "unsupported protocol version '$version'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | ▶ | my $count = $s->getline() or die "can't get object-length byte-count: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 140 | 0 | ▶ | die "document was short: $nread of $count bytes" if $nread != $count; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 0 | ▶ | my $term = $s->getline() or die "can't get terminator: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | 0 | ▶ | $term eq "--end--\n" or die "incorrect terminator '$term'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 162 | 0 | ▶ | $f2->close() or die "can't close '$filename': $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 192 | 2 | ▶ | mkdir($dir, 0777) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 193 | or die "can't create directory '$dir': $!"; | ||||||||||
| 194 | 2 | ▶ | my $f = new IO::File(">$seqfile") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 195 | or die "can't create initial '$seqfile': $!"; | ||||||||||
| 196 | 2 | $f->close(); | |||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | 21 | ▶ | $fh = new IO::File("+<$seqfile") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 200 | or die "can't read '$seqfile': $!"; | ||||||||||
| 201 | |||||||||||
| 202 | 21 | ▶ | flock($fh, LOCK_EX) or die "can't lock '$seqfile': $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | 21 | ▶ | seek($fh, 0, SEEK_SET) or die "can't seek to start of '$seqfile': $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 204 | 21 | my($lastread, $lastwrite); | |||||||||
| 205 | 21 | my $line = $fh->getline(); | |||||||||
| 206 | 21 | ▶ | if (defined $line) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 225 | 21 | ▶ | $fh->print("$lastread $lastwrite\n") or die "can't rewrite '$seqfile': $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 226 | 21 | ▶ | flock($fh, LOCK_UN) or die "can't unlock '$seqfile': $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 227 | 21 | ▶ | $fh->close() or die "Truly unbelievable"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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; | |||||||||