d9674b7e18c21763b3e11aae74d74534ece871f4
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy.pm
1 # $Id: IRSpy.pm,v 1.9 2006-07-21 11:28:16 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, $newtarget) = _parse_target_string($target);
80         if ($newtarget ne $target) {
81             $this->log("irspy_debug", "rewriting '$target' to '$newtarget'");
82             $target = $newtarget; # This written through the ref
83         }
84         push @qlist,
85             (qq[(host = "$host" and port = "$port" and path="$db")]);
86     }
87
88     $this->{targets} = \@targets;
89     $this->{query} = join(" or ", @qlist);
90 }
91
92
93 # Also used by ZOOM::IRSpy::Record
94 sub _parse_target_string {
95     my($target) = @_;
96
97     my($host, $port, $db) = ($target =~ /(.*?):(.*?)\/(.*)/);
98     if (!defined $host) {
99         $port = 210;
100         ($host, $db) = ($target =~ /(.*?)\/(.*)/);
101         $target = "$host:$port/$db";
102     }
103     die "invalid target string '$target'"
104         if !defined $host;
105
106     return ($host, $port, $db, $target);
107 }
108
109
110 # There are two cases.
111 #
112 # 1. A specific set of targets is nominated on the command line.
113 #       - Records must be fetched for those targets that are in the DB
114 #       - New, empty records must be made for those that are not.
115 #       - Updated records written to the DB may or may not be new.
116 #
117 # 2. All records in the database are to be checked.
118 #       - Records must be fetched for all targets in the DB
119 #       - Updated records written to the DB may not be new.
120 #
121 # That's all -- what could be simpler?
122 #
123 sub initialise {
124     my $this = shift();
125
126     my %target2record;
127     if ($this->{allrecords}) {
128         # We need to check on every target in the database, which
129         # means we need to do a "find all".  According to the BIB-1
130         # semantics document at
131         #       http://www.loc.gov/z3950/agency/bib1.html
132         # the query
133         #       @attr 2=103 @attr 1=1035 x
134         # should find all records, but it seems that Zebra doesn't
135         # support this.  Furthermore, when using the "alvis" filter
136         # (as we do for IRSpy) it doesn't support the use of any BIB-1
137         # access point -- not even 1035 "everywhere" -- so instead we
138         # hack together a search that we know will find all records.
139         $this->{query} = "port=?*";
140     } else {
141         # Prepopulate the target map with nulls so that after we fill
142         # in what we can from the database query, we know which target
143         # IDs we need new records for.
144         foreach my $target (@{ $this->{targets} }) {
145             $target2record{lc($target)} = undef;
146         }
147     }
148
149     my $rs = $this->{conn}->search(new ZOOM::Query::CQL($this->{query}));
150     foreach my $i (1 .. $rs->size()) {
151         my $target = _render_record($rs, $i-1, "id");
152         my $zeerex = _render_record($rs, $i-1, "zeerex");
153         $target2record{lc($target)} =
154             new ZOOM::IRSpy::Record($target, $zeerex);
155     }
156
157     foreach my $target (keys %target2record) {
158         my $record = $target2record{$target};
159         if (!defined $record) {
160             $this->log("irspy_debug", "made new record for '$target'");
161             $target2record{$target} = new ZOOM::IRSpy::Record($target);
162         } else {
163             $this->log("irspy_debug", "using existing record for '$target'");
164         }
165     }
166
167     $this->{target2record} = \%target2record;
168     $this->{pod} = new ZOOM::Pod(@{ $this->{targets} });
169     delete $this->{targets};    # The information is now in the Pod.
170     delete $this->{query};      # Not needed at all
171 }
172
173
174 sub _render_record {
175     my($rs, $which, $elementSetName) = @_;
176
177     # There is a slight race condition here on the element-set name,
178     # but it shouldn't be a problem as this is (currently) only called
179     # from parts of the program that run single-threaded.
180     my $old = $rs->option(elementSetName => $elementSetName);
181     my $rec = $rs->record($which);
182     $rs->option(elementSetName => $old);
183
184     return $rec->render();
185 }
186
187
188 # Returns:
189 #       0 all tests successfully run
190 #       1 some tests skipped
191 #
192 sub check {
193     my $this = shift();
194
195     return $this->_run_test("Main");
196 }
197
198
199 sub _run_test {
200     my $this = shift();
201     my($tname) = @_;
202
203     eval {
204         my $slashSeperatedTname = $tname;
205         $slashSeperatedTname =~ s/::/\//g;
206         require "ZOOM/IRSpy/Test/$slashSeperatedTname.pm";
207     }; if ($@) {
208         $this->log("warn", "can't load test '$tname': skipping",
209                    $@ =~ /^Can.t locate/ ? () : " ($@)");
210         return 1;
211     }
212
213     $this->log("irspy", "running test '$tname'");
214     my $test = "ZOOM::IRSpy::Test::$tname"->new($this);
215     return $test->run();
216 }
217
218
219 # Access methods for the use of Test modules
220 sub pod {
221     my $this = shift();
222     return $this->{pod};
223 }
224
225 sub record {
226     my $this = shift();
227     my($target) = @_;
228
229     if (ref($target) && $target->isa("ZOOM::Connection")) {
230         # Can be called with a Connection instead of a target-name
231         my $conn = $target;
232         $target = $conn->option("host");
233     }
234
235     return $this->{target2record}->{lc($target)};
236 }
237
238
239
240 =head1 SEE ALSO
241
242 ZOOM::IRSpy::Record
243
244 The ZOOM-Perl module,
245 http://search.cpan.org/~mirk/Net-Z3950-ZOOM/
246
247 The Zebra Database,
248 http://indexdata.com/zebra/
249
250 =head1 AUTHOR
251
252 Mike Taylor, E<lt>mike@indexdata.comE<gt>
253
254 =head1 COPYRIGHT AND LICENSE
255
256 Copyright (C) 2006 by Index Data ApS.
257
258 This library is free software; you can redistribute it and/or modify
259 it under the same terms as Perl itself, either Perl version 5.8.7 or,
260 at your option, any later version of Perl 5 you may have available.
261
262 =cut
263
264 1;