1 # $Id: IRSpy.pm,v 1.7 2006-07-18 11:10:38 mike Exp $
8 use ZOOM::IRSpy::Record;
12 our $VERSION = '0.02';
16 ZOOM::IRSpy - Perl extension for discovering and analysing IR services
21 $spy = new ZOOM::IRSpy("target/string/for/irspy/database");
22 print $spy->report_status();
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.
33 ZOOM::Log::mask_str("irspy");
34 ZOOM::Log::mask_str("irspy_test");
35 ZOOM::Log::mask_str("irspy_debug");
42 my $conn = new ZOOM::Connection($dbname)
43 or die "$0: can't connection to IRSpy database 'dbname'";
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
53 $this->log("irspy", "starting up with database '$dbname'");
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.
74 $this->log("irspy", "setting explicit list of targets '$targetList'");
75 $this->{allrecords} = 0;
76 my @targets = split /\s+/, $targetList;
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; ### Does this get written through the ref?
85 (qq[(host = "$host" and port = "$port" and path="$db")]);
88 $this->{targets} = \@targets;
89 $this->{query} = join(" or ", @qlist);
93 # Also used by ZOOM::IRSpy::Record
94 sub _parse_target_string {
97 my($host, $port, $db) = ($target =~ /(.*?):(.*?)\/(.*)/);
100 ($host, $db) = ($target =~ /(.*?)\/(.*)/);
101 $target = "$host:$port/$db";
103 die "invalid target string '$target'"
106 return ($host, $port, $db, $target);
110 # There are two cases.
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.
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.
121 # That's all -- what could be simpler?
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
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=?*";
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;
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);
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);
163 $this->log("irspy_debug", "using existing record for '$target'");
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
175 my($rs, $which, $elementSetName) = @_;
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);
184 return $rec->render();
189 # 0 all tests successfully run
190 # 1 some tests skipped
195 return $this->_run_test("Main");
204 my $slashSeperatedTname = $tname;
205 $slashSeperatedTname =~ s/::/\//g;
206 require "ZOOM/IRSpy/Test/$slashSeperatedTname.pm";
208 $this->log("warn", "can't load test '$tname': skipping",
209 $@ =~ /^Can.t locate/ ? () : " ($@)");
213 $this->log("irspy", "running test '$tname'");
214 my $test = "ZOOM::IRSpy::Test::$tname"->new($this);
219 # Access methods for the use of Test modules
229 if (ref($target) && $target->isa("ZOOM::Connection")) {
230 # Can be called with a Connection instead of a target-name
232 $target = $conn->option("host");
233 $this->log("irspy_debug", "record() resolved $conn to '$target'");
236 return $this->{target2record}->{lc($target)};
245 The ZOOM-Perl module,
246 http://search.cpan.org/~mirk/Net-Z3950-ZOOM/
249 http://indexdata.com/zebra/
253 Mike Taylor, E<lt>mike@indexdata.comE<gt>
255 =head1 COPYRIGHT AND LICENSE
257 Copyright (C) 2006 by Index Data ApS.
259 This library is free software; you can redistribute it and/or modify
260 it under the same terms as Perl itself, either Perl version 5.8.7 or,
261 at your option, any later version of Perl 5 you may have available.