Default context set is "net" (not "zeerex")
[irspy-moved-to-github.git] / test-pod.pl
1 #!/usr/bin/perl -w
2
3 # $Id: test-pod.pl,v 1.7 2006-05-10 15:55:19 mike Exp $
4 #
5 # Run like this:
6 #       YAZ_LOG=pod perl -I lib test-pod.pl "bagel.indexdata.com/gils" "bagel.indexdata.com/marc"
7
8 use strict;
9 use warnings;
10
11 use ZOOM::Pod;
12
13 if (@ARGV == 0) {
14     printf STDERR "Usage: $0 <target1> [<target2> ...]\n";
15     exit 1;
16 }
17
18 ZOOM::Log::mask_str("appl");
19 my $pod = new ZOOM::Pod(@ARGV);
20 $pod->option(elementSetName => "b");
21 $pod->callback(ZOOM::Event::RECV_SEARCH, \&completed_search);
22 $pod->callback(ZOOM::Event::RECV_RECORD, \&got_record);
23 #$pod->callback(exception => \&exception_thrown);
24 $pod->search_pqf("the");
25 my $err = $pod->wait();
26 die "$pod->wait() failed with error $err" if $err;
27
28 sub completed_search {
29     my($conn, $state, $rs, $event) = @_;
30     print $conn->option("host"), ": found ", $rs->size(), " records\n";
31     $state->{next_to_fetch} = 0;
32     $state->{next_to_show} = 0;
33     request_records($conn, $rs, $state, 2);
34     return 0;
35 }
36
37 sub got_record {
38     my($conn, $state, $rs, $event) = @_;
39
40     {
41         # Sanity-checking assertions.  These should be impossible
42         my $ns = $state->{next_to_show};
43         my $nf = $state->{next_to_fetch};
44         if ($ns > $nf) {
45             die "next_to_show > next_to_fetch ($ns > $nf)";
46         } elsif ($ns == $nf) {
47             die "next_to_show == next_to_fetch ($ns)";
48         }
49     }
50
51     my $i = $state->{next_to_show}++;
52     my $rec = $rs->record($i);
53     print $conn->option("host"), ": record $i is ", render_record($rec), "\n";
54     request_records($conn, $rs, $state, 3)
55         if $i == $state->{next_to_fetch}-1;
56
57     return 0;
58 }
59
60 sub exception_thrown {
61     my($conn, $state, $rs, $exception) = @_;
62     print "Uh-oh!  $exception\n";
63     return 0;
64 }
65
66 sub request_records {
67     my($conn, $rs, $state, $count) = @_;
68
69     my $i = $state->{next_to_fetch};
70     ZOOM::Log::log("appl", "requesting $count records from $i");
71     $rs->records($i, $count, 0);
72     $state->{next_to_fetch} += $count;
73 }
74
75 sub render_record {
76     my($rec) = @_;
77
78     return "undefined" if !defined $rec;
79     return "'" . $rec->render() . "'";
80 }