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