0f76d5f824185df4704e1b721205a9526d161cf6
[simpleserver-moved-to-github.git] / ztest.pl
1 #!/usr/bin/perl -w
2
3 use ExtUtils::testlib;
4 use Net::Z3950::SimpleServer;
5 use Net::Z3950::OID;
6 use strict;
7
8
9 sub dump_hash {
10         my $href = shift;
11         my $key;
12
13         foreach $key (keys %$href) {
14                 printf("%10s    =>      %s\n", $key, $href->{$key});
15         }
16 }
17
18
19 sub my_init_handler {
20         my $args = shift;
21         my $session = {};
22
23         $args->{IMP_NAME} = "DemoServer";
24         $args->{IMP_VER} = "3.14159";
25         $args->{ERR_CODE} = 0;
26         $args->{HANDLE} = $session;
27 }
28
29 sub my_scan_handler {
30         my $args = shift;
31         my $term = $args->{TERM};
32         my $entries = [
33                                 {       TERM            =>      'Number 1',
34                                         OCCURRENCE      =>      10 },
35                                 {       TERM            =>      'Number 2',
36                                         OCCURRENCE      =>      8 },
37                                 {       TERM            =>      'Number 3',
38                                         OCCURRENCE      =>      8 },
39                                 {       TERM            =>      'Number 4',
40                                         OCCURRENCE      =>      8 },
41                                 {       TERM            =>      'Number 5',
42                                         OCCURRENCE      =>      8 },
43                                 {       TERM            =>      'Number 6',
44                                         OCCURRENCE      =>      8 },
45                                 {       TERM            =>      'Number 7',
46                                         OCCURRENCE      =>      8 },
47                                 {       TERM            =>      'Number 8',
48                                         OCCURRENCE      =>      8 },
49                                 {       TERM            =>      'Number 9',
50                                         OCCURRENCE      =>      8 },
51                                 {       TERM            =>      'Number 10',
52                                         OCCURRENCE      =>      4 },
53                         ];
54
55
56         $args->{NUMBER} = 10;
57         $args->{ENTRIES} = $entries;
58         $args->{STATUS} = Net::Z3950::SimpleServer::ScanPartial;
59         print "Welcome to scan....\n";
60         print "You scanned for term '$term'\n";
61 }
62
63
64 sub my_search_handler { 
65         my $args = shift;
66         my $data = [{
67                         name            =>      "Peter Dornan",
68                         title           =>      "Spokesman",
69                         collaboration   =>      "ATLAS"
70                     }, {
71                         name            =>      "Jorn Dines Hansen",
72                         title           =>      "Professor",
73                         collaboration   =>      "HERA-B"
74                     }, {
75                         name            =>      "Alain Blondel",
76                         title           =>      "Head of coll.",
77                         collaboration   =>      "ALEPH"
78                     }];
79
80         my $session = $args->{HANDLE};
81         my $set_id = $args->{SETNAME};
82         my @database_list = @{ $args->{DATABASES} };
83         my $query = $args->{QUERY};
84         my $hits = 3;
85
86         print "------------------------------------------------------------\n";
87         print "Processing query : $query\n";
88         printf("Database set     : %s\n", join(" ", @database_list));
89         print "Setname          : $set_id\n";
90         print "------------------------------------------------------------\n";
91
92         $args->{HITS} = $hits;
93         $session->{$set_id} = $data;
94         $session->{__HITS} = $hits;
95 }
96
97
98 sub my_fetch_handler {
99         my $args = shift;
100         my $session = $args->{HANDLE};
101         my $set_id = $args->{SETNAME};
102         my $data = $session->{$set_id};
103         my $offset = $args->{OFFSET};
104         my $record = "<xml>";
105         my $field;
106         my $hits = $session->{__HITS};
107         my $href = $data->[$offset - 1];
108
109         $args->{REP_FORM} = Net::Z3950::OID::xml;
110         foreach $field (keys %$href) {
111                 $record .= "<" . $field . ">" . $href->{$field} . "</" . $field . ">";
112         }
113
114         $record .= "</xml>";
115         $args->{RECORD} = $record;
116         if ($offset == $session->{__HITS}) {
117                 $args->{LAST} = 1;
118         }
119 }
120
121
122 my $handler = new Net::Z3950::SimpleServer( 
123                 INIT    =>      \&my_init_handler,
124                 SEARCH  =>      \&my_search_handler,
125                 SCAN    =>      \&my_scan_handler,
126                 FETCH   =>      \&my_fetch_handler );
127
128 $handler->launch_server("ztest.pl", @ARGV);
129
130
131 ## $Log: ztest.pl,v $
132 ## Revision 1.9  2001-08-29 11:48:36  sondberg
133 ## Added routines
134 ##
135 ##      Net::Z3950::SimpleServer::ScanSuccess
136 ##      Net::Z3950::SimpleServer::ScanPartial
137 ##
138 ## and a bit of documentation.
139 ##
140 ## Revision 1.8  2001/08/24 14:00:20  sondberg
141 ## Added support for scan.
142 ##
143 ## Revision 1.7  2001/03/13 14:20:21  sondberg
144 ## Added CVS logging
145 ##
146