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 package Net::Z3950::SimpleServer;
31 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
38 @ISA = qw(Exporter AutoLoader DynaLoader);
39 # Items to export into callers namespace by default. Note: do not export
40 # names by default without a very good reason. Use EXPORT_OK instead.
41 # Do not simply export all your public functions/methods/constants.
47 bootstrap Net::Z3950::SimpleServer $VERSION;
49 # Preloaded methods go here.
55 my $args = shift || croak "SimpleServer::new: Usage new(argument hash)";
59 carp "SimpleServer.pm: WARNING: Multithreaded server unsupported";
63 $self->{INIT} = $args->{INIT};
64 $self->{SEARCH} = $args->{SEARCH} || croak "SimpleServer.pm: ERROR: Unspecified search handler";
65 $self->{FETCH} = $args->{FETCH} || croak "SimpleServer.pm: ERROR: Unspecified fetch handler";
66 $self->{CLOSE} = $args->{CLOSE};
77 if (defined($self->{INIT})) {
78 set_init_handler($self->{INIT});
80 set_search_handler($self->{SEARCH});
81 set_fetch_handler($self->{FETCH});
82 if (defined($self->{CLOSE})) {
83 set_close_handler($self->{CLOSE});
90 # Autoload methods go after =cut, and are processed by the autosplit program.
94 # Below is the stub of documentation for your module. You better edit it!
98 Net::Z3950::SimpleServer - Simple Perl API for building Z39.50 servers.
102 use Net::Z3950::SimpleServer;
104 sub my_search_handler {
107 my $set_id = $args->{SETNAME};
108 my @database_list = @{ $args->{DATABASES} };
109 my $query = $args->{QUERY};
111 ## Perform the query on the specified set of databases
112 ## and return the number of hits:
114 $args->{HITS} = $hits;
117 sub my_fetch_handler { # Get a record for the user
120 my $set_id = $args->{SETNAME};
122 my $record = fetch_a_record($args->{OFFSET);
124 $args->{RECORD} = $record;
125 if (number_of_hits() == $args->{OFFSET}) { ## Last record in set?
133 ## Register custom event handlers:
135 my $handle = Net::Z3950::SimpleServer->new({
136 INIT => \&my_init_handler,
137 CLOSE => \&my_close_handler,
138 SEARCH => \&my_search_handler,
139 FETCH => \&my_fetch_handler
143 $handle->launch_server("ztest.pl", @ARGV);
147 The SimpleServer module is a tool for constructing Z39.50 "Information
148 Retrieval" servers in Perl. The module is easy to use, but it
149 does help to have an understanding of the Z39.50 query
150 structure and the construction of structured retrieval records.
152 Z39.50 is a network protocol for searching remote databases and
153 retrieving the results in the form of structured "records". It is widely
154 used in libraries around the world, as well as in the US Federal Government.
155 In addition, it is generally useful whenever you wish to integrate a number
156 of different database systems around a shared, asbtract data model.
158 The model of the module is simple: It implements a "generic" Z39.50
159 server, which invokes callback functions supplied by you to search
160 for content in your database. You can use any tools available in
161 Perl to supply the content, including modules like DBI and
164 The server will take care of managing the network connections for
165 you, and it will spawn a new process (or thread, in some
166 environments) whenever a new connection is received.
168 The programmer can specify subroutines to take care of the following type
173 - Fetching of records
174 - Closing down connection
176 Note that only the Search and Fetch handler functions are required.
177 The module can supply default responses to the other on its own.
179 After the launching of the server, all control is given away from
180 the Perl script to the server. The server calls the registered
181 subroutines to field incoming requests from Z39.50 clients.
183 A reference to an anonymous hash is passed to each handle. Some of
184 the entries of these hashes are to be considered input and others
187 The Perl programmer specifies the event handles for the server by
188 means of the the SimpleServer object constructor
190 my $handle = Net::Z3950::SimpleServer->new({
191 INIT => \&my_init_handler,
192 CLOSE => \&my_close_handler,
193 SEARCH => \&my_search_handler,
194 FETCH => \&my_fetch_handler });
196 After the custom event handles are declared, the server is launched
197 by means of the method
199 $handle->launch_server("MyServer.pl", @ARGV);
201 Notice, the first argument should be the name of your server
202 script (for logging purposes), while the rest of the arguments
203 are documented in the YAZ toolkit manual: The section on
204 application invocation: <http://www.indexdata.dk/yaz/yaz-7.php>
208 The init handler is called whenever a Z39.50 client is attempting
209 to logon to the server. The exchange of parameters between the
210 server and the handler is carried out via an anonymous hash reached
215 The argument hash passed to the init handler has the form
218 ## Response parameters:
220 IMP_NAME => "" ## Z39.50 Implementation name
221 IMP_VER => "" ## Z39.50 Implementation version
222 ERR_CODE => 0 ## Error code, cnf. Z39.50 manual
223 HANDLE => undef ## Handler of Perl data structure
226 The HANDLE member can be used to store any scalar value which will then
227 be provided as input to all subsequent calls (ie. for searching, record
228 retrieval, etc.). A common use of the handle is to store a reference to
229 a hash which may then be used to store session-specific parameters.
230 If you have any session-specific information (such as a list of
231 result sets or a handle to a back-end search engine of some sort),
232 it is always best to store them in a private session structure -
233 rather than leaving them in global variables in your script.
235 The Implementation name and version are only really used by Z39.50
236 client developers to see what kind of server they're dealing with.
237 Filling these in is optional.
239 The ERR_CODE should be left at 0 (the default value) if you wish to
240 accept the connection. Any other value is interpreted as a failure
241 and the client will be shown the door.
243 =head2 Search handler
245 Similarly, the search handler is called with a reference to an anony-
246 mous hash. The structure is the following:
249 ## Request parameters:
251 HANDLE => ref ## Your session reference.
252 SETNAME => "id" ## ID of the result set
253 REPL_SET => 0 ## Replace set if already existing?
254 DATABASES => ["xxx"] ## Reference to a list of data-
256 QUERY => "query" ## The query expression
258 ## Response parameters:
260 ERR_CODE => 0 ## Error code (0=Succesful search)
261 ERR_STR => "" ## Error string
262 HITS => 0 ## Number of matches
265 Note that a search which finds 0 hits is considered successful in
266 Z39.50 terms - you should only set the ERR_CODE to a non-zero value
267 if there was a problem processing the request. The Z39.50 standard
268 provides a comprehensive list of standard diagnostic codes, and you
269 should use these whenever possible.
271 The QUERY is a tree-structure of terms combined by operators, the
272 terms being qualified by lists of attributes. The query is presented
273 to the search function in the Prefix Query Format (PQF) which is
274 used in many applications based on the YAZ toolkit. The full grammar
275 is described in the YAZ manual.
277 The following are all examples of valid queries in the PQF.
283 @or "dylan" "zimmerman"
287 @or @and bob dylan @set Result-1
289 @and @attr 1=1 "bob dylan" @attr 1=4 "slow train coming"
291 @attrset @attr 4=1 @attr 1=4 "self portrait"
293 You will need to write a recursive function or something similar to
294 parse incoming query expressions, and this is usually where a lot of
295 the work in writing a database-backend happens. Fortunately, you don't
296 need to support anymore functionality than you want to. For instance,
297 it is perfectly legal to not accept boolean operators, but you SHOULD
298 try to return good error codes if you run into something you can't or
303 The fetch handler is asked to retrieve a SINGLE record from a given
304 result set (the front-end server will automatically call the fetch
305 handler as many times as required).
307 The parameters exchanged between the server and the fetch handler are
310 ## Client/server request:
312 HANDLE => ref ## Reference to data structure
313 SETNAME => "id" ## ID of the requested result set
314 OFFSET => nnn ## Record offset number
315 REQ_FORM => "n.m.k.l"## Client requested format OID
316 COMP => "xyz" ## Formatting instructions
320 RECORD => "" ## Record string
321 BASENAME => "" ## Origin of returned record
322 LAST => 0 ## Last record in set?
323 ERR_CODE => 0 ## Error code
324 ERR_STR => "" ## Error string
325 SUR_FLAG => 0 ## Surrogate diagnostic flag
326 REP_FORM => "n.m.k.l"## Provided format OID
329 The REP_FORM value has by default the REQ_FORM value but can be set to
330 something different if the handler desires. The BASENAME value should
331 contain the name of the database from where the returned record originates.
332 The ERR_CODE and ERR_STR works the same way they do in the search
333 handler. If there is an error condition, the SUR_FLAG is used to
334 indicate whether the error condition pertains to the record currently
335 being retrieved, or whether it pertains to the operation as a whole
336 (eg. the client has specified a result set which does not exist.)
338 If you need to return USMARC records, you might want to have a look at
339 the MARC module on CPAN, if you don't already have a way of generating
342 NOTE: The record offset is 1-indexed - 1 is the offset of the first
347 The argument hash recieved by the close handler has one element only:
351 HANDLE => ref ## Reference to data structure
354 What ever data structure the HANDLE value points at goes out of scope
355 after this call. If you need to close down a connection to your server
356 or something similar, this is the place to do it.
360 Anders Sønderberg (sondberg@indexdata.dk) and Sebastian Hammer
361 (quinn@indexdata.dk).
367 Any Perl module which is useful for accessing the database of your