Markdown
[simpleserver-moved-to-github.git] / ztest.pl
1 #!/usr/bin/perl -w
2
3 ## This file is part of simpleserver
4 ## Copyright (C) 2000-2015 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                                         DISPLAY_TERM    =>      'Number .1',
74                                         OCCURRENCE      =>      10 },
75                                 {       TERM            =>      'Number 2',
76                                         OCCURRENCE      =>      8 },
77                                 {       TERM            =>      'Number 3',
78                                         OCCURRENCE      =>      8 },
79                                 {       TERM            =>      'Number 4',
80                                         OCCURRENCE      =>      8 },
81                                 {       TERM            =>      'Number 5',
82                                         OCCURRENCE      =>      8 },
83                                 {       TERM            =>      'Number 6',
84                                         OCCURRENCE      =>      8 },
85                                 {       TERM            =>      'Number 7',
86                                         OCCURRENCE      =>      8 },
87                                 {       TERM            =>      'Number 8',
88                                         OCCURRENCE      =>      8 },
89                                 {       TERM            =>      'Number 9',
90                                         OCCURRENCE      =>      8 },
91                                 {       TERM            =>      'Number 10',
92                                         OCCURRENCE      =>      4 },
93                         ];
94         $args->{NUMBER} = 10;
95         $args->{ENTRIES} = $entries;
96         $args->{STATUS} = Net::Z3950::SimpleServer::ScanPartial;
97         print "Welcome to scan....\n";
98         $args->{EXTRA_RESPONSE_DATA} = '<scanextra>b</scanextra>';
99         print "You scanned for term '$term'\n";
100 }
101
102
103 my $_fail_frequency = 0;
104 my $_counter = 0;
105
106 sub my_search_handler {
107         my $args = shift;
108
109         my $data = [{
110                         name            =>      "Peter Dornan",
111                         title           =>      "Spokesman",
112                         collaboration   =>      "ATLAS"
113                     }, {
114                         name            =>      "Jorn Dines Hansen",
115                         title           =>      "Professor",
116                         collaboration   =>      "HERA-B"
117                     }, {
118                         name            =>      "Alain Blondel",
119                         title           =>      "Head of coll.",
120                         collaboration   =>      "ALEPH"
121                     }];
122
123         my $session = $args->{HANDLE};
124         my $set_id = $args->{SETNAME};
125         my $rpn = $args->{RPN};
126         my @database_list = @{ $args->{DATABASES} };
127         my $query = $args->{QUERY};
128         my $facets = $args->{INPUTFACETS};
129         my $hits = 3;
130
131         print "------------------------------------------------------------\n";
132         print "Processing query : $query\n";
133         printf("Database set     : %s\n", join(" ", @database_list));
134         print "Setname          : $set_id\n";
135         print " inputfacets:\n";
136         print Dumper($facets);
137         print " extra args:\n";
138         print Dumper($args->{EXTRA_ARGS});
139         print "------------------------------------------------------------\n";
140
141         $args->{OUTPUTFACETS} = $facets;
142
143         $args->{EXTRA_RESPONSE_DATA} = '<searchextra>b</searchextra>';
144         $args->{HITS} = $hits;
145         $session->{$set_id} = $data;
146         $session->{__HITS} = $hits;
147         if ($_fail_frequency != 0 && ++$_counter % $_fail_frequency == 0) {
148             print "Exiting to be nasty to client\n";
149             exit(1);
150         }
151 }
152
153
154 sub my_fetch_handler {
155         my $args = shift;
156         my $session = $args->{HANDLE};
157         my $set_id = $args->{SETNAME};
158         my $data = $session->{$set_id};
159         my $offset = $args->{OFFSET};
160         my $record = "<xml>";
161         my $field;
162         my $hits = $session->{__HITS};
163         my $href = $data->[$offset - 1];
164
165         $args->{REP_FORM} = Net::Z3950::OID::xml;
166         foreach $field (keys %$href) {
167                 $record .= "<" . $field . ">" . $href->{$field} . "</" . $field . ">";
168         }
169
170         $record .= "</xml>";
171         $args->{RECORD} = $record;
172         if ($offset == $session->{__HITS}) {
173                 $args->{LAST} = 1;
174         }
175 }
176
177 sub my_start_handler {
178     my $args = shift;
179     my $config = $args->{CONFIG};
180 }
181
182 Net::Z3950::SimpleServer::yazlog("hello");
183
184 my $handler = new Net::Z3950::SimpleServer(
185                 START   =>      "main::my_start_handler",
186                 INIT    =>      "main::my_init_handler",
187                 SEARCH  =>      "main::my_search_handler",
188                 SCAN    =>      "main::my_scan_handler",
189                 SORT    =>      "main::my_sort_handler",
190                 FETCH   =>      "main::my_fetch_handler" );
191
192 if (@ARGV >= 2 && $ARGV[0] eq "-n") {
193     $_fail_frequency = $ARGV[1];
194     shift;
195     shift;
196 }
197 $handler->launch_server("ztest.pl", @ARGV);