New, more or less works, but not quite.
[ZOOM-Perl-moved-to-github.git] / samples / net-z3950-zoom / zoomtst3.pl
1 # $Id: zoomtst3.pl,v 1.1 2006-04-06 13:47:55 mike Exp $
2 #
3 # See ../README for a description of this program.
4 # perl -I../../blib/lib -I../../blib/arch zoomtst3.pl <t1> [...] <tN> <query>
5
6 use strict;
7 use warnings;
8 use Net::Z3950::ZOOM;
9
10 if (@ARGV < 2) {
11     print STDERR "Usage: $0 target1 target2 ... targetN query\n";
12     print STDERR "      eg. $0 bagel.indexdata.dk/gils localhost:9999 fish\n";
13     exit 1;
14 }
15
16 my $n = @ARGV-1;
17 my(@z, @r);                     # connections, result sets
18 my $o = Net::Z3950::ZOOM::options_create();
19 Net::Z3950::ZOOM::options_set($o, async => 1);
20
21 # Get first 10 records of result set (using piggyback)
22 Net::Z3950::ZOOM::options_set($o, count => 10);
23
24 # Preferred record syntax
25 Net::Z3950::ZOOM::options_set($o, preferredRecordSyntax => "usmarc");
26 Net::Z3950::ZOOM::options_set($o, elementSetName => "F");
27
28 # Connect to all targets: options are the same for all of them
29 for (my $i = 0; $i < $n; $i++) {
30     $z[$i] = Net::Z3950::ZOOM::connection_create($o);
31     Net::Z3950::ZOOM::connection_connect($z[$i], $ARGV[$i], 0);
32 }
33
34 # Search all
35 for (my $i = 0; $i < $n; $i++) {
36     $r[$i] = Net::Z3950::ZOOM::connection_search_pqf($z[$i], $ARGV[-1]);
37 }
38
39 # Network I/O.  Pass number of connections and array of connections
40 while ((my $i = Net::Z3950::ZOOM::event([ @z ])) != 0) {
41     print("n = ", $i-1, " event = ",
42           Net::Z3950::ZOOM::connection_last_event($z[$i-1]), "\n");
43 }
44
45 # No more to be done.  Inspect results
46 for (my $i = 0; $i < $n; $i++) {
47     my($error, $errmsg, $addinfo) = (undef, "dummy", "dummy");
48     my $tname = $ARGV[$i];
49
50     # Display errors if any
51     $error = Net::Z3950::ZOOM::connection_error($z[$i], $errmsg, $addinfo);
52     if ($error) {
53         print STDERR "$tname error: $errmsg ($error) $addinfo\n";
54         next;
55     }
56
57     # OK, no major errors.  Look at the result count
58     my $size = Net::Z3950::ZOOM::resultset_size($r[$i]);
59     print "$tname: $size hits\n";
60
61     # Go through all records at target
62     $size = 10 if $size > 10;
63     for (my $pos = 0; $pos < $size; $pos++) {
64         my $len; # length of buffer rec
65         print "$tname: fetching $pos of $size\n";
66         my $tmp = Net::Z3950::ZOOM::resultset_record($r[$i], $pos);
67         my $rec = Net::Z3950::ZOOM::record_get($tmp, "render", $len);
68         # if rec is non-null, we got a record for display
69         if (defined $rec) {
70             print $pos+1, "\n", $rec, "\n";
71         }
72     }
73 }
74
75 # Housekeeping
76 for (my $i = 0; $i < $n; $i++) {
77     Net::Z3950::ZOOM::resultset_destroy($r[$i]);
78     Net::Z3950::ZOOM::connection_destroy($z[$i]);
79 }
80
81 Net::Z3950::ZOOM::options_destroy($o);