2 * Copyright (c) 1995-2002, Index Data.
3 * See the file LICENSE for details.
5 * $Id: ztest.c,v 1.49 2002-01-21 12:54:06 adam Exp $
9 * Demonstration of simple server
16 #include <yaz/backend.h>
23 Z_GenericRecord *read_grs1(FILE *f, ODR o);
25 int ztest_search (void *handle, bend_search_rr *rr);
26 int ztest_sort (void *handle, bend_sort_rr *rr);
27 int ztest_present (void *handle, bend_present_rr *rr);
28 int ztest_esrequest (void *handle, bend_esrequest_rr *rr);
29 int ztest_delete (void *handle, bend_delete_rr *rr);
31 int ztest_search (void *handle, bend_search_rr *rr)
33 if (rr->num_bases != 1)
38 if (strcmp (rr->basenames[0], "Default"))
41 rr->errstring = rr->basenames[0];
44 rr->hits = rand() % 22;
48 int ztest_present (void *handle, bend_present_rr *rr)
53 int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
55 /* user-defined handle - created in bend_init */
56 int *counter = (int*) handle;
58 yaz_log(LOG_LOG, "ESRequest no %d", *counter);
62 if (rr->esr->packageName)
63 yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
64 yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
67 yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
69 if (!rr->esr->taskSpecificParameters)
71 yaz_log (LOG_WARN, "No task specific parameters");
73 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
75 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
76 yaz_log (LOG_LOG, "Received ItemOrder");
77 if (it->which == Z_IOItemOrder_esRequest)
79 Z_IORequest *ir = it->u.esRequest;
80 Z_IOOriginPartToKeep *k = ir->toKeep;
81 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
86 yaz_log(LOG_LOG, "contact name %s", k->contact->name);
87 if (k->contact->phone)
88 yaz_log(LOG_LOG, "contact phone %s", k->contact->phone);
89 if (k->contact->email)
90 yaz_log(LOG_LOG, "contact email %s", k->contact->email);
94 yaz_log(LOG_LOG, "Billing info (not shown)");
99 yaz_log(LOG_LOG, "resultsetItem");
100 yaz_log(LOG_LOG, "setId: %s", n->resultSetItem->resultSetId);
101 yaz_log(LOG_LOG, "item: %d", *n->resultSetItem->item);
106 Z_External *r = (Z_External*) n->itemRequest;
107 ILL_ItemRequest *item_req = 0;
108 ILL_APDU *ill_apdu = 0;
109 if (r->direct_reference)
111 oident *ent = oid_getentbyoid(r->direct_reference);
113 yaz_log(LOG_LOG, "OID %s", ent->desc);
114 if (ent && ent->value == VAL_TEXT_XML)
116 yaz_log (LOG_LOG, "ILL XML request");
117 if (r->which == Z_External_octet)
118 yaz_log (LOG_LOG, "%.*s", r->u.octet_aligned->len,
119 r->u.octet_aligned->buf);
121 if (ent && ent->value == VAL_ISO_ILL_1)
123 yaz_log (LOG_LOG, "Decode ItemRequest begin");
124 if (r->which == ODR_EXTERNAL_single)
126 odr_setbuf(rr->decode,
127 (char *) r->u.single_ASN1_type->buf,
128 r->u.single_ASN1_type->len, 0);
130 if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
133 "Couldn't decode ItemRequest %s near %d",
134 odr_errmsg(odr_geterror(rr->decode)),
135 odr_offset(rr->decode));
138 yaz_log(LOG_LOG, "Decode ItemRequest OK");
141 ill_ItemRequest (rr->print, &item_req, 0,
143 odr_reset (rr->print);
146 if (!item_req && r->which == ODR_EXTERNAL_single)
148 yaz_log (LOG_LOG, "Decode ILL APDU begin");
149 odr_setbuf(rr->decode,
150 (char*) r->u.single_ASN1_type->buf,
151 r->u.single_ASN1_type->len, 0);
153 if (!ill_APDU (rr->decode, &ill_apdu, 0, 0))
156 "Couldn't decode ILL APDU %s near %d",
157 odr_errmsg(odr_geterror(rr->decode)),
158 odr_offset(rr->decode));
159 yaz_log(LOG_LOG, "PDU dump:");
160 odr_dumpBER(yaz_log_file(),
161 (char *) r->u.single_ASN1_type->buf,
162 r->u.single_ASN1_type->len);
165 yaz_log(LOG_LOG, "Decode ILL APDU OK");
168 ill_APDU (rr->print, &ill_apdu, 0,
170 odr_reset (rr->print);
177 yaz_log (LOG_LOG, "ILL protocol version = %d",
178 *item_req->protocol_version_num);
185 Z_External *ext = (Z_External *)
186 odr_malloc (rr->stream, sizeof(*ext));
187 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
188 odr_malloc (rr->stream, sizeof(*keep));
189 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
190 odr_malloc (rr->stream, sizeof(*targetPart));
192 rr->taskPackage = (Z_TaskPackage *)
193 odr_malloc (rr->stream, sizeof(*rr->taskPackage));
194 rr->taskPackage->packageType =
195 odr_oiddup (rr->stream, rr->esr->packageType);
196 rr->taskPackage->packageName = 0;
197 rr->taskPackage->userId = 0;
198 rr->taskPackage->retentionTime = 0;
199 rr->taskPackage->permissions = 0;
200 rr->taskPackage->description = 0;
201 rr->taskPackage->targetReference = (Odr_oct *)
202 odr_malloc (rr->stream, sizeof(Odr_oct));
203 rr->taskPackage->targetReference->buf =
204 (unsigned char *) odr_strdup (rr->stream, "911");
205 rr->taskPackage->targetReference->len =
206 rr->taskPackage->targetReference->size =
207 strlen((char *) (rr->taskPackage->targetReference->buf));
208 rr->taskPackage->creationDateTime = 0;
209 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
210 rr->taskPackage->packageDiagnostics = 0;
211 rr->taskPackage->taskSpecificParameters = ext;
213 ext->direct_reference =
214 odr_oiddup (rr->stream, rr->esr->packageType);
215 ext->indirect_reference = 0;
217 ext->which = Z_External_itemOrder;
218 ext->u.itemOrder = (Z_ItemOrder *)
219 odr_malloc (rr->stream, sizeof(*ext->u.update));
220 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
221 ext->u.itemOrder->u.taskPackage = (Z_IOTaskPackage *)
222 odr_malloc (rr->stream, sizeof(Z_IOTaskPackage));
223 ext->u.itemOrder->u.taskPackage->originPart = k;
224 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
226 targetPart->itemRequest = 0;
227 targetPart->statusOrErrorReport = 0;
228 targetPart->auxiliaryStatus = 0;
232 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
234 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
235 yaz_log (LOG_LOG, "Received DB Update");
236 if (up->which == Z_IUUpdate_esRequest)
238 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
239 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
240 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
242 yaz_log (LOG_LOG, "action");
245 switch (*toKeep->action)
247 case Z_IUOriginPartToKeep_recordInsert:
248 yaz_log (LOG_LOG, " recordInsert");
250 case Z_IUOriginPartToKeep_recordReplace:
251 yaz_log (LOG_LOG, " recordReplace");
253 case Z_IUOriginPartToKeep_recordDelete:
254 yaz_log (LOG_LOG, " recordDelete");
256 case Z_IUOriginPartToKeep_elementUpdate:
257 yaz_log (LOG_LOG, " elementUpdate");
259 case Z_IUOriginPartToKeep_specialUpdate:
260 yaz_log (LOG_LOG, " specialUpdate");
263 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
266 if (toKeep->databaseName)
268 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
269 if (!strcmp(toKeep->databaseName, "fault"))
272 rr->errstring = toKeep->databaseName;
274 if (!strcmp(toKeep->databaseName, "accept"))
279 Z_External *ext = (Z_External *)
280 odr_malloc (rr->stream, sizeof(*ext));
281 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
282 odr_malloc (rr->stream, sizeof(*keep));
283 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
284 odr_malloc (rr->stream, sizeof(*targetPart));
286 rr->taskPackage = (Z_TaskPackage *)
287 odr_malloc (rr->stream, sizeof(*rr->taskPackage));
288 rr->taskPackage->packageType =
289 odr_oiddup (rr->stream, rr->esr->packageType);
290 rr->taskPackage->packageName = 0;
291 rr->taskPackage->userId = 0;
292 rr->taskPackage->retentionTime = 0;
293 rr->taskPackage->permissions = 0;
294 rr->taskPackage->description = 0;
295 rr->taskPackage->targetReference = (Odr_oct *)
296 odr_malloc (rr->stream, sizeof(Odr_oct));
297 rr->taskPackage->targetReference->buf =
298 (unsigned char *) odr_strdup (rr->stream, "123");
299 rr->taskPackage->targetReference->len =
300 rr->taskPackage->targetReference->size =
301 strlen((char *) (rr->taskPackage->targetReference->buf));
302 rr->taskPackage->creationDateTime = 0;
303 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
304 rr->taskPackage->packageDiagnostics = 0;
305 rr->taskPackage->taskSpecificParameters = ext;
307 ext->direct_reference =
308 odr_oiddup (rr->stream, rr->esr->packageType);
309 ext->indirect_reference = 0;
311 ext->which = Z_External_update;
312 ext->u.update = (Z_IUUpdate *)
313 odr_malloc (rr->stream, sizeof(*ext->u.update));
314 ext->u.update->which = Z_IUUpdate_taskPackage;
315 ext->u.update->u.taskPackage = (Z_IUUpdateTaskPackage *)
316 odr_malloc (rr->stream, sizeof(Z_IUUpdateTaskPackage));
317 ext->u.update->u.taskPackage->originPart = keep;
318 ext->u.update->u.taskPackage->targetPart = targetPart;
320 keep->action = (int *) odr_malloc (rr->stream, sizeof(int));
321 *keep->action = *toKeep->action;
323 odr_strdup (rr->stream, toKeep->databaseName);
325 keep->elementSetName = 0;
326 keep->actionQualifier = 0;
328 targetPart->updateStatus = odr_intdup (rr->stream, 1);
329 targetPart->num_globalDiagnostics = 0;
330 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
331 targetPart->num_taskPackageRecords = 1;
332 targetPart->taskPackageRecords =
333 (Z_IUTaskPackageRecordStructure **)
334 odr_malloc (rr->stream,
335 sizeof(Z_IUTaskPackageRecordStructure *));
336 targetPart->taskPackageRecords[0] =
337 (Z_IUTaskPackageRecordStructure *)
338 odr_malloc (rr->stream,
339 sizeof(Z_IUTaskPackageRecordStructure));
341 targetPart->taskPackageRecords[0]->which =
342 Z_IUTaskPackageRecordStructure_record;
343 targetPart->taskPackageRecords[0]->u.record =
344 z_ext_record (rr->stream, VAL_SUTRS, "test", 4);
345 targetPart->taskPackageRecords[0]->correlationInfo = 0;
346 targetPart->taskPackageRecords[0]->recordStatus =
347 odr_intdup (rr->stream,
348 Z_IUTaskPackageRecordStructure_success);
349 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
352 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
357 for (i = 0; i < notToKeep->num; i++)
359 Z_External *rec = notToKeep->elements[i]->record;
361 if (rec->direct_reference)
363 struct oident *oident;
364 oident = oid_getentbyoid(rec->direct_reference);
366 yaz_log (LOG_LOG, "record %d type %s", i,
371 case Z_External_sutrs:
372 if (rec->u.octet_aligned->len > 170)
373 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
377 yaz_log (LOG_LOG, "%d bytes:\n%s",
381 case Z_External_octet :
382 if (rec->u.octet_aligned->len > 170)
383 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
384 rec->u.octet_aligned->len,
385 rec->u.octet_aligned->buf);
387 yaz_log (LOG_LOG, "%d bytes\n%s",
388 rec->u.octet_aligned->len,
389 rec->u.octet_aligned->buf);
397 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
398 rr->esr->taskSpecificParameters->which);
404 int ztest_delete (void *handle, bend_delete_rr *rr)
406 if (rr->num_setnames == 1 && !strcmp (rr->setnames[0], "1"))
407 rr->delete_status = Z_DeleteStatus_success;
409 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
413 /* Our sort handler really doesn't sort... */
414 int ztest_sort (void *handle, bend_sort_rr *rr)
417 rr->sort_status = Z_SortStatus_success;
421 static int atoin (const char *buf, int n)
427 val = val*10 + (*buf - '0');
433 char *marc_read(FILE *inf, ODR odr)
439 if (fread (length_str, 1, 5, inf) != 5)
441 size = atoin (length_str, 5);
444 if (!(buf = (char*) odr_malloc (odr, size+1)))
446 if (fread (buf+5, 1, size-5, inf) != (size-5))
451 memcpy (buf, length_str, 5);
456 static char *dummy_database_record (int num, ODR odr)
458 FILE *inf = fopen ("dummy-records", "r");
464 { /* this will generate a very bad MARC record (testing only) */
465 buf = (char*) odr_malloc(odr, 2101);
466 memset(buf, '7', 2100);
471 /* OK, try to get proper MARC records from the file */
474 buf = marc_read (inf, odr);
483 static Z_GenericRecord *dummy_grs_record (int num, ODR o)
485 FILE *f = fopen("dummy-grs", "r");
487 Z_GenericRecord *r = 0;
492 while (fgets(line, 512, f))
493 if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
502 int ztest_fetch(void *handle, bend_fetch_rr *r)
507 r->basename = "DUMMY";
508 r->output_format = r->request_format;
509 if (r->request_format == VAL_SUTRS)
512 /* this section returns a huge record (for testing non-blocking write, etc) */
514 r->record = odr_malloc (r->stream, r->len);
515 memset (r->record, 'x', r->len);
517 /* this section returns a small record */
520 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
522 r->len = strlen(buf);
523 r->record = (char *) odr_malloc (r->stream, r->len+1);
524 strcpy(r->record, buf);
527 else if (r->request_format == VAL_GRS1)
530 r->record = (char*) dummy_grs_record(r->number, r->stream);
537 else if ((cp = dummy_database_record(r->number, r->stream)))
541 r->output_format = VAL_USMARC;
553 * silly dummy-scan what reads words from a file.
555 int ztest_scan(void *handle, bend_scan_rr *q)
558 static struct scan_entry list[200];
559 static char entries[200][80];
563 int term_position_req = q->term_position;
564 int num_entries_req = q->num_entries;
569 q->status = BEND_SCAN_SUCCESS;
570 if (!f && !(f = fopen("dummy-words", "r")))
572 perror("dummy-words");
575 if (q->term->term->which != Z_Term_general)
577 q->errcode = 229; /* unsupported term type */
580 if (*q->step_size != 0)
582 q->errcode = 205; /*Only zero step size supported for Scan */
585 if (q->term->term->u.general->len >= 80)
587 q->errcode = 11; /* term too long */
590 if (q->num_entries > 200)
595 memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
596 term[q->term->term->u.general->len] = '\0';
597 for (p = term; *p; p++)
601 fseek(f, 0, SEEK_SET);
604 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
605 i++, pos < 199 ? pos++ : (pos = 0))
607 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
609 if ((q->term_position = term_position_req) > i + 1)
611 q->term_position = i + 1;
612 q->status = BEND_SCAN_PARTIAL;
614 for (; q->num_entries < q->term_position; q->num_entries++)
618 po = pos - q->term_position + q->num_entries+1; /* find pos */
622 if (!strcmp (term, "SD") && q->num_entries == 2)
624 list[q->num_entries].term = entries[pos];
625 list[q->num_entries].occurrences = -1;
626 list[q->num_entries].errcode = 233;
627 list[q->num_entries].errstring = "SD for Scan Term";
631 list[q->num_entries].term = entries[po];
632 list[q->num_entries].occurrences = hits[po];
636 else if (q->num_entries)
638 list[q->num_entries].term = entries[pos];
639 list[q->num_entries].occurrences = hits[pos];
642 if (q->num_entries >= num_entries_req)
646 q->status = BEND_SCAN_PARTIAL;
650 bend_initresult *bend_init(bend_initrequest *q)
652 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
653 int *counter = (int *) xmalloc (sizeof(int));
658 r->handle = counter; /* user handle, in this case a simple int */
659 q->bend_sort = ztest_sort; /* register sort handler */
660 q->bend_search = ztest_search; /* register search handler */
661 q->bend_present = ztest_present; /* register present handle */
662 q->bend_esrequest = ztest_esrequest;
663 q->bend_delete = ztest_delete;
664 q->bend_fetch = ztest_fetch;
665 q->bend_scan = ztest_scan;
669 void bend_close(void *handle)
671 xfree (handle); /* release our user-defined handle */
675 int main(int argc, char **argv)
677 return statserv_main(argc, argv, bend_init, bend_close);