1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
7 * \brief yaz-ztest Generic Frontend Server
18 #include <yaz/backend.h>
20 #include <yaz/diagbib1.h>
24 static int log_level=0;
25 static int log_level_set=0;
27 int ztest_search(void *handle, bend_search_rr *rr);
28 int ztest_sort(void *handle, bend_sort_rr *rr);
29 int ztest_present(void *handle, bend_present_rr *rr);
30 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
31 int ztest_delete(void *handle, bend_delete_rr *rr);
33 /** \brief use term value as hit count
34 \param s RPN structure
35 \return >= 0: search term number or -1: not found
37 Traverse RPN tree 'in order' and use term value as hit count.
38 Only terms that looks a numeric is used.. Returns -1 if
39 no sub tree has a hit count term
41 static int get_term_hit(Z_RPNStructure *s)
46 case Z_RPNStructure_simple:
47 if (s->u.simple->which == Z_Operand_APT)
49 Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
50 if (apt->term->which == Z_Term_general)
52 Odr_oct *oct = apt->term->u.general;
53 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
54 h = atoi_n((const char *) oct->buf, oct->len);
58 case Z_RPNStructure_complex:
59 h = get_term_hit(s->u.complex->s1);
61 h = get_term_hit(s->u.complex->s2);
67 /** \brief gets hit count for numeric terms in RPN queries
69 \return number of hits (random or number for term)
71 This is just for testing.. A real database of course uses
72 the content of a database to establish a value.. In our case, we
73 have a way to trigger a certain hit count. Good for testing of
74 client applications etc
76 static int get_hit_count(Z_Query *q)
79 if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
80 h = get_term_hit(q->u.type_1->RPNStructure);
86 /** \brief checks if it's a dummy Slow database..
87 \param basename database name to check
88 \param association backend association (or NULL if not available)
89 \retval 1 is slow database
90 \retval 0 is not a slow database
92 The Slow database is for testing.. It allows us to simulate
95 static int check_slow(const char *basename, bend_association association)
97 if (strncmp(basename, "Slow", 4) == 0)
102 sscanf(basename+4, "%d", &w);
103 /* wait up to 3 seconds and check if connection is still alive */
104 for (i = 0; i < w; i++)
106 if (association && !bend_assoc_is_alive(association))
108 yaz_log(YLOG_LOG, "search aborted");
119 int ztest_search(void *handle, bend_search_rr *rr)
121 if (rr->num_bases != 1)
123 rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
126 /* Throw Database unavailable if other than Default or Slow */
127 if (!yaz_matchstr(rr->basenames[0], "Default"))
128 ; /* Default is OK in our test */
129 else if (check_slow(rr->basenames[0], rr->association))
131 rr->estimated_hit_count = 1;
135 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
136 rr->errstring = rr->basenames[0];
140 rr->hits = get_hit_count(rr->query);
145 /* this huge function handles extended services */
146 int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
148 /* user-defined handle - created in bend_init */
149 int *counter = (int*) handle;
151 yaz_log(log_level, "ESRequest no %d", *counter);
155 if (rr->esr->packageName)
156 yaz_log(log_level, "packagename: %s", rr->esr->packageName);
157 yaz_log(log_level, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
160 yaz_log(log_level, "function: " ODR_INT_PRINTF, *rr->esr->function);
162 if (!rr->esr->taskSpecificParameters)
164 yaz_log(log_level, "No task specific parameters");
166 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
168 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
169 yaz_log(log_level, "Received ItemOrder");
170 if (it->which == Z_IOItemOrder_esRequest)
172 Z_IORequest *ir = it->u.esRequest;
173 Z_IOOriginPartToKeep *k = ir->toKeep;
174 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
175 const char *xml_in_response = 0;
179 if (k->contact->name)
180 yaz_log(log_level, "contact name %s", k->contact->name);
181 if (k->contact->phone)
182 yaz_log(log_level, "contact phone %s", k->contact->phone);
183 if (k->contact->email)
184 yaz_log(log_level, "contact email %s", k->contact->email);
188 yaz_log(log_level, "Billing info (not shown)");
191 if (n->resultSetItem)
193 yaz_log(log_level, "resultsetItem");
194 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
195 yaz_log(log_level, "item: " ODR_INT_PRINTF, *n->resultSetItem->item);
199 Z_External *r = (Z_External*) n->itemRequest;
200 ILL_ItemRequest *item_req = 0;
201 ILL_APDU *ill_apdu = 0;
202 if (r->direct_reference)
204 char oid_name_str[OID_STR_MAX];
206 const char *oid_name =
207 yaz_oid_to_string_buf(r->direct_reference,
208 &oclass, oid_name_str);
210 yaz_log(log_level, "OID %s", oid_name);
211 if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
213 yaz_log(log_level, "ILL XML request");
214 if (r->which == Z_External_octet)
215 yaz_log(log_level, "%.*s",
216 r->u.octet_aligned->len,
217 r->u.octet_aligned->buf);
218 xml_in_response = "<dummy>x</dummy>";
220 if (!oid_oidcmp(r->direct_reference,
221 yaz_oid_general_isoill_1))
223 yaz_log(log_level, "Decode ItemRequest begin");
224 if (r->which == ODR_EXTERNAL_single)
226 odr_setbuf(rr->decode,
227 (char *) r->u.single_ASN1_type->buf,
228 r->u.single_ASN1_type->len, 0);
230 if (!ill_ItemRequest(rr->decode, &item_req, 0, 0))
233 "Couldn't decode ItemRequest %s near %ld",
234 odr_errmsg(odr_geterror(rr->decode)),
235 (long) odr_offset(rr->decode));
238 yaz_log(log_level, "Decode ItemRequest OK");
241 ill_ItemRequest(rr->print, &item_req, 0,
243 odr_reset(rr->print);
246 if (!item_req && r->which == ODR_EXTERNAL_single)
248 yaz_log(log_level, "Decode ILL APDU begin");
249 odr_setbuf(rr->decode,
250 (char*) r->u.single_ASN1_type->buf,
251 r->u.single_ASN1_type->len, 0);
253 if (!ill_APDU(rr->decode, &ill_apdu, 0, 0))
256 "Couldn't decode ILL APDU %s near %ld",
257 odr_errmsg(odr_geterror(rr->decode)),
258 (long) odr_offset(rr->decode));
259 yaz_log(log_level, "PDU dump:");
260 odr_dumpBER(yaz_log_file(),
261 (char *) r->u.single_ASN1_type->buf,
262 r->u.single_ASN1_type->len);
265 yaz_log(log_level, "Decode ILL APDU OK");
268 ill_APDU(rr->print, &ill_apdu, 0,
270 odr_reset(rr->print);
277 yaz_log(log_level, "ILL protocol version = "
279 *item_req->protocol_version_num);
285 Z_External *ext = (Z_External *)
286 odr_malloc(rr->stream, sizeof(*ext));
287 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
288 odr_malloc(rr->stream, sizeof(*keep));
289 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
290 odr_malloc(rr->stream, sizeof(*targetPart));
292 rr->taskPackage = (Z_TaskPackage *)
293 odr_malloc(rr->stream, sizeof(*rr->taskPackage));
294 rr->taskPackage->packageType =
295 odr_oiddup(rr->stream, rr->esr->packageType);
296 rr->taskPackage->packageName = 0;
297 rr->taskPackage->userId = 0;
298 rr->taskPackage->retentionTime = 0;
299 rr->taskPackage->permissions = 0;
300 rr->taskPackage->description = 0;
301 rr->taskPackage->targetReference = (Odr_oct *)
302 odr_malloc(rr->stream, sizeof(Odr_oct));
303 rr->taskPackage->targetReference->buf =
304 (unsigned char *) odr_strdup(rr->stream, "911");
305 rr->taskPackage->targetReference->len =
306 rr->taskPackage->targetReference->size =
307 strlen((char *) (rr->taskPackage->targetReference->buf));
308 rr->taskPackage->creationDateTime = 0;
309 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
310 rr->taskPackage->packageDiagnostics = 0;
311 rr->taskPackage->taskSpecificParameters = ext;
313 ext->direct_reference =
314 odr_oiddup(rr->stream, rr->esr->packageType);
315 ext->indirect_reference = 0;
317 ext->which = Z_External_itemOrder;
318 ext->u.itemOrder = (Z_ItemOrder *)
319 odr_malloc(rr->stream, sizeof(*ext->u.update));
320 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
321 ext->u.itemOrder->u.taskPackage = (Z_IOTaskPackage *)
322 odr_malloc(rr->stream, sizeof(Z_IOTaskPackage));
323 ext->u.itemOrder->u.taskPackage->originPart = k;
324 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
327 targetPart->itemRequest =
328 z_ext_record_xml(rr->stream, xml_in_response,
329 strlen(xml_in_response));
331 targetPart->itemRequest = 0;
333 targetPart->statusOrErrorReport = 0;
334 targetPart->auxiliaryStatus = 0;
338 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
340 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
341 yaz_log(log_level, "Received DB Update");
342 if (up->which == Z_IUUpdate_esRequest)
344 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
345 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
346 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
348 yaz_log(log_level, "action");
351 switch (*toKeep->action)
353 case Z_IUOriginPartToKeep_recordInsert:
354 yaz_log(log_level, " recordInsert");
356 case Z_IUOriginPartToKeep_recordReplace:
357 yaz_log(log_level, " recordReplace");
359 case Z_IUOriginPartToKeep_recordDelete:
360 yaz_log(log_level, " recordDelete");
362 case Z_IUOriginPartToKeep_elementUpdate:
363 yaz_log(log_level, " elementUpdate");
365 case Z_IUOriginPartToKeep_specialUpdate:
366 yaz_log(log_level, " specialUpdate");
369 yaz_log(log_level, " unknown (" ODR_INT_PRINTF ")",
373 if (toKeep->databaseName)
375 yaz_log(log_level, "database: %s", toKeep->databaseName);
376 if (!strcmp(toKeep->databaseName, "fault"))
378 rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
379 rr->errstring = toKeep->databaseName;
381 if (!strcmp(toKeep->databaseName, "accept"))
386 Z_External *ext = (Z_External *)
387 odr_malloc(rr->stream, sizeof(*ext));
388 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
389 odr_malloc(rr->stream, sizeof(*keep));
390 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
391 odr_malloc(rr->stream, sizeof(*targetPart));
393 rr->taskPackage = (Z_TaskPackage *)
394 odr_malloc(rr->stream, sizeof(*rr->taskPackage));
395 rr->taskPackage->packageType =
396 odr_oiddup(rr->stream, rr->esr->packageType);
397 rr->taskPackage->packageName = 0;
398 rr->taskPackage->userId = 0;
399 rr->taskPackage->retentionTime = 0;
400 rr->taskPackage->permissions = 0;
401 rr->taskPackage->description = 0;
402 rr->taskPackage->targetReference = (Odr_oct *)
403 odr_malloc(rr->stream, sizeof(Odr_oct));
404 rr->taskPackage->targetReference->buf =
405 (unsigned char *) odr_strdup(rr->stream, "123");
406 rr->taskPackage->targetReference->len =
407 rr->taskPackage->targetReference->size =
408 strlen((char *) (rr->taskPackage->targetReference->buf));
409 rr->taskPackage->creationDateTime = 0;
410 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
411 rr->taskPackage->packageDiagnostics = 0;
412 rr->taskPackage->taskSpecificParameters = ext;
414 ext->direct_reference =
415 odr_oiddup(rr->stream, rr->esr->packageType);
416 ext->indirect_reference = 0;
418 ext->which = Z_External_update;
419 ext->u.update = (Z_IUUpdate *)
420 odr_malloc(rr->stream, sizeof(*ext->u.update));
421 ext->u.update->which = Z_IUUpdate_taskPackage;
422 ext->u.update->u.taskPackage = (Z_IUUpdateTaskPackage *)
423 odr_malloc(rr->stream, sizeof(Z_IUUpdateTaskPackage));
424 ext->u.update->u.taskPackage->originPart = keep;
425 ext->u.update->u.taskPackage->targetPart = targetPart;
427 keep->action = odr_intdup(rr->stream, *toKeep->action);
429 odr_strdup(rr->stream, toKeep->databaseName);
431 keep->elementSetName = 0;
432 keep->actionQualifier = 0;
434 targetPart->updateStatus = odr_intdup(rr->stream, 1);
435 targetPart->num_globalDiagnostics = 0;
436 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
437 targetPart->num_taskPackageRecords = 1;
438 targetPart->taskPackageRecords =
439 (Z_IUTaskPackageRecordStructure **)
440 odr_malloc(rr->stream,
441 sizeof(Z_IUTaskPackageRecordStructure *));
442 targetPart->taskPackageRecords[0] =
443 (Z_IUTaskPackageRecordStructure *)
444 odr_malloc(rr->stream,
445 sizeof(Z_IUTaskPackageRecordStructure));
447 targetPart->taskPackageRecords[0]->which =
448 Z_IUTaskPackageRecordStructure_record;
449 targetPart->taskPackageRecords[0]->u.record =
450 z_ext_record_sutrs(rr->stream, "test", 4);
451 targetPart->taskPackageRecords[0]->correlationInfo = 0;
452 targetPart->taskPackageRecords[0]->recordStatus =
453 odr_intdup(rr->stream,
454 Z_IUTaskPackageRecordStructure_success);
455 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
458 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
463 for (i = 0; i < notToKeep->num; i++)
465 Z_External *rec = notToKeep->elements[i]->record;
467 if (rec->direct_reference)
469 char oid_name_str[OID_STR_MAX];
471 = oid_name = yaz_oid_to_string_buf(
472 rec->direct_reference, 0,
475 yaz_log(log_level, "record %d type %s", i,
480 case Z_External_sutrs:
481 if (rec->u.octet_aligned->len > 170)
482 yaz_log(log_level, "%d bytes:\n%.168s ...",
486 yaz_log(log_level, "%d bytes:\n%s",
490 case Z_External_octet :
491 if (rec->u.octet_aligned->len > 170)
492 yaz_log(log_level, "%d bytes:\n%.168s ...",
493 rec->u.octet_aligned->len,
494 rec->u.octet_aligned->buf);
496 yaz_log(log_level, "%d bytes\n%s",
497 rec->u.octet_aligned->len,
498 rec->u.octet_aligned->buf);
507 /* result set delete */
508 int ztest_delete(void *handle, bend_delete_rr *rr)
510 if (rr->num_setnames == 1 && !strcmp(rr->setnames[0], "1"))
511 rr->delete_status = Z_DeleteStatus_success;
513 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
517 /* Our sort handler really doesn't sort... */
518 int ztest_sort(void *handle, bend_sort_rr *rr)
521 rr->sort_status = Z_SortResponse_success;
526 /* present request handler */
527 int ztest_present(void *handle, bend_present_rr *rr)
532 /* retrieval of a single record (present, and piggy back search) */
533 int ztest_fetch(void *handle, bend_fetch_rr *r)
536 const Odr_oid *oid = r->request_format;
539 r->basename = "Default";
540 r->output_format = r->request_format;
542 if (!oid || yaz_oid_is_iso2709(oid))
544 cp = dummy_marc_record(r->number, r->stream);
547 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
554 r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
557 else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
559 cp = dummy_marc_record(r->number, r->stream);
562 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
565 r->record = (char *) dummy_opac(r->number, r->stream, cp);
568 else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
570 /* this section returns a small record */
573 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
575 r->len = strlen(buf);
576 r->record = (char *) odr_malloc(r->stream, r->len+1);
577 strcpy(r->record, buf);
579 else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
582 r->record = (char*) dummy_grs_record(r->number, r->stream);
585 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
589 else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript))
595 sprintf(fname, "part.%d.ps", r->number);
596 f = fopen(fname, "rb");
599 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
602 fseek(f, 0L, SEEK_END);
604 if (size <= 0 || size >= 5000000)
606 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
608 fseek(f, 0L, SEEK_SET);
609 r->record = (char*) odr_malloc(r->stream, size);
611 if (fread(r->record, size, 1, f) != 1)
613 r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
617 else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
619 if ((cp = dummy_xml_record(r->number, r->stream)))
626 r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
627 r->surrogate_flag = 1;
633 char buf[OID_STR_MAX];
634 r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
635 r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
643 * silly dummy-scan what reads words from a file.
645 int ztest_scan(void *handle, bend_scan_rr *q)
648 static struct scan_entry list[200];
649 static char entries[200][80];
653 int term_position_req = q->term_position;
654 int num_entries_req = q->num_entries;
656 /* Throw Database unavailable if other than Default or Slow */
657 if (!yaz_matchstr(q->basenames[0], "Default"))
658 ; /* Default is OK in our test */
659 else if (check_slow(q->basenames[0], 0 /* no assoc for scan */))
663 q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
664 q->errstring = q->basenames[0];
671 q->status = BEND_SCAN_SUCCESS;
672 if (!f && !(f = fopen("dummy-words", "r")))
674 perror("dummy-words");
677 if (q->num_entries > 200)
679 q->errcode = YAZ_BIB1_RESOURCES_EXHAUSTED_NO_RESULTS_AVAILABLE;
685 if (q->term->term->which != Z_Term_general)
687 q->errcode = YAZ_BIB1_TERM_TYPE_UNSUPP;
690 if (*q->step_size != 0)
692 q->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
695 len = q->term->term->u.general->len;
696 if (len >= sizeof(term))
697 len = sizeof(term)-1;
698 memcpy(term, q->term->term->u.general->buf, len);
701 else if (q->scanClause)
703 strncpy(term, q->scanClause, sizeof(term)-1);
704 term[sizeof(term)-1] = '\0';
709 for (p = term; *p; p++)
710 if (islower(*(unsigned char *) p))
713 fseek(f, 0, SEEK_SET);
716 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
717 i++, pos < 199 ? pos++ : (pos = 0))
719 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
721 if ((q->term_position = term_position_req) > i + 1)
723 q->term_position = i + 1;
724 q->status = BEND_SCAN_PARTIAL;
726 for (; q->num_entries < q->term_position; q->num_entries++)
730 po = pos - q->term_position + q->num_entries+1; /* find pos */
734 if (!strcmp(term, "SD") && q->num_entries == 2)
736 list[q->num_entries].term = entries[pos];
737 list[q->num_entries].occurrences = -1;
738 list[q->num_entries].errcode =
739 YAZ_BIB1_SCAN_UNSUPP_VALUE_OF_POSITION_IN_RESPONSE;
740 list[q->num_entries].errstring = "SD for Scan Term";
744 list[q->num_entries].term = entries[po];
745 list[q->num_entries].occurrences = hits[po];
749 else if (q->num_entries)
751 list[q->num_entries].term = entries[pos];
752 list[q->num_entries].occurrences = hits[pos];
755 if (q->num_entries >= num_entries_req)
759 q->status = BEND_SCAN_PARTIAL;
763 int ztest_explain(void *handle, bend_explain_rr *rr)
765 if (rr->database && !strcmp(rr->database, "Default"))
767 rr->explain_buf = "<explain>\n"
769 "\t\t<host>localhost</host>\n"
770 "\t\t<port>210</port>\n"
777 int ztest_update(void *handle, bend_update_rr *rr)
779 rr->operation_status = "success";
783 bend_initresult *bend_init(bend_initrequest *q)
785 bend_initresult *r = (bend_initresult *)
786 odr_malloc(q->stream, sizeof(*r));
787 int *counter = (int *) xmalloc(sizeof(int));
791 log_level=yaz_log_module_level("ztest");
798 r->handle = counter; /* user handle, in this case a simple int */
799 q->bend_sort = ztest_sort; /* register sort handler */
800 q->bend_search = ztest_search; /* register search handler */
801 q->bend_present = ztest_present; /* register present handle */
802 q->bend_esrequest = ztest_esrequest;
803 q->bend_delete = ztest_delete;
804 q->bend_fetch = ztest_fetch;
805 q->bend_scan = ztest_scan;
807 q->bend_explain = ztest_explain;
809 q->bend_srw_scan = ztest_scan;
810 q->bend_srw_update = ztest_update;
812 q->query_charset = "ISO-8859-1";
813 q->records_in_same_charset = 0;
818 void bend_close(void *handle)
820 xfree(handle); /* release our user-defined handle */
824 int main(int argc, char **argv)
826 return statserv_main(argc, argv, bend_init, bend_close);
831 * c-file-style: "Stroustrup"
832 * indent-tabs-mode: nil
834 * vim: shiftwidth=4 tabstop=8 expandtab