2 * Copyright (c) 1995-2002, Index Data.
3 * See the file LICENSE for details.
5 * $Id: ztest.c,v 1.48 2002-01-17 23:22:40 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));
137 yaz_log(LOG_LOG, "PDU dump:");
138 odr_dumpBER(yaz_log_file(),
139 r->u.single_ASN1_type->buf,
140 r->u.single_ASN1_type->len);
144 yaz_log(LOG_LOG, "Decode ItemRequest OK");
147 ill_ItemRequest (rr->print, &item_req, 0,
149 odr_reset (rr->print);
152 if (!item_req && r->which == ODR_EXTERNAL_single)
154 yaz_log (LOG_LOG, "Decode ILL APDU begin");
155 odr_setbuf(rr->decode,
156 (char*) r->u.single_ASN1_type->buf,
157 r->u.single_ASN1_type->len, 0);
159 if (!ill_APDU (rr->decode, &ill_apdu, 0, 0))
162 "Couldn't decode ILL APDU %s near %d",
163 odr_errmsg(odr_geterror(rr->decode)),
164 odr_offset(rr->decode));
165 yaz_log(LOG_LOG, "PDU dump:");
166 odr_dumpBER(yaz_log_file(),
167 (char *) r->u.single_ASN1_type->buf,
168 r->u.single_ASN1_type->len);
171 yaz_log(LOG_LOG, "Decode ILL APDU OK");
174 ill_APDU (rr->print, &ill_apdu, 0,
176 odr_reset (rr->print);
183 yaz_log (LOG_LOG, "ILL protocol version = %d",
184 *item_req->protocol_version_num);
191 Z_External *ext = (Z_External *)
192 odr_malloc (rr->stream, sizeof(*ext));
193 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
194 odr_malloc (rr->stream, sizeof(*keep));
195 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
196 odr_malloc (rr->stream, sizeof(*targetPart));
198 rr->taskPackage = (Z_TaskPackage *)
199 odr_malloc (rr->stream, sizeof(*rr->taskPackage));
200 rr->taskPackage->packageType =
201 odr_oiddup (rr->stream, rr->esr->packageType);
202 rr->taskPackage->packageName = 0;
203 rr->taskPackage->userId = 0;
204 rr->taskPackage->retentionTime = 0;
205 rr->taskPackage->permissions = 0;
206 rr->taskPackage->description = 0;
207 rr->taskPackage->targetReference = (Odr_oct *)
208 odr_malloc (rr->stream, sizeof(Odr_oct));
209 rr->taskPackage->targetReference->buf =
210 (unsigned char *) odr_strdup (rr->stream, "911");
211 rr->taskPackage->targetReference->len =
212 rr->taskPackage->targetReference->size =
213 strlen((char *) (rr->taskPackage->targetReference->buf));
214 rr->taskPackage->creationDateTime = 0;
215 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
216 rr->taskPackage->packageDiagnostics = 0;
217 rr->taskPackage->taskSpecificParameters = ext;
219 ext->direct_reference =
220 odr_oiddup (rr->stream, rr->esr->packageType);
221 ext->indirect_reference = 0;
223 ext->which = Z_External_itemOrder;
224 ext->u.itemOrder = (Z_ItemOrder *)
225 odr_malloc (rr->stream, sizeof(*ext->u.update));
226 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
227 ext->u.itemOrder->u.taskPackage = (Z_IOTaskPackage *)
228 odr_malloc (rr->stream, sizeof(Z_IOTaskPackage));
229 ext->u.itemOrder->u.taskPackage->originPart = k;
230 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
232 targetPart->itemRequest = 0;
233 targetPart->statusOrErrorReport = 0;
234 targetPart->auxiliaryStatus = 0;
238 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
240 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
241 yaz_log (LOG_LOG, "Received DB Update");
242 if (up->which == Z_IUUpdate_esRequest)
244 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
245 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
246 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
248 yaz_log (LOG_LOG, "action");
251 switch (*toKeep->action)
253 case Z_IUOriginPartToKeep_recordInsert:
254 yaz_log (LOG_LOG, " recordInsert");
256 case Z_IUOriginPartToKeep_recordReplace:
257 yaz_log (LOG_LOG, " recordReplace");
259 case Z_IUOriginPartToKeep_recordDelete:
260 yaz_log (LOG_LOG, " recordDelete");
262 case Z_IUOriginPartToKeep_elementUpdate:
263 yaz_log (LOG_LOG, " elementUpdate");
265 case Z_IUOriginPartToKeep_specialUpdate:
266 yaz_log (LOG_LOG, " specialUpdate");
269 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
272 if (toKeep->databaseName)
274 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
275 if (!strcmp(toKeep->databaseName, "fault"))
278 rr->errstring = toKeep->databaseName;
280 if (!strcmp(toKeep->databaseName, "accept"))
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_IUTargetPart *targetPart = (Z_IUTargetPart *)
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, "123");
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_update;
318 ext->u.update = (Z_IUUpdate *)
319 odr_malloc (rr->stream, sizeof(*ext->u.update));
320 ext->u.update->which = Z_IUUpdate_taskPackage;
321 ext->u.update->u.taskPackage = (Z_IUUpdateTaskPackage *)
322 odr_malloc (rr->stream, sizeof(Z_IUUpdateTaskPackage));
323 ext->u.update->u.taskPackage->originPart = keep;
324 ext->u.update->u.taskPackage->targetPart = targetPart;
326 keep->action = (int *) odr_malloc (rr->stream, sizeof(int));
327 *keep->action = *toKeep->action;
329 odr_strdup (rr->stream, toKeep->databaseName);
331 keep->elementSetName = 0;
332 keep->actionQualifier = 0;
334 targetPart->updateStatus = odr_intdup (rr->stream, 1);
335 targetPart->num_globalDiagnostics = 0;
336 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
337 targetPart->num_taskPackageRecords = 0;
338 targetPart->taskPackageRecords =
339 (Z_IUTaskPackageRecordStructure **) odr_nullval();
344 for (i = 0; i < notToKeep->num; i++)
346 Z_External *rec = notToKeep->elements[i]->record;
348 if (rec->direct_reference)
350 struct oident *oident;
351 oident = oid_getentbyoid(rec->direct_reference);
353 yaz_log (LOG_LOG, "record %d type %s", i,
358 case Z_External_sutrs:
359 if (rec->u.octet_aligned->len > 170)
360 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
364 yaz_log (LOG_LOG, "%d bytes:\n%s",
368 case Z_External_octet :
369 if (rec->u.octet_aligned->len > 170)
370 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
371 rec->u.octet_aligned->len,
372 rec->u.octet_aligned->buf);
374 yaz_log (LOG_LOG, "%d bytes\n%s",
375 rec->u.octet_aligned->len,
376 rec->u.octet_aligned->buf);
384 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
385 rr->esr->taskSpecificParameters->which);
391 int ztest_delete (void *handle, bend_delete_rr *rr)
393 if (rr->num_setnames == 1 && !strcmp (rr->setnames[0], "1"))
394 rr->delete_status = Z_DeleteStatus_success;
396 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
400 /* Our sort handler really doesn't sort... */
401 int ztest_sort (void *handle, bend_sort_rr *rr)
404 rr->sort_status = Z_SortStatus_success;
408 static int atoin (const char *buf, int n)
414 val = val*10 + (*buf - '0');
420 char *marc_read(FILE *inf, ODR odr)
426 if (fread (length_str, 1, 5, inf) != 5)
428 size = atoin (length_str, 5);
431 if (!(buf = (char*) odr_malloc (odr, size+1)))
433 if (fread (buf+5, 1, size-5, inf) != (size-5))
438 memcpy (buf, length_str, 5);
443 static char *dummy_database_record (int num, ODR odr)
445 FILE *inf = fopen ("dummy-records", "r");
451 { /* this will generate a very bad MARC record (testing only) */
452 buf = (char*) odr_malloc(odr, 2101);
453 memset(buf, '7', 2100);
458 /* OK, try to get proper MARC records from the file */
461 buf = marc_read (inf, odr);
470 static Z_GenericRecord *dummy_grs_record (int num, ODR o)
472 FILE *f = fopen("dummy-grs", "r");
474 Z_GenericRecord *r = 0;
479 while (fgets(line, 512, f))
480 if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
489 int ztest_fetch(void *handle, bend_fetch_rr *r)
494 r->basename = "DUMMY";
495 r->output_format = r->request_format;
496 if (r->request_format == VAL_SUTRS)
499 /* this section returns a huge record (for testing non-blocking write, etc) */
501 r->record = odr_malloc (r->stream, r->len);
502 memset (r->record, 'x', r->len);
504 /* this section returns a small record */
507 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
509 r->len = strlen(buf);
510 r->record = (char *) odr_malloc (r->stream, r->len+1);
511 strcpy(r->record, buf);
514 else if (r->request_format == VAL_GRS1)
517 r->record = (char*) dummy_grs_record(r->number, r->stream);
524 else if ((cp = dummy_database_record(r->number, r->stream)))
528 r->output_format = VAL_USMARC;
540 * silly dummy-scan what reads words from a file.
542 int ztest_scan(void *handle, bend_scan_rr *q)
545 static struct scan_entry list[200];
546 static char entries[200][80];
550 int term_position_req = q->term_position;
551 int num_entries_req = q->num_entries;
556 q->status = BEND_SCAN_SUCCESS;
557 if (!f && !(f = fopen("dummy-words", "r")))
559 perror("dummy-words");
562 if (q->term->term->which != Z_Term_general)
564 q->errcode = 229; /* unsupported term type */
567 if (*q->step_size != 0)
569 q->errcode = 205; /*Only zero step size supported for Scan */
572 if (q->term->term->u.general->len >= 80)
574 q->errcode = 11; /* term too long */
577 if (q->num_entries > 200)
582 memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
583 term[q->term->term->u.general->len] = '\0';
584 for (p = term; *p; p++)
588 fseek(f, 0, SEEK_SET);
591 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
592 i++, pos < 199 ? pos++ : (pos = 0))
594 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
596 if ((q->term_position = term_position_req) > i + 1)
598 q->term_position = i + 1;
599 q->status = BEND_SCAN_PARTIAL;
601 for (; q->num_entries < q->term_position; q->num_entries++)
605 po = pos - q->term_position + q->num_entries+1; /* find pos */
609 if (!strcmp (term, "SD") && q->num_entries == 2)
611 list[q->num_entries].term = entries[pos];
612 list[q->num_entries].occurrences = -1;
613 list[q->num_entries].errcode = 233;
614 list[q->num_entries].errstring = "SD for Scan Term";
618 list[q->num_entries].term = entries[po];
619 list[q->num_entries].occurrences = hits[po];
623 else if (q->num_entries)
625 list[q->num_entries].term = entries[pos];
626 list[q->num_entries].occurrences = hits[pos];
629 if (q->num_entries >= num_entries_req)
633 q->status = BEND_SCAN_PARTIAL;
637 bend_initresult *bend_init(bend_initrequest *q)
639 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
640 int *counter = (int *) xmalloc (sizeof(int));
645 r->handle = counter; /* user handle, in this case a simple int */
646 q->bend_sort = ztest_sort; /* register sort handler */
647 q->bend_search = ztest_search; /* register search handler */
648 q->bend_present = ztest_present; /* register present handle */
649 q->bend_esrequest = ztest_esrequest;
650 q->bend_delete = ztest_delete;
651 q->bend_fetch = ztest_fetch;
652 q->bend_scan = ztest_scan;
656 void bend_close(void *handle)
658 xfree (handle); /* release our user-defined handle */
662 int main(int argc, char **argv)
664 return statserv_main(argc, argv, bend_init, bend_close);