Add diag_str() function.
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pm
1 # $Id: ZOOM.pm,v 1.6 2005-10-12 16:13:20 mike Exp $
2
3 use strict;
4 use warnings;
5 use Net::Z3950::ZOOM;
6
7
8 package ZOOM;
9
10 sub diag_str {
11     my($code) = @_;
12     return Net::Z3950::ZOOM::diag_str($code);
13 }
14
15
16 # Member naming convention: hash-element names which begin with an
17 # underscore represent underlying ZOOM-C object descriptors; those
18 # which lack them represent Perl's ZOOM objects.  (The same convention
19 # is used in naming local variables where appropriate.)
20 #
21 # So, for example, the ZOOM::Connection class has an {_conn} element,
22 # which is a pointer to the ZOOM-C Connection object; but the
23 # ZOOM::ResultSet class has a {conn} element, which is a reference to
24 # the Perl-level Connection object by which it was created.  (It may
25 # be that we find we have no need for these references, but for now
26 # they are retained.)
27 #
28 # To get at the underlying ZOOM-C connection object of a result-set
29 # (if you ever needed to do such a thing, which you probably don't)
30 # you'd use $rs->{conn}->_conn().
31
32 # ----------------------------------------------------------------------------
33
34 # The "Error" package contains constants returned as error-codes.
35 package ZOOM::Error;
36 sub NONE { Net::Z3950::ZOOM::ERROR_NONE }
37 sub CONNECT { Net::Z3950::ZOOM::ERROR_CONNECT }
38 sub MEMORY { Net::Z3950::ZOOM::ERROR_MEMORY }
39 sub ENCODE { Net::Z3950::ZOOM::ERROR_ENCODE }
40 sub DECODE { Net::Z3950::ZOOM::ERROR_DECODE }
41 sub CONNECTION_LOST { Net::Z3950::ZOOM::ERROR_CONNECTION_LOST }
42 sub INIT { Net::Z3950::ZOOM::ERROR_INIT }
43 sub INTERNAL { Net::Z3950::ZOOM::ERROR_INTERNAL }
44 sub TIMEOUT { Net::Z3950::ZOOM::ERROR_TIMEOUT }
45 sub UNSUPPORTED_PROTOCOL { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_PROTOCOL }
46 sub UNSUPPORTED_QUERY { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_QUERY }
47 sub INVALID_QUERY { Net::Z3950::ZOOM::ERROR_INVALID_QUERY }
48
49 # The "Event" package contains constants returned by last_event()
50 package ZOOM::Event;
51 sub NONE { Net::Z3950::ZOOM::EVENT_NONE }
52 sub CONNECT { Net::Z3950::ZOOM::EVENT_CONNECT }
53 sub SEND_DATA { Net::Z3950::ZOOM::EVENT_SEND_DATA }
54 sub RECV_DATA { Net::Z3950::ZOOM::EVENT_RECV_DATA }
55 sub TIMEOUT { Net::Z3950::ZOOM::EVENT_TIMEOUT }
56 sub UNKNOWN { Net::Z3950::ZOOM::EVENT_UNKNOWN }
57 sub SEND_APDU { Net::Z3950::ZOOM::EVENT_SEND_APDU }
58 sub RECV_APDU { Net::Z3950::ZOOM::EVENT_RECV_APDU }
59 sub RECV_RECORD { Net::Z3950::ZOOM::EVENT_RECV_RECORD }
60 sub RECV_SEARCH { Net::Z3950::ZOOM::EVENT_RECV_SEARCH }
61
62
63 # ----------------------------------------------------------------------------
64
65 package ZOOM::Exception;
66
67 sub new {
68     my $class = shift();
69     my($code, $message, $addinfo) = @_;
70
71     return bless {
72         code => $code,
73         message => $message,
74         addinfo => $addinfo,
75     }, $class;
76 }
77
78 sub code {
79     my $this = shift();
80     return $this->{code};
81 }
82
83 sub message {
84     my $this = shift();
85     return $this->{message};
86 }
87
88 sub addinfo {
89     my $this = shift();
90     return $this->{addinfo};
91 }
92
93
94 # ----------------------------------------------------------------------------
95
96 package ZOOM::Connection;
97
98 sub new {
99     my $class = shift();
100     my($host, $port) = @_;
101
102     my $_conn = Net::Z3950::ZOOM::connection_new($host, $port);
103     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
104     $errcode = Net::Z3950::ZOOM::connection_error($_conn, $errmsg, $addinfo);
105     die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
106
107     return bless {
108         host => $host,
109         port => $port,
110         _conn => $_conn,
111     };
112 }
113
114 # PRIVATE within this class
115 sub _conn {
116     my $this = shift();
117
118     my $_conn = $this->{_conn};
119     die "{_conn} undefined: has this ResultSet been destroy()ed?"
120         if !defined $_conn;
121
122     return $_conn;
123 }
124
125 sub option {
126     my $this = shift();
127     my($key, $value) = @_;
128
129     my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
130     Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
131         if defined $value;
132
133     return $oldval;
134 }
135
136 sub search_pqf {
137     my $this = shift();
138     my($query) = @_;
139
140     my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $query);
141     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
142     $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
143                                                   $errmsg, $addinfo);
144     die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
145
146     return _new ZOOM::ResultSet($this, $query, $_rs);
147 }
148
149 sub destroy {
150     my $this = shift();
151
152     Net::Z3950::ZOOM::connection_destroy($this->_conn());
153     $this->{_conn} = undef;
154 }
155
156
157 # ----------------------------------------------------------------------------
158
159 package ZOOM::ResultSet;
160
161 sub new {
162     my $class = shift();
163     die "You can't create $class objects directly";
164 }
165
166 # PRIVATE to ZOOM::Connection::search()
167 sub _new {
168     my $class = shift();
169     my($conn, $query, $_rs) = @_;
170
171     return bless {
172         conn => $conn,
173         query => $query,
174         _rs => $_rs,
175     }, $class;
176 }
177
178 # PRIVATE within this class
179 sub _rs {
180     my $this = shift();
181
182     my $_rs = $this->{_rs};
183     die "{_rs} undefined: has this ResultSet been destroy()ed?"
184         if !defined $_rs;
185
186     return $_rs;
187 }
188
189 sub size {
190     my $this = shift();
191
192     return Net::Z3950::ZOOM::resultset_size($this->_rs());
193 }
194
195 sub record {
196     my $this = shift();
197     my($which) = @_;
198
199     my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
200     ### Check for error -- but how?
201
202     # For some reason, I have to use the explicit "->" syntax in order
203     # to invoke the ZOOM::Record constructor here, even though I don't
204     # have to do the same for _new ZOOM::ResultSet above.  Weird.
205     return ZOOM::Record->_new($this, $which, $_rec);
206 }
207
208 sub destroy {
209     my $this = shift();
210
211     Net::Z3950::ZOOM::resultset_destroy($this->_rs());
212     $this->{_rs} = undef;
213 }
214
215
216 # ----------------------------------------------------------------------------
217
218 package ZOOM::Record;
219
220 sub new {
221     my $class = shift();
222     die "You can't create $class objects directly";
223 }
224
225 # PRIVATE to ZOOM::ResultSet::record()
226 sub _new {
227     my $class = shift();
228     my($rs, $which, $_rec) = @_;
229
230     return bless {
231         rs => $rs,
232         which => $which,
233         _rec => $_rec,
234     }, $class;
235 }
236
237 # PRIVATE within this class
238 sub _rec {
239     my $this = shift();
240
241     return $this->{_rec};
242 }
243
244 sub render {
245     my $this = shift();
246
247     my $len = 0;
248     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "render", $len);
249     # I don't think we need '$len' at all.  ### Probably the Perl-to-C
250     # glue code should use the value of `len' as well as the opaque
251     # data-pointer returned, to ensure that the SV contains all of the
252     # returned data and does not stop at the first NUL character in
253     # binary data.  Carefully check the ZOOM_record_get() documentation.
254     return $string;
255 }
256
257 sub raw {
258     my $this = shift();
259
260     my $len = 0;
261     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "raw", $len);
262     # See comment about $len in render()
263     return $string;
264 }
265
266
267 1;