Remove extraneous output.
[irspy-moved-to-github.git] / lib / ZOOM / Pod.pm
1 # $Id: Pod.pm,v 1.2 2006-05-09 12:03:37 mike Exp $
2
3 package ZOOM::Pod;
4
5 use strict;
6 use warnings;
7
8 use ZOOM;
9
10 =head1 SYNOPSIS
11
12  $conn1 = new ZOOM::Connection("bagel.indexdata.com/gils");
13  $conn2 = new ZOOM::Connection("z3950.loc.gov:7090/Voyager");
14  $pod = new ZOOM::Pod($conn1, $conn2);
15  $pod->callback(ZOOM::Event::RECV_SEARCH, \&show_result);
16  $pod->search_pqf("mineral");
17  $pod->wait();
18
19  sub show_result {
20      ($conn, $rs, $event) = @_;
21      print "$conn: found ", $rs->size(), " records\n";
22  }
23
24 =cut
25
26 sub new {
27     my $class = shift();
28     my(@conn) = @_;
29
30     foreach my $conn (@conn) {
31         if (!ref $conn) {
32             $conn = new ZOOM::Connection($conn, 0, async => 1);
33         }
34     }
35
36     return bless {
37         conn => \@conn,
38         rs => [],
39         callback => {},
40     }, $class;
41 }
42
43 sub callback {
44     my $this = shift();
45     my($event, $sub) = @_;
46
47     my $old = $this->{callback}->{$event};
48     $this->{callback}->{$event} = $sub
49         if defined $sub;
50
51     return $old;
52 }
53
54 sub search_pqf {
55     my $this = shift();
56     my($pqf) = @_;
57
58     foreach my $i (0..@{ $this->{conn} }-1) {
59         $this->{rs}->[$i] = $this->{conn}->[$i]->search_pqf($pqf);
60     }
61 }
62
63 sub wait {
64     my $this = shift();
65     my $res = 0;
66
67     while ((my $i = ZOOM::event($this->{conn})) != 0) {
68         my $conn = $this->{conn}->[$i-1];
69         my $ev = $conn->last_event();
70         print("connection ", $i-1, ": ", ZOOM::event_str($ev), "\n");
71         my $sub = $this->{callback}->{$ev};
72         if (defined $sub) {
73             $res = &$sub($conn, $this->{rs}->[$i-1], $ev);
74             last if $res != 0;
75         }
76     }
77
78     return $res;
79 }
80
81
82 1;