Update sample command-line comment for moved rules file.
[irspy-moved-to-github.git] / bin / test-net-z3950-zoom.pl
1 #!/usr/bin/perl
2 #
3 # Run the same way as "test-pod.pl".  This is supposed to be an
4 # exactly equivalent program but written using the Net::Z3950::ZOOM
5 # imperative API for asynchronous events directly rather than through
6 # the intermediary of ZOOM::Pod and ZOOM-Perl.
7
8 use strict;
9 use warnings;
10
11 use Net::Z3950::ZOOM;
12
13 if (@ARGV == 0) {
14     printf STDERR "Usage: $0 <target1> [<target2> ...]\n";
15     exit 1;
16 }
17
18 Net::Z3950::ZOOM::yaz_log_mask_str("appl");
19 my @conn;
20 my %rs;                         # maps connection to result-set
21 my %conn2state;                 # maps connection to app. state structure
22 foreach my $target (@ARGV) {
23     my $options = Net::Z3950::ZOOM::options_create();
24     Net::Z3950::ZOOM::options_set($options, async => 1);
25     Net::Z3950::ZOOM::options_set($options, elementSetName => "b");
26     my $conn = Net::Z3950::ZOOM::connection_create($options);
27     Net::Z3950::ZOOM::connection_connect($conn, $target, 0);
28     push @conn, $conn;
29     $rs{$conn} = Net::Z3950::ZOOM::connection_search_pqf($conn, "the");
30 }
31
32 my $res = 0;
33 while ((my $i = Net::Z3950::ZOOM::event(\@conn)) != 0) {
34     my $conn = $conn[$i-1];
35     my $ev = Net::Z3950::ZOOM::connection_last_event($conn);
36     my $evstr = Net::Z3950::ZOOM::event_str($ev);
37     Net::Z3950::ZOOM::yaz_log(Net::Z3950::ZOOM::yaz_log_module_level("pod"),
38                               "connection " . ($i-1) . ": event $ev ($evstr)");
39
40     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
41     $errcode = Net::Z3950::ZOOM::connection_error($conn, $errmsg, $addinfo);
42     die "error $errcode ($errmsg) [$addinfo]"
43         if $errcode != 0;
44
45     if ($ev == Net::Z3950::ZOOM::EVENT_RECV_SEARCH) {
46         $res = completed_search($conn, \%conn2state, $rs{$conn}, $ev);
47         die "recieve search failed with error $res" if $res;
48     } elsif ($ev == Net::Z3950::ZOOM::EVENT_RECV_RECORD) {
49         $res = got_record($conn, \%conn2state, $rs{$conn}, $ev);
50         die "recieve record failed with error $res" if $res;
51     }
52 }
53
54 sub completed_search {
55     my($conn, $arg, $rs, $event) = @_;
56
57     my $host = Net::Z3950::ZOOM::connection_option_get($conn, "host");
58     print "$host : found ", Net::Z3950::ZOOM::resultset_size($rs), " records\n";
59     my %state = (next_to_show => 0, next_to_fetch => 0);
60     request_records($conn, $rs, \%state, 2);
61     $arg->{$host} = \%state;
62     return 0;
63 }
64
65 sub got_record {
66     my($conn, $arg, $rs, $event) = @_;
67
68     my $host = Net::Z3950::ZOOM::connection_option_get($conn, "host");
69     my $state = $arg->{$host};
70
71     {
72         # Sanity-checking assertions.  These should be impossible
73         my $ns = $state->{next_to_show};
74         my $nf = $state->{next_to_fetch};
75         if ($ns > $nf) {
76             die "next_to_show > next_to_fetch ($ns > $nf)";
77         } elsif ($ns == $nf) {
78             die "next_to_show == next_to_fetch ($ns)";
79         }
80     }
81
82     my $i = $state->{next_to_show}++;
83     my $rec = Net::Z3950::ZOOM::resultset_record($rs, $i);
84     print "$host: record $i is ", render_record($rec), "\n";
85     request_records($conn, $rs, $state, 3)
86         if $i == $state->{next_to_fetch}-1;
87
88     return 0;
89 }
90
91 sub request_records {
92     my($conn, $rs, $state, $count) = @_;
93
94     my $host = Net::Z3950::ZOOM::connection_option_get($conn, "host");
95     my $i = $state->{next_to_fetch};
96     Net::Z3950::ZOOM::yaz_log(Net::Z3950::ZOOM::yaz_log_module_level("appl"),
97                               "requesting $count records from $i for $host");
98
99     Net::Z3950::ZOOM::resultset_records($rs, $i, $count, 0);
100     $state->{next_to_fetch} += $count;
101 }
102
103 sub render_record {
104     my($rec) = @_;
105
106     return "undefined" if !defined $rec;
107     my $len = 0;
108     return "'" . Net::Z3950::ZOOM::record_get($rec, "render", $len) . "'";
109 }