| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | # $Id: alvis-zsink.pl,v 1.6 2006/08/31 16:13:16 mike Exp $ | ||
| 4 | # | ||
| 5 | # This is an Alvis Sink -- that is, a program that sits at the end of | ||
| 6 | # an Alvis pipeline absorbing documents that are fed to it from an | ||
| 7 | # Alvis Source, most likely through a series of one or more filters | ||
| 8 | # that add semantic annotation. It deals with the records by feeding | ||
| 9 | # them to an Extended-Services-compliant Z39.50 server, most likely | ||
| 10 | # Zebra. | ||
| 11 | # | ||
| 12 | # The program needs to be told what port to listen on (for the | ||
| 13 | # pipeline) and what host/port to write to (for the Z39.50 server). | ||
| 14 | # For example, to listen on port 8021 and feed documents to a server | ||
| 15 | # on localhost:1314, use: | ||
| 16 | # alvis-zsink.pl 8021 localhost:1314 | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | use Alvis::Pipeline 0.07; | ||
| 21 | use Net::Z3950::ZOOM 1.11; # We don't use this, but it's ZOOM's release | ||
| 22 | use ZOOM; | ||
| 23 | |||
| 24 | if (@ARGV != 2 && @ARGV != 4) { | ||
| 25 | print STDERR <<__EOT__; | ||
| 26 | Usage: $0 <listen-port> <z-host> [<user> <password>] | ||
| 27 | <listen-port> is the port on which to listen for documents being | ||
| 28 | fed down the Alvis pipeline; <z-host> is ZOOM-style Z39.50 host | ||
| 29 | string such as 'localhost:1314' or 'tcp:alvis.indexdata.com:8122' | ||
| 30 | __EOT__ | ||
| 31 | exit 1; | ||
| 32 | } | ||
| 33 | |||
| 34 | my($port, $zhost, $user, $password) = @ARGV; | ||
| 35 | my $pipe = new Alvis::Pipeline::Read(port => $port, | ||
| 36 | spooldir => "/tmp/alvis-spool") | ||
| 37 | or die "can't create read-pipe on port $port: $!"; | ||
| 38 | $pipe->option(sleep => 1); | ||
| 39 | print "Listening on pipeline\n"; | ||
| 40 | |||
| 41 | my $options = new ZOOM::Options(); | ||
| 42 | $options->option(user => $user) if defined $user; | ||
| 43 | $options->option(password => $password) if defined $password; | ||
| 44 | my $conn = create ZOOM::Connection($options); | ||
| 45 | my $connected = 0; | ||
| 46 | |||
| 47 | my $n = 0; | ||
| 48 | $| = 1; | ||
| 49 | while (my $xml = $pipe->read(1)) { | ||
| 50 | eval { | ||
| 51 | $conn->connect($zhost); | ||
| 52 | }; if ($@ && ref $@ && $@->isa("ZOOM::Exception") && | ||
| 53 | $@->diagset() eq "Bib-1" && $@->code() == 224) { | ||
| 54 | # Do nothing: this allows for a bug in ZOOM-C (as of YAZ | ||
| 55 | # version 2.1.27) whereby a no-opping call to connect() does | ||
| 56 | # not clear any old error indication. | ||
| 57 | print "ignoring re-occurrence of old error in connect()\n"; | ||
| 58 | } elsif ($@) { | ||
| 59 | die $@; # re-throw | ||
| 60 | } | ||
| 61 | |||
| 62 | if (!$connected) { | ||
| 63 | print "connected to Z39.50 server\n"; | ||
| 64 | $connected = 1; | ||
| 65 | } | ||
| 66 | print "got document ", ++$n, " ... "; | ||
| 67 | |||
| 68 | eval { | ||
| 69 | my $p = $conn->package(); | ||
| 70 | $p->option(action => "specialUpdate"); | ||
| 71 | $p->option(record => $xml); | ||
| 72 | eval { $p->send("update"); }; | ||
| 73 | if ($@) { die $@; } | ||
| 74 | print "sent package ... "; | ||
| 75 | $p->destroy(); | ||
| 76 | $p = $conn->package(); | ||
| 77 | $p->option(action => "commit"); | ||
| 78 | $p->send("commit"); | ||
| 79 | print "commit ... "; | ||
| 80 | }; | ||
| 81 | |||
| 82 | if (!$@) { | ||
| 83 | print "added document\n"; | ||
| 84 | next; | ||
| 85 | } elsif (!ref $@ || !$@->isa("ZOOM::Exception")) { | ||
| 86 | # A non-ZOOM error, which is totally unexepected. Treat this | ||
| 87 | # as fatal: we need to shut the read-pipe down properly so | ||
| 88 | # that the spooling child process is killed. | ||
| 89 | $pipe->close(); | ||
| 90 | die $@; | ||
| 91 | } else { | ||
| 92 | # A ZOOM error, e.g. BIB-1 224, "ES: immediate execution | ||
| 93 | # failed". Most such cases need not be treated as fatal, so | ||
| 94 | # we just log it and continue to listen for subsequent | ||
| 95 | # documents. | ||
| 96 | print "failed\n"; | ||
| 97 | warn "discarding record: $@\n"; | ||
| 98 | } | ||
| 99 | } |