1 # $Id: IRSpy.pm,v 1.6 2006-06-21 16:24:55 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) = ($target =~ /(.*?):(.*?)\/(.*)/);
82 ($host, $db) = ($target =~ /(.*?)\/(.*)/);
83 my $new = "$host:$port/$db";
84 $this->log("irspy_debug", "rewriting '$target' to '$new'");
87 die "invalid target string '$target'"
90 (qq[(host = "$host" and port = "$port" and path="$db")]);
93 $this->{targets} = \@targets;
94 $this->{query} = join(" or ", @qlist);
98 # There are two cases.
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.
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.
109 # That's all -- what could be simpler?
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
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=?*";
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;
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);
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);
151 $this->log("irspy_debug", "using existing record for '$target'");
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
163 my($rs, $which, $elementSetName) = @_;
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);
172 return $rec->render();
177 # 0 all tests successfully run
178 # 1 some tests skipped
183 return $this->_run_test("Main");
192 my $slashSeperatedTname = $tname;
193 $slashSeperatedTname =~ s/::/\//g;
194 require "ZOOM/IRSpy/Test/$slashSeperatedTname.pm";
196 $this->log("warn", "can't load test '$tname': skipping",
197 $@ =~ /^Can.t locate/ ? () : " ($@)");
201 $this->log("irspy", "running test '$tname'");
202 my $test = "ZOOM::IRSpy::Test::$tname"->new($this);
207 # Access methods for the use of Test modules
217 if (ref($target) && $target->isa("ZOOM::Connection")) {
218 # Can be called with a Connection instead of a target-name
220 $target = $conn->option("host");
221 $this->log("irspy_debug", "record() resolved $conn to '$target'");
224 return $this->{target2record}->{lc($target)};
233 The ZOOM-Perl module,
234 http://search.cpan.org/~mirk/Net-Z3950-ZOOM/
237 http://indexdata.com/zebra/
241 Mike Taylor, E<lt>mike@indexdata.comE<gt>
243 =head1 COPYRIGHT AND LICENSE
245 Copyright (C) 2006 by Index Data ApS.
247 This library is free software; you can redistribute it and/or modify
248 it under the same terms as Perl itself, either Perl version 5.8.7 or,
249 at your option, any later version of Perl 5 you may have available.