2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: ztest.c,v 1.87 2007-04-13 09:55:41 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);
36 int ztest_search(void *handle, bend_search_rr *rr)
38 if (rr->num_bases != 1)
44 /* if database is stop, stop this process.. For debugging only. */
45 if (!yaz_matchstr (rr->basenames[0], "stop"))
47 nmem_print_list_l(YLOG_LOG);
51 /* Throw Database unavailable if other than Default or Slow */
52 if (!yaz_matchstr (rr->basenames[0], "Default"))
53 ; /* Default is OK in our test */
54 else if(!yaz_matchstr (rr->basenames[0], "Slow"))
57 /* wait up to 3 seconds and check if connection is still alive */
61 if (!bend_assoc_is_alive(rr->association))
63 yaz_log(YLOG_LOG, "search aborted");
69 rr->estimated_hit_count = 1;
74 rr->errstring = rr->basenames[0];
77 rr->hits = rand() % 24;
82 /* this huge function handles extended services */
83 int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
85 /* user-defined handle - created in bend_init */
86 int *counter = (int*) handle;
88 yaz_log(log_level, "ESRequest no %d", *counter);
92 if (rr->esr->packageName)
93 yaz_log(log_level, "packagename: %s", rr->esr->packageName);
94 yaz_log(log_level, "Waitaction: %d", *rr->esr->waitAction);
97 yaz_log(log_level, "function: %d", *rr->esr->function);
99 if (!rr->esr->taskSpecificParameters)
101 yaz_log (log_level, "No task specific parameters");
103 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
105 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
106 yaz_log (log_level, "Received ItemOrder");
107 if (it->which == Z_IOItemOrder_esRequest)
109 Z_IORequest *ir = it->u.esRequest;
110 Z_IOOriginPartToKeep *k = ir->toKeep;
111 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
115 if (k->contact->name)
116 yaz_log(log_level, "contact name %s", k->contact->name);
117 if (k->contact->phone)
118 yaz_log(log_level, "contact phone %s", k->contact->phone);
119 if (k->contact->email)
120 yaz_log(log_level, "contact email %s", k->contact->email);
124 yaz_log(log_level, "Billing info (not shown)");
127 if (n->resultSetItem)
129 yaz_log(log_level, "resultsetItem");
130 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
131 yaz_log(log_level, "item: %d", *n->resultSetItem->item);
135 Z_External *r = (Z_External*) n->itemRequest;
136 ILL_ItemRequest *item_req = 0;
137 ILL_APDU *ill_apdu = 0;
138 if (r->direct_reference)
140 char oid_name_str[OID_STR_MAX];
142 const char *oid_name =
143 yaz_oid_to_string_buf(r->direct_reference,
144 &oclass, oid_name_str);
146 yaz_log(log_level, "OID %s", oid_name);
147 if (oid_name && !strcmp(oid_name, OID_STR_XML))
149 yaz_log (log_level, "ILL XML request");
150 if (r->which == Z_External_octet)
151 yaz_log (log_level, "%.*s", r->u.octet_aligned->len,
152 r->u.octet_aligned->buf);
154 if (oid_name && !strcmp(oid_name, OID_STR_ILL_1))
156 yaz_log (log_level, "Decode ItemRequest begin");
157 if (r->which == ODR_EXTERNAL_single)
159 odr_setbuf(rr->decode,
160 (char *) r->u.single_ASN1_type->buf,
161 r->u.single_ASN1_type->len, 0);
163 if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
166 "Couldn't decode ItemRequest %s near %ld",
167 odr_errmsg(odr_geterror(rr->decode)),
168 (long) odr_offset(rr->decode));
171 yaz_log(log_level, "Decode ItemRequest OK");
174 ill_ItemRequest (rr->print, &item_req, 0,
176 odr_reset (rr->print);
179 if (!item_req && r->which == ODR_EXTERNAL_single)
181 yaz_log (log_level, "Decode ILL APDU begin");
182 odr_setbuf(rr->decode,
183 (char*) r->u.single_ASN1_type->buf,
184 r->u.single_ASN1_type->len, 0);
186 if (!ill_APDU (rr->decode, &ill_apdu, 0, 0))
189 "Couldn't decode ILL APDU %s near %ld",
190 odr_errmsg(odr_geterror(rr->decode)),
191 (long) odr_offset(rr->decode));
192 yaz_log(log_level, "PDU dump:");
193 odr_dumpBER(yaz_log_file(),
194 (char *) r->u.single_ASN1_type->buf,
195 r->u.single_ASN1_type->len);
198 yaz_log(log_level, "Decode ILL APDU OK");
201 ill_APDU (rr->print, &ill_apdu, 0,
203 odr_reset (rr->print);
210 yaz_log (log_level, "ILL protocol version = %d",
211 *item_req->protocol_version_num);
217 Z_External *ext = (Z_External *)
218 odr_malloc (rr->stream, sizeof(*ext));
219 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
220 odr_malloc (rr->stream, sizeof(*keep));
221 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
222 odr_malloc (rr->stream, sizeof(*targetPart));
224 rr->taskPackage = (Z_TaskPackage *)
225 odr_malloc (rr->stream, sizeof(*rr->taskPackage));
226 rr->taskPackage->packageType =
227 odr_oiddup (rr->stream, rr->esr->packageType);
228 rr->taskPackage->packageName = 0;
229 rr->taskPackage->userId = 0;
230 rr->taskPackage->retentionTime = 0;
231 rr->taskPackage->permissions = 0;
232 rr->taskPackage->description = 0;
233 rr->taskPackage->targetReference = (Odr_oct *)
234 odr_malloc (rr->stream, sizeof(Odr_oct));
235 rr->taskPackage->targetReference->buf =
236 (unsigned char *) odr_strdup (rr->stream, "911");
237 rr->taskPackage->targetReference->len =
238 rr->taskPackage->targetReference->size =
239 strlen((char *) (rr->taskPackage->targetReference->buf));
240 rr->taskPackage->creationDateTime = 0;
241 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
242 rr->taskPackage->packageDiagnostics = 0;
243 rr->taskPackage->taskSpecificParameters = ext;
245 ext->direct_reference =
246 odr_oiddup (rr->stream, rr->esr->packageType);
247 ext->indirect_reference = 0;
249 ext->which = Z_External_itemOrder;
250 ext->u.itemOrder = (Z_ItemOrder *)
251 odr_malloc (rr->stream, sizeof(*ext->u.update));
252 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
253 ext->u.itemOrder->u.taskPackage = (Z_IOTaskPackage *)
254 odr_malloc (rr->stream, sizeof(Z_IOTaskPackage));
255 ext->u.itemOrder->u.taskPackage->originPart = k;
256 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
258 targetPart->itemRequest = 0;
259 targetPart->statusOrErrorReport = 0;
260 targetPart->auxiliaryStatus = 0;
264 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
266 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
267 yaz_log (log_level, "Received DB Update");
268 if (up->which == Z_IUUpdate_esRequest)
270 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
271 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
272 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
274 yaz_log (log_level, "action");
277 switch (*toKeep->action)
279 case Z_IUOriginPartToKeep_recordInsert:
280 yaz_log (log_level, " recordInsert");
282 case Z_IUOriginPartToKeep_recordReplace:
283 yaz_log (log_level, " recordReplace");
285 case Z_IUOriginPartToKeep_recordDelete:
286 yaz_log (log_level, " recordDelete");
288 case Z_IUOriginPartToKeep_elementUpdate:
289 yaz_log (log_level, " elementUpdate");
291 case Z_IUOriginPartToKeep_specialUpdate:
292 yaz_log (log_level, " specialUpdate");
295 yaz_log (log_level, " unknown (%d)", *toKeep->action);
298 if (toKeep->databaseName)
300 yaz_log (log_level, "database: %s", toKeep->databaseName);
301 if (!strcmp(toKeep->databaseName, "fault"))
304 rr->errstring = toKeep->databaseName;
306 if (!strcmp(toKeep->databaseName, "accept"))
311 Z_External *ext = (Z_External *)
312 odr_malloc (rr->stream, sizeof(*ext));
313 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
314 odr_malloc (rr->stream, sizeof(*keep));
315 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
316 odr_malloc (rr->stream, sizeof(*targetPart));
318 rr->taskPackage = (Z_TaskPackage *)
319 odr_malloc (rr->stream, sizeof(*rr->taskPackage));
320 rr->taskPackage->packageType =
321 odr_oiddup (rr->stream, rr->esr->packageType);
322 rr->taskPackage->packageName = 0;
323 rr->taskPackage->userId = 0;
324 rr->taskPackage->retentionTime = 0;
325 rr->taskPackage->permissions = 0;
326 rr->taskPackage->description = 0;
327 rr->taskPackage->targetReference = (Odr_oct *)
328 odr_malloc (rr->stream, sizeof(Odr_oct));
329 rr->taskPackage->targetReference->buf =
330 (unsigned char *) odr_strdup (rr->stream, "123");
331 rr->taskPackage->targetReference->len =
332 rr->taskPackage->targetReference->size =
333 strlen((char *) (rr->taskPackage->targetReference->buf));
334 rr->taskPackage->creationDateTime = 0;
335 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
336 rr->taskPackage->packageDiagnostics = 0;
337 rr->taskPackage->taskSpecificParameters = ext;
339 ext->direct_reference =
340 odr_oiddup (rr->stream, rr->esr->packageType);
341 ext->indirect_reference = 0;
343 ext->which = Z_External_update;
344 ext->u.update = (Z_IUUpdate *)
345 odr_malloc (rr->stream, sizeof(*ext->u.update));
346 ext->u.update->which = Z_IUUpdate_taskPackage;
347 ext->u.update->u.taskPackage = (Z_IUUpdateTaskPackage *)
348 odr_malloc (rr->stream, sizeof(Z_IUUpdateTaskPackage));
349 ext->u.update->u.taskPackage->originPart = keep;
350 ext->u.update->u.taskPackage->targetPart = targetPart;
352 keep->action = (int *) odr_malloc (rr->stream, sizeof(int));
353 *keep->action = *toKeep->action;
355 odr_strdup (rr->stream, toKeep->databaseName);
357 keep->elementSetName = 0;
358 keep->actionQualifier = 0;
360 targetPart->updateStatus = odr_intdup (rr->stream, 1);
361 targetPart->num_globalDiagnostics = 0;
362 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
363 targetPart->num_taskPackageRecords = 1;
364 targetPart->taskPackageRecords =
365 (Z_IUTaskPackageRecordStructure **)
366 odr_malloc (rr->stream,
367 sizeof(Z_IUTaskPackageRecordStructure *));
368 targetPart->taskPackageRecords[0] =
369 (Z_IUTaskPackageRecordStructure *)
370 odr_malloc (rr->stream,
371 sizeof(Z_IUTaskPackageRecordStructure));
373 targetPart->taskPackageRecords[0]->which =
374 Z_IUTaskPackageRecordStructure_record;
375 targetPart->taskPackageRecords[0]->u.record =
376 z_ext_record_sutrs(rr->stream, "test", 4);
377 targetPart->taskPackageRecords[0]->correlationInfo = 0;
378 targetPart->taskPackageRecords[0]->recordStatus =
379 odr_intdup (rr->stream,
380 Z_IUTaskPackageRecordStructure_success);
381 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
384 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
389 for (i = 0; i < notToKeep->num; i++)
391 Z_External *rec = notToKeep->elements[i]->record;
393 if (rec->direct_reference)
395 char oid_name_str[OID_STR_MAX];
397 = oid_name = yaz_oid_to_string_buf(
398 rec->direct_reference, 0,
401 yaz_log (log_level, "record %d type %s", i,
406 case Z_External_sutrs:
407 if (rec->u.octet_aligned->len > 170)
408 yaz_log (log_level, "%d bytes:\n%.168s ...",
412 yaz_log (log_level, "%d bytes:\n%s",
416 case Z_External_octet :
417 if (rec->u.octet_aligned->len > 170)
418 yaz_log (log_level, "%d bytes:\n%.168s ...",
419 rec->u.octet_aligned->len,
420 rec->u.octet_aligned->buf);
422 yaz_log (log_level, "%d bytes\n%s",
423 rec->u.octet_aligned->len,
424 rec->u.octet_aligned->buf);
433 /* result set delete */
434 int ztest_delete (void *handle, bend_delete_rr *rr)
436 if (rr->num_setnames == 1 && !strcmp (rr->setnames[0], "1"))
437 rr->delete_status = Z_DeleteStatus_success;
439 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
443 /* Our sort handler really doesn't sort... */
444 int ztest_sort (void *handle, bend_sort_rr *rr)
447 rr->sort_status = Z_SortResponse_success;
452 /* present request handler */
453 int ztest_present (void *handle, bend_present_rr *rr)
458 /* retrieval of a single record (present, and piggy back search) */
459 int ztest_fetch(void *handle, bend_fetch_rr *r)
463 char oid_str_buf[OID_STR_MAX];
464 const char *oid_str = 0;
467 r->basename = "Default";
468 r->output_format = r->request_format;
470 oid_str = yaz_oid_to_string_buf(r->request_format, &oclass,
473 if (oid_str && !strcmp(oid_str, OID_STR_SUTRS))
475 /* this section returns a small record */
478 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
480 r->len = strlen(buf);
481 r->record = (char *) odr_malloc (r->stream, r->len+1);
482 strcpy(r->record, buf);
484 else if (oid_str && !strcmp(oid_str, OID_STR_GRS1))
487 r->record = (char*) dummy_grs_record(r->number, r->stream);
494 else if (oid_str && !strcmp(oid_str, OID_STR_POSTSCRIPT))
500 sprintf (fname, "part.%d.ps", r->number);
501 f = fopen(fname, "rb");
507 fseek (f, 0L, SEEK_END);
509 if (size <= 0 || size >= 5000000)
514 fseek (f, 0L, SEEK_SET);
515 r->record = (char*) odr_malloc (r->stream, size);
517 fread (r->record, size, 1, f);
520 else if (oid_str && !strcmp(oid_str, OID_STR_XML))
522 if ((cp = dummy_xml_record (r->number, r->stream)))
530 r->surrogate_flag = 1;
534 else if ((cp = dummy_marc_record(r->number, r->stream)))
538 r->output_format = yaz_string_to_oid_odr(
539 yaz_oid_std(), CLASS_RECSYN, OID_STR_USMARC, r->stream);
551 * silly dummy-scan what reads words from a file.
553 int ztest_scan(void *handle, bend_scan_rr *q)
556 static struct scan_entry list[200];
557 static char entries[200][80];
561 int term_position_req = q->term_position;
562 int num_entries_req = q->num_entries;
564 /* Throw Database unavailable if other than Default or Slow */
565 if (!yaz_matchstr (q->basenames[0], "Default"))
566 ; /* Default is OK in our test */
567 else if(!yaz_matchstr (q->basenames[0], "Slow"))
577 q->errstring = q->basenames[0];
584 q->status = BEND_SCAN_SUCCESS;
585 if (!f && !(f = fopen("dummy-words", "r")))
587 perror("dummy-words");
590 if (q->num_entries > 200)
598 if (q->term->term->which != Z_Term_general)
600 q->errcode = 229; /* unsupported term type */
603 if (*q->step_size != 0)
605 q->errcode = 205; /*Only zero step size supported for Scan */
608 len = q->term->term->u.general->len;
609 if (len >= sizeof(term))
610 len = sizeof(term)-1;
611 memcpy(term, q->term->term->u.general->buf, len);
614 else if (q->scanClause)
616 strncpy(term, q->scanClause, sizeof(term)-1);
617 term[sizeof(term)-1] = '\0';
622 for (p = term; *p; p++)
623 if (islower(*(unsigned char *) p))
626 fseek(f, 0, SEEK_SET);
629 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
630 i++, pos < 199 ? pos++ : (pos = 0))
632 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
634 if ((q->term_position = term_position_req) > i + 1)
636 q->term_position = i + 1;
637 q->status = BEND_SCAN_PARTIAL;
639 for (; q->num_entries < q->term_position; q->num_entries++)
643 po = pos - q->term_position + q->num_entries+1; /* find pos */
647 if (!strcmp (term, "SD") && q->num_entries == 2)
649 list[q->num_entries].term = entries[pos];
650 list[q->num_entries].occurrences = -1;
651 list[q->num_entries].errcode = 233;
652 list[q->num_entries].errstring = "SD for Scan Term";
656 list[q->num_entries].term = entries[po];
657 list[q->num_entries].occurrences = hits[po];
661 else if (q->num_entries)
663 list[q->num_entries].term = entries[pos];
664 list[q->num_entries].occurrences = hits[pos];
667 if (q->num_entries >= num_entries_req)
671 q->status = BEND_SCAN_PARTIAL;
675 int ztest_explain(void *handle, bend_explain_rr *rr)
677 if (rr->database && !strcmp(rr->database, "Default"))
679 rr->explain_buf = "<explain>\n"
681 "\t\t<host>localhost</host>\n"
682 "\t\t<port>210</port>\n"
689 int ztest_update(void *handle, bend_update_rr *rr)
691 rr->operation_status = "success";
695 bend_initresult *bend_init(bend_initrequest *q)
697 bend_initresult *r = (bend_initresult *)
698 odr_malloc (q->stream, sizeof(*r));
699 int *counter = (int *) xmalloc (sizeof(int));
703 log_level=yaz_log_module_level("ztest");
710 r->handle = counter; /* user handle, in this case a simple int */
711 q->bend_sort = ztest_sort; /* register sort handler */
712 q->bend_search = ztest_search; /* register search handler */
713 q->bend_present = ztest_present; /* register present handle */
714 q->bend_esrequest = ztest_esrequest;
715 q->bend_delete = ztest_delete;
716 q->bend_fetch = ztest_fetch;
717 q->bend_scan = ztest_scan;
719 q->bend_explain = ztest_explain;
721 q->bend_srw_scan = ztest_scan;
722 q->bend_srw_update = ztest_update;
727 void bend_close(void *handle)
729 xfree (handle); /* release our user-defined handle */
733 int main(int argc, char **argv)
735 return statserv_main(argc, argv, bend_init, bend_close);
740 * indent-tabs-mode: nil
742 * vim: shiftwidth=4 tabstop=8 expandtab