2 * Copyright (c) 1995-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: ztest.c,v 1.66 2004-11-16 17:08:11 heikki Exp $
9 * Demonstration of simple server
16 #include <yaz/yaz-util.h>
17 #include <yaz/backend.h>
20 static int log_level=0;
21 static int log_level_set=0;
23 Z_GenericRecord *dummy_grs_record (int num, ODR o);
24 char *dummy_marc_record (int num, ODR odr);
25 char *dummy_xml_record (int num, ODR odr);
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 int ztest_search (void *handle, bend_search_rr *rr)
35 if (rr->num_bases != 1)
41 /* if database is stop, stop this process.. For debugging only. */
42 if (!yaz_matchstr (rr->basenames[0], "stop"))
44 nmem_print_list_l(LOG_LOG);
48 /* Throw Database unavailable if other than Default */
49 if (yaz_matchstr (rr->basenames[0], "Default"))
52 rr->errstring = rr->basenames[0];
55 rr->hits = rand() % 24;
60 /* this huge function handles extended services */
61 int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
63 /* user-defined handle - created in bend_init */
64 int *counter = (int*) handle;
66 yaz_log(log_level, "ESRequest no %d", *counter);
70 if (rr->esr->packageName)
71 yaz_log(log_level, "packagename: %s", rr->esr->packageName);
72 yaz_log(log_level, "Waitaction: %d", *rr->esr->waitAction);
75 yaz_log(log_level, "function: %d", *rr->esr->function);
77 if (!rr->esr->taskSpecificParameters)
79 yaz_log (log_level, "No task specific parameters");
81 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
83 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
84 yaz_log (log_level, "Received ItemOrder");
85 if (it->which == Z_IOItemOrder_esRequest)
87 Z_IORequest *ir = it->u.esRequest;
88 Z_IOOriginPartToKeep *k = ir->toKeep;
89 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
94 yaz_log(log_level, "contact name %s", k->contact->name);
95 if (k->contact->phone)
96 yaz_log(log_level, "contact phone %s", k->contact->phone);
97 if (k->contact->email)
98 yaz_log(log_level, "contact email %s", k->contact->email);
102 yaz_log(log_level, "Billing info (not shown)");
105 if (n->resultSetItem)
107 yaz_log(log_level, "resultsetItem");
108 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
109 yaz_log(log_level, "item: %d", *n->resultSetItem->item);
113 Z_External *r = (Z_External*) n->itemRequest;
114 ILL_ItemRequest *item_req = 0;
115 ILL_APDU *ill_apdu = 0;
116 if (r->direct_reference)
118 oident *ent = oid_getentbyoid(r->direct_reference);
120 yaz_log(log_level, "OID %s", ent->desc);
121 if (ent && ent->value == VAL_TEXT_XML)
123 yaz_log (log_level, "ILL XML request");
124 if (r->which == Z_External_octet)
125 yaz_log (log_level, "%.*s", r->u.octet_aligned->len,
126 r->u.octet_aligned->buf);
128 if (ent && ent->value == VAL_ISO_ILL_1)
130 yaz_log (log_level, "Decode ItemRequest begin");
131 if (r->which == ODR_EXTERNAL_single)
133 odr_setbuf(rr->decode,
134 (char *) r->u.single_ASN1_type->buf,
135 r->u.single_ASN1_type->len, 0);
137 if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
140 "Couldn't decode ItemRequest %s near %d",
141 odr_errmsg(odr_geterror(rr->decode)),
142 odr_offset(rr->decode));
145 yaz_log(log_level, "Decode ItemRequest OK");
148 ill_ItemRequest (rr->print, &item_req, 0,
150 odr_reset (rr->print);
153 if (!item_req && r->which == ODR_EXTERNAL_single)
155 yaz_log (log_level, "Decode ILL APDU begin");
156 odr_setbuf(rr->decode,
157 (char*) r->u.single_ASN1_type->buf,
158 r->u.single_ASN1_type->len, 0);
160 if (!ill_APDU (rr->decode, &ill_apdu, 0, 0))
163 "Couldn't decode ILL APDU %s near %d",
164 odr_errmsg(odr_geterror(rr->decode)),
165 odr_offset(rr->decode));
166 yaz_log(log_level, "PDU dump:");
167 odr_dumpBER(yaz_log_file(),
168 (char *) r->u.single_ASN1_type->buf,
169 r->u.single_ASN1_type->len);
172 yaz_log(log_level, "Decode ILL APDU OK");
175 ill_APDU (rr->print, &ill_apdu, 0,
177 odr_reset (rr->print);
184 yaz_log (log_level, "ILL protocol version = %d",
185 *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_level, "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_level, "action");
251 switch (*toKeep->action)
253 case Z_IUOriginPartToKeep_recordInsert:
254 yaz_log (log_level, " recordInsert");
256 case Z_IUOriginPartToKeep_recordReplace:
257 yaz_log (log_level, " recordReplace");
259 case Z_IUOriginPartToKeep_recordDelete:
260 yaz_log (log_level, " recordDelete");
262 case Z_IUOriginPartToKeep_elementUpdate:
263 yaz_log (log_level, " elementUpdate");
265 case Z_IUOriginPartToKeep_specialUpdate:
266 yaz_log (log_level, " specialUpdate");
269 yaz_log (log_level, " unknown (%d)", *toKeep->action);
272 if (toKeep->databaseName)
274 yaz_log (log_level, "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 = 1;
338 targetPart->taskPackageRecords =
339 (Z_IUTaskPackageRecordStructure **)
340 odr_malloc (rr->stream,
341 sizeof(Z_IUTaskPackageRecordStructure *));
342 targetPart->taskPackageRecords[0] =
343 (Z_IUTaskPackageRecordStructure *)
344 odr_malloc (rr->stream,
345 sizeof(Z_IUTaskPackageRecordStructure));
347 targetPart->taskPackageRecords[0]->which =
348 Z_IUTaskPackageRecordStructure_record;
349 targetPart->taskPackageRecords[0]->u.record =
350 z_ext_record (rr->stream, VAL_SUTRS, "test", 4);
351 targetPart->taskPackageRecords[0]->correlationInfo = 0;
352 targetPart->taskPackageRecords[0]->recordStatus =
353 odr_intdup (rr->stream,
354 Z_IUTaskPackageRecordStructure_success);
355 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
358 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
363 for (i = 0; i < notToKeep->num; i++)
365 Z_External *rec = notToKeep->elements[i]->record;
367 if (rec->direct_reference)
369 struct oident *oident;
370 oident = oid_getentbyoid(rec->direct_reference);
372 yaz_log (log_level, "record %d type %s", i,
377 case Z_External_sutrs:
378 if (rec->u.octet_aligned->len > 170)
379 yaz_log (log_level, "%d bytes:\n%.168s ...",
383 yaz_log (log_level, "%d bytes:\n%s",
387 case Z_External_octet :
388 if (rec->u.octet_aligned->len > 170)
389 yaz_log (log_level, "%d bytes:\n%.168s ...",
390 rec->u.octet_aligned->len,
391 rec->u.octet_aligned->buf);
393 yaz_log (log_level, "%d bytes\n%s",
394 rec->u.octet_aligned->len,
395 rec->u.octet_aligned->buf);
401 else if (rr->esr->taskSpecificParameters->which == Z_External_update0)
403 yaz_log(log_level, "Received DB Update (version 0)");
407 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
408 rr->esr->taskSpecificParameters->which);
414 /* result set delete */
415 int ztest_delete (void *handle, bend_delete_rr *rr)
417 if (rr->num_setnames == 1 && !strcmp (rr->setnames[0], "1"))
418 rr->delete_status = Z_DeleteStatus_success;
420 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
424 /* Our sort handler really doesn't sort... */
425 int ztest_sort (void *handle, bend_sort_rr *rr)
428 rr->sort_status = Z_SortResponse_success;
433 /* present request handler */
434 int ztest_present (void *handle, bend_present_rr *rr)
439 /* retrieval of a single record (present, and piggy back search) */
440 int ztest_fetch(void *handle, bend_fetch_rr *r)
445 r->basename = "Default";
446 r->output_format = r->request_format;
447 if (r->request_format == VAL_SUTRS)
449 /* this section returns a small record */
452 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
454 r->len = strlen(buf);
455 r->record = (char *) odr_malloc (r->stream, r->len+1);
456 strcpy(r->record, buf);
458 else if (r->request_format == VAL_GRS1)
461 r->record = (char*) dummy_grs_record(r->number, r->stream);
468 else if (r->request_format == VAL_POSTSCRIPT)
474 sprintf (fname, "part.%d.ps", r->number);
475 f = fopen(fname, "rb");
481 fseek (f, 0L, SEEK_END);
483 if (size <= 0 || size >= 5000000)
488 fseek (f, 0L, SEEK_SET);
489 r->record = (char*) odr_malloc (r->stream, size);
491 r->output_format = VAL_POSTSCRIPT;
492 fread (r->record, size, 1, f);
495 else if (r->request_format == VAL_TEXT_XML)
497 if ((cp = dummy_xml_record (r->number, r->stream)))
501 r->output_format = VAL_TEXT_XML;
506 r->surrogate_flag = 1;
510 else if ((cp = dummy_marc_record(r->number, r->stream)))
514 r->output_format = VAL_USMARC;
526 * silly dummy-scan what reads words from a file.
528 int ztest_scan(void *handle, bend_scan_rr *q)
531 static struct scan_entry list[200];
532 static char entries[200][80];
536 int term_position_req = q->term_position;
537 int num_entries_req = q->num_entries;
542 q->status = BEND_SCAN_SUCCESS;
543 if (!f && !(f = fopen("dummy-words", "r")))
545 perror("dummy-words");
548 if (q->term->term->which != Z_Term_general)
550 q->errcode = 229; /* unsupported term type */
553 if (*q->step_size != 0)
555 q->errcode = 205; /*Only zero step size supported for Scan */
558 if (q->term->term->u.general->len >= 80)
560 q->errcode = 11; /* term too long */
563 if (q->num_entries > 200)
568 memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
569 term[q->term->term->u.general->len] = '\0';
570 for (p = term; *p; p++)
574 fseek(f, 0, SEEK_SET);
577 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
578 i++, pos < 199 ? pos++ : (pos = 0))
580 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
582 if ((q->term_position = term_position_req) > i + 1)
584 q->term_position = i + 1;
585 q->status = BEND_SCAN_PARTIAL;
587 for (; q->num_entries < q->term_position; q->num_entries++)
591 po = pos - q->term_position + q->num_entries+1; /* find pos */
595 if (!strcmp (term, "SD") && q->num_entries == 2)
597 list[q->num_entries].term = entries[pos];
598 list[q->num_entries].occurrences = -1;
599 list[q->num_entries].errcode = 233;
600 list[q->num_entries].errstring = "SD for Scan Term";
604 list[q->num_entries].term = entries[po];
605 list[q->num_entries].occurrences = hits[po];
609 else if (q->num_entries)
611 list[q->num_entries].term = entries[pos];
612 list[q->num_entries].occurrences = hits[pos];
615 if (q->num_entries >= num_entries_req)
619 q->status = BEND_SCAN_PARTIAL;
623 static int ztest_explain(void *handle, bend_explain_rr *rr)
625 if (rr->database && !strcmp(rr->database, "Default"))
627 rr->explain_buf = "<explain>\n"
629 "\t\t<host>localhost</host>\n"
630 "\t\t<port>210</port>\n"
637 bend_initresult *bend_init(bend_initrequest *q)
639 bend_initresult *r = (bend_initresult *)
640 odr_malloc (q->stream, sizeof(*r));
641 int *counter = (int *) xmalloc (sizeof(int));
645 log_level=yaz_log_module_level("ztest");
652 r->handle = counter; /* user handle, in this case a simple int */
653 q->bend_sort = ztest_sort; /* register sort handler */
654 q->bend_search = ztest_search; /* register search handler */
655 q->bend_present = ztest_present; /* register present handle */
656 q->bend_esrequest = ztest_esrequest;
657 q->bend_delete = ztest_delete;
658 q->bend_fetch = ztest_fetch;
659 q->bend_scan = ztest_scan;
660 q->bend_explain = ztest_explain;
665 void bend_close(void *handle)
667 xfree (handle); /* release our user-defined handle */
671 int main(int argc, char **argv)
673 return statserv_main(argc, argv, bend_init, bend_close);