Version 1.15
[simpleserver-moved-to-github.git] / ztest.pl
1 #!/usr/bin/perl -w
2
3 ## This file is part of simpleserver
4 ## Copyright (C) 2000-2011 Index Data.
5 ## All rights reserved.
6 ## Redistribution and use in source and binary forms, with or without
7 ## modification, are permitted provided that the following conditions are met:
8 ##
9 ##     * Redistributions of source code must retain the above copyright
10 ##       notice, this list of conditions and the following disclaimer.
11 ##     * Redistributions in binary form must reproduce the above copyright
12 ##       notice, this list of conditions and the following disclaimer in the
13 ##       documentation and/or other materials provided with the distribution.
14 ##     * Neither the name of Index Data nor the names of its contributors
15 ##       may be used to endorse or promote products derived from this
16 ##       software without specific prior written permission.
17 ##
18 ## THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
19 ## EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 ## DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
22 ## DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 ## (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 ## THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 use ExtUtils::testlib;
30 use Data::Dumper;
31 use Net::Z3950::SimpleServer;
32 use Net::Z3950::OID;
33 use strict;
34
35 sub dump_hash {
36         my $href = shift;
37         my $key;
38
39         foreach $key (keys %$href) {
40                 printf("%10s    =>      %s\n", $key, $href->{$key});
41         }
42 }
43
44
45 sub my_init_handler {
46         my $args = shift;
47         my $session = {};
48
49         $args->{IMP_NAME} = "DemoServer";
50         $args->{IMP_ID} = "81";
51         $args->{IMP_VER} = "3.14159";
52         $args->{ERR_CODE} = 0;
53         $args->{HANDLE} = $session;
54         if (defined($args->{PASS}) && defined($args->{USER})) {
55             printf("Received USER/PASS=%s/%s\n", $args->{USER},$args->{PASS});
56         }
57             
58 }
59
60
61 sub my_sort_handler {
62     my ($args) = @_;
63
64     print "Sort handler called\n";
65     print Dumper( $args );
66 }
67
68 sub my_scan_handler {
69         my $args = shift;
70         my $term = $args->{TERM};
71         my $entries = [
72                                 {       TERM            =>      'Number 1',
73                                         OCCURRENCE      =>      10 },
74                                 {       TERM            =>      'Number 2',
75                                         OCCURRENCE      =>      8 },
76                                 {       TERM            =>      'Number 3',
77                                         OCCURRENCE      =>      8 },
78                                 {       TERM            =>      'Number 4',
79                                         OCCURRENCE      =>      8 },
80                                 {       TERM            =>      'Number 5',
81                                         OCCURRENCE      =>      8 },
82                                 {       TERM            =>      'Number 6',
83                                         OCCURRENCE      =>      8 },
84                                 {       TERM            =>      'Number 7',
85                                         OCCURRENCE      =>      8 },
86                                 {       TERM            =>      'Number 8',
87                                         OCCURRENCE      =>      8 },
88                                 {       TERM            =>      'Number 9',
89                                         OCCURRENCE      =>      8 },
90                                 {       TERM            =>      'Number 10',
91                                         OCCURRENCE      =>      4 },
92                         ];
93         $args->{NUMBER} = 10;
94         $args->{ENTRIES} = $entries;
95         $args->{STATUS} = Net::Z3950::SimpleServer::ScanPartial;
96         print "Welcome to scan....\n";
97         print "You scanned for term '$term'\n";
98 }
99
100
101 my $_fail_frequency = 0;
102 my $_counter = 0;
103
104 sub my_search_handler { 
105         my $args = shift;
106
107         my $data = [{
108                         name            =>      "Peter Dornan",
109                         title           =>      "Spokesman",
110                         collaboration   =>      "ATLAS"
111                     }, {
112                         name            =>      "Jorn Dines Hansen",
113                         title           =>      "Professor",
114                         collaboration   =>      "HERA-B"
115                     }, {
116                         name            =>      "Alain Blondel",
117                         title           =>      "Head of coll.",
118                         collaboration   =>      "ALEPH"
119                     }];
120
121         my $session = $args->{HANDLE};
122         my $set_id = $args->{SETNAME};
123         my $rpn = $args->{RPN};
124         my @database_list = @{ $args->{DATABASES} };
125         my $query = $args->{QUERY};
126         my $facets = $args->{INPUTFACETS};
127         my $hits = 3;
128
129         print "------------------------------------------------------------\n";
130         print "Processing query : $query\n";
131         printf("Database set     : %s\n", join(" ", @database_list));
132         print "Setname          : $set_id\n";
133         print " inputfacets:\n";
134         print Dumper($facets);
135         print "------------------------------------------------------------\n";
136
137         $args->{OUTPUTFACETS} = $facets;
138
139         $args->{HITS} = $hits;
140         $session->{$set_id} = $data;
141         $session->{__HITS} = $hits;
142         if ($_fail_frequency != 0 && ++$_counter % $_fail_frequency == 0) {
143             print "Exiting to be nasty to client\n";
144             exit(1);
145         }
146 }
147
148
149 sub my_fetch_handler {
150         my $args = shift;
151         my $session = $args->{HANDLE};
152         my $set_id = $args->{SETNAME};
153         my $data = $session->{$set_id};
154         my $offset = $args->{OFFSET};
155         my $record = "<xml>";
156         my $field;
157         my $hits = $session->{__HITS};
158         my $href = $data->[$offset - 1];
159
160         $args->{REP_FORM} = Net::Z3950::OID::xml;
161         foreach $field (keys %$href) {
162                 $record .= "<" . $field . ">" . $href->{$field} . "</" . $field . ">";
163         }
164
165         $record .= "</xml>";
166         $args->{RECORD} = $record;
167         if ($offset == $session->{__HITS}) {
168                 $args->{LAST} = 1;
169         }
170 }
171
172 Net::Z3950::SimpleServer::yazlog("hello");
173
174 my $handler = new Net::Z3950::SimpleServer( 
175                 INIT    =>      "main::my_init_handler",
176                 SEARCH  =>      "main::my_search_handler",
177                 SCAN    =>      "main::my_scan_handler",
178                 SORT    =>      "main::my_sort_handler",
179                 FETCH   =>      "main::my_fetch_handler" );
180
181 if (@ARGV >= 2 && $ARGV[0] eq "-n") {
182     $_fail_frequency = $ARGV[1];
183     shift;
184     shift;
185 }
186 $handler->launch_server("ztest.pl", @ARGV);