record() may take a Connection instead of a target-string
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy.pm
1 # $Id: IRSpy.pm,v 1.5 2006-06-21 16:09:02 mike Exp $
2
3 package ZOOM::IRSpy;
4
5 use 5.008;
6 use strict;
7 use warnings;
8 use ZOOM::IRSpy::Record;
9 use ZOOM::Pod;
10
11 our @ISA = qw();
12 our $VERSION = '0.02';
13
14 =head1 NAME
15
16 ZOOM::IRSpy - Perl extension for discovering and analysing IR services
17
18 =head1 SYNOPSIS
19
20  use ZOOM::IRSpy;
21  $spy = new ZOOM::IRSpy("target/string/for/irspy/database");
22  print $spy->report_status();
23
24 =head1 DESCRIPTION
25
26 This module exists to implement the IRspy program, which discovers,
27 analyses and monitors IR servers implementing the Z39.50 and SRU/W
28 protocols.  It is a successor to the ZSpy program.
29
30 =cut
31
32 BEGIN {
33     ZOOM::Log::mask_str("irspy");
34     ZOOM::Log::mask_str("irspy_test");
35     ZOOM::Log::mask_str("irspy_debug");
36 }
37
38 sub new {
39     my $class = shift();
40     my($dbname) = @_;
41
42     my $conn = new ZOOM::Connection($dbname)
43         or die "$0: can't connection to IRSpy database 'dbname'";
44
45     my $this = bless {
46         conn => $conn,
47         allrecords => 1,        # unless overridden by targets()
48         query => undef,         # filled in later
49         targets => undef,       # filled in later
50         target2record => undef, # filled in later
51         pod => undef,           # filled in later
52     }, $class;
53     $this->log("irspy", "starting up with database '$dbname'");
54
55     return $this;
56 }
57
58
59 sub log {
60     my $this = shift();
61     ZOOM::Log::log(@_);
62 }
63
64
65 # Explicitly nominate a set of targets to check, overriding the
66 # default which is to re-check everything in the database.  Each
67 # target already in the database results in the existing record being
68 # updated; each new target causes a new record to be added.
69 #
70 sub targets {
71     my $this = shift();
72     my($targetList) = @_;
73
74     $this->log("irspy", "setting explicit list of targets '$targetList'");
75     $this->{allrecords} = 0;
76     my @targets = split /\s+/, $targetList;
77     my @qlist;
78     foreach my $target (@targets) {
79         my($host, $port, $db) = ($target =~ /(.*?):(.*?)\/(.*)/);
80         if (!defined $host) {
81             $port = 210;
82             ($host, $db) = ($target =~ /(.*?)\/(.*)/);
83             my $new = "$host:$port/$db";
84             $this->log("irspy_debug", "rewriting '$target' to '$new'");
85             $target = $new;
86         }
87         die "invalid target string '$target'"
88             if !defined $host;
89         push @qlist,
90             (qq[(host = "$host" and port = "$port" and path="$db")]);
91     }
92
93     $this->{targets} = \@targets;
94     $this->{query} = join(" or ", @qlist);
95 }
96
97
98 # There are two cases.
99 #
100 # 1. A specific set of targets is nominated on the command line.
101 #       - Records must be fetched for those targets that are in the DB
102 #       - New, empty records must be made for those that are not.
103 #       - Updated records written to the DB may or may not be new.
104 #
105 # 2. All records in the database are to be checked.
106 #       - Records must be fetched for all targets in the DB
107 #       - Updated records written to the DB may not be new.
108 #
109 # That's all -- what could be simpler?
110 #
111 sub initialise {
112     my $this = shift();
113
114     my %target2record;
115     if ($this->{allrecords}) {
116         # We need to check on every target in the database, which
117         # means we need to do a "find all".  According to the BIB-1
118         # semantics document at
119         #       http://www.loc.gov/z3950/agency/bib1.html
120         # the query
121         #       @attr 2=103 @attr 1=1035 x
122         # should find all records, but it seems that Zebra doesn't
123         # support this.  Furthermore, when using the "alvis" filter
124         # (as we do for IRSpy) it doesn't support the use of any BIB-1
125         # access point -- not even 1035 "everywhere" -- so instead we
126         # hack together a search that we know will find all records.
127         $this->{query} = "port=?*";
128     } else {
129         # Prepopulate the target map with nulls so that after we fill
130         # in what we can from the database query, we know which target
131         # IDs we need new records for.
132         foreach my $target (@{ $this->{targets} }) {
133             $target2record{lc($target)} = undef;
134         }
135     }
136
137     my $rs = $this->{conn}->search(new ZOOM::Query::CQL($this->{query}));
138     foreach my $i (1 .. $rs->size()) {
139         my $target = _render_record($rs, $i-1, "id");
140         my $zeerex = _render_record($rs, $i-1, "zeerex");
141         $target2record{lc($target)} =
142             new ZOOM::IRSpy::Record($target, $zeerex);
143     }
144
145     foreach my $target (keys %target2record) {
146         my $record = $target2record{$target};
147         if (!defined $record) {
148             $this->log("irspy_debug", "made new record for '$target'");
149             $target2record{$target} = new ZOOM::IRSpy::Record($target);
150         } else {
151             $this->log("irspy_debug", "using existing record for '$target'");
152         }
153     }
154
155     $this->{target2record} = \%target2record;
156     $this->{pod} = new ZOOM::Pod(@{ $this->{targets} });
157     delete $this->{targets};    # The information is now in the Pod.
158     delete $this->{query};      # Not needed at all
159 }
160
161
162 sub _render_record {
163     my($rs, $which, $elementSetName) = @_;
164
165     # There is a slight race condition here on the element-set name,
166     # but it shouldn't be a problem as this is (currently) only called
167     # from parts of the program that run single-threaded.
168     my $old = $rs->option(elementSetName => $elementSetName);
169     my $rec = $rs->record($which);
170     $rs->option(elementSetName => $old);
171
172     return $rec->render();
173 }
174
175
176 # Returns:
177 #       0 all tests successfully run
178 #       1 some tests skipped
179 #
180 sub check {
181     my $this = shift();
182
183     return $this->_run_test("Main");
184 }
185
186
187 sub _run_test {
188     my $this = shift();
189     my($tname) = @_;
190
191     eval {
192         require "ZOOM/IRSpy/Test/$tname.pm";
193     }; if ($@) {
194         $this->log("warn", "can't load test '$tname': skipping",
195                    $@ =~ /^Can.t locate/ ? () : " ($@)");
196         return 1;
197     }
198
199     $this->log("irspy", "running test '$tname'");
200     my $test = "ZOOM::IRSpy::Test::$tname"->new($this);
201     return $test->run();
202 }
203
204
205 # Access methods for the use of Test modules
206 sub pod {
207     my $this = shift();
208     return $this->{pod};
209 }
210
211 sub record {
212     my $this = shift();
213     my($target) = @_;
214
215     if (ref($target) && $target->isa("ZOOM::Connection")) {
216         # Can be called with a Connection instead of a target-name
217         my $conn = $target;
218         $target = $conn->option("host");
219         $this->log("irspy_debug", "record() resolved $conn to '$target'");
220     }
221
222     return $this->{target2record}->{lc($target)};
223 }
224
225
226
227 =head1 SEE ALSO
228
229 ZOOM::IRSpy::Record
230
231 The ZOOM-Perl module,
232 http://search.cpan.org/~mirk/Net-Z3950-ZOOM/
233
234 The Zebra Database,
235 http://indexdata.com/zebra/
236
237 =head1 AUTHOR
238
239 Mike Taylor, E<lt>mike@indexdata.comE<gt>
240
241 =head1 COPYRIGHT AND LICENSE
242
243 Copyright (C) 2006 by Index Data ApS.
244
245 This library is free software; you can redistribute it and/or modify
246 it under the same terms as Perl itself, either Perl version 5.8.7 or,
247 at your option, any later version of Perl 5 you may have available.
248
249 =cut
250
251 1;