1 package OpenILS::Utils::ZClient;
2 use UNIVERSAL::require;
4 use overload 'bool' => sub { return $_[0]->{connection} ? 1 : 0 };
7 sub EVENT_CONNECT { 1 }
8 sub EVENT_SEND_DATA { 2 }
9 sub EVENT_RECV_DATA { 3 }
10 sub EVENT_TIMEOUT { 4 }
11 sub EVENT_UNKNOWN { 5 }
12 sub EVENT_SEND_APDU { 6 }
13 sub EVENT_RECV_APDU { 7 }
14 sub EVENT_RECV_RECORD { 8 }
15 sub EVENT_RECV_SEARCH { 9 }
18 our $conn_class = 'ZOOM::Connection';
19 our $imp_class = 'ZOOM';
22 # Detect the installed z client, prefering ZOOM.
23 if (!$imp_class->use()) {
25 $imp_class = 'Net::Z3950'; # Try Net::Z3950
26 if ($imp_class->use()) {
28 # Tell 'new' how to build the connection
29 $conn_class = 'Net::Z3950::Connection';
32 die "Cannot load a z39.50 client implementation! Please install either ZOOM or Net::Z3950.\n";
36 # 'new' is called thusly:
37 # my $conn = OpenILS::Utils::ZClient->new( $host, $port, databaseName => $db, user => $username )
43 if ($class ne __PACKAGE__) { # NOT called OO-ishly
44 # put the first param back if called like OpenILS::Utils::ZClient::new()
45 unshift @args, $class;
48 return bless { connection => $conn_class->new(@_) } => __PACKAGE__;
53 my $r = $imp_class eq 'Net::Z3950' ?
54 $self->{connection}->search( @_ ) :
55 $self->{connection}->search_pqf( @_ );
57 return OpenILS::Utils::ZClient::ResultSet->new( $r );
62 if ($imp_class eq 'Net::Z3950') {
63 if (defined $$list[0]{_async_index}) {
64 return 0 if ($$list[0]{_async_index} == @$list);
65 return ++$$list[0]{_async_index};
67 return $$list[0]{_async_index} = 1;
71 return ZOOM::event([map { ($_->{connection}) } @$list]);
74 *{__PACKAGE__ . '::search_pqf'} = \&search;
79 my $method = $AUTOLOAD;
80 $method =~ s/.*://; # strip fully-qualified portion
82 return $self->{connection}->$method( @_ );
85 #-------------------------------------------------------------------------------
86 package OpenILS::Utils::ZClient::ResultSet;
94 if ($class ne __PACKAGE__) { # NOT called OO-ishly
95 # put the first param back if called like OpenILS::Utils::ZClient::ResultSet::new()
96 unshift @args, $class;
100 return bless { result => $args[0] } => __PACKAGE__;
106 my $r = $imp_class eq 'Net::Z3950' ?
107 $self->{result}->record( ++$offset ) :
108 $self->{result}->record( $offset );
110 return OpenILS::Utils::ZClient::Record->new( $r );
115 return OpenILS::Utils::ZClient::EVENT_END() if ($imp_class eq 'Net::Z3950');
116 $self->{result}->last_event();
122 my $method = $AUTOLOAD;
123 $method =~ s/.*://; # strip fully-qualified portion
125 return $self->{result}->$method( @_ );
128 #-------------------------------------------------------------------------------
129 package OpenILS::Utils::ZClient::Record;
137 if ($class ne __PACKAGE__) { # NOT called OO-ishly
138 # put the first param back if called like OpenILS::Utils::ZClient::ResultSet::new()
139 unshift @args, $class;
143 return bless { record => shift() } => __PACKAGE__;
148 return $OpenILS::Utils::ZClient::imp_class eq 'Net::Z3950' ?
149 $self->{record}->rawdata( @_ ) :
150 $self->{record}->raw( @_ );
153 *{__PACKAGE__ . '::raw'} = \&rawdata;
158 my $method = $AUTOLOAD;
159 $method =~ s/.*://; # strip fully-qualified portion
161 return $self->{record}->$method( @_ );