1 |
|
---|
2 |
|
---|
3 | // Standard headers
|
---|
4 |
|
---|
5 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
6 | # include <ospace\std\iostream>
|
---|
7 | # include <ospace\std\fstream>
|
---|
8 | # include <ospace\std\sstream>
|
---|
9 | #elif defined(GSDL_USE_IOS_H)
|
---|
10 | # include <iostream.h>
|
---|
11 | # include <fstream.h>
|
---|
12 | # include <strstream.h>
|
---|
13 | #else
|
---|
14 | # include <iostream>
|
---|
15 | # include <fstream>
|
---|
16 | # include <sstream>
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | #include <string>
|
---|
20 | #include <list>
|
---|
21 |
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <stdlib.h>
|
---|
24 | #include <ctype.h>
|
---|
25 | #include <time.h>
|
---|
26 |
|
---|
27 | // Greenstone headers
|
---|
28 | #include "fileutil.h"
|
---|
29 | #include "comtypes.h"
|
---|
30 | #include "recptproto.h"
|
---|
31 | #include "nullproto.h"
|
---|
32 |
|
---|
33 | // Z39.50 server headers
|
---|
34 | #include "z3950parser.h"
|
---|
35 | #include "z3950explain.h"
|
---|
36 |
|
---|
37 | #include "z3950server.h"
|
---|
38 |
|
---|
39 | // Dublin Core -> MARC (d2m) headers and source files
|
---|
40 | #include "d2m4gs.h"
|
---|
41 |
|
---|
42 | // these globals are local to each thread,
|
---|
43 | // one Z39.50 connection = one thread.
|
---|
44 | text_t gsdlhome; // this is specified externally when Greenstone is installed
|
---|
45 | list<FilterResponse_t> Response_tlist; // list of response sets
|
---|
46 | list<text_t> Response_tlist_colnames; // collection names for the above
|
---|
47 | list<int> Response_tlist_sizes; // sizes (number of records) for the above
|
---|
48 | z3950Server *Server = NULL;
|
---|
49 | collectset *Cservers = NULL;
|
---|
50 | nullproto *Protocol = NULL;
|
---|
51 |
|
---|
52 | map<text_t, gsdlCollection> Collection_map; // info and tools for collections
|
---|
53 | map<text_t, text_t> Resultsets; // mapping from ResultSetId to GSQuery
|
---|
54 |
|
---|
55 | int z3950_verbosity_ = 3;
|
---|
56 |
|
---|
57 | // *** initialize things here ***
|
---|
58 | bend_initresult *bend_init(bend_initrequest *q)
|
---|
59 | {
|
---|
60 | // *** called when client connects to the server ***
|
---|
61 | if (z3950_verbosity_ > 1) {
|
---|
62 | cerr << "Entering bend_init" << endl;
|
---|
63 | }
|
---|
64 |
|
---|
65 | Protocol = new nullproto();
|
---|
66 | Cservers = new collectset(gsdlhome);
|
---|
67 | Protocol->set_collectset(Cservers);
|
---|
68 |
|
---|
69 | cerr << "Starting Z39.50 Server ..." << endl;
|
---|
70 |
|
---|
71 | Server = new z3950Server(Protocol, gsdlhome);
|
---|
72 | cerr << "Server constructed" << endl;
|
---|
73 |
|
---|
74 | const bool status = Server->initialise();
|
---|
75 | cerr << "Initialised server: return status = " << status << endl;
|
---|
76 |
|
---|
77 | text_tarray collist;
|
---|
78 | comerror_t err;
|
---|
79 | Protocol->get_collection_list( collist, err, cout );
|
---|
80 |
|
---|
81 | // display the list of available Greenstone collections
|
---|
82 | text_tarray::iterator cols_here=collist.begin();
|
---|
83 | text_tarray::iterator cols_end=collist.end();
|
---|
84 | while (cols_here != cols_end) {
|
---|
85 | gsdlCollection tmpcol(*cols_here, gsdlhome);
|
---|
86 |
|
---|
87 | Collection_map[*cols_here] = tmpcol;
|
---|
88 |
|
---|
89 | if (tmpcol.z3950Capeable()) {
|
---|
90 | cerr << " " << tmpcol.getName() << "\tis Z39.50 capable" << endl;
|
---|
91 | }
|
---|
92 | cols_here++;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
|
---|
97 | //static char *dummy = "Hej fister"; // what's this for?
|
---|
98 |
|
---|
99 | r->errcode = 0;
|
---|
100 | r->errstring = 0;
|
---|
101 | r->handle = Protocol;
|
---|
102 | q->bend_sort = ztest_sort; /* register sort handler */
|
---|
103 | q->bend_search = ztest_search; /* register search handler */
|
---|
104 | q->bend_present = ztest_present; /* register present handle */
|
---|
105 | q->bend_esrequest = ztest_esrequest;
|
---|
106 | q->bend_delete = ztest_delete;
|
---|
107 | q->bend_fetch = ztest_fetch;
|
---|
108 | q->bend_scan = ztest_scan;
|
---|
109 |
|
---|
110 | return r;
|
---|
111 | }
|
---|
112 |
|
---|
113 | // *** perform query, get number of hits ***
|
---|
114 | int ztest_search (void *handle, bend_search_rr *rr)
|
---|
115 | {
|
---|
116 | // *** called when client issues a "find" command
|
---|
117 | if (z3950_verbosity_>1) {
|
---|
118 | cerr << "Entering ztest_search" << endl;
|
---|
119 | }
|
---|
120 |
|
---|
121 | Response_tlist.clear();
|
---|
122 | Response_tlist_colnames.clear();
|
---|
123 | Response_tlist_sizes.clear();
|
---|
124 |
|
---|
125 | comerror_t err;
|
---|
126 | FilterRequest_t request;
|
---|
127 |
|
---|
128 | request.filterName = "QueryFilter";
|
---|
129 | request.filterResultOptions = FROID | FRtermFreq | FRmetadata;
|
---|
130 |
|
---|
131 | OptionValue_t option;
|
---|
132 |
|
---|
133 | // how many results to get. grouping all results together into
|
---|
134 | // one set instead of breaking up into "pages".
|
---|
135 | // note: see if Z39.50 has a concept of pages.
|
---|
136 | option.name = "StartResults";
|
---|
137 | option.value = "1";
|
---|
138 | request.filterOptions.push_back (option);
|
---|
139 | option.name = "EndResults";
|
---|
140 | option.value = MAXHITS;
|
---|
141 | request.filterOptions.push_back (option);
|
---|
142 | option.name = "Maxdocs";
|
---|
143 | option.value = MAXHITS;
|
---|
144 | request.filterOptions.push_back (option);
|
---|
145 |
|
---|
146 | text_t GSQuery = ZQueryToGSQuery(rr);
|
---|
147 | option.name = "Term";
|
---|
148 | option.value = GSQuery;
|
---|
149 | request.filterOptions.push_back (option);
|
---|
150 | Resultsets[Resultsets.size()+1] = GSQuery;
|
---|
151 |
|
---|
152 | option.name = "QueryType";
|
---|
153 | option.value = "boolean";
|
---|
154 | request.filterOptions.push_back (option);
|
---|
155 |
|
---|
156 | option.name = "MatchMode";
|
---|
157 | option.value = "all";
|
---|
158 | request.filterOptions.push_back (option);
|
---|
159 |
|
---|
160 | option.name = "Casefold";
|
---|
161 | option.value = "true";
|
---|
162 | request.filterOptions.push_back (option);
|
---|
163 |
|
---|
164 | option.name = "Stem";
|
---|
165 | option.value = "false";
|
---|
166 | request.filterOptions.push_back (option);
|
---|
167 |
|
---|
168 | option.name = "AccentFold";
|
---|
169 | option.value = "false";
|
---|
170 | request.filterOptions.push_back (option);
|
---|
171 |
|
---|
172 | // MG queries don't need this, MGPP queries do.
|
---|
173 | // doesn't do any harm to include it anyway.
|
---|
174 | // note: not all collections have document-level granularity...
|
---|
175 | option.name = "Level";
|
---|
176 | option.value = "Doc";
|
---|
177 | request.filterOptions.push_back (option);
|
---|
178 |
|
---|
179 | // Z39.50 supports the searching of multiple databases
|
---|
180 | rr->hits = 0;
|
---|
181 | for (int i = 0; i < rr->num_bases; i++) {
|
---|
182 | FilterResponse_t response;
|
---|
183 | cerr << "Searching basename = " << rr->basenames[i] << endl;
|
---|
184 | // intercept Explain requests
|
---|
185 | if (strcmp(rr->basenames[i], "IR-Explain-1")==0) {
|
---|
186 | //ColInfoResponse_t collectinfo;
|
---|
187 | //Protocol->get_collectinfo("csbib", collectinfo, err, cerr);
|
---|
188 | //response.numDocs = 1;
|
---|
189 | //ResultDocInfo_t docinfo;
|
---|
190 | //docinfo.metadata["foo"].values.push_back("bar");
|
---|
191 | getExplainInfo(rr, response, err);
|
---|
192 | //response.docInfo.push_back(docinfo);
|
---|
193 | } else {
|
---|
194 | cerr << "calling filter..." << endl;
|
---|
195 | Protocol->filter(rr->basenames[i], request, response, err, cerr);
|
---|
196 | cerr << "returned from filter." << endl;
|
---|
197 | if (response.numDocs > MAXHITS) {
|
---|
198 | response.numDocs = MAXHITS;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | rr->hits += response.numDocs;
|
---|
202 | Response_tlist.push_back(response);
|
---|
203 | text_t basename = rr->basenames[i];
|
---|
204 | Response_tlist_colnames.push_back(basename);
|
---|
205 | Response_tlist_sizes.push_back(response.numDocs);
|
---|
206 | cerr << "search complete." << endl;
|
---|
207 | }
|
---|
208 |
|
---|
209 | if (rr->hits > MAXHITS) rr->hits = MAXHITS;
|
---|
210 | return 0;
|
---|
211 | }
|
---|
212 |
|
---|
213 | // *** extra code for showing results, not needed ***
|
---|
214 | int ztest_present (void *handle, bend_present_rr *rr)
|
---|
215 | {
|
---|
216 | // *** called when client issues a "show" command, 1st of 5
|
---|
217 | if (z3950_verbosity_>1) {
|
---|
218 | cerr << "entering ztest_present" << endl;
|
---|
219 | }
|
---|
220 | return 0;
|
---|
221 | }
|
---|
222 |
|
---|
223 | int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
|
---|
224 | {
|
---|
225 |
|
---|
226 | if (z3950_verbosity_>1) {
|
---|
227 | cerr << "Entering ztest_esrequest" << endl;
|
---|
228 | }
|
---|
229 | yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
|
---|
230 | if (rr->esr->packageName)
|
---|
231 | yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
|
---|
232 | yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
|
---|
233 |
|
---|
234 | if (!rr->esr->taskSpecificParameters)
|
---|
235 | {
|
---|
236 | yaz_log (LOG_WARN, "No task specific parameters");
|
---|
237 | }
|
---|
238 | else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
|
---|
239 | {
|
---|
240 | Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
|
---|
241 | yaz_log (LOG_LOG, "Received ItemOrder");
|
---|
242 | switch (it->which)
|
---|
243 | {
|
---|
244 | #ifdef ASN_COMPILED
|
---|
245 | case Z_IOItemOrder_esRequest:
|
---|
246 | #else
|
---|
247 | case Z_ItemOrder_esRequest:
|
---|
248 | #endif
|
---|
249 | {
|
---|
250 | Z_IORequest *ir = it->u.esRequest;
|
---|
251 | Z_IOOriginPartToKeep *k = ir->toKeep;
|
---|
252 | Z_IOOriginPartNotToKeep *n = ir->notToKeep;
|
---|
253 |
|
---|
254 | if (k && k->contact)
|
---|
255 | {
|
---|
256 | if (k->contact->name)
|
---|
257 | yaz_log(LOG_LOG, "contact name %s", k->contact->name);
|
---|
258 | if (k->contact->phone)
|
---|
259 | yaz_log(LOG_LOG, "contact phone %s", k->contact->phone);
|
---|
260 | if (k->contact->email)
|
---|
261 | yaz_log(LOG_LOG, "contact email %s", k->contact->email);
|
---|
262 | }
|
---|
263 | if (k->addlBilling)
|
---|
264 | {
|
---|
265 | yaz_log(LOG_LOG, "Billing info (not shown)");
|
---|
266 | }
|
---|
267 |
|
---|
268 | if (n->resultSetItem)
|
---|
269 | {
|
---|
270 | yaz_log(LOG_LOG, "resultsetItem");
|
---|
271 | yaz_log(LOG_LOG, "setId: %s", n->resultSetItem->resultSetId);
|
---|
272 | yaz_log(LOG_LOG, "item: %d", *n->resultSetItem->item);
|
---|
273 | }
|
---|
274 | #ifdef ASN_COMPILED
|
---|
275 | if (n->itemRequest)
|
---|
276 | {
|
---|
277 | Z_External *r = (Z_External*) n->itemRequest;
|
---|
278 | ILL_ItemRequest *item_req = 0;
|
---|
279 | ILL_Request *ill_req = 0;
|
---|
280 | if (r->direct_reference)
|
---|
281 | {
|
---|
282 | oident *ent = oid_getentbyoid(r->direct_reference);
|
---|
283 | if (ent)
|
---|
284 | yaz_log(LOG_LOG, "OID %s", ent->desc);
|
---|
285 | if (ent && ent->value == VAL_ISO_ILL_1)
|
---|
286 | {
|
---|
287 | yaz_log (LOG_LOG, "ItemRequest");
|
---|
288 | if (r->which == ODR_EXTERNAL_single)
|
---|
289 | {
|
---|
290 | odr_setbuf(rr->decode,
|
---|
291 | (char*)(r->u.single_ASN1_type->buf),
|
---|
292 | r->u.single_ASN1_type->len, 0);
|
---|
293 |
|
---|
294 | if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
|
---|
295 | {
|
---|
296 | yaz_log (LOG_LOG,
|
---|
297 | "Couldn't decode ItemRequest %s near %d",
|
---|
298 | odr_errmsg(odr_geterror(rr->decode)),
|
---|
299 | odr_offset(rr->decode));
|
---|
300 | yaz_log(LOG_LOG, "PDU dump:");
|
---|
301 | odr_dumpBER(yaz_log_file(),
|
---|
302 | (const char*)(r->u.single_ASN1_type->buf),
|
---|
303 | r->u.single_ASN1_type->len);
|
---|
304 | }
|
---|
305 | if (rr->print)
|
---|
306 | {
|
---|
307 | ill_ItemRequest (rr->print, &item_req, 0,
|
---|
308 | "ItemRequest");
|
---|
309 | odr_reset (rr->print);
|
---|
310 | }
|
---|
311 | }
|
---|
312 | if (!item_req && r->which == ODR_EXTERNAL_single)
|
---|
313 | {
|
---|
314 | yaz_log (LOG_LOG, "ILLRequest");
|
---|
315 | odr_setbuf(rr->decode,
|
---|
316 | (char*)(r->u.single_ASN1_type->buf),
|
---|
317 | r->u.single_ASN1_type->len, 0);
|
---|
318 |
|
---|
319 | if (!ill_Request (rr->decode, &ill_req, 0, 0))
|
---|
320 | {
|
---|
321 | yaz_log (LOG_LOG,
|
---|
322 | "Couldn't decode ILLRequest %s near %d",
|
---|
323 | odr_errmsg(odr_geterror(rr->decode)),
|
---|
324 | odr_offset(rr->decode));
|
---|
325 | yaz_log(LOG_LOG, "PDU dump:");
|
---|
326 | odr_dumpBER(yaz_log_file(),
|
---|
327 | (const char*)(r->u.single_ASN1_type->buf),
|
---|
328 | r->u.single_ASN1_type->len);
|
---|
329 | }
|
---|
330 | if (rr->print)
|
---|
331 | {
|
---|
332 | ill_Request (rr->print, &ill_req, 0,
|
---|
333 | "ILLRequest");
|
---|
334 | odr_reset (rr->print);
|
---|
335 | }
|
---|
336 | }
|
---|
337 | }
|
---|
338 | }
|
---|
339 | if (item_req)
|
---|
340 | {
|
---|
341 | yaz_log (LOG_LOG, "ILL protocol version = %d",
|
---|
342 | *item_req->protocol_version_num);
|
---|
343 | }
|
---|
344 | }
|
---|
345 | #endif
|
---|
346 | }
|
---|
347 | break;
|
---|
348 | }
|
---|
349 | }
|
---|
350 | else if (rr->esr->taskSpecificParameters->which == Z_External_update)
|
---|
351 | {
|
---|
352 | Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
|
---|
353 | yaz_log (LOG_LOG, "Received DB Update");
|
---|
354 | if (up->which == Z_IUUpdate_esRequest)
|
---|
355 | {
|
---|
356 | Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
|
---|
357 | Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
|
---|
358 | Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
|
---|
359 |
|
---|
360 | yaz_log (LOG_LOG, "action");
|
---|
361 | if (toKeep->action)
|
---|
362 | {
|
---|
363 | switch (*toKeep->action)
|
---|
364 | {
|
---|
365 | case Z_IUOriginPartToKeep_recordInsert:
|
---|
366 | yaz_log (LOG_LOG, " recordInsert");
|
---|
367 | break;
|
---|
368 | case Z_IUOriginPartToKeep_recordReplace:
|
---|
369 | yaz_log (LOG_LOG, " recordUpdate");
|
---|
370 | break;
|
---|
371 | case Z_IUOriginPartToKeep_recordDelete:
|
---|
372 | yaz_log (LOG_LOG, " recordDelete");
|
---|
373 | break;
|
---|
374 | case Z_IUOriginPartToKeep_elementUpdate:
|
---|
375 | yaz_log (LOG_LOG, " elementUpdate");
|
---|
376 | break;
|
---|
377 | case Z_IUOriginPartToKeep_specialUpdate:
|
---|
378 | yaz_log (LOG_LOG, " specialUpdate");
|
---|
379 | break;
|
---|
380 | default:
|
---|
381 | yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
|
---|
382 | }
|
---|
383 | }
|
---|
384 | if (toKeep->databaseName)
|
---|
385 | {
|
---|
386 | yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
|
---|
387 | if (!strcmp(toKeep->databaseName, "fault"))
|
---|
388 | {
|
---|
389 | rr->errcode = 109;
|
---|
390 | rr->errstring = toKeep->databaseName;
|
---|
391 | }
|
---|
392 | if (!strcmp(toKeep->databaseName, "accept"))
|
---|
393 | rr->errcode = -1;
|
---|
394 | }
|
---|
395 | if (notToKeep)
|
---|
396 | {
|
---|
397 | int i;
|
---|
398 | for (i = 0; i < notToKeep->num; i++)
|
---|
399 | {
|
---|
400 | Z_External *rec = notToKeep->elements[i]->record;
|
---|
401 |
|
---|
402 | if (rec->direct_reference)
|
---|
403 | {
|
---|
404 | struct oident *oident;
|
---|
405 | oident = oid_getentbyoid(rec->direct_reference);
|
---|
406 | if (oident)
|
---|
407 | yaz_log (LOG_LOG, "record %d type %s", i,
|
---|
408 | oident->desc);
|
---|
409 | }
|
---|
410 | switch (rec->which)
|
---|
411 | {
|
---|
412 | case Z_External_sutrs:
|
---|
413 | if (rec->u.octet_aligned->len > 170)
|
---|
414 | yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
|
---|
415 | rec->u.sutrs->len,
|
---|
416 | rec->u.sutrs->buf);
|
---|
417 | else
|
---|
418 | yaz_log (LOG_LOG, "%d bytes:\n%s",
|
---|
419 | rec->u.sutrs->len,
|
---|
420 | rec->u.sutrs->buf);
|
---|
421 | break;
|
---|
422 | case Z_External_octet :
|
---|
423 | if (rec->u.octet_aligned->len > 170)
|
---|
424 | yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
|
---|
425 | rec->u.octet_aligned->len,
|
---|
426 | rec->u.octet_aligned->buf);
|
---|
427 | else
|
---|
428 | yaz_log (LOG_LOG, "%d bytes\n%s",
|
---|
429 | rec->u.octet_aligned->len,
|
---|
430 | rec->u.octet_aligned->buf);
|
---|
431 | }
|
---|
432 | }
|
---|
433 | }
|
---|
434 | }
|
---|
435 | }
|
---|
436 | else
|
---|
437 | {
|
---|
438 | yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
|
---|
439 | rr->esr->taskSpecificParameters->which);
|
---|
440 |
|
---|
441 | }
|
---|
442 | return 0;
|
---|
443 | }
|
---|
444 |
|
---|
445 | int ztest_delete (void *handle, bend_delete_rr *rr)
|
---|
446 | {
|
---|
447 | // *** called when client issues a "delete" command
|
---|
448 | if (z3950_verbosity_>1) {
|
---|
449 | cerr << "Entering ztest_delete" << endl;
|
---|
450 | }
|
---|
451 | if (rr->num_setnames == 1 && !strcmp (rr->setnames[0], "1"))
|
---|
452 | rr->delete_status = Z_DeleteStatus_success;
|
---|
453 | else
|
---|
454 | rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
|
---|
455 | return 0;
|
---|
456 | }
|
---|
457 |
|
---|
458 | // *** do we want to sort the results? ***
|
---|
459 | /* Our sort handler really doesn't sort... */
|
---|
460 | int ztest_sort (void *handle, bend_sort_rr *rr)
|
---|
461 | {
|
---|
462 | if (z3950_verbosity_>1) {
|
---|
463 | cerr << "entering ztest_sort" << endl;
|
---|
464 | }
|
---|
465 | rr->errcode = 0;
|
---|
466 | rr->sort_status = Z_SortStatus_success;
|
---|
467 | return 0;
|
---|
468 | }
|
---|
469 |
|
---|
470 | static int atoin (const char *buf, int n)
|
---|
471 | {
|
---|
472 | // *** called when client issues a "show" command, 5th of 5
|
---|
473 | // *** NOT called if the "show" command has an invalid argument
|
---|
474 | if (z3950_verbosity_>1) {
|
---|
475 | cerr << "Entering atoin" << endl;
|
---|
476 | }
|
---|
477 | int val = 0;
|
---|
478 | while (--n >= 0)
|
---|
479 | {
|
---|
480 | if (isdigit(*buf))
|
---|
481 | val = val*10 + (*buf - '0');
|
---|
482 | buf++;
|
---|
483 | }
|
---|
484 | return val;
|
---|
485 | }
|
---|
486 |
|
---|
487 | // *** don't think we want this ***
|
---|
488 | static Z_GenericRecord *dummy_grs_record (int num, ODR o)
|
---|
489 | {
|
---|
490 | if (z3950_verbosity_>1) {
|
---|
491 | cerr << "Entering dummy_grs_record" << endl;
|
---|
492 | }
|
---|
493 |
|
---|
494 | FILE *f = fopen("dummy-grs", "r");
|
---|
495 | char line[512];
|
---|
496 | Z_GenericRecord *r = 0;
|
---|
497 | int n;
|
---|
498 |
|
---|
499 | if (!f)
|
---|
500 | return 0;
|
---|
501 | while (fgets(line, 512, f))
|
---|
502 | if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
|
---|
503 | {
|
---|
504 | r = read_grs1(f, o);
|
---|
505 | break;
|
---|
506 | }
|
---|
507 | fclose(f);
|
---|
508 | return r;
|
---|
509 | }
|
---|
510 |
|
---|
511 | bool get_sutrs_record (int recnum, text_t &MetadataResult, text_t &CollectionName)
|
---|
512 | {
|
---|
513 | if (z3950_verbosity_>1) {
|
---|
514 | cerr << "Entering get_sutrs_record" << endl;
|
---|
515 | }
|
---|
516 | int total_records = 0;
|
---|
517 |
|
---|
518 | list<FilterResponse_t>::iterator RESPONSE_here = Response_tlist.begin();
|
---|
519 | list<FilterResponse_t>::iterator RESPONSE_end = Response_tlist.end();
|
---|
520 | list<text_t>::iterator RESPONSE_COLNAMES_here = Response_tlist_colnames.begin();
|
---|
521 | list<text_t>::iterator RESPONSE_COLNAMES_end = Response_tlist_colnames.end();
|
---|
522 | list<int>::iterator RESPONSE_SIZES_here = Response_tlist_sizes.begin();
|
---|
523 | list<int>::iterator RESPONSE_SIZES_end = Response_tlist_sizes.end();
|
---|
524 |
|
---|
525 | RESPONSE_here = Response_tlist.begin();
|
---|
526 | RESPONSE_end = Response_tlist.end();
|
---|
527 | while (RESPONSE_here != RESPONSE_end) {
|
---|
528 | total_records += (*RESPONSE_here).numDocs;
|
---|
529 | RESPONSE_here++;
|
---|
530 | }
|
---|
531 |
|
---|
532 | // check that the index is within bounds
|
---|
533 | if (recnum < 1 || recnum > total_records) {
|
---|
534 | cerr << "get_sutrs_record failed" << endl;
|
---|
535 | return false; // failed
|
---|
536 | } else {
|
---|
537 | FilterResponse_t response;
|
---|
538 | // iterate through Response_tlist to find the right response
|
---|
539 | RESPONSE_here = Response_tlist.begin();
|
---|
540 | RESPONSE_end = Response_tlist.end();
|
---|
541 | RESPONSE_COLNAMES_here = Response_tlist_colnames.begin();
|
---|
542 | RESPONSE_COLNAMES_end = Response_tlist_colnames.end();
|
---|
543 | RESPONSE_SIZES_here = Response_tlist_sizes.begin();
|
---|
544 | RESPONSE_SIZES_end = Response_tlist_sizes.end();
|
---|
545 | int n = 0;
|
---|
546 |
|
---|
547 | while (RESPONSE_here != RESPONSE_end) {
|
---|
548 | n += *RESPONSE_SIZES_here;
|
---|
549 | if (n >= recnum) {
|
---|
550 | response = *RESPONSE_here;
|
---|
551 | CollectionName = *RESPONSE_COLNAMES_here;
|
---|
552 | recnum = *RESPONSE_SIZES_here - (n - recnum);
|
---|
553 | break;
|
---|
554 | }
|
---|
555 | // these should all have the same number of elements,
|
---|
556 | // so only need to check that one is within bounds.
|
---|
557 | // may want to combine these into a larger data structure later.
|
---|
558 | RESPONSE_here++;
|
---|
559 | RESPONSE_COLNAMES_here++;
|
---|
560 | RESPONSE_SIZES_here++;
|
---|
561 | }
|
---|
562 |
|
---|
563 | // locate the desired record in the response
|
---|
564 |
|
---|
565 | MetadataInfo_tmap::iterator METAFIELD_here = response.docInfo[recnum-1].metadata.begin();
|
---|
566 | MetadataInfo_tmap::iterator METAFIELD_end = response.docInfo[recnum-1].metadata.end();
|
---|
567 |
|
---|
568 | while (METAFIELD_here!=METAFIELD_end) {
|
---|
569 | text_tarray::iterator METAVALUE_here = METAFIELD_here->second.values.begin();
|
---|
570 | text_tarray::iterator METAVALUE_end = METAFIELD_here->second.values.end();
|
---|
571 | while (METAVALUE_here!=METAVALUE_end) {
|
---|
572 | // construct a text_t containing the record:
|
---|
573 | // there are multiple metadata fields,
|
---|
574 | // each field can have multiple values.
|
---|
575 | /*
|
---|
576 | // don't include fields starting with a lowercase letter,
|
---|
577 | // these are system fields.
|
---|
578 | // later: include these anyway, since Explain fields start with lowercase letters
|
---|
579 | if (METAFIELD_here->first[0] >= 'A' &&
|
---|
580 | METAFIELD_here->first[0] <= 'Z') {
|
---|
581 | */
|
---|
582 | MetadataResult += METAFIELD_here->first; // field name
|
---|
583 | MetadataResult += " ";
|
---|
584 | MetadataResult += *METAVALUE_here; // field contents
|
---|
585 | MetadataResult += "\n";
|
---|
586 | //}
|
---|
587 | METAVALUE_here++;
|
---|
588 | }
|
---|
589 | METAFIELD_here++;
|
---|
590 | }
|
---|
591 | return true; // succeeded
|
---|
592 | }
|
---|
593 | }
|
---|
594 |
|
---|
595 | bool get_marc_record (int recnum, list<metatag*> &Metatag_list, text_t &CollectionName)
|
---|
596 | {
|
---|
597 | if (z3950_verbosity_>1) {
|
---|
598 | cerr << "Entering get_marc_record" << endl;
|
---|
599 | }
|
---|
600 | int total_records = 0;
|
---|
601 |
|
---|
602 | list<FilterResponse_t>::iterator RESPONSE_here = Response_tlist.begin();
|
---|
603 | list<FilterResponse_t>::iterator RESPONSE_end = Response_tlist.end();
|
---|
604 | list<text_t>::iterator RESPONSE_COLNAMES_here = Response_tlist_colnames.begin();
|
---|
605 | list<text_t>::iterator RESPONSE_COLNAMES_end = Response_tlist_colnames.end();
|
---|
606 | list<int>::iterator RESPONSE_SIZES_here = Response_tlist_sizes.begin();
|
---|
607 | list<int>::iterator RESPONSE_SIZES_end = Response_tlist_sizes.end();
|
---|
608 |
|
---|
609 | RESPONSE_here = Response_tlist.begin();
|
---|
610 | RESPONSE_end = Response_tlist.end();
|
---|
611 | while (RESPONSE_here != RESPONSE_end) {
|
---|
612 | total_records += (*RESPONSE_here).numDocs;
|
---|
613 | RESPONSE_here++;
|
---|
614 | }
|
---|
615 |
|
---|
616 | // check that the index is within bounds
|
---|
617 | if (recnum < 1 || recnum > total_records) {
|
---|
618 | cerr << "get_marc_record failed" << endl;
|
---|
619 | return false; // failed
|
---|
620 | } else {
|
---|
621 | FilterResponse_t response;
|
---|
622 | // iterate through Response_tlist to find the right response
|
---|
623 | RESPONSE_here = Response_tlist.begin();
|
---|
624 | RESPONSE_end = Response_tlist.end();
|
---|
625 | RESPONSE_COLNAMES_here = Response_tlist_colnames.begin();
|
---|
626 | RESPONSE_COLNAMES_end = Response_tlist_colnames.end();
|
---|
627 | RESPONSE_SIZES_here = Response_tlist_sizes.begin();
|
---|
628 | RESPONSE_SIZES_end = Response_tlist_sizes.end();
|
---|
629 | int n = 0;
|
---|
630 | while (RESPONSE_here != RESPONSE_end) {
|
---|
631 | n += *RESPONSE_SIZES_here;
|
---|
632 | if (n >= recnum) {
|
---|
633 | response = *RESPONSE_here;
|
---|
634 | CollectionName = *RESPONSE_COLNAMES_here;
|
---|
635 | recnum = *RESPONSE_SIZES_here - (n - recnum);
|
---|
636 | break;
|
---|
637 | }
|
---|
638 | // these should all have the same number of elements,
|
---|
639 | // so only need to check that one is within bounds.
|
---|
640 | // may want to combine these into a larger data structure later.
|
---|
641 | RESPONSE_here++;
|
---|
642 | RESPONSE_COLNAMES_here++;
|
---|
643 | RESPONSE_SIZES_here++;
|
---|
644 | }
|
---|
645 |
|
---|
646 | // locate the desired record in the response
|
---|
647 |
|
---|
648 | MetadataInfo_tmap::iterator METAFIELD_here = response.docInfo[recnum-1].metadata.begin();
|
---|
649 | MetadataInfo_tmap::iterator METAFIELD_end = response.docInfo[recnum-1].metadata.end();
|
---|
650 |
|
---|
651 | while (METAFIELD_here!=METAFIELD_end) {
|
---|
652 | text_tarray::iterator METAVALUE_here = METAFIELD_here->second.values.begin();
|
---|
653 | text_tarray::iterator METAVALUE_end = METAFIELD_here->second.values.end();
|
---|
654 | while (METAVALUE_here!=METAVALUE_end) {
|
---|
655 | if (METAFIELD_here->first[0] >= 'A' &&
|
---|
656 | METAFIELD_here->first[0] <= 'Z') {
|
---|
657 | metatag *tmptag = new metatag();
|
---|
658 | tmptag->name = METAFIELD_here->first.getcstr();
|
---|
659 | tmptag->type = "";
|
---|
660 | tmptag->scheme = "";
|
---|
661 | tmptag->value = (*METAVALUE_here).getcstr();
|
---|
662 | Metatag_list.push_back(tmptag);
|
---|
663 | }
|
---|
664 | METAVALUE_here++;
|
---|
665 | }
|
---|
666 | METAFIELD_here++;
|
---|
667 | }
|
---|
668 | return true; // succeeded
|
---|
669 | }
|
---|
670 | }
|
---|
671 |
|
---|
672 | // *** implement this ***
|
---|
673 | int ztest_fetch(void *handle, bend_fetch_rr *r)
|
---|
674 | {
|
---|
675 | // *** called when client issues a "show" command, 2nd of 5
|
---|
676 | if (z3950_verbosity_>1) {
|
---|
677 | cerr << "Entering ztest_fetch" << endl;
|
---|
678 | }
|
---|
679 | char *cp;
|
---|
680 | r->errstring = 0;
|
---|
681 | r->last_in_set = 0;
|
---|
682 | r->basename = "DUMMY"; // this is set below
|
---|
683 | r->output_format = r->request_format;
|
---|
684 |
|
---|
685 | // *** use this until found a good way of mapping Greenstone
|
---|
686 | // *** metadata fields to USMARC fields
|
---|
687 | if (r->request_format == VAL_SUTRS)
|
---|
688 | {
|
---|
689 | // copies record into r, errcode will be 13 if fails
|
---|
690 | text_t MetadataResult;
|
---|
691 | text_t CollectionName;
|
---|
692 | if (!get_sutrs_record(r->number, MetadataResult, CollectionName)) {
|
---|
693 | r->errcode = 13;
|
---|
694 | return 0;
|
---|
695 | } else {
|
---|
696 | r->len = MetadataResult.size();
|
---|
697 | r->record = (char*) odr_malloc(r->stream, r->len+1);
|
---|
698 | char *tmptxt = MetadataResult.getcstr();
|
---|
699 | strcpy(r->record, tmptxt);
|
---|
700 | delete tmptxt;
|
---|
701 | r->basename = (char*) odr_malloc(r->stream, CollectionName.size()+1);
|
---|
702 | tmptxt = CollectionName.getcstr();
|
---|
703 | strcpy(r->basename, tmptxt);
|
---|
704 | delete tmptxt;
|
---|
705 | }
|
---|
706 | }
|
---|
707 | // *** do we want to use this format? ***
|
---|
708 | else if (r->request_format == VAL_GRS1)
|
---|
709 | {
|
---|
710 | // bail out, since we don't support grs (yet?)
|
---|
711 | r->errcode = 107; // "Query type not supported"
|
---|
712 | return 0;
|
---|
713 |
|
---|
714 | }
|
---|
715 | // assume that here on in is some sort of MARC format
|
---|
716 | else {
|
---|
717 | marcrec *mrec = new marcrec;
|
---|
718 |
|
---|
719 | mrec = (marcrec*) malloc(sizeof(*mrec));
|
---|
720 |
|
---|
721 | mrec->marcline = (char*) malloc(100000);
|
---|
722 | mrec->partitle = (char*) malloc(10000);
|
---|
723 | mrec->subtitle = (char*) malloc(10000);
|
---|
724 | mrec->year = (char*) malloc(1000);
|
---|
725 | mrec->url = (char*) malloc(1000);
|
---|
726 | mrec->fmat = (char*) malloc(1000);
|
---|
727 | mrec->s008 = (char*) malloc(41);
|
---|
728 |
|
---|
729 | mrec->ncreators = 0;
|
---|
730 | mrec->ntitles = 0;
|
---|
731 | *mrec->marcline = 0;
|
---|
732 | *mrec->subtitle = 0;
|
---|
733 | *mrec->partitle = 0;
|
---|
734 | *mrec->year = 0;
|
---|
735 | *mrec->url = 0;
|
---|
736 | *mrec->fmat = 0;
|
---|
737 | strcpy(mrec->s008, " ");
|
---|
738 |
|
---|
739 | text_t CollectionName;
|
---|
740 | list<metatag*> Metatag_list;
|
---|
741 |
|
---|
742 | if (!get_marc_record(r->number, Metatag_list, CollectionName)) {
|
---|
743 | r->errcode = 13;
|
---|
744 | return 0;
|
---|
745 | } else {
|
---|
746 | list<metatag*>::iterator METATAG_here = Metatag_list.begin();
|
---|
747 | list<metatag*>::iterator METATAG_end = Metatag_list.end();
|
---|
748 | while (METATAG_here != METATAG_end) {
|
---|
749 | usMARC(*METATAG_here, mrec);
|
---|
750 | METATAG_here++;
|
---|
751 | }
|
---|
752 |
|
---|
753 | // set the date in the marc record to the current date
|
---|
754 | char today[32];
|
---|
755 | time_t d;
|
---|
756 | struct tm *time_struct;
|
---|
757 | //putenv("TZ=NFT-1DFT");
|
---|
758 | d = time(NULL);
|
---|
759 | time_struct = localtime(&d);
|
---|
760 | strftime(today, 16, "%y%m%d", time_struct);
|
---|
761 | put008(mrec->s008, today, F008_DATE_ENTERED);
|
---|
762 | put008(mrec->s008, "s", F008_TYPE_OF_DATE);
|
---|
763 |
|
---|
764 | // to do: make this safer + more efficient
|
---|
765 | cp = new char[1000000];
|
---|
766 | char *cp2 = new char[1000000];
|
---|
767 |
|
---|
768 | switch(r->request_format) {
|
---|
769 | case VAL_DANMARC: {
|
---|
770 | MARCtidy(mrec, cp, DANMARC);
|
---|
771 | r->output_format = VAL_DANMARC;
|
---|
772 | break; }
|
---|
773 | case VAL_FINMARC: {
|
---|
774 | MARCtidy(mrec, cp, FINMARC);
|
---|
775 | r->output_format = VAL_FINMARC;
|
---|
776 | break; }
|
---|
777 | // YAZ doesn't know about ISMARC
|
---|
778 | /*
|
---|
779 | case VAL_ISMARC: {
|
---|
780 | put008(mrec->s008, "k", F008_FORM_OF_PUBLICATION);
|
---|
781 | MARCtidy(mrec, cp, ISMARC);
|
---|
782 | r->output_format = VAL_ISMARC;
|
---|
783 | break; }
|
---|
784 | */
|
---|
785 | case VAL_NORMARC: {
|
---|
786 | MARCtidy(mrec, cp, NORMARC);
|
---|
787 | r->output_format = VAL_NORMARC;
|
---|
788 | break; }
|
---|
789 | case VAL_SWEMARC: {
|
---|
790 | MARCtidy(mrec, cp, SWEMARC);
|
---|
791 | r->output_format = VAL_SWEMARC;
|
---|
792 | break; }
|
---|
793 | default: {
|
---|
794 | MARCtidy(mrec, cp, USMARC);
|
---|
795 | r->output_format = VAL_USMARC;
|
---|
796 | break; }
|
---|
797 | }
|
---|
798 |
|
---|
799 | sprintf(cp2, "%s", (char*) MARC2709(cp, 0, 6, 'n', 'm', ' '));
|
---|
800 | r->len = strlen(cp2);
|
---|
801 | r->record = (char*) odr_malloc(r->stream, r->len+1);
|
---|
802 | strcpy(r->record, cp2);
|
---|
803 | r->basename = (char*) odr_malloc(r->stream, CollectionName.size()+1);
|
---|
804 | char *tmptxt;
|
---|
805 | tmptxt = CollectionName.getcstr();
|
---|
806 | strcpy(r->basename, tmptxt);
|
---|
807 | delete tmptxt;
|
---|
808 | delete cp2;
|
---|
809 | cerr << "dumping record:`" << cp << "'" << endl;
|
---|
810 | delete cp;
|
---|
811 | }
|
---|
812 | }
|
---|
813 |
|
---|
814 | r->errcode = 0;
|
---|
815 | return 0;
|
---|
816 | }
|
---|
817 |
|
---|
818 | /*
|
---|
819 | * silly dummy-scan what reads words from a file.
|
---|
820 | */
|
---|
821 | int ztest_scan(void *handle, bend_scan_rr *q)
|
---|
822 | {
|
---|
823 | // *** called when client issues a "scan" command
|
---|
824 | if (z3950_verbosity_>1) {
|
---|
825 | cerr << "Entering ztest_scan" << endl;
|
---|
826 | }
|
---|
827 |
|
---|
828 | // bail out, since we don't support scan (yet?)
|
---|
829 | q->errcode = 107; // "Query type not supported"
|
---|
830 | return 0;
|
---|
831 |
|
---|
832 | static FILE *f = 0;
|
---|
833 | static struct scan_entry list[200];
|
---|
834 | static char entries[200][80];
|
---|
835 | int hits[200];
|
---|
836 | char term[80], *p;
|
---|
837 | int i, pos;
|
---|
838 | int term_position_req = q->term_position;
|
---|
839 | int num_entries_req = q->num_entries;
|
---|
840 |
|
---|
841 | q->errcode = 0;
|
---|
842 | q->errstring = 0;
|
---|
843 | q->entries = list;
|
---|
844 | q->status = BEND_SCAN_SUCCESS;
|
---|
845 | if (!f && !(f = fopen("dummy-words", "r")))
|
---|
846 | {
|
---|
847 | perror("dummy-words");
|
---|
848 | exit(1);
|
---|
849 | }
|
---|
850 | if (q->term->term->which != Z_Term_general)
|
---|
851 | {
|
---|
852 | q->errcode = 229; /* unsupported term type */
|
---|
853 | return 0;
|
---|
854 | }
|
---|
855 | if (*q->step_size != 0)
|
---|
856 | {
|
---|
857 | q->errcode = 205; /*Only zero step size supported for Scan */
|
---|
858 | return 0;
|
---|
859 | }
|
---|
860 | if (q->term->term->u.general->len >= 80)
|
---|
861 | {
|
---|
862 | q->errcode = 11; /* term too long */
|
---|
863 | return 0;
|
---|
864 | }
|
---|
865 | if (q->num_entries > 200)
|
---|
866 | {
|
---|
867 | q->errcode = 31;
|
---|
868 | return 0;
|
---|
869 | }
|
---|
870 | memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
|
---|
871 | term[q->term->term->u.general->len] = '\0';
|
---|
872 | for (p = term; *p; p++)
|
---|
873 | if (islower(*p))
|
---|
874 | *p = toupper(*p);
|
---|
875 |
|
---|
876 | fseek(f, 0, SEEK_SET);
|
---|
877 | q->num_entries = 0;
|
---|
878 |
|
---|
879 | for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
|
---|
880 | i++, pos < 199 ? pos++ : (pos = 0))
|
---|
881 | {
|
---|
882 | if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
|
---|
883 | {
|
---|
884 | if ((q->term_position = term_position_req) > i + 1)
|
---|
885 | {
|
---|
886 | q->term_position = i + 1;
|
---|
887 | q->status = BEND_SCAN_PARTIAL;
|
---|
888 | }
|
---|
889 | for (; q->num_entries < q->term_position; q->num_entries++)
|
---|
890 | {
|
---|
891 | int po;
|
---|
892 |
|
---|
893 | po = pos - q->term_position + q->num_entries+1; /* find pos */
|
---|
894 | if (po < 0)
|
---|
895 | po += 200;
|
---|
896 |
|
---|
897 | if (!strcmp (term, "SD") && q->num_entries == 2)
|
---|
898 | {
|
---|
899 | list[q->num_entries].term = entries[pos];
|
---|
900 | list[q->num_entries].occurrences = -1;
|
---|
901 | list[q->num_entries].errcode = 233;
|
---|
902 | list[q->num_entries].errstring = "SD for Scan Term";
|
---|
903 | }
|
---|
904 | else
|
---|
905 | {
|
---|
906 | list[q->num_entries].term = entries[po];
|
---|
907 | list[q->num_entries].occurrences = hits[po];
|
---|
908 | }
|
---|
909 | }
|
---|
910 | }
|
---|
911 | else if (q->num_entries)
|
---|
912 | {
|
---|
913 | list[q->num_entries].term = entries[pos];
|
---|
914 | list[q->num_entries].occurrences = hits[pos];
|
---|
915 | q->num_entries++;
|
---|
916 | }
|
---|
917 | if (q->num_entries >= num_entries_req)
|
---|
918 | break;
|
---|
919 | }
|
---|
920 | if (feof(f))
|
---|
921 | q->status = BEND_SCAN_PARTIAL;
|
---|
922 | return 0;
|
---|
923 | }
|
---|
924 |
|
---|
925 |
|
---|
926 |
|
---|
927 | void bend_close(void *handle)
|
---|
928 | {
|
---|
929 | if (z3950_verbosity_>1) {
|
---|
930 | cerr << "Entering bend_close" << endl;
|
---|
931 | }
|
---|
932 | delete Server;
|
---|
933 | delete Cservers;
|
---|
934 | delete Protocol;
|
---|
935 | if (z3950_verbosity_>1) {
|
---|
936 | cerr << "heap cleaned up, exiting bend_close" << endl;
|
---|
937 | }
|
---|
938 | return;
|
---|
939 | }
|
---|
940 |
|
---|
941 | int main (int argc, char *argv[])
|
---|
942 | {
|
---|
943 |
|
---|
944 | const int statserv_var = statserv_main(argc, argv, bend_init, bend_close);
|
---|
945 | cerr << "statserv_main returns: " << statserv_var << endl;
|
---|
946 | return statserv_var;
|
---|
947 |
|
---|
948 | return 0;
|
---|
949 | }
|
---|
950 |
|
---|
951 |
|
---|