Super-groovy asynchronicity, baby!
[irspy-moved-to-github.git] / test-pod.pl
1 #!/usr/bin/perl -w
2
3 # $Id: test-pod.pl,v 1.3 2006-05-09 16:21:37 mike Exp $
4 #
5 # Run like this:
6 #       ZOOM_RECORD_NO_FORCE_SYNC=1 YAZ_LOG=pod perl -I lib test-pod.pl
7 # (at least until the default sync. behaviour of ZOOM-C changes.)
8
9 use strict;
10 use warnings;
11
12 use ZOOM::Pod;
13
14 my $pod = new ZOOM::Pod("bagel.indexdata.com/gils",
15                         "bagel.indexdata.com/marc",
16                         #"z3950.loc.gov:7090/Voyager",
17                         #"localhost:9999",
18                         );
19 $pod->option(elementSetName => "b");
20 $pod->callback(ZOOM::Event::RECV_SEARCH, \&completed_search);
21 $pod->callback(ZOOM::Event::RECV_RECORD, \&got_record);
22 $pod->search_pqf("the");
23 my $err = $pod->wait();
24 die "$pod->wait() failed with error $err" if $err;
25
26 sub completed_search {
27     my($conn, $state, $rs, $event) = @_;
28     print $conn->option("host"), ": found ", $rs->size(), " records\n";
29     $state->{next_to_fetch} = 0;
30     $state->{next_to_show} = 0;
31     request_record($conn, $rs, $state);
32     return 0;
33 }
34
35 sub got_record {
36     my($conn, $state, $rs, $event) = @_;
37
38     {
39         # Sanity-checking assertions.  These should be impossible
40         my $ns = $state->{next_to_show};
41         my $nf = $state->{next_to_fetch};
42         if ($ns > $nf) {
43             die "next_to_show > next_to_fetch ($ns > $nf)";
44         } elsif ($ns == $nf) {
45             die "next_to_show == next_to_fetch ($ns)";
46         }
47     }
48
49     my $i = $state->{next_to_show}++;
50     my $rec = $rs->record($i);
51     print $conn->option("host"), ": record $i is ", render_record($rec), "\n";
52     request_record($conn, $rs, $state);
53     return 0;
54 }
55
56 sub request_record {
57     my($conn, $rs, $state) = @_;
58
59     my $i = $state->{next_to_fetch}++;
60     my $rec = $rs->record($i);
61     print($conn->option("host"), ": pre-fetch: record $i is ",
62           render_record($rec), "\n");
63 }
64
65 sub render_record {
66     my($rec) = @_;
67
68     return "undefined" if !defined $rec;
69     return "'" . $rec->render() . "'";
70 }