Describe ::Node class and its toPQF() method.
[simpleserver-moved-to-github.git] / SimpleServer.pm
1 ##
2 ##  Copyright (c) 2000-2006, Index Data.
3 ##
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,
6 ##  provided that:
7 ##
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.
11 ##
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.
15 ##
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
24 ##  OF THIS SOFTWARE.
25 ##
26 ##
27
28 ## $Id: SimpleServer.pm,v 1.38 2007-08-20 10:55:29 mike Exp $
29
30 package Net::Z3950::SimpleServer;
31
32 use strict;
33 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
34 use Carp;
35
36 require Exporter;
37 require DynaLoader;
38 require AutoLoader;
39
40 @ISA = qw(Exporter AutoLoader DynaLoader);
41 @EXPORT = qw( );
42 $VERSION = '1.07';
43
44 bootstrap Net::Z3950::SimpleServer $VERSION;
45
46 # Preloaded methods go here.
47
48 my $count = 0;
49
50 sub new {
51         my $class = shift;
52         my %args = @_;
53         my $self = \%args;
54
55         if ($count) {
56                 carp "SimpleServer.pm: WARNING: Multithreaded server unsupported";
57         }
58         $count = 1;
59
60         croak "SimpleServer.pm: ERROR: Unspecified search handler" unless defined($self->{SEARCH});
61         croak "SimpleServer.pm: ERROR: Unspecified fetch handler" unless defined($self->{FETCH});
62
63         bless $self, $class;
64         return $self;
65 }
66
67
68 sub launch_server {
69         my $self = shift;
70         my @args = @_;
71
72         ### This modal internal interface, in which we set a bunch of
73         #   globals and then call start_server(), is asking for
74         #   trouble.  Instead, we should just pass the $self object
75         #   as a parameter into start_server().
76         if (defined($self->{GHANDLE})) {
77                 set_ghandle($self->{GHANDLE});
78         }
79         if (defined($self->{INIT})) {
80                 set_init_handler($self->{INIT});
81         }
82         set_search_handler($self->{SEARCH});
83         set_fetch_handler($self->{FETCH});
84         if (defined($self->{CLOSE})) {
85                 set_close_handler($self->{CLOSE});
86         }
87         if (defined($self->{PRESENT})) {
88                 set_present_handler($self->{PRESENT});
89         }
90         if (defined($self->{SCAN})) {
91                 set_scan_handler($self->{SCAN});
92         }
93         if (defined($self->{SORT})) {
94                 set_sort_handler($self->{SORT});
95         }
96         if (defined($self->{EXPLAIN})) {
97                 set_explain_handler($self->{EXPLAIN});
98         }
99
100         start_server(@args);
101 }
102
103
104 # Register packages that we will use in translated RPNs
105 package Net::Z3950::RPN::Node;
106 package Net::Z3950::APDU::Query;
107 our @ISA = qw(Net::Z3950::RPN::Node);
108 package Net::Z3950::APDU::OID;
109 package Net::Z3950::RPN::And;
110 our @ISA = qw(Net::Z3950::RPN::Node);
111 package Net::Z3950::RPN::Or;
112 our @ISA = qw(Net::Z3950::RPN::Node);
113 package Net::Z3950::RPN::AndNot;
114 our @ISA = qw(Net::Z3950::RPN::Node);
115 package Net::Z3950::RPN::Term;
116 our @ISA = qw(Net::Z3950::RPN::Node);
117 package Net::Z3950::RPN::RSID;
118 our @ISA = qw(Net::Z3950::RPN::Node);
119 package Net::Z3950::RPN::Attributes;
120 package Net::Z3950::RPN::Attribute;
121
122
123 # Utility method for re-rendering Type-1 query back down to PQF
124 package Net::Z3950::RPN::Node;
125
126 sub toPQF {
127     my $this = shift();
128     my $class = ref $this;
129
130     if ($class eq "Net::Z3950::APDU::Query") {
131         my $res = "";
132         my $set = $this->{attributeSet};
133         $res .= "\@attrset $set " if defined $set;
134         return $res . $this->{query}->toPQF();
135     } elsif ($class eq "Net::Z3950::RPN::Or") {
136         return '@or ' . $this->[0]->toPQF() . ' ' . $this->[1]->toPQF();
137     } elsif ($class eq "Net::Z3950::RPN::And") {
138         return '@and ' . $this->[0]->toPQF() . ' ' . $this->[1]->toPQF();
139     } elsif ($class eq "Net::Z3950::RPN::AndNot") {
140         return '@not ' . $this->[0]->toPQF() . ' ' . $this->[1]->toPQF();
141     } elsif ($class eq "Net::Z3950::RPN::RSID") {
142         return '@set ' . $this->{id};
143     } elsif ($class ne "Net::Z3950::RPN::Term") {
144         die "unknown PQF node-type '$class'";
145     }
146
147     my $res = "";
148     foreach my $attr (@{ $this->{attributes} }) {
149         use Data::Dumper; print "considering attr: ", Dumper($attr);
150         $res .= "\@attr ";
151         my $set = $attr->{attributeSet};
152         $res .= "$set " if defined $set;
153         $res .= $attr->{attributeType} . "=" . $attr->{attributeValue} . " ";
154     }
155
156     return $res . $this->{term};
157 }
158
159
160 # Must revert to original package for Autoloader's benefit
161 package Net::Z3950::SimpleServer;
162
163
164 # Autoload methods go after =cut, and are processed by the autosplit program.
165
166 1;
167 __END__
168 # Below is the stub of documentation for your module. You better edit it!
169
170 =head1 NAME
171
172 Net::Z3950::SimpleServer - Simple Perl API for building Z39.50 servers. 
173
174 =head1 SYNOPSIS
175
176   use Net::Z3950::SimpleServer;
177
178   sub my_search_handler {
179         my $args = shift;
180
181         my $set_id = $args->{SETNAME};
182         my @database_list = @{ $args->{DATABASES} };
183         my $query = $args->{QUERY};
184
185         ## Perform the query on the specified set of databases
186         ## and return the number of hits:
187
188         $args->{HITS} = $hits;
189   }
190
191   sub my_fetch_handler {        # Get a record for the user
192         my $args = shift;
193
194         my $set_id = $args->{SETNAME};
195
196         my $record = fetch_a_record($args->{OFFSET});
197
198         $args->{RECORD} = $record;
199         if (number_of_hits() == $args->{OFFSET}) {      ## Last record in set?
200                 $args->{LAST} = 1;
201         } else {
202                 $args->{LAST} = 0;
203         }
204   }
205
206   ## Register custom event handlers:
207   my $z = new Net::Z3950::SimpleServer(GHANDLE = $someObject,
208                                        INIT   =>  \&my_init_handler,
209                                        CLOSE  =>  \&my_close_handler,
210                                        SEARCH =>  \&my_search_handler,
211                                        FETCH  =>  \&my_fetch_handler);
212
213   ## Launch server:
214   $z->launch_server("ztest.pl", @ARGV);
215
216 =head1 DESCRIPTION
217
218 The SimpleServer module is a tool for constructing Z39.50 "Information
219 Retrieval" servers in Perl. The module is easy to use, but it
220 does help to have an understanding of the Z39.50 query
221 structure and the construction of structured retrieval records.
222
223 Z39.50 is a network protocol for searching remote databases and
224 retrieving the results in the form of structured "records". It is widely
225 used in libraries around the world, as well as in the US Federal Government.
226 In addition, it is generally useful whenever you wish to integrate a number
227 of different database systems around a shared, asbtract data model.
228
229 The model of the module is simple: It implements a "generic" Z39.50
230 server, which invokes callback functions supplied by you to search
231 for content in your database. You can use any tools available in
232 Perl to supply the content, including modules like DBI and
233 WWW::Search.
234
235 The server will take care of managing the network connections for
236 you, and it will spawn a new process (or thread, in some
237 environments) whenever a new connection is received.
238
239 The programmer can specify subroutines to take care of the following type
240 of events:
241
242   - Initialize request
243   - Search request
244   - Present request
245   - Fetching of records
246   - Scan request (browsing) 
247   - Closing down connection
248
249 Note that only the Search and Fetch handler functions are required.
250 The module can supply default responses to the other on its own.
251
252 After the launching of the server, all control is given away from
253 the Perl script to the server. The server calls the registered
254 subroutines to field incoming requests from Z39.50 clients.
255
256 A reference to an anonymous hash is passed to each handler. Some of
257 the entries of these hashes are to be considered input and others
258 output parameters.
259
260 The Perl programmer specifies the event handlers for the server by
261 means of the SimpleServer object constructor
262
263   my $z = new Net::Z3950::SimpleServer(
264                         INIT    =>      \&my_init_handler,
265                         CLOSE   =>      \&my_close_handler,
266                         SEARCH  =>      \&my_search_handler,
267                         PRESENT =>      \&my_present_handler,
268                         SCAN    =>      \&my_scan_handler,
269                         FETCH   =>      \&my_fetch_handler,
270                         EXPLAIN =>      \&my_explain_handler);
271
272 In addition, the arguments to the constructor may include GHANDLE, a
273 global handle which is made available to each invocation of every
274 callback function.  This is typically a reference to either a hash or
275 an object.
276
277 If you want your SimpleServer to start a thread (threaded mode) to
278 handle each incoming Z39.50 request instead of forking a process
279 (forking mode), you need to register the handlers by symbol rather
280 than by code reference. Thus, in threaded mode, you will need to
281 register your handlers this way:
282
283   my $z = new Net::Z3950::SimpleServer(
284                         INIT    =>      "my_package::my_init_handler",
285                         CLOSE   =>      "my_package::my_close_handler",
286                         ....
287                         ....          );
288
289 where my_package is the Perl package in which your handler is
290 located.
291
292 After the custom event handlers are declared, the server is launched
293 by means of the method
294
295   $z->launch_server("MyServer.pl", @ARGV);
296
297 Notice, the first argument should be the name of your server
298 script (for logging purposes), while the rest of the arguments
299 are documented in the YAZ toolkit manual: The section on
300 application invocation: <http://www.indexdata.dk/yaz/yaz-7.php>
301
302 In particular, you need to use the -T switch to start your SimpleServer
303 in threaded mode.
304
305 =head2 Init handler
306
307 The init handler is called whenever a Z39.50 client is attempting
308 to logon to the server. The exchange of parameters between the
309 server and the handler is carried out via an anonymous hash reached
310 by a reference, i.e.
311
312   $args = shift;
313
314 The argument hash passed to the init handler has the form
315
316   $args = {
317                                     ## Response parameters:
318
319              IMP_ID    =>  "",      ## Z39.50 Implementation ID
320              IMP_NAME  =>  "",      ## Z39.50 Implementation name
321              IMP_VER   =>  "",      ## Z39.50 Implementation version
322              ERR_CODE  =>  0,       ## Error code, cnf. Z39.50 manual
323              ERR_STR   =>  "",      ## Error string (additional info.)
324              USER      =>  "xxx"    ## If Z39.50 authentication is used,
325                                     ## this member contains user name
326              PASS      =>  "yyy"    ## Under same conditions, this member
327                                     ## contains the password in clear text
328              GHANDLE   =>  $obj     ## Global handler specified at creation
329              HANDLE    =>  undef    ## Handler of Perl data structure
330           };
331
332 The HANDLE member can be used to store any scalar value which will then
333 be provided as input to all subsequent calls (ie. for searching, record
334 retrieval, etc.). A common use of the handle is to store a reference to
335 a hash which may then be used to store session-specific parameters.
336 If you have any session-specific information (such as a list of
337 result sets or a handle to a back-end search engine of some sort),
338 it is always best to store them in a private session structure -
339 rather than leaving them in global variables in your script.
340
341 The Implementation ID, name and version are only really used by Z39.50
342 client developers to see what kind of server they're dealing with.
343 Filling these in is optional.
344
345 The ERR_CODE should be left at 0 (the default value) if you wish to
346 accept the connection. Any other value is interpreted as a failure
347 and the client will be shown the door, with the code and the
348 associated additional information, ERR_STR returned.
349
350 =head2 Search handler
351
352 Similarly, the search handler is called with a reference to an anony-
353 mous hash. The structure is the following:
354
355   $args = {
356                                     ## Request parameters:
357
358              GHANDLE   =>  $obj     ## Global handler specified at creation
359              HANDLE    =>  ref,     ## Your session reference.
360              SETNAME   =>  "id",    ## ID of the result set
361              REPL_SET  =>  0,       ## Replace set if already existing?
362              DATABASES =>  ["xxx"], ## Reference to a list of data-
363                                     ## bases to search
364              QUERY     =>  "query", ## The query expression
365              RPN       =>  $obj,    ## Reference to a Net::Z3950::APDU::Query
366
367                                     ## Response parameters:
368
369              ERR_CODE  =>  0,       ## Error code (0=Succesful search)
370              ERR_STR   =>  "",      ## Error string
371              HITS      =>  0        ## Number of matches
372           };
373
374 Note that a search which finds 0 hits is considered successful in
375 Z39.50 terms - you should only set the ERR_CODE to a non-zero value
376 if there was a problem processing the request. The Z39.50 standard
377 provides a comprehensive list of standard diagnostic codes, and you
378 should use these whenever possible.
379
380 The QUERY is a tree-structure of terms combined by operators, the
381 terms being qualified by lists of attributes. The query is presented
382 to the search function in the Prefix Query Format (PQF) which is
383 used in many applications based on the YAZ toolkit. The full grammar
384 is described in the YAZ manual.
385
386 The following are all examples of valid queries in the PQF. 
387
388         dylan
389
390         "bob dylan"
391
392         @or "dylan" "zimmerman"
393
394         @set Result-1
395
396         @or @and bob dylan @set Result-1
397
398         @and @attr 1=1 "bob dylan" @attr 1=4 "slow train coming"
399
400         @attrset @attr 4=1 @attr 1=4 "self portrait"
401
402 You will need to write a recursive function or something similar to
403 parse incoming query expressions, and this is usually where a lot of
404 the work in writing a database-backend happens. Fortunately, you don't
405 need to support anymore functionality than you want to. For instance,
406 it is perfectly legal to not accept boolean operators, but you SHOULD
407 try to return good error codes if you run into something you can't or
408 won't support.
409
410 A more convenient alternative to the QUERY member may be the RPN
411 member, which is a reference to a Net::Z3950::APDU::Query object
412 representing the RPN query tree.  The structure of that object is
413 supposed to be self-documenting, but here's a brief summary of what
414 you get:
415
416 =over 4
417
418 =item *
419
420 C<Net::Z3950::APDU::Query> is a hash with two fields:
421
422 Z<>
423
424 =over 4
425
426 =item C<attributeSet>
427
428 Optional.  If present, it is a reference to a
429 C<Net::Z3950::APDU::OID>.  This is a string of dot-separated integers
430 representing the OID of the query's top-level attribute set.
431
432 =item C<query>
433
434 Mandatory: a refererence to the RPN tree itself.
435
436 =back
437
438 =item *
439
440 Each node of the tree is an object of one of the following types:
441
442 Z<>
443
444 =over 4
445
446 =item C<Net::Z3950::RPN::And>
447
448 =item C<Net::Z3950::RPN::Or>
449
450 =item C<Net::Z3950::RPN::AndNot>
451
452 These three classes are all arrays of two elements, each of which is a
453 node of one of the above types.
454
455 =item C<Net::Z3950::RPN::Term>
456
457 See below for details.
458
459 =item C<Net::Z3950::RPN::RSID>
460
461 A reference to a result-set ID indicating a previous search.  The ID
462 of the result-set is in the C<id> element.
463
464 =back
465
466 All of these classes are subclasses of the abstrac class
467 C<Net::Z3950::RPN::Node>.  That class has a single method, C<toPQF()>,
468 which may be used to turn an RPN tree, or part of one, back into a
469 textual prefix query.
470
471 =back
472
473 =over 4
474
475 =item *
476
477 C<Net::Z3950::RPN::Term> is a hash with two fields:
478
479 Z<>
480
481 =over 4
482
483 =item C<term>
484
485 A string containing the search term itself.
486
487 =item C<attributes>
488
489 A reference to a C<Net::Z3950::RPN::Attributes> object.
490
491 =back
492
493 =item *
494
495 C<Net::Z3950::RPN::Attributes> is an array of references to
496 C<Net::Z3950::RPN::Attribute> objects.  (Note the plural/singular
497 distinction.)
498
499 =item *
500
501 C<Net::Z3950::RPN::Attribute> is a hash with three elements:
502
503 Z<>
504
505 =over 4
506
507 =item C<attributeSet>
508
509 Optional.  If present, it is dot-separated OID string, as above.
510
511 =item C<attributeType>
512
513 An integer indicating the type of the attribute - for example, under
514 the BIB-1 attribute set, type 1 indicates a ``use'' attribute, type 2
515 a ``relation'' attribute, etc.
516
517 =item C<attributeValue>
518
519 An integer or string indicating the value of the attribute - for example, under
520 BIB-1, if the attribute type is 1, then value 4 indictates a title
521 search and 7 indictates an ISBN search; but if the attribute type is
522 2, then value 4 indicates a ``greater than or equal'' search, and 102
523 indicates a relevance match.
524
525 =back
526
527 =back
528
529 Note that, apart to C<toPQF()>, none of these classes have any methods at
530 all: the blessing into classes is largely just a documentation thing
531 so that, for example, if you do
532
533         { use Data::Dumper; print Dumper($args->{RPN}) }
534
535 you get something fairly human-readable.  But of course, the type
536 distinction between the three different kinds of boolean node is
537 important.
538
539 By adding your own methods to these classes (building what I call
540 ``augmented classes''), you can easily build code that walks the tree
541 of the incoming RPN.  Take a look at C<samples/render-search.pl> for a
542 sample implementation of such an augmented classes technique.
543
544
545 =head2 Present handler
546
547 The presence of a present handler in a SimpleServer front-end is optional.
548 Each time a client wishes to retrieve records, the present service is
549 called. The present service allows the origin to request a certain number
550 of records retrieved from a given result set.
551 When the present handler is called, the front-end server should prepare a
552 result set for fetching. In practice, this means to get access to the
553 data from the backend database and store the data in a temporary fashion
554 for fast and efficient fetching. The present handler does *not* fetch
555 anything. This task is taken care of by the fetch handler, which will be
556 called the correct number of times by the YAZ library. More about this
557 below.
558 If no present handler is implemented in the front-end, the YAZ toolkit
559 will take care of a minimum of preparations itself. This default present
560 handler is sufficient in many situations, where only a small amount of
561 records are expected to be retrieved. If on the other hand, large result
562 sets are likely to occur, the implementation of a reasonable present
563 handler can gain performance significantly.
564
565 The informations exchanged between client and present handle are:
566
567   $args = {
568                                     ## Client/server request:
569
570              GHANDLE   =>  $obj     ## Global handler specified at creation
571              HANDLE    =>  ref,     ## Reference to datastructure
572              SETNAME   =>  "id",    ## Result set ID
573              START     =>  xxx,     ## Start position
574              COMP      =>  "",      ## Desired record composition
575              NUMBER    =>  yyy,     ## Number of requested records
576
577
578                                     ## Respons parameters:
579
580              HITS      =>  zzz,     ## Number of returned records
581              ERR_CODE  =>  0,       ## Error code
582              ERR_STR   =>  ""       ## Error message
583           };
584
585
586 =head2 Fetch handler
587
588 The fetch handler is asked to retrieve a SINGLE record from a given
589 result set (the front-end server will automatically call the fetch
590 handler as many times as required).
591
592 The parameters exchanged between the server and the fetch handler are
593
594   $args = {
595                                     ## Client/server request:
596
597              GHANDLE   =>  $obj     ## Global handler specified at creation
598              HANDLE    =>  ref      ## Reference to data structure
599              SETNAME   =>  "id"     ## ID of the requested result set
600              OFFSET    =>  nnn      ## Record offset number
601              REQ_FORM  =>  "n.m.k.l"## Client requested format OID
602              COMP      =>  "xyz"    ## Formatting instructions
603              SCHEMA    =>  "abc"    ## Requested schema, if any
604
605                                     ## Handler response:
606
607              RECORD    =>  ""       ## Record string
608              BASENAME  =>  ""       ## Origin of returned record
609              LAST      =>  0        ## Last record in set?
610              ERR_CODE  =>  0        ## Error code
611              ERR_STR   =>  ""       ## Error string
612              SUR_FLAG  =>  0        ## Surrogate diagnostic flag
613              REP_FORM  =>  "n.m.k.l"## Provided format OID
614              SCHEMA    =>  "abc"    ## Provided schema, if any
615           };
616
617 The REP_FORM value has by default the REQ_FORM value but can be set to
618 something different if the handler desires. The BASENAME value should
619 contain the name of the database from where the returned record originates.
620 The ERR_CODE and ERR_STR works the same way they do in the search
621 handler. If there is an error condition, the SUR_FLAG is used to
622 indicate whether the error condition pertains to the record currently
623 being retrieved, or whether it pertains to the operation as a whole
624 (eg. the client has specified a result set which does not exist.)
625
626 If you need to return USMARC records, you might want to have a look at
627 the MARC module on CPAN, if you don't already have a way of generating
628 these.
629
630 NOTE: The record offset is 1-indexed - 1 is the offset of the first
631 record in the set.
632
633 =head2 Scan handler
634
635 A full featured Z39.50 server should support scan (or in some literature
636 browse). The client specifies a starting term of the scan, and the server
637 should return an ordered list of specified length consisting of terms
638 actually occurring in the data base. Each of these terms should be close
639 to or equal to the term originally specified. The quality of scan compared
640 to simple search is a guarantee of hits. It is simply like browsing through
641 an index of a book, you always find something! The parameters exchanged are
642
643   $args = {
644                                                 ## Client request
645
646                 GHANDLE         => $obj,        ## Global handler specified at creation
647                 HANDLE          => $ref,        ## Reference to data structure
648                 DATABASES       => ["xxx"],     ## Reference to a list of data-
649                                                 ## bases to search
650                 TERM            => 'start',     ## The start term
651                 RPN             =>  $obj,       ## Reference to a Net::Z3950::RPN::Term
652
653                 NUMBER          => xx,          ## Number of requested terms
654                 POS             => yy,          ## Position of starting point
655                                                 ## within returned list
656                 STEP            => 0,           ## Step size
657
658                                                 ## Server response
659
660                 ERR_CODE        => 0,           ## Error code
661                 ERR_STR         => '',          ## Diagnostic message
662                 NUMBER          => zz,          ## Number of returned terms
663                 STATUS          => $status,     ## ScanSuccess/ScanFailure
664                 ENTRIES         => $entries     ## Referenced list of terms
665         };
666
667 where the term list is returned by reference in the scalar $entries, which
668 should point at a data structure of this kind,
669
670   my $entries = [
671                         {       TERM            => 'energy',
672                                 OCCURRENCE      => 5            },
673
674                         {       TERM            => 'energy density',
675                                 OCCURRENCE      => 6,           },
676
677                         {       TERM            => 'energy flow',
678                                 OCCURRENCE      => 3            },
679
680                                 ...
681
682                                 ...
683         ];
684
685 The $status flag is only meaningful after a successful scan, and
686 should be assigned one of two values:
687
688   Net::Z3950::SimpleServer::ScanSuccess  Full success (default)
689   Net::Z3950::SimpleServer::ScanPartial  Fewer terms returned than requested
690
691 The STEP member contains the requested number of entries in the term-list
692 between two adjacent entries in the response.
693
694 A better alternative to the TERM member is the the RPN
695 member, which is a reference to a Net::Z3950::RPN::Term object
696 representing the scan cloause.  The structure of that object is the
697 same as for Term objects included as part of the RPN tree passed to
698 search handlers.  This is more useful than the simple TERM because it
699 includes attributes (e.g. access points associated with the term),
700 which are discarded by the TERM element.
701
702 =head2 Close handler
703
704 The argument hash recieved by the close handler has two elements only:
705
706   $args = {
707                                     ## Server provides:
708
709              GHANDLE   =>  $obj     ## Global handler specified at creation
710              HANDLE    =>  ref      ## Reference to data structure
711           };
712
713 What ever data structure the HANDLE value points at goes out of scope
714 after this call. If you need to close down a connection to your server
715 or something similar, this is the place to do it.
716
717 =head2 Support for SRU and SRW
718
719 Since release 1.0, SimpleServer includes support for serving the SRU
720 and SRW protocols as well as Z39.50.  These ``web-friendly'' protocols
721 enable similar functionality to that of Z39.50, but by means of rich
722 URLs in the case of SRU, and a SOAP-based web-service in the case of
723 SRW.  These protocols are described at
724 http://www.loc.gov/sru
725
726 In order to serve these protocols from a SimpleServer-based
727 application, it is necessary to launch the application with a YAZ
728 Generic Frontend Server (GFS) configuration file, which can be
729 specified using the command-line argument C<-f> I<filename>.  A
730 minimal configuration file looks like this:
731
732   <yazgfs>
733     <server>
734       <cql2rpn>pqf.properties</cql2rpn>
735     </server>
736   </yazgfs>
737
738 This file specifies only that C<pqf.properties> should be used to
739 translate the CQL queries of SRU and SRW into corresponding Z39.50
740 Type-1 queries.  For more information about YAZ GFS configuration,
741 including how to specify an Explain record, see the I<Virtual Hosts>
742 section of the YAZ manual at
743 http://indexdata.com/yaz/doc/server.vhosts.tkl
744
745 The mapping of CQL queries into Z39.50 Type-1 queries is specified by
746 a file that indicates which BIB-1 attributes should be generated for
747 each CQL index, relation, modifiers, etc.  A typical section of this
748 file looks like this:
749
750   index.dc.title                        = 1=4
751   index.dc.subject                      = 1=21
752   index.dc.creator                      = 1=1003
753   relation.<                            = 2=1
754   relation.le                           = 2=2
755
756 This file specifies the BIB-1 access points (type=1) for the Dublin
757 Core indexes C<title>, C<subject> and C<creator>, and the BIB-1
758 relations (type=2) corresponding to the CQL relations C<E<lt>> and
759 C<E<lt>=>.  For more information about the format of this file, see
760 the I<CQL> section of the YAZ manual at
761 http://indexdata.com/yaz/doc/tools.tkl#tools.cql
762
763 The YAZ distribution include a sample CQL-to-PQF mapping configuration
764 file called C<pqf.properties>; this is sufficient for many
765 applications, and a good base to work from for most others.
766
767 If a SimpleServer-based application is run without this SRU-specific
768 configuration, it can still serve SRU; however, CQL queries will not
769 be translated, but passed straight through to the search-handler
770 function, as the C<CQL> member of the parameters hash.  It is then the
771 responsibility of the back-end application to parse and handle the CQL
772 query, which is most easily done using Ed Summers' fine C<CQL::Parser>
773 module, available from CPAN at
774 http://search.cpan.org/~esummers/CQL-Parser/
775
776 =head1 AUTHORS
777
778 Anders Sønderberg (sondberg@indexdata.dk),
779 Sebastian Hammer (quinn@indexdata.dk),
780 Mike Taylor (indexdata.com).
781
782 =head1 SEE ALSO
783
784 Any Perl module which is useful for accessing the database of your
785 choice.
786
787 =cut