1 package OpenILS::Utils::ZClient;
2 use UNIVERSAL::require;
6 use overload 'bool' => sub { return $_[0]->{connection} ? 1 : 0 };
9 sub EVENT_CONNECT { 1 }
10 sub EVENT_SEND_DATA { 2 }
11 sub EVENT_RECV_DATA { 3 }
12 sub EVENT_TIMEOUT { 4 }
13 sub EVENT_UNKNOWN { 5 }
14 sub EVENT_SEND_APDU { 6 }
15 sub EVENT_RECV_APDU { 7 }
16 sub EVENT_RECV_RECORD { 8 }
17 sub EVENT_RECV_SEARCH { 9 }
20 our $conn_class = 'ZOOM::Connection';
21 our $imp_class = 'ZOOM';
24 # Detect the installed z client, prefering ZOOM.
25 if (!$imp_class->use()) {
27 $imp_class = 'Net::Z3950'; # Try Net::Z3950
28 if ($imp_class->use()) {
30 # Tell 'new' how to build the connection
31 $conn_class = 'Net::Z3950::Connection';
34 die "Cannot load a z39.50 client implementation! Please install either ZOOM or Net::Z3950.\n";
38 # 'new' is called thusly:
39 # my $conn = OpenILS::Utils::ZClient->new( $host, $port, databaseName => $db, user => $username )
45 if ($class ne __PACKAGE__) { # NOT called OO-ishly
46 # put the first param back if called like OpenILS::Utils::ZClient::new()
47 unshift @args, $class;
50 return bless { connection => $conn_class->new(@_) } => __PACKAGE__;
55 my $r = $imp_class eq 'Net::Z3950' ?
56 $self->{connection}->search( @_ ) :
57 $self->{connection}->search_pqf( @_ );
59 return OpenILS::Utils::ZClient::ResultSet->new( $r );
64 if ($imp_class eq 'Net::Z3950') {
65 if (defined $$list[0]{_async_index}) {
66 return 0 if ($$list[0]{_async_index} == @$list);
67 return ++$$list[0]{_async_index};
69 return $$list[0]{_async_index} = 1;
73 return ZOOM::event([map { ($_->{connection}) } @$list]);
76 *{__PACKAGE__ . '::search_pqf'} = \&search;
81 my $method = $AUTOLOAD;
82 $method =~ s/.*://; # strip fully-qualified portion
84 return $self->{connection}->$method( @_ );
87 #-------------------------------------------------------------------------------
88 package OpenILS::Utils::ZClient::ResultSet;
97 if ($class ne __PACKAGE__) { # NOT called OO-ishly
98 # put the first param back if called like OpenILS::Utils::ZClient::ResultSet::new()
99 unshift @args, $class;
103 return bless { result => $args[0] } => __PACKAGE__;
109 my $r = $imp_class eq 'Net::Z3950' ?
110 $self->{result}->record( ++$offset ) :
111 $self->{result}->record( $offset );
113 return OpenILS::Utils::ZClient::Record->new( $r );
118 return OpenILS::Utils::ZClient::EVENT_END() if ($imp_class eq 'Net::Z3950');
119 $self->{result}->last_event();
125 my $method = $AUTOLOAD;
126 $method =~ s/.*://; # strip fully-qualified portion
128 return $self->{result}->$method( @_ );
131 #-------------------------------------------------------------------------------
132 package OpenILS::Utils::ZClient::Record;
141 if ($class ne __PACKAGE__) { # NOT called OO-ishly
142 # put the first param back if called like OpenILS::Utils::ZClient::ResultSet::new()
143 unshift @args, $class;
147 return bless { record => shift() } => __PACKAGE__;
152 return $OpenILS::Utils::ZClient::imp_class eq 'Net::Z3950' ?
153 $self->{record}->rawdata( @_ ) :
154 $self->{record}->raw( @_ );
157 *{__PACKAGE__ . '::raw'} = \&rawdata;
162 my $method = $AUTOLOAD;
163 $method =~ s/.*://; # strip fully-qualified portion
165 return $self->{record}->$method( @_ );