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

Last change on this file since 12276 was 11553, checked in by kjdon, 18 years ago

Latest compile complained about redefinition of funciton yaz_log, defined in a math header. Turns out that yaz log.h #defines logf to yaz_log.
So moved the yaz headers includes to come last in z3950parser.h, and removed them from z3950server.cpp cos that includes z3950parser.h

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