source: gsdl/trunk/src/colservr/z3950server.cpp@ 15413

Last change on this file since 15413 was 15413, checked in by mdewsnip, 16 years ago

(Untangling colservr/recpt) Removed all the inclusions of recptproto.h, as these aren't actually used.

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