2 ## Copyright (c) 2000, Index Data.
4 ## Permission to use, copy, modify, distribute, and sell this software and
5 ## its documentation, in whole or in part, for any purpose, is hereby granted,
8 ## 1. This copyright and permission notice appear in all copies of the
9 ## software and its documentation. Notices of copyright or attribution
10 ## which appear at the beginning of any file must remain unchanged.
12 ## 2. The name of Index Data or the individual authors may not be used to
13 ## endorse or promote products derived from this software without specific
14 ## prior written permission.
16 ## THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
17 ## EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18 ## WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 ## IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
20 ## INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
21 ## WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
22 ## NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 ## LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 ## $Log: SimpleServer.pm,v $
29 ## Revision 1.6 2001-03-13 14:17:15 sondberg
30 ## Added support for GRS-1.
33 package Net::Z3950::SimpleServer;
36 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
43 @ISA = qw(Exporter AutoLoader DynaLoader);
44 # Items to export into callers namespace by default. Note: do not export
45 # names by default without a very good reason. Use EXPORT_OK instead.
46 # Do not simply export all your public functions/methods/constants.
52 bootstrap Net::Z3950::SimpleServer $VERSION;
54 # Preloaded methods go here.
60 my $args = shift || croak "SimpleServer::new: Usage new(argument hash)";
64 carp "SimpleServer.pm: WARNING: Multithreaded server unsupported";
68 $self->{INIT} = $args->{INIT};
69 $self->{SEARCH} = $args->{SEARCH} || croak "SimpleServer.pm: ERROR: Unspecified search handler";
70 $self->{FETCH} = $args->{FETCH} || croak "SimpleServer.pm: ERROR: Unspecified fetch handler";
71 $self->{CLOSE} = $args->{CLOSE};
72 $self->{PRESENT} = $args->{PRESENT};
83 if (defined($self->{INIT})) {
84 set_init_handler($self->{INIT});
86 set_search_handler($self->{SEARCH});
87 set_fetch_handler($self->{FETCH});
88 if (defined($self->{CLOSE})) {
89 set_close_handler($self->{CLOSE});
91 if (defined($self->{PRESENT})) {
92 set_present_handler($self->{PRESENT});
99 # Autoload methods go after =cut, and are processed by the autosplit program.
103 # Below is the stub of documentation for your module. You better edit it!
107 Net::Z3950::SimpleServer - Simple Perl API for building Z39.50 servers.
111 use Net::Z3950::SimpleServer;
113 sub my_search_handler {
116 my $set_id = $args->{SETNAME};
117 my @database_list = @{ $args->{DATABASES} };
118 my $query = $args->{QUERY};
120 ## Perform the query on the specified set of databases
121 ## and return the number of hits:
123 $args->{HITS} = $hits;
126 sub my_fetch_handler { # Get a record for the user
129 my $set_id = $args->{SETNAME};
131 my $record = fetch_a_record($args->{OFFSET);
133 $args->{RECORD} = $record;
134 if (number_of_hits() == $args->{OFFSET}) { ## Last record in set?
142 ## Register custom event handlers:
144 my $handle = Net::Z3950::SimpleServer->new({
145 INIT => \&my_init_handler,
146 CLOSE => \&my_close_handler,
147 SEARCH => \&my_search_handler,
148 FETCH => \&my_fetch_handler
152 $handle->launch_server("ztest.pl", @ARGV);
156 The SimpleServer module is a tool for constructing Z39.50 "Information
157 Retrieval" servers in Perl. The module is easy to use, but it
158 does help to have an understanding of the Z39.50 query
159 structure and the construction of structured retrieval records.
161 Z39.50 is a network protocol for searching remote databases and
162 retrieving the results in the form of structured "records". It is widely
163 used in libraries around the world, as well as in the US Federal Government.
164 In addition, it is generally useful whenever you wish to integrate a number
165 of different database systems around a shared, asbtract data model.
167 The model of the module is simple: It implements a "generic" Z39.50
168 server, which invokes callback functions supplied by you to search
169 for content in your database. You can use any tools available in
170 Perl to supply the content, including modules like DBI and
173 The server will take care of managing the network connections for
174 you, and it will spawn a new process (or thread, in some
175 environments) whenever a new connection is received.
177 The programmer can specify subroutines to take care of the following type
183 - Fetching of records
184 - Closing down connection
186 Note that only the Search and Fetch handler functions are required.
187 The module can supply default responses to the other on its own.
189 After the launching of the server, all control is given away from
190 the Perl script to the server. The server calls the registered
191 subroutines to field incoming requests from Z39.50 clients.
193 A reference to an anonymous hash is passed to each handle. Some of
194 the entries of these hashes are to be considered input and others
197 The Perl programmer specifies the event handles for the server by
198 means of the the SimpleServer object constructor
200 my $handle = Net::Z3950::SimpleServer->new({
201 INIT => \&my_init_handler,
202 CLOSE => \&my_close_handler,
203 SEARCH => \&my_search_handler,
204 PRESENT => \&my_present_handler,
205 FETCH => \&my_fetch_handler });
207 After the custom event handles are declared, the server is launched
208 by means of the method
210 $handle->launch_server("MyServer.pl", @ARGV);
212 Notice, the first argument should be the name of your server
213 script (for logging purposes), while the rest of the arguments
214 are documented in the YAZ toolkit manual: The section on
215 application invocation: <http://www.indexdata.dk/yaz/yaz-7.php>
219 The init handler is called whenever a Z39.50 client is attempting
220 to logon to the server. The exchange of parameters between the
221 server and the handler is carried out via an anonymous hash reached
226 The argument hash passed to the init handler has the form
229 ## Response parameters:
231 IMP_NAME => "", ## Z39.50 Implementation name
232 IMP_VER => "", ## Z39.50 Implementation version
233 ERR_CODE => 0, ## Error code, cnf. Z39.50 manual
234 HANDLE => undef ## Handler of Perl data structure
237 The HANDLE member can be used to store any scalar value which will then
238 be provided as input to all subsequent calls (ie. for searching, record
239 retrieval, etc.). A common use of the handle is to store a reference to
240 a hash which may then be used to store session-specific parameters.
241 If you have any session-specific information (such as a list of
242 result sets or a handle to a back-end search engine of some sort),
243 it is always best to store them in a private session structure -
244 rather than leaving them in global variables in your script.
246 The Implementation name and version are only really used by Z39.50
247 client developers to see what kind of server they're dealing with.
248 Filling these in is optional.
250 The ERR_CODE should be left at 0 (the default value) if you wish to
251 accept the connection. Any other value is interpreted as a failure
252 and the client will be shown the door.
254 =head2 Search handler
256 Similarly, the search handler is called with a reference to an anony-
257 mous hash. The structure is the following:
260 ## Request parameters:
262 HANDLE => ref, ## Your session reference.
263 SETNAME => "id", ## ID of the result set
264 REPL_SET => 0, ## Replace set if already existing?
265 DATABASES => ["xxx"], ## Reference to a list of data-
267 QUERY => "query", ## The query expression
269 ## Response parameters:
271 ERR_CODE => 0, ## Error code (0=Succesful search)
272 ERR_STR => "", ## Error string
273 HITS => 0 ## Number of matches
276 Note that a search which finds 0 hits is considered successful in
277 Z39.50 terms - you should only set the ERR_CODE to a non-zero value
278 if there was a problem processing the request. The Z39.50 standard
279 provides a comprehensive list of standard diagnostic codes, and you
280 should use these whenever possible.
282 The QUERY is a tree-structure of terms combined by operators, the
283 terms being qualified by lists of attributes. The query is presented
284 to the search function in the Prefix Query Format (PQF) which is
285 used in many applications based on the YAZ toolkit. The full grammar
286 is described in the YAZ manual.
288 The following are all examples of valid queries in the PQF.
294 @or "dylan" "zimmerman"
298 @or @and bob dylan @set Result-1
300 @and @attr 1=1 "bob dylan" @attr 1=4 "slow train coming"
302 @attrset @attr 4=1 @attr 1=4 "self portrait"
304 You will need to write a recursive function or something similar to
305 parse incoming query expressions, and this is usually where a lot of
306 the work in writing a database-backend happens. Fortunately, you don't
307 need to support anymore functionality than you want to. For instance,
308 it is perfectly legal to not accept boolean operators, but you SHOULD
309 try to return good error codes if you run into something you can't or
312 =head2 Present handler
314 The presence of a present handler in a SimpleServer front-end is optional.
315 Each time a client wishes to retrieve records, the present service is
316 called. The present service allows the origin to request a certain number
317 of records retrieved from a given result set.
318 When the present handler is called, the front-end server should prepare a
319 result set for fetching. In practice, this means to get access to the
320 data from the backend database and store the data in a temporary fashion
321 for fast and efficient fetching. The present handler does *not* fetch
322 anything. This task is taken care of by the fetch handler, which will be
323 called the correct number of times by the YAZ library. More about this
325 If no present handler is implemented in the front-end, the YAZ toolkit
326 will take care of a minimum of preparations itself. This default present
327 handler is sufficient in many situations, where only a small amount of
328 records are expected to be retrieved. If on the other hand, large result
329 sets are likely to occur, the implementation of a reasonable present
330 handler can gain performance significantly.
332 The informations exchanged between client and present handle are:
335 ## Client/server request:
337 HANDLE => ref, ## Reference to datastructure
338 SETNAME => "id", ## Result set ID
339 START => xxx, ## Start position
340 COMP => "", ## Desired record composition
341 NUMBER => yyy, ## Number of requested records
344 ## Respons parameters:
346 HITS => zzz, ## Number of returned records
347 ERR_CODE => 0, ## Error code
348 ERR_STR => "" ## Error message
354 The fetch handler is asked to retrieve a SINGLE record from a given
355 result set (the front-end server will automatically call the fetch
356 handler as many times as required).
358 The parameters exchanged between the server and the fetch handler are
361 ## Client/server request:
363 HANDLE => ref ## Reference to data structure
364 SETNAME => "id" ## ID of the requested result set
365 OFFSET => nnn ## Record offset number
366 REQ_FORM => "n.m.k.l"## Client requested format OID
367 COMP => "xyz" ## Formatting instructions
371 RECORD => "" ## Record string
372 BASENAME => "" ## Origin of returned record
373 LAST => 0 ## Last record in set?
374 ERR_CODE => 0 ## Error code
375 ERR_STR => "" ## Error string
376 SUR_FLAG => 0 ## Surrogate diagnostic flag
377 REP_FORM => "n.m.k.l"## Provided format OID
380 The REP_FORM value has by default the REQ_FORM value but can be set to
381 something different if the handler desires. The BASENAME value should
382 contain the name of the database from where the returned record originates.
383 The ERR_CODE and ERR_STR works the same way they do in the search
384 handler. If there is an error condition, the SUR_FLAG is used to
385 indicate whether the error condition pertains to the record currently
386 being retrieved, or whether it pertains to the operation as a whole
387 (eg. the client has specified a result set which does not exist.)
389 If you need to return USMARC records, you might want to have a look at
390 the MARC module on CPAN, if you don't already have a way of generating
393 NOTE: The record offset is 1-indexed - 1 is the offset of the first
398 The argument hash recieved by the close handler has one element only:
402 HANDLE => ref ## Reference to data structure
405 What ever data structure the HANDLE value points at goes out of scope
406 after this call. If you need to close down a connection to your server
407 or something similar, this is the place to do it.
411 Anders Sønderberg (sondberg@indexdata.dk) and Sebastian Hammer
412 (quinn@indexdata.dk).
418 Any Perl module which is useful for accessing the database of your