Better "no" link.
[irspy-moved-to-github.git] / bin / test-zoom.pl
1 #!/usr/bin/perl -w
2
3 # $Id: test-zoom.pl,v 1.1 2006-07-19 11:40:26 mike Exp $
4 #
5 # Run the same way as "test-pod.pl".  This is supposed to be an
6 # exactly equivalent program but written using the ZOOM-Perl
7 # asynchronous-event API directly rather than through the intermediary
8 # of ZOOM::Pod.
9
10 use strict;
11 use warnings;
12
13 use ZOOM;
14
15 if (@ARGV == 0) {
16     printf STDERR "Usage: $0 <target1> [<target2> ...]\n";
17     exit 1;
18 }
19
20 ZOOM::Log::mask_str("appl");
21 my @conn;
22 my %rs;                         # maps connection to result-set
23 my %state;                      # maps connection to app. state structure
24 foreach my $target (@ARGV) {
25     my $conn = new ZOOM::Connection($target, 0, async => 1,
26                                     elementSetName => "b");
27     push @conn, $conn;
28     $rs{$conn} = $conn->search_pqf("the");
29 }
30
31 my $res = 0;
32 while ((my $i = ZOOM::event(\@conn)) != 0) {
33     my $conn = $conn[$i-1];
34     my $ev = $conn->last_event();
35     my $evstr = ZOOM::event_str($ev);
36     ZOOM::Log::log("pod", "connection ", $i-1, ": event $ev ($evstr)");
37     $conn->_check();            # die if any errors occur
38
39     if ($ev == ZOOM::Event::RECV_SEARCH) {
40         $res = completed_search($conn, \%state, $rs{$conn}, $ev);
41         die "recieve search failed with error $res" if $res;
42     } elsif ($ev == ZOOM::Event::RECV_RECORD) {
43         $res = got_record($conn, \%state, $rs{$conn}, $ev);
44         die "recieve record failed with error $res" if $res;
45     }
46 }
47
48
49
50
51 sub completed_search {
52     my($conn, $arg, $rs, $event) = @_;
53
54     my $host = $conn->option("host");
55     print "$host : found ", $rs->size(), " records\n";
56     my %state = (next_to_show => 0, next_to_fetch => 0);
57     request_records($conn, $rs, \%state, 2);
58     $arg->{$host} = \%state;
59     return 0;
60 }
61
62 sub got_record {
63     my($conn, $arg, $rs, $event) = @_;
64
65     my $host = $conn->option("host");
66     my $state = $arg->{$host};
67
68     {
69         # Sanity-checking assertions.  These should be impossible
70         my $ns = $state->{next_to_show};
71         my $nf = $state->{next_to_fetch};
72         if ($ns > $nf) {
73             die "next_to_show > next_to_fetch ($ns > $nf)";
74         } elsif ($ns == $nf) {
75             die "next_to_show == next_to_fetch ($ns)";
76         }
77     }
78
79     my $i = $state->{next_to_show}++;
80     my $rec = $rs->record($i);
81     print "$host: record $i is ", render_record($rec), "\n";
82     request_records($conn, $rs, $state, 3)
83         if $i == $state->{next_to_fetch}-1;
84
85     return 0;
86 }
87
88 sub request_records {
89     my($conn, $rs, $state, $count) = @_;
90
91     my $i = $state->{next_to_fetch};
92     ZOOM::Log::log("appl", "requesting $count records from $i for ",
93                    $conn->option("host"));
94     $rs->records($i, $count, 0);
95     $state->{next_to_fetch} += $count;
96 }
97
98 sub render_record {
99     my($rec) = @_;
100
101     return "undefined" if !defined $rec;
102     return "'" . $rec->render() . "'";
103 }