Added enumerations ZOOM::Error and ZOOM::Event.
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pm
index 5d378be..f0c1a82 100644 (file)
-package ZOOM;
+# $Id: ZOOM.pm,v 1.5 2005-10-12 14:33:40 mike Exp $
 
-use 5.008;
 use strict;
 use warnings;
-use AutoLoader qw(AUTOLOAD);
+use Net::Z3950::ZOOM;
 
-our @ISA = qw();
 
-our $VERSION = '0.01';
+# Member naming convention: hash-element names which begin with an
+# underscore represent underlying ZOOM-C object descriptors; those
+# which lack them represent Perl's ZOOM objects.  (The same convention
+# is used in naming local variables where appropriate.)
+#
+# So, for example, the ZOOM::Connection class has an {_conn} element,
+# which is a pointer to the ZOOM-C Connection object; but the
+# ZOOM::ResultSet class has a {conn} element, which is a reference to
+# the Perl-level Connection object by which it was created.  (It may
+# be that we find we have no need for these references, but for now
+# they are retained.)
+#
+# To get at the underlying ZOOM-C connection object of a result-set
+# (if you ever needed to do such a thing, which you probably don't)
+# you'd use $rs->{conn}->_conn().
 
-require XSLoader;
-XSLoader::load('ZOOM', $VERSION);
+# ----------------------------------------------------------------------------
 
-# Preloaded methods go here.
+# The "Error" package contains constants returned as error-codes.
+package ZOOM::Error;
+sub NONE { Net::Z3950::ZOOM::ERROR_NONE }
+sub CONNECT { Net::Z3950::ZOOM::ERROR_CONNECT }
+sub MEMORY { Net::Z3950::ZOOM::ERROR_MEMORY }
+sub ENCODE { Net::Z3950::ZOOM::ERROR_ENCODE }
+sub DECODE { Net::Z3950::ZOOM::ERROR_DECODE }
+sub CONNECTION_LOST { Net::Z3950::ZOOM::ERROR_CONNECTION_LOST }
+sub INIT { Net::Z3950::ZOOM::ERROR_INIT }
+sub INTERNAL { Net::Z3950::ZOOM::ERROR_INTERNAL }
+sub TIMEOUT { Net::Z3950::ZOOM::ERROR_TIMEOUT }
+sub UNSUPPORTED_PROTOCOL { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_PROTOCOL }
+sub UNSUPPORTED_QUERY { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_QUERY }
+sub INVALID_QUERY { Net::Z3950::ZOOM::ERROR_INVALID_QUERY }
 
-# Autoload methods go after =cut, and are processed by the autosplit program.
+# The "Event" package contains constants returned by last_event()
+package ZOOM::Event;
+sub NONE { Net::Z3950::ZOOM::EVENT_NONE }
+sub CONNECT { Net::Z3950::ZOOM::EVENT_CONNECT }
+sub SEND_DATA { Net::Z3950::ZOOM::EVENT_SEND_DATA }
+sub RECV_DATA { Net::Z3950::ZOOM::EVENT_RECV_DATA }
+sub TIMEOUT { Net::Z3950::ZOOM::EVENT_TIMEOUT }
+sub UNKNOWN { Net::Z3950::ZOOM::EVENT_UNKNOWN }
+sub SEND_APDU { Net::Z3950::ZOOM::EVENT_SEND_APDU }
+sub RECV_APDU { Net::Z3950::ZOOM::EVENT_RECV_APDU }
+sub RECV_RECORD { Net::Z3950::ZOOM::EVENT_RECV_RECORD }
+sub RECV_SEARCH { Net::Z3950::ZOOM::EVENT_RECV_SEARCH }
 
-1;
-__END__
-# Below is stub documentation for your module. You'd better edit it!
 
-=head1 NAME
+# ----------------------------------------------------------------------------
+
+package ZOOM::Exception;
+
+sub new {
+    my $class = shift();
+    my($code, $message, $addinfo) = @_;
+
+    return bless {
+       code => $code,
+       message => $message,
+       addinfo => $addinfo,
+    }, $class;
+}
+
+sub code {
+    my $this = shift();
+    return $this->{code};
+}
+
+sub message {
+    my $this = shift();
+    return $this->{message};
+}
+
+sub addinfo {
+    my $this = shift();
+    return $this->{addinfo};
+}
+
+
+# ----------------------------------------------------------------------------
+
+package ZOOM::Connection;
+
+sub new {
+    my $class = shift();
+    my($host, $port) = @_;
+
+    my $_conn = Net::Z3950::ZOOM::connection_new($host, $port);
+    my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
+    $errcode = Net::Z3950::ZOOM::connection_error($_conn, $errmsg, $addinfo);
+    die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
+
+    return bless {
+       host => $host,
+       port => $port,
+       _conn => $_conn,
+    };
+}
+
+# PRIVATE within this class
+sub _conn {
+    my $this = shift();
+
+    my $_conn = $this->{_conn};
+    die "{_conn} undefined: has this ResultSet been destroy()ed?"
+       if !defined $_conn;
+
+    return $_conn;
+}
+
+sub option {
+    my $this = shift();
+    my($key, $value) = @_;
+
+    my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
+    Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
+       if defined $value;
+
+    return $oldval;
+}
 
-ZOOM - Perl extension for blah blah blah
+sub search_pqf {
+    my $this = shift();
+    my($query) = @_;
 
-=head1 SYNOPSIS
+    my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $query);
+    my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
+    $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
+                                                 $errmsg, $addinfo);
+    die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
 
-  use ZOOM;
-  blah blah blah
+    return _new ZOOM::ResultSet($this, $query, $_rs);
+}
 
-=head1 DESCRIPTION
+sub destroy {
+    my $this = shift();
 
-Stub documentation for ZOOM, created by h2xs. It looks like the
-author of the extension was negligent enough to leave the stub
-unedited.
+    Net::Z3950::ZOOM::connection_destroy($this->_conn());
+    $this->{_conn} = undef;
+}
 
-Blah blah blah.
 
+# ----------------------------------------------------------------------------
 
-=head1 SEE ALSO
+package ZOOM::ResultSet;
 
-Mention other useful documentation such as the documentation of
-related modules or operating system documentation (such as man pages
-in UNIX), or any relevant external documentation such as RFCs or
-standards.
+sub new {
+    my $class = shift();
+    die "You can't create $class objects directly";
+}
 
-If you have a mailing list set up for your module, mention it here.
+# PRIVATE to ZOOM::Connection::search()
+sub _new {
+    my $class = shift();
+    my($conn, $query, $_rs) = @_;
 
-If you have a web site set up for your module, mention it here.
+    return bless {
+       conn => $conn,
+       query => $query,
+       _rs => $_rs,
+    }, $class;
+}
 
-=head1 AUTHOR
+# PRIVATE within this class
+sub _rs {
+    my $this = shift();
 
-Mike Taylor, E<lt>mike@E<gt>
+    my $_rs = $this->{_rs};
+    die "{_rs} undefined: has this ResultSet been destroy()ed?"
+       if !defined $_rs;
 
-=head1 COPYRIGHT AND LICENSE
+    return $_rs;
+}
 
-Copyright (C) 2005 by Mike Taylor
+sub size {
+    my $this = shift();
 
-This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself, either Perl version 5.8.4 or,
-at your option, any later version of Perl 5 you may have available.
+    return Net::Z3950::ZOOM::resultset_size($this->_rs());
+}
 
+sub record {
+    my $this = shift();
+    my($which) = @_;
 
-=cut
+    my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
+    ### Check for error -- but how?
+
+    # For some reason, I have to use the explicit "->" syntax in order
+    # to invoke the ZOOM::Record constructor here, even though I don't
+    # have to do the same for _new ZOOM::ResultSet above.  Weird.
+    return ZOOM::Record->_new($this, $which, $_rec);
+}
+
+sub destroy {
+    my $this = shift();
+
+    Net::Z3950::ZOOM::resultset_destroy($this->_rs());
+    $this->{_rs} = undef;
+}
+
+
+# ----------------------------------------------------------------------------
+
+package ZOOM::Record;
+
+sub new {
+    my $class = shift();
+    die "You can't create $class objects directly";
+}
+
+# PRIVATE to ZOOM::ResultSet::record()
+sub _new {
+    my $class = shift();
+    my($rs, $which, $_rec) = @_;
+
+    return bless {
+       rs => $rs,
+       which => $which,
+       _rec => $_rec,
+    }, $class;
+}
+
+# PRIVATE within this class
+sub _rec {
+    my $this = shift();
+
+    return $this->{_rec};
+}
+
+sub render {
+    my $this = shift();
+
+    my $len = 0;
+    my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "render", $len);
+    # I don't think we need '$len' at all.  ### Probably the Perl-to-C
+    # glue code should use the value of `len' as well as the opaque
+    # data-pointer returned, to ensure that the SV contains all of the
+    # returned data and does not stop at the first NUL character in
+    # binary data.  Carefully check the ZOOM_record_get() documentation.
+    return $string;
+}
+
+sub raw {
+    my $this = shift();
+
+    my $len = 0;
+    my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "raw", $len);
+    # See comment about $len in render()
+    return $string;
+}
+
+
+1;