Add CVS Id
[simpleserver-moved-to-github.git] / ztest.pl
1 #!/usr/bin/perl -w
2
3 ##  $Id: ztest.pl,v 1.14 2005-11-09 09:35:47 adam Exp $
4 ##  ------------------------------------------------------------------
5 ##
6 ##  Copyright (c) 2000-2004, Index Data.
7 ##
8 ##  Permission to use, copy, modify, distribute, and sell this software and
9 ##  its documentation, in whole or in part, for any purpose, is hereby granted,
10 ##  provided that:
11 ##
12 ##  1. This copyright and permission notice appear in all copies of the
13 ##  software and its documentation. Notices of copyright or attribution
14 ##  which appear at the beginning of any file must remain unchanged.
15 ##
16 ##  2. The name of Index Data or the individual authors may not be used to
17 ##  endorse or promote products derived from this software without specific
18 ##  prior written permission.
19 ##
20 ##  THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
21 ##  EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
22 ##  WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
23 ##  IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
24 ##  INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
25 ##  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
26 ##  NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
27 ##  LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 ##  OF THIS SOFTWARE.
29 ##
30
31 use ExtUtils::testlib;
32 use Net::Z3950::SimpleServer;
33 use Net::Z3950::OID;
34 use strict;
35
36 sub dump_hash {
37         my $href = shift;
38         my $key;
39
40         foreach $key (keys %$href) {
41                 printf("%10s    =>      %s\n", $key, $href->{$key});
42         }
43 }
44
45
46 sub my_init_handler {
47         my $args = shift;
48         my $session = {};
49
50         $args->{IMP_NAME} = "DemoServer";
51         $args->{IMP_ID} = "81";
52         $args->{IMP_VER} = "3.14159";
53         $args->{ERR_CODE} = 0;
54         $args->{HANDLE} = $session;
55         if (defined($args->{PASS}) && defined($args->{USER})) {
56             printf("Received USER/PASS=%s/%s\n", $args->{USER},$args->{PASS});
57         }
58             
59 }
60
61 sub my_scan_handler {
62         my $args = shift;
63         my $term = $args->{TERM};
64         my $entries = [
65                                 {       TERM            =>      'Number 1',
66                                         OCCURRENCE      =>      10 },
67                                 {       TERM            =>      'Number 2',
68                                         OCCURRENCE      =>      8 },
69                                 {       TERM            =>      'Number 3',
70                                         OCCURRENCE      =>      8 },
71                                 {       TERM            =>      'Number 4',
72                                         OCCURRENCE      =>      8 },
73                                 {       TERM            =>      'Number 5',
74                                         OCCURRENCE      =>      8 },
75                                 {       TERM            =>      'Number 6',
76                                         OCCURRENCE      =>      8 },
77                                 {       TERM            =>      'Number 7',
78                                         OCCURRENCE      =>      8 },
79                                 {       TERM            =>      'Number 8',
80                                         OCCURRENCE      =>      8 },
81                                 {       TERM            =>      'Number 9',
82                                         OCCURRENCE      =>      8 },
83                                 {       TERM            =>      'Number 10',
84                                         OCCURRENCE      =>      4 },
85                         ];
86         $args->{NUMBER} = 10;
87         $args->{ENTRIES} = $entries;
88         $args->{STATUS} = Net::Z3950::SimpleServer::ScanPartial;
89         print "Welcome to scan....\n";
90         print "You scanned for term '$term'\n";
91 }
92
93
94 sub my_search_handler { 
95         my $args = shift;
96         my $data = [{
97                         name            =>      "Peter Dornan",
98                         title           =>      "Spokesman",
99                         collaboration   =>      "ATLAS"
100                     }, {
101                         name            =>      "Jorn Dines Hansen",
102                         title           =>      "Professor",
103                         collaboration   =>      "HERA-B"
104                     }, {
105                         name            =>      "Alain Blondel",
106                         title           =>      "Head of coll.",
107                         collaboration   =>      "ALEPH"
108                     }];
109
110         my $session = $args->{HANDLE};
111         my $set_id = $args->{SETNAME};
112         my @database_list = @{ $args->{DATABASES} };
113         my $query = $args->{QUERY};
114         my $hits = 3;
115
116         print "------------------------------------------------------------\n";
117         print "Processing query : $query\n";
118         printf("Database set     : %s\n", join(" ", @database_list));
119         print "Setname          : $set_id\n";
120         print "------------------------------------------------------------\n";
121
122         $args->{HITS} = $hits;
123         $session->{$set_id} = $data;
124         $session->{__HITS} = $hits;
125 }
126
127
128 sub my_fetch_handler {
129         my $args = shift;
130         my $session = $args->{HANDLE};
131         my $set_id = $args->{SETNAME};
132         my $data = $session->{$set_id};
133         my $offset = $args->{OFFSET};
134         my $record = "<xml>";
135         my $field;
136         my $hits = $session->{__HITS};
137         my $href = $data->[$offset - 1];
138
139         $args->{REP_FORM} = Net::Z3950::OID::xml;
140         foreach $field (keys %$href) {
141                 $record .= "<" . $field . ">" . $href->{$field} . "</" . $field . ">";
142         }
143
144         $record .= "</xml>";
145         $args->{RECORD} = $record;
146         if ($offset == $session->{__HITS}) {
147                 $args->{LAST} = 1;
148         }
149 }
150
151 Net::Z3950::SimpleServer::yazlog("hello");
152
153 my $handler = new Net::Z3950::SimpleServer( 
154                 INIT    =>      "main::my_init_handler",
155                 SEARCH  =>      "main::my_search_handler",
156                 SCAN    =>      "main::my_scan_handler",
157                 FETCH   =>      "main::my_fetch_handler" );
158
159 $handler->launch_server("ztest.pl", @ARGV);