2 * Copyright (c) 1995-2001, Index Data.
3 * See the file LICENSE for details.
5 * NT Service interface by
6 * Chas Woodfield, Fretwell Downing Datasystems.
9 * Revision 1.43 2001-06-28 09:27:25 adam
10 * Number of Extended Services Requests logged.
12 * Revision 1.42 2001/04/06 12:26:46 adam
13 * Optional CCL module. Moved atoi_n to marcdisp.h from yaz-util.h.
15 * Revision 1.41 2001/04/05 13:08:48 adam
16 * New configure options: --enable-module.
18 * Revision 1.40 2001/03/25 21:55:13 adam
19 * Added odr_intdup. Ztest server returns TaskPackage for ItemUpdate.
21 * Revision 1.39 2001/03/12 14:40:57 adam
22 * Minor change of print of item update info.
24 * Revision 1.38 2001/02/21 13:46:54 adam
27 * Revision 1.37 2001/02/20 11:25:32 adam
28 * Added ill_get_APDU and ill_get_Cancel.
30 * Revision 1.36 2001/01/30 21:34:18 adam
31 * Added step-size for Scan backend interface.
33 * Revision 1.35 2000/11/23 10:58:33 adam
34 * SSL comstack support. Separate POSIX thread support library.
36 * Revision 1.34 2000/09/04 08:58:15 adam
37 * Added prefix yaz_ for most logging utility functions.
39 * Revision 1.33 2000/08/10 08:41:26 adam
42 * Revision 1.32 2000/04/05 07:39:55 adam
43 * Added shared library support (libtool).
45 * Revision 1.31 2000/01/31 13:15:21 adam
46 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
47 * that some characters are not surrounded by spaces in resulting term.
50 * Revision 1.30 2000/01/13 23:05:50 adam
51 * Fixed tagging for member requester-CHECKED-IN in ILL ASN.1 spec.
53 * Revision 1.29 2000/01/12 14:36:07 adam
54 * Added printing stream (ODR) for backend functions.
56 * Revision 1.28 1999/12/16 23:36:19 adam
57 * Implemented ILL protocol. Minor updates ASN.1 compiler.
59 * Revision 1.27 1999/11/30 13:47:12 adam
60 * Improved installation. Moved header files to include/yaz.
62 * Revision 1.26 1999/08/27 09:40:32 adam
63 * Renamed logf function to yaz_log. Removed VC++ project files.
65 * Revision 1.25 1999/06/01 14:29:12 adam
66 * Work on Extended Services.
68 * Revision 1.24 1999/05/27 13:07:54 adam
71 * Revision 1.23 1999/05/27 13:02:20 adam
72 * Assigned OID for old DB Update (VAL_DBUPDATE0).
74 * Revision 1.22 1999/05/26 13:49:12 adam
75 * DB Update implemented in client (very basic).
77 * Revision 1.21 1998/12/15 12:45:42 adam
80 * Revision 1.20 1998/12/14 14:48:05 adam
81 * Fixed memory leak - happened when fetching MARC records.
83 * Revision 1.19 1998/10/20 15:16:22 adam
84 * Minor change to prevent warning.
86 * Revision 1.18 1998/10/20 15:13:45 adam
87 * Minor fix regarding output for Item Order.
89 * Revision 1.17 1998/10/18 22:33:35 quinn
90 * Added diagnostic dump of Item Order Eservice.
92 * Revision 1.16 1998/10/15 08:26:23 adam
93 * Added type cast to make C++ happy.
95 * Revision 1.15 1998/10/13 20:05:57 adam
98 * Revision 1.14 1998/10/13 16:12:25 adam
99 * Added support for Surrogate Diagnostics for Scan Term entries.
101 * Revision 1.13 1998/08/19 16:10:09 adam
102 * Changed som member names of DeleteResultSetRequest/Response.
104 * Revision 1.12 1998/07/20 12:38:44 adam
105 * Implemented delete result set service to server API.
107 * Revision 1.11 1998/06/09 13:55:08 adam
110 * Revision 1.10 1998/05/27 16:55:54 adam
113 * Revision 1.9 1998/03/31 11:07:45 adam
114 * Furhter work on UNIverse resource report.
115 * Added Extended Services handling in frontend server.
117 * Revision 1.8 1998/02/11 11:53:36 adam
118 * Changed code so that it compiles as C++.
120 * Revision 1.7 1998/02/10 11:03:57 adam
121 * Added support for extended handlers in backend server interface.
123 * Revision 1.6 1998/01/29 13:16:02 adam
124 * Added dummy sort in test server.
126 * Revision 1.5 1997/11/07 13:31:58 adam
127 * Added NT Service name part of statserv_options_block. Moved NT
128 * service utility to server library.
130 * Revision 1.4 1997/09/17 12:10:43 adam
133 * Revision 1.3 1997/09/09 10:10:20 adam
134 * Another MSV5.0 port. Changed projects to include proper
135 * library/include paths.
136 * Server starts server in test-mode when no options are given.
138 * Revision 1.2 1997/09/04 13:50:31 adam
144 * Demonstration of simple server
151 #include <yaz/backend.h>
158 Z_GenericRecord *read_grs1(FILE *f, ODR o);
160 int ztest_search (void *handle, bend_search_rr *rr);
161 int ztest_sort (void *handle, bend_sort_rr *rr);
162 int ztest_present (void *handle, bend_present_rr *rr);
163 int ztest_esrequest (void *handle, bend_esrequest_rr *rr);
164 int ztest_delete (void *handle, bend_delete_rr *rr);
166 int ztest_search (void *handle, bend_search_rr *rr)
168 rr->hits = rand() % 22;
172 int ztest_present (void *handle, bend_present_rr *rr)
177 int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
179 int *counter = handle; /* user-defined handle - created in bend_init */
181 yaz_log(LOG_LOG, "ESRequest no %d", *counter);
185 if (rr->esr->packageName)
186 yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
187 yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
190 yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
192 if (!rr->esr->taskSpecificParameters)
194 yaz_log (LOG_WARN, "No task specific parameters");
196 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
198 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
199 yaz_log (LOG_LOG, "Received ItemOrder");
203 case Z_IOItemOrder_esRequest:
205 case Z_ItemOrder_esRequest:
208 Z_IORequest *ir = it->u.esRequest;
209 Z_IOOriginPartToKeep *k = ir->toKeep;
210 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
214 if (k->contact->name)
215 yaz_log(LOG_LOG, "contact name %s", k->contact->name);
216 if (k->contact->phone)
217 yaz_log(LOG_LOG, "contact phone %s", k->contact->phone);
218 if (k->contact->email)
219 yaz_log(LOG_LOG, "contact email %s", k->contact->email);
223 yaz_log(LOG_LOG, "Billing info (not shown)");
226 if (n->resultSetItem)
228 yaz_log(LOG_LOG, "resultsetItem");
229 yaz_log(LOG_LOG, "setId: %s", n->resultSetItem->resultSetId);
230 yaz_log(LOG_LOG, "item: %d", *n->resultSetItem->item);
235 Z_External *r = (Z_External*) n->itemRequest;
236 ILL_ItemRequest *item_req = 0;
237 ILL_APDU *ill_apdu = 0;
238 if (r->direct_reference)
240 oident *ent = oid_getentbyoid(r->direct_reference);
242 yaz_log(LOG_LOG, "OID %s", ent->desc);
243 if (ent && ent->value == VAL_TEXT_XML)
245 yaz_log (LOG_LOG, "ILL XML request");
246 if (r->which == Z_External_octet)
247 yaz_log (LOG_LOG, "%.*s", r->u.octet_aligned->len,
248 r->u.octet_aligned->buf);
250 if (ent && ent->value == VAL_ISO_ILL_1)
252 yaz_log (LOG_LOG, "Decode ItemRequest begin");
253 if (r->which == ODR_EXTERNAL_single)
255 odr_setbuf(rr->decode,
256 (char *) r->u.single_ASN1_type->buf,
257 r->u.single_ASN1_type->len, 0);
259 if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
262 "Couldn't decode ItemRequest %s near %d",
263 odr_errmsg(odr_geterror(rr->decode)),
264 odr_offset(rr->decode));
266 yaz_log(LOG_LOG, "PDU dump:");
267 odr_dumpBER(yaz_log_file(),
268 r->u.single_ASN1_type->buf,
269 r->u.single_ASN1_type->len);
273 yaz_log(LOG_LOG, "Decode ItemRequest OK");
276 ill_ItemRequest (rr->print, &item_req, 0,
278 odr_reset (rr->print);
281 if (!item_req && r->which == ODR_EXTERNAL_single)
283 yaz_log (LOG_LOG, "Decode ILL APDU begin");
284 odr_setbuf(rr->decode,
285 (char*) r->u.single_ASN1_type->buf,
286 r->u.single_ASN1_type->len, 0);
288 if (!ill_APDU (rr->decode, &ill_apdu, 0, 0))
291 "Couldn't decode ILL APDU %s near %d",
292 odr_errmsg(odr_geterror(rr->decode)),
293 odr_offset(rr->decode));
294 yaz_log(LOG_LOG, "PDU dump:");
295 odr_dumpBER(yaz_log_file(),
296 (char *) r->u.single_ASN1_type->buf,
297 r->u.single_ASN1_type->len);
300 yaz_log(LOG_LOG, "Decode ILL APDU OK");
303 ill_APDU (rr->print, &ill_apdu, 0,
305 odr_reset (rr->print);
312 yaz_log (LOG_LOG, "ILL protocol version = %d",
313 *item_req->protocol_version_num);
321 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
323 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
324 yaz_log (LOG_LOG, "Received DB Update");
325 if (up->which == Z_IUUpdate_esRequest)
327 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
328 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
329 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
331 yaz_log (LOG_LOG, "action");
334 switch (*toKeep->action)
336 case Z_IUOriginPartToKeep_recordInsert:
337 yaz_log (LOG_LOG, " recordInsert");
339 case Z_IUOriginPartToKeep_recordReplace:
340 yaz_log (LOG_LOG, " recordReplace");
342 case Z_IUOriginPartToKeep_recordDelete:
343 yaz_log (LOG_LOG, " recordDelete");
345 case Z_IUOriginPartToKeep_elementUpdate:
346 yaz_log (LOG_LOG, " elementUpdate");
348 case Z_IUOriginPartToKeep_specialUpdate:
349 yaz_log (LOG_LOG, " specialUpdate");
352 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
355 if (toKeep->databaseName)
357 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
358 if (!strcmp(toKeep->databaseName, "fault"))
361 rr->errstring = toKeep->databaseName;
363 if (!strcmp(toKeep->databaseName, "accept"))
368 Z_External *ext = odr_malloc (rr->stream, sizeof(*ext));
369 Z_IUOriginPartToKeep *keep =
370 odr_malloc (rr->stream, sizeof(*keep));
371 Z_IUTargetPart *targetPart =
372 odr_malloc (rr->stream, sizeof(*targetPart));
373 rr->taskPackage = odr_malloc (rr->stream, sizeof(*rr->taskPackage));
374 rr->taskPackage->packageType =
375 odr_oiddup (rr->stream, rr->esr->packageType);
376 rr->taskPackage->packageName = 0;
377 rr->taskPackage->userId = 0;
378 rr->taskPackage->retentionTime = 0;
379 rr->taskPackage->permissions = 0;
380 rr->taskPackage->description = 0;
381 rr->taskPackage->targetReference = (Odr_oct *)
382 odr_malloc (rr->stream, sizeof(Odr_oct));
383 rr->taskPackage->targetReference->buf =
384 odr_strdup (rr->stream, "123");
385 rr->taskPackage->targetReference->len =
386 rr->taskPackage->targetReference->size =
387 strlen(rr->taskPackage->targetReference->buf);
388 rr->taskPackage->creationDateTime = 0;
389 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
390 rr->taskPackage->packageDiagnostics = 0;
391 rr->taskPackage->taskSpecificParameters = ext;
393 ext->direct_reference =
394 odr_oiddup (rr->stream, rr->esr->packageType);
395 ext->indirect_reference = 0;
397 ext->which = Z_External_update;
398 ext->u.update = (Z_IUUpdate *)
399 odr_malloc (rr->stream, sizeof(*ext->u.update));
400 ext->u.update->which = Z_IUUpdate_taskPackage;
401 ext->u.update->u.taskPackage = (Z_IUUpdateTaskPackage *)
402 odr_malloc (rr->stream, sizeof(Z_IUUpdateTaskPackage));
403 ext->u.update->u.taskPackage->originPart = keep;
404 ext->u.update->u.taskPackage->targetPart = targetPart;
406 keep->action = odr_malloc (rr->stream, sizeof(int));
407 *keep->action = *toKeep->action;
409 odr_strdup (rr->stream, toKeep->databaseName);
411 keep->elementSetName = 0;
412 keep->actionQualifier = 0;
414 targetPart->updateStatus = odr_intdup (rr->stream, 1);
415 targetPart->num_globalDiagnostics = 0;
416 targetPart->globalDiagnostics = odr_nullval();
417 targetPart->num_taskPackageRecords = 0;
418 targetPart->taskPackageRecords = odr_nullval();
423 for (i = 0; i < notToKeep->num; i++)
425 Z_External *rec = notToKeep->elements[i]->record;
427 if (rec->direct_reference)
429 struct oident *oident;
430 oident = oid_getentbyoid(rec->direct_reference);
432 yaz_log (LOG_LOG, "record %d type %s", i,
437 case Z_External_sutrs:
438 if (rec->u.octet_aligned->len > 170)
439 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
443 yaz_log (LOG_LOG, "%d bytes:\n%s",
447 case Z_External_octet :
448 if (rec->u.octet_aligned->len > 170)
449 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
450 rec->u.octet_aligned->len,
451 rec->u.octet_aligned->buf);
453 yaz_log (LOG_LOG, "%d bytes\n%s",
454 rec->u.octet_aligned->len,
455 rec->u.octet_aligned->buf);
463 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
464 rr->esr->taskSpecificParameters->which);
470 int ztest_delete (void *handle, bend_delete_rr *rr)
472 if (rr->num_setnames == 1 && !strcmp (rr->setnames[0], "1"))
473 rr->delete_status = Z_DeleteStatus_success;
475 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
479 /* Our sort handler really doesn't sort... */
480 int ztest_sort (void *handle, bend_sort_rr *rr)
483 rr->sort_status = Z_SortStatus_success;
487 static int atoin (const char *buf, int n)
493 val = val*10 + (*buf - '0');
499 char *marc_read(FILE *inf, ODR odr)
505 if (fread (length_str, 1, 5, inf) != 5)
507 size = atoin (length_str, 5);
510 if (!(buf = (char*) odr_malloc (odr, size+1)))
512 if (fread (buf+5, 1, size-5, inf) != (size-5))
517 memcpy (buf, length_str, 5);
522 static char *dummy_database_record (int num, ODR odr)
524 FILE *inf = fopen ("dummy-records", "r");
533 buf = (char*) odr_malloc(odr, 2101);
534 memset(buf, 'A', 2100);
539 buf = marc_read (inf, odr);
549 static Z_GenericRecord *dummy_grs_record (int num, ODR o)
551 FILE *f = fopen("dummy-grs", "r");
553 Z_GenericRecord *r = 0;
558 while (fgets(line, 512, f))
559 if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
568 int ztest_fetch(void *handle, bend_fetch_rr *r)
573 r->basename = "DUMMY";
574 r->output_format = r->request_format;
575 if (r->request_format == VAL_SUTRS)
578 /* this section returns a huge record (for testing non-blocking write, etc) */
580 r->record = odr_malloc (r->stream, r->len);
581 memset (r->record, 'x', r->len);
583 /* this section returns a small record */
586 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
588 r->len = strlen(buf);
589 r->record = (char *) odr_malloc (r->stream, r->len+1);
590 strcpy(r->record, buf);
593 else if (r->request_format == VAL_GRS1)
596 r->record = (char*) dummy_grs_record(r->number, r->stream);
603 else if ((cp = dummy_database_record(r->number, r->stream)))
607 r->output_format = VAL_USMARC;
619 * silly dummy-scan what reads words from a file.
621 int ztest_scan(void *handle, bend_scan_rr *q)
624 static struct scan_entry list[200];
625 static char entries[200][80];
629 int term_position_req = q->term_position;
630 int num_entries_req = q->num_entries;
635 q->status = BEND_SCAN_SUCCESS;
636 if (!f && !(f = fopen("dummy-words", "r")))
638 perror("dummy-words");
641 if (q->term->term->which != Z_Term_general)
643 q->errcode = 229; /* unsupported term type */
646 if (*q->step_size != 0)
648 q->errcode = 205; /*Only zero step size supported for Scan */
651 if (q->term->term->u.general->len >= 80)
653 q->errcode = 11; /* term too long */
656 if (q->num_entries > 200)
661 memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
662 term[q->term->term->u.general->len] = '\0';
663 for (p = term; *p; p++)
667 fseek(f, 0, SEEK_SET);
670 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
671 i++, pos < 199 ? pos++ : (pos = 0))
673 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
675 if ((q->term_position = term_position_req) > i + 1)
677 q->term_position = i + 1;
678 q->status = BEND_SCAN_PARTIAL;
680 for (; q->num_entries < q->term_position; q->num_entries++)
684 po = pos - q->term_position + q->num_entries+1; /* find pos */
688 if (!strcmp (term, "SD") && q->num_entries == 2)
690 list[q->num_entries].term = entries[pos];
691 list[q->num_entries].occurrences = -1;
692 list[q->num_entries].errcode = 233;
693 list[q->num_entries].errstring = "SD for Scan Term";
697 list[q->num_entries].term = entries[po];
698 list[q->num_entries].occurrences = hits[po];
702 else if (q->num_entries)
704 list[q->num_entries].term = entries[pos];
705 list[q->num_entries].occurrences = hits[pos];
708 if (q->num_entries >= num_entries_req)
712 q->status = BEND_SCAN_PARTIAL;
716 bend_initresult *bend_init(bend_initrequest *q)
718 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
719 int *counter = (int *) xmalloc (sizeof(int));
724 r->handle = counter; /* user handle, in this case a simple int */
725 q->bend_sort = ztest_sort; /* register sort handler */
726 q->bend_search = ztest_search; /* register search handler */
727 q->bend_present = ztest_present; /* register present handle */
728 q->bend_esrequest = ztest_esrequest;
729 q->bend_delete = ztest_delete;
730 q->bend_fetch = ztest_fetch;
731 q->bend_scan = ztest_scan;
735 void bend_close(void *handle)
737 xfree (handle); /* release our user-defined handle */
741 int main(int argc, char **argv)
743 return statserv_main(argc, argv, bend_init, bend_close);