2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: ztest.c,v 1.94 2007-06-14 10:44:18 adam Exp $
9 * Demonstration of simple server
20 #include <yaz/backend.h>
23 static int log_level=0;
24 static int log_level_set=0;
26 Z_GenericRecord *dummy_grs_record (int num, ODR o);
27 char *dummy_marc_record (int num, ODR odr);
28 char *dummy_xml_record (int num, ODR odr);
30 int ztest_search(void *handle, bend_search_rr *rr);
31 int ztest_sort(void *handle, bend_sort_rr *rr);
32 int ztest_present(void *handle, bend_present_rr *rr);
33 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
34 int ztest_delete(void *handle, bend_delete_rr *rr);
37 \brief use term value as hit count
39 Traverse RPN tree 'in order' and use term value as hit count.
40 Only terms that looks a numeric is used.. Returns -1 if
41 no sub tree has a hit count term
43 static int get_term_hit(Z_RPNStructure *s)
48 case Z_RPNStructure_simple:
49 if (s->u.simple->which == Z_Operand_APT)
51 Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
52 if (apt->term->which == Z_Term_general)
54 Odr_oct *oct = apt->term->u.general;
55 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
56 h = atoi_n((const char *) oct->buf, oct->len);
60 case Z_RPNStructure_complex:
61 h = get_term_hit(s->u.complex->s1);
63 h = get_term_hit(s->u.complex->s2);
70 \brief gets hit count for numeric terms in RPN queries
72 This is just for testing.. A real database of course uses
73 the content of a database to establish a value.. In our case, we
74 have a way to trigger a certain hit count. Good for testing of
75 client applications etc
77 static int get_hit_count(Z_Query *q)
80 if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
81 h = get_term_hit(q->u.type_1->RPNStructure);
87 int ztest_search(void *handle, bend_search_rr *rr)
89 if (rr->num_bases != 1)
94 /* Throw Database unavailable if other than Default or Slow */
95 if (!yaz_matchstr (rr->basenames[0], "Default"))
96 ; /* Default is OK in our test */
97 else if(!yaz_matchstr (rr->basenames[0], "Slow"))
100 /* wait up to 3 seconds and check if connection is still alive */
102 for (i = 0; i<3; i++)
104 if (!bend_assoc_is_alive(rr->association))
106 yaz_log(YLOG_LOG, "search aborted");
112 rr->estimated_hit_count = 1;
117 rr->errstring = rr->basenames[0];
121 rr->hits = get_hit_count(rr->query);
126 /* this huge function handles extended services */
127 int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
129 /* user-defined handle - created in bend_init */
130 int *counter = (int*) handle;
132 yaz_log(log_level, "ESRequest no %d", *counter);
136 if (rr->esr->packageName)
137 yaz_log(log_level, "packagename: %s", rr->esr->packageName);
138 yaz_log(log_level, "Waitaction: %d", *rr->esr->waitAction);
141 yaz_log(log_level, "function: %d", *rr->esr->function);
143 if (!rr->esr->taskSpecificParameters)
145 yaz_log (log_level, "No task specific parameters");
147 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
149 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
150 yaz_log (log_level, "Received ItemOrder");
151 if (it->which == Z_IOItemOrder_esRequest)
153 Z_IORequest *ir = it->u.esRequest;
154 Z_IOOriginPartToKeep *k = ir->toKeep;
155 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
159 if (k->contact->name)
160 yaz_log(log_level, "contact name %s", k->contact->name);
161 if (k->contact->phone)
162 yaz_log(log_level, "contact phone %s", k->contact->phone);
163 if (k->contact->email)
164 yaz_log(log_level, "contact email %s", k->contact->email);
168 yaz_log(log_level, "Billing info (not shown)");
171 if (n->resultSetItem)
173 yaz_log(log_level, "resultsetItem");
174 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
175 yaz_log(log_level, "item: %d", *n->resultSetItem->item);
179 Z_External *r = (Z_External*) n->itemRequest;
180 ILL_ItemRequest *item_req = 0;
181 ILL_APDU *ill_apdu = 0;
182 if (r->direct_reference)
184 char oid_name_str[OID_STR_MAX];
186 const char *oid_name =
187 yaz_oid_to_string_buf(r->direct_reference,
188 &oclass, oid_name_str);
190 yaz_log(log_level, "OID %s", oid_name);
191 if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
193 yaz_log (log_level, "ILL XML request");
194 if (r->which == Z_External_octet)
195 yaz_log (log_level, "%.*s", r->u.octet_aligned->len,
196 r->u.octet_aligned->buf);
198 if (!oid_oidcmp(r->direct_reference,
199 yaz_oid_general_isoill_1))
201 yaz_log (log_level, "Decode ItemRequest begin");
202 if (r->which == ODR_EXTERNAL_single)
204 odr_setbuf(rr->decode,
205 (char *) r->u.single_ASN1_type->buf,
206 r->u.single_ASN1_type->len, 0);
208 if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
211 "Couldn't decode ItemRequest %s near %ld",
212 odr_errmsg(odr_geterror(rr->decode)),
213 (long) odr_offset(rr->decode));
216 yaz_log(log_level, "Decode ItemRequest OK");
219 ill_ItemRequest (rr->print, &item_req, 0,
221 odr_reset (rr->print);
224 if (!item_req && r->which == ODR_EXTERNAL_single)
226 yaz_log (log_level, "Decode ILL APDU begin");
227 odr_setbuf(rr->decode,
228 (char*) r->u.single_ASN1_type->buf,
229 r->u.single_ASN1_type->len, 0);
231 if (!ill_APDU (rr->decode, &ill_apdu, 0, 0))
234 "Couldn't decode ILL APDU %s near %ld",
235 odr_errmsg(odr_geterror(rr->decode)),
236 (long) odr_offset(rr->decode));
237 yaz_log(log_level, "PDU dump:");
238 odr_dumpBER(yaz_log_file(),
239 (char *) r->u.single_ASN1_type->buf,
240 r->u.single_ASN1_type->len);
243 yaz_log(log_level, "Decode ILL APDU OK");
246 ill_APDU (rr->print, &ill_apdu, 0,
248 odr_reset (rr->print);
255 yaz_log (log_level, "ILL protocol version = %d",
256 *item_req->protocol_version_num);
262 Z_External *ext = (Z_External *)
263 odr_malloc (rr->stream, sizeof(*ext));
264 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
265 odr_malloc (rr->stream, sizeof(*keep));
266 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
267 odr_malloc (rr->stream, sizeof(*targetPart));
269 rr->taskPackage = (Z_TaskPackage *)
270 odr_malloc (rr->stream, sizeof(*rr->taskPackage));
271 rr->taskPackage->packageType =
272 odr_oiddup (rr->stream, rr->esr->packageType);
273 rr->taskPackage->packageName = 0;
274 rr->taskPackage->userId = 0;
275 rr->taskPackage->retentionTime = 0;
276 rr->taskPackage->permissions = 0;
277 rr->taskPackage->description = 0;
278 rr->taskPackage->targetReference = (Odr_oct *)
279 odr_malloc (rr->stream, sizeof(Odr_oct));
280 rr->taskPackage->targetReference->buf =
281 (unsigned char *) odr_strdup (rr->stream, "911");
282 rr->taskPackage->targetReference->len =
283 rr->taskPackage->targetReference->size =
284 strlen((char *) (rr->taskPackage->targetReference->buf));
285 rr->taskPackage->creationDateTime = 0;
286 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
287 rr->taskPackage->packageDiagnostics = 0;
288 rr->taskPackage->taskSpecificParameters = ext;
290 ext->direct_reference =
291 odr_oiddup (rr->stream, rr->esr->packageType);
292 ext->indirect_reference = 0;
294 ext->which = Z_External_itemOrder;
295 ext->u.itemOrder = (Z_ItemOrder *)
296 odr_malloc (rr->stream, sizeof(*ext->u.update));
297 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
298 ext->u.itemOrder->u.taskPackage = (Z_IOTaskPackage *)
299 odr_malloc (rr->stream, sizeof(Z_IOTaskPackage));
300 ext->u.itemOrder->u.taskPackage->originPart = k;
301 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
303 targetPart->itemRequest = 0;
304 targetPart->statusOrErrorReport = 0;
305 targetPart->auxiliaryStatus = 0;
309 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
311 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
312 yaz_log (log_level, "Received DB Update");
313 if (up->which == Z_IUUpdate_esRequest)
315 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
316 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
317 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
319 yaz_log (log_level, "action");
322 switch (*toKeep->action)
324 case Z_IUOriginPartToKeep_recordInsert:
325 yaz_log (log_level, " recordInsert");
327 case Z_IUOriginPartToKeep_recordReplace:
328 yaz_log (log_level, " recordReplace");
330 case Z_IUOriginPartToKeep_recordDelete:
331 yaz_log (log_level, " recordDelete");
333 case Z_IUOriginPartToKeep_elementUpdate:
334 yaz_log (log_level, " elementUpdate");
336 case Z_IUOriginPartToKeep_specialUpdate:
337 yaz_log (log_level, " specialUpdate");
340 yaz_log (log_level, " unknown (%d)", *toKeep->action);
343 if (toKeep->databaseName)
345 yaz_log (log_level, "database: %s", toKeep->databaseName);
346 if (!strcmp(toKeep->databaseName, "fault"))
349 rr->errstring = toKeep->databaseName;
351 if (!strcmp(toKeep->databaseName, "accept"))
356 Z_External *ext = (Z_External *)
357 odr_malloc (rr->stream, sizeof(*ext));
358 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
359 odr_malloc (rr->stream, sizeof(*keep));
360 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
361 odr_malloc (rr->stream, sizeof(*targetPart));
363 rr->taskPackage = (Z_TaskPackage *)
364 odr_malloc (rr->stream, sizeof(*rr->taskPackage));
365 rr->taskPackage->packageType =
366 odr_oiddup (rr->stream, rr->esr->packageType);
367 rr->taskPackage->packageName = 0;
368 rr->taskPackage->userId = 0;
369 rr->taskPackage->retentionTime = 0;
370 rr->taskPackage->permissions = 0;
371 rr->taskPackage->description = 0;
372 rr->taskPackage->targetReference = (Odr_oct *)
373 odr_malloc (rr->stream, sizeof(Odr_oct));
374 rr->taskPackage->targetReference->buf =
375 (unsigned char *) odr_strdup (rr->stream, "123");
376 rr->taskPackage->targetReference->len =
377 rr->taskPackage->targetReference->size =
378 strlen((char *) (rr->taskPackage->targetReference->buf));
379 rr->taskPackage->creationDateTime = 0;
380 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
381 rr->taskPackage->packageDiagnostics = 0;
382 rr->taskPackage->taskSpecificParameters = ext;
384 ext->direct_reference =
385 odr_oiddup (rr->stream, rr->esr->packageType);
386 ext->indirect_reference = 0;
388 ext->which = Z_External_update;
389 ext->u.update = (Z_IUUpdate *)
390 odr_malloc (rr->stream, sizeof(*ext->u.update));
391 ext->u.update->which = Z_IUUpdate_taskPackage;
392 ext->u.update->u.taskPackage = (Z_IUUpdateTaskPackage *)
393 odr_malloc (rr->stream, sizeof(Z_IUUpdateTaskPackage));
394 ext->u.update->u.taskPackage->originPart = keep;
395 ext->u.update->u.taskPackage->targetPart = targetPart;
397 keep->action = (int *) odr_malloc (rr->stream, sizeof(int));
398 *keep->action = *toKeep->action;
400 odr_strdup (rr->stream, toKeep->databaseName);
402 keep->elementSetName = 0;
403 keep->actionQualifier = 0;
405 targetPart->updateStatus = odr_intdup (rr->stream, 1);
406 targetPart->num_globalDiagnostics = 0;
407 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
408 targetPart->num_taskPackageRecords = 1;
409 targetPart->taskPackageRecords =
410 (Z_IUTaskPackageRecordStructure **)
411 odr_malloc (rr->stream,
412 sizeof(Z_IUTaskPackageRecordStructure *));
413 targetPart->taskPackageRecords[0] =
414 (Z_IUTaskPackageRecordStructure *)
415 odr_malloc (rr->stream,
416 sizeof(Z_IUTaskPackageRecordStructure));
418 targetPart->taskPackageRecords[0]->which =
419 Z_IUTaskPackageRecordStructure_record;
420 targetPart->taskPackageRecords[0]->u.record =
421 z_ext_record_sutrs(rr->stream, "test", 4);
422 targetPart->taskPackageRecords[0]->correlationInfo = 0;
423 targetPart->taskPackageRecords[0]->recordStatus =
424 odr_intdup (rr->stream,
425 Z_IUTaskPackageRecordStructure_success);
426 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
429 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
434 for (i = 0; i < notToKeep->num; i++)
436 Z_External *rec = notToKeep->elements[i]->record;
438 if (rec->direct_reference)
440 char oid_name_str[OID_STR_MAX];
442 = oid_name = yaz_oid_to_string_buf(
443 rec->direct_reference, 0,
446 yaz_log (log_level, "record %d type %s", i,
451 case Z_External_sutrs:
452 if (rec->u.octet_aligned->len > 170)
453 yaz_log (log_level, "%d bytes:\n%.168s ...",
457 yaz_log (log_level, "%d bytes:\n%s",
461 case Z_External_octet :
462 if (rec->u.octet_aligned->len > 170)
463 yaz_log (log_level, "%d bytes:\n%.168s ...",
464 rec->u.octet_aligned->len,
465 rec->u.octet_aligned->buf);
467 yaz_log (log_level, "%d bytes\n%s",
468 rec->u.octet_aligned->len,
469 rec->u.octet_aligned->buf);
478 /* result set delete */
479 int ztest_delete (void *handle, bend_delete_rr *rr)
481 if (rr->num_setnames == 1 && !strcmp (rr->setnames[0], "1"))
482 rr->delete_status = Z_DeleteStatus_success;
484 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
488 /* Our sort handler really doesn't sort... */
489 int ztest_sort (void *handle, bend_sort_rr *rr)
492 rr->sort_status = Z_SortResponse_success;
497 /* present request handler */
498 int ztest_present (void *handle, bend_present_rr *rr)
503 /* retrieval of a single record (present, and piggy back search) */
504 int ztest_fetch(void *handle, bend_fetch_rr *r)
507 const Odr_oid *oid = r->request_format;
510 r->basename = "Default";
511 r->output_format = r->request_format;
513 if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
515 /* this section returns a small record */
518 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
520 r->len = strlen(buf);
521 r->record = (char *) odr_malloc (r->stream, r->len+1);
522 strcpy(r->record, buf);
524 else if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
527 r->record = (char*) dummy_grs_record(r->number, r->stream);
534 else if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_postscript))
540 sprintf (fname, "part.%d.ps", r->number);
541 f = fopen(fname, "rb");
547 fseek (f, 0L, SEEK_END);
549 if (size <= 0 || size >= 5000000)
554 fseek (f, 0L, SEEK_SET);
555 r->record = (char*) odr_malloc (r->stream, size);
557 fread (r->record, size, 1, f);
560 else if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_xml))
562 if ((cp = dummy_xml_record (r->number, r->stream)))
570 r->surrogate_flag = 1;
574 else if ((cp = dummy_marc_record(r->number, r->stream)))
578 r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
590 * silly dummy-scan what reads words from a file.
592 int ztest_scan(void *handle, bend_scan_rr *q)
595 static struct scan_entry list[200];
596 static char entries[200][80];
600 int term_position_req = q->term_position;
601 int num_entries_req = q->num_entries;
603 /* Throw Database unavailable if other than Default or Slow */
604 if (!yaz_matchstr (q->basenames[0], "Default"))
605 ; /* Default is OK in our test */
606 else if(!yaz_matchstr (q->basenames[0], "Slow"))
616 q->errstring = q->basenames[0];
623 q->status = BEND_SCAN_SUCCESS;
624 if (!f && !(f = fopen("dummy-words", "r")))
626 perror("dummy-words");
629 if (q->num_entries > 200)
637 if (q->term->term->which != Z_Term_general)
639 q->errcode = 229; /* unsupported term type */
642 if (*q->step_size != 0)
644 q->errcode = 205; /*Only zero step size supported for Scan */
647 len = q->term->term->u.general->len;
648 if (len >= sizeof(term))
649 len = sizeof(term)-1;
650 memcpy(term, q->term->term->u.general->buf, len);
653 else if (q->scanClause)
655 strncpy(term, q->scanClause, sizeof(term)-1);
656 term[sizeof(term)-1] = '\0';
661 for (p = term; *p; p++)
662 if (islower(*(unsigned char *) p))
665 fseek(f, 0, SEEK_SET);
668 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
669 i++, pos < 199 ? pos++ : (pos = 0))
671 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
673 if ((q->term_position = term_position_req) > i + 1)
675 q->term_position = i + 1;
676 q->status = BEND_SCAN_PARTIAL;
678 for (; q->num_entries < q->term_position; q->num_entries++)
682 po = pos - q->term_position + q->num_entries+1; /* find pos */
686 if (!strcmp (term, "SD") && q->num_entries == 2)
688 list[q->num_entries].term = entries[pos];
689 list[q->num_entries].occurrences = -1;
690 list[q->num_entries].errcode = 233;
691 list[q->num_entries].errstring = "SD for Scan Term";
695 list[q->num_entries].term = entries[po];
696 list[q->num_entries].occurrences = hits[po];
700 else if (q->num_entries)
702 list[q->num_entries].term = entries[pos];
703 list[q->num_entries].occurrences = hits[pos];
706 if (q->num_entries >= num_entries_req)
710 q->status = BEND_SCAN_PARTIAL;
714 int ztest_explain(void *handle, bend_explain_rr *rr)
716 if (rr->database && !strcmp(rr->database, "Default"))
718 rr->explain_buf = "<explain>\n"
720 "\t\t<host>localhost</host>\n"
721 "\t\t<port>210</port>\n"
728 int ztest_update(void *handle, bend_update_rr *rr)
730 rr->operation_status = "success";
734 bend_initresult *bend_init(bend_initrequest *q)
736 bend_initresult *r = (bend_initresult *)
737 odr_malloc (q->stream, sizeof(*r));
738 int *counter = (int *) xmalloc (sizeof(int));
742 log_level=yaz_log_module_level("ztest");
749 r->handle = counter; /* user handle, in this case a simple int */
750 q->bend_sort = ztest_sort; /* register sort handler */
751 q->bend_search = ztest_search; /* register search handler */
752 q->bend_present = ztest_present; /* register present handle */
753 q->bend_esrequest = ztest_esrequest;
754 q->bend_delete = ztest_delete;
755 q->bend_fetch = ztest_fetch;
756 q->bend_scan = ztest_scan;
758 q->bend_explain = ztest_explain;
760 q->bend_srw_scan = ztest_scan;
761 q->bend_srw_update = ztest_update;
763 q->query_charset = "ISO-8859-1";
764 q->records_in_same_charset = 0;
769 void bend_close(void *handle)
771 xfree (handle); /* release our user-defined handle */
775 int main(int argc, char **argv)
777 return statserv_main(argc, argv, bend_init, bend_close);
782 * indent-tabs-mode: nil
784 * vim: shiftwidth=4 tabstop=8 expandtab