Note that ZOOM::Exception should support diagnostic set.
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pm
1 # $Id: ZOOM.pm,v 1.7 2005-10-17 13:47:25 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     ### support diag-set, too
71
72     return bless {
73         code => $code,
74         message => $message,
75         addinfo => $addinfo,
76     }, $class;
77 }
78
79 sub code {
80     my $this = shift();
81     return $this->{code};
82 }
83
84 sub message {
85     my $this = shift();
86     return $this->{message};
87 }
88
89 sub addinfo {
90     my $this = shift();
91     return $this->{addinfo};
92 }
93
94
95 # ----------------------------------------------------------------------------
96
97 package ZOOM::Options;
98
99 sub new {
100     my $class = shift();
101     ### Should use create_with_parent{,2}() depending on arguments
102
103     return bless {
104         _opts => Net::Z3950::ZOOM::options_create(),
105     }, $class;
106 }
107
108 sub _opts {
109     my $this = shift();
110
111     return $this->{_opts};
112 }
113
114 # ----------------------------------------------------------------------------
115
116 package ZOOM::Connection;
117
118 sub new {
119     my $class = shift();
120     my($host, $port) = @_;
121
122     my $_conn = Net::Z3950::ZOOM::connection_new($host, $port);
123     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
124     $errcode = Net::Z3950::ZOOM::connection_error($_conn, $errmsg, $addinfo);
125     die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
126
127     return bless {
128         host => $host,
129         port => $port,
130         _conn => $_conn,
131     };
132 }
133
134 sub create {
135     my $class = shift();
136     my($options) = @_;
137
138     my $_conn = Net::Z3950::ZOOM::connection_create($options->_opts());
139     return bless {
140         host => undef,
141         port => undef,
142         _conn => $_conn,
143     };
144 }
145
146 # PRIVATE within this class
147 sub _conn {
148     my $this = shift();
149
150     my $_conn = $this->{_conn};
151     die "{_conn} undefined: has this ResultSet been destroy()ed?"
152         if !defined $_conn;
153
154     return $_conn;
155 }
156
157 sub error_x {
158     my $this = shift();
159
160     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "dummy", "dummy", "d");
161     $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
162                                                     $addinfo, $diagset);
163     return ($errcode, $errmsg, $addinfo, $diagset);
164 }
165
166 sub errcode {
167     my $this = shift();
168     return Net::Z3950::ZOOM::connection_errcode($this->_conn());
169 }
170
171 sub errmsg {
172     my $this = shift();
173     return Net::Z3950::ZOOM::connection_errmsg($this->_conn());
174 }
175
176 sub addinfo {
177     my $this = shift();
178     return Net::Z3950::ZOOM::connection_addinfo($this->_conn());
179 }
180
181 sub connect {
182     my $this = shift();
183     my($host, $port) = @_;
184
185     Net::Z3950::ZOOM::connection_connect($this->_conn(), $host, $port);
186     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
187     $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
188                                                   $errmsg, $addinfo);
189     die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
190     # No return value
191 }
192
193 sub option {
194     my $this = shift();
195     my($key, $value) = @_;
196
197     my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
198     Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
199         if defined $value;
200
201     return $oldval;
202 }
203
204 sub option_binary {
205     my $this = shift();
206     my($key, $value) = @_;
207
208     my $dummylen = 0;
209     my $oldval = Net::Z3950::ZOOM::connection_option_getl($this->_conn(),
210                                                           $key, $dummylen);
211     Net::Z3950::ZOOM::connection_option_setl($this->_conn(), $key,
212                                              $value, length($value))
213         if defined $value;
214
215     return $oldval;
216 }
217
218
219 sub search_pqf {
220     my $this = shift();
221     my($query) = @_;
222
223     my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $query);
224     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
225     $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
226                                                   $errmsg, $addinfo);
227     die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
228
229     return _new ZOOM::ResultSet($this, $query, $_rs);
230 }
231
232 sub destroy {
233     my $this = shift();
234
235     Net::Z3950::ZOOM::connection_destroy($this->_conn());
236     $this->{_conn} = undef;
237 }
238
239
240 # ----------------------------------------------------------------------------
241
242 package ZOOM::ResultSet;
243
244 sub new {
245     my $class = shift();
246     die "You can't create $class objects directly";
247 }
248
249 # PRIVATE to ZOOM::Connection::search()
250 sub _new {
251     my $class = shift();
252     my($conn, $query, $_rs) = @_;
253
254     return bless {
255         conn => $conn,
256         query => $query,
257         _rs => $_rs,
258     }, $class;
259 }
260
261 # PRIVATE within this class
262 sub _rs {
263     my $this = shift();
264
265     my $_rs = $this->{_rs};
266     die "{_rs} undefined: has this ResultSet been destroy()ed?"
267         if !defined $_rs;
268
269     return $_rs;
270 }
271
272 sub size {
273     my $this = shift();
274
275     return Net::Z3950::ZOOM::resultset_size($this->_rs());
276 }
277
278 sub record {
279     my $this = shift();
280     my($which) = @_;
281
282     my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
283     ### Check for error -- but how?
284
285     # For some reason, I have to use the explicit "->" syntax in order
286     # to invoke the ZOOM::Record constructor here, even though I don't
287     # have to do the same for _new ZOOM::ResultSet above.  Weird.
288     return ZOOM::Record->_new($this, $which, $_rec);
289 }
290
291 sub destroy {
292     my $this = shift();
293
294     Net::Z3950::ZOOM::resultset_destroy($this->_rs());
295     $this->{_rs} = undef;
296 }
297
298
299 # ----------------------------------------------------------------------------
300
301 package ZOOM::Record;
302
303 sub new {
304     my $class = shift();
305     die "You can't create $class objects directly";
306 }
307
308 # PRIVATE to ZOOM::ResultSet::record()
309 sub _new {
310     my $class = shift();
311     my($rs, $which, $_rec) = @_;
312
313     return bless {
314         rs => $rs,
315         which => $which,
316         _rec => $_rec,
317     }, $class;
318 }
319
320 # PRIVATE within this class
321 sub _rec {
322     my $this = shift();
323
324     return $this->{_rec};
325 }
326
327 sub render {
328     my $this = shift();
329
330     my $len = 0;
331     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "render", $len);
332     # I don't think we need '$len' at all.  ### Probably the Perl-to-C
333     # glue code should use the value of `len' as well as the opaque
334     # data-pointer returned, to ensure that the SV contains all of the
335     # returned data and does not stop at the first NUL character in
336     # binary data.  Carefully check the ZOOM_record_get() documentation.
337     return $string;
338 }
339
340 sub raw {
341     my $this = shift();
342
343     my $len = 0;
344     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "raw", $len);
345     # See comment about $len in render()
346     return $string;
347 }
348
349
350 1;