Add licence.
[simpleserver-moved-to-github.git] / ztest.pl
1 #!/usr/bin/perl -w
2
3 ##  $Id: ztest.pl,v 1.17 2007-03-08 14:51:32 mike 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 Data::Dumper;
33 use Net::Z3950::SimpleServer;
34 use Net::Z3950::OID;
35 use strict;
36
37 sub dump_hash {
38         my $href = shift;
39         my $key;
40
41         foreach $key (keys %$href) {
42                 printf("%10s    =>      %s\n", $key, $href->{$key});
43         }
44 }
45
46
47 sub my_init_handler {
48         my $args = shift;
49         my $session = {};
50
51         $args->{IMP_NAME} = "DemoServer";
52         $args->{IMP_ID} = "81";
53         $args->{IMP_VER} = "3.14159";
54         $args->{ERR_CODE} = 0;
55         $args->{HANDLE} = $session;
56         if (defined($args->{PASS}) && defined($args->{USER})) {
57             printf("Received USER/PASS=%s/%s\n", $args->{USER},$args->{PASS});
58         }
59             
60 }
61
62
63 sub my_sort_handler {
64     my ($args) = @_;
65
66     print "Sort handler called\n";
67     print Dumper( $args );
68 }
69
70 sub my_scan_handler {
71         my $args = shift;
72         my $term = $args->{TERM};
73         my $entries = [
74                                 {       TERM            =>      'Number 1',
75                                         OCCURRENCE      =>      10 },
76                                 {       TERM            =>      'Number 2',
77                                         OCCURRENCE      =>      8 },
78                                 {       TERM            =>      'Number 3',
79                                         OCCURRENCE      =>      8 },
80                                 {       TERM            =>      'Number 4',
81                                         OCCURRENCE      =>      8 },
82                                 {       TERM            =>      'Number 5',
83                                         OCCURRENCE      =>      8 },
84                                 {       TERM            =>      'Number 6',
85                                         OCCURRENCE      =>      8 },
86                                 {       TERM            =>      'Number 7',
87                                         OCCURRENCE      =>      8 },
88                                 {       TERM            =>      'Number 8',
89                                         OCCURRENCE      =>      8 },
90                                 {       TERM            =>      'Number 9',
91                                         OCCURRENCE      =>      8 },
92                                 {       TERM            =>      'Number 10',
93                                         OCCURRENCE      =>      4 },
94                         ];
95         $args->{NUMBER} = 10;
96         $args->{ENTRIES} = $entries;
97         $args->{STATUS} = Net::Z3950::SimpleServer::ScanPartial;
98         print "Welcome to scan....\n";
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 @database_list = @{ $args->{DATABASES} };
126         my $query = $args->{QUERY};
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 "------------------------------------------------------------\n";
134
135         $args->{HITS} = $hits;
136         $session->{$set_id} = $data;
137         $session->{__HITS} = $hits;
138         if ($_fail_frequency != 0 && ++$_counter % $_fail_frequency == 0) {
139             print "Exiting to be nasty to client\n";
140             exit(1);
141         }
142 }
143
144
145 sub my_fetch_handler {
146         my $args = shift;
147         my $session = $args->{HANDLE};
148         my $set_id = $args->{SETNAME};
149         my $data = $session->{$set_id};
150         my $offset = $args->{OFFSET};
151         my $record = "<xml>";
152         my $field;
153         my $hits = $session->{__HITS};
154         my $href = $data->[$offset - 1];
155
156         $args->{REP_FORM} = Net::Z3950::OID::xml;
157         foreach $field (keys %$href) {
158                 $record .= "<" . $field . ">" . $href->{$field} . "</" . $field . ">";
159         }
160
161         $record .= "</xml>";
162         $args->{RECORD} = $record;
163         if ($offset == $session->{__HITS}) {
164                 $args->{LAST} = 1;
165         }
166 }
167
168 Net::Z3950::SimpleServer::yazlog("hello");
169
170 my $handler = new Net::Z3950::SimpleServer( 
171                 INIT    =>      "main::my_init_handler",
172                 SEARCH  =>      "main::my_search_handler",
173                 SCAN    =>      "main::my_scan_handler",
174                 SORT    =>      "main::my_sort_handler",
175                 FETCH   =>      "main::my_fetch_handler" );
176
177 if (@ARGV >= 2 && $ARGV[0] eq "-n") {
178     $_fail_frequency = $ARGV[1];
179     shift;
180     shift;
181 }
182 $handler->launch_server("ztest.pl", @ARGV);