Corrected a memory leak, one more to go.
[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_VER} = "3.14159";
24         $args->{ERR_CODE} = 0;
25         $args->{HANDLE} = $session;
26 }
27
28 sub my_scan_handler {
29         my $args = shift;
30         my $term = $args->{TERM};
31         my $entries = [
32                                 {       TERM            =>      'Number 1',
33                                         OCCURRENCE      =>      10 },
34                                 {       TERM            =>      'Number 2',
35                                         OCCURRENCE      =>      8 },
36                                 {       TERM            =>      'Number 3',
37                                         OCCURRENCE      =>      8 },
38                                 {       TERM            =>      'Number 4',
39                                         OCCURRENCE      =>      8 },
40                                 {       TERM            =>      'Number 5',
41                                         OCCURRENCE      =>      8 },
42                                 {       TERM            =>      'Number 6',
43                                         OCCURRENCE      =>      8 },
44                                 {       TERM            =>      'Number 7',
45                                         OCCURRENCE      =>      8 },
46                                 {       TERM            =>      'Number 8',
47                                         OCCURRENCE      =>      8 },
48                                 {       TERM            =>      'Number 9',
49                                         OCCURRENCE      =>      8 },
50                                 {       TERM            =>      'Number 10',
51                                         OCCURRENCE      =>      4 },
52                         ];
53         $args->{NUMBER} = 10;
54         $args->{ENTRIES} = $entries;
55         $args->{STATUS} = Net::Z3950::SimpleServer::ScanPartial;
56         print "Welcome to scan....\n";
57         print "You scanned for term '$term'\n";
58 }
59
60
61 sub my_search_handler { 
62         my $args = shift;
63         my $data = [{
64                         name            =>      "Peter Dornan",
65                         title           =>      "Spokesman",
66                         collaboration   =>      "ATLAS"
67                     }, {
68                         name            =>      "Jorn Dines Hansen",
69                         title           =>      "Professor",
70                         collaboration   =>      "HERA-B"
71                     }, {
72                         name            =>      "Alain Blondel",
73                         title           =>      "Head of coll.",
74                         collaboration   =>      "ALEPH"
75                     }];
76
77         my $session = $args->{HANDLE};
78         my $set_id = $args->{SETNAME};
79         my @database_list = @{ $args->{DATABASES} };
80         my $query = $args->{QUERY};
81         my $hits = 3;
82
83         print "------------------------------------------------------------\n";
84         print "Processing query : $query\n";
85         printf("Database set     : %s\n", join(" ", @database_list));
86         print "Setname          : $set_id\n";
87         print "------------------------------------------------------------\n";
88
89         $args->{HITS} = $hits;
90         $session->{$set_id} = $data;
91         $session->{__HITS} = $hits;
92 }
93
94
95 sub my_fetch_handler {
96         my $args = shift;
97         my $session = $args->{HANDLE};
98         my $set_id = $args->{SETNAME};
99         my $data = $session->{$set_id};
100         my $offset = $args->{OFFSET};
101         my $record = "<xml>";
102         my $field;
103         my $hits = $session->{__HITS};
104         my $href = $data->[$offset - 1];
105
106         $args->{REP_FORM} = Net::Z3950::OID::xml;
107         foreach $field (keys %$href) {
108                 $record .= "<" . $field . ">" . $href->{$field} . "</" . $field . ">";
109         }
110
111         $record .= "</xml>";
112         $args->{RECORD} = $record;
113         if ($offset == $session->{__HITS}) {
114                 $args->{LAST} = 1;
115         }
116 }
117
118
119 my $handler = new Net::Z3950::SimpleServer( 
120                 INIT    =>      \&my_init_handler,
121                 SEARCH  =>      \&my_search_handler,
122                 SCAN    =>      \&my_scan_handler,
123                 FETCH   =>      \&my_fetch_handler );
124
125 $handler->launch_server("ztest.pl", @ARGV);
126
127
128 ## $Log: ztest.pl,v $
129 ## Revision 1.10  2001-08-30 13:15:11  sondberg
130 ## Corrected a memory leak, one more to go.
131 ##
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