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.45 2001-10-29 09:17:19 adam
10 * New function marc_display_exl - used by YAZ client. Server returns
11 * bad record on position 98 (for testing).
13 * Revision 1.44 2001/07/19 19:51:42 adam
14 * Added typecasts to make C++ happy.
16 * Revision 1.43 2001/06/28 09:27:25 adam
17 * Number of Extended Services Requests logged.
19 * Revision 1.42 2001/04/06 12:26:46 adam
20 * Optional CCL module. Moved atoi_n to marcdisp.h from yaz-util.h.
22 * Revision 1.41 2001/04/05 13:08:48 adam
23 * New configure options: --enable-module.
25 * Revision 1.40 2001/03/25 21:55:13 adam
26 * Added odr_intdup. Ztest server returns TaskPackage for ItemUpdate.
28 * Revision 1.39 2001/03/12 14:40:57 adam
29 * Minor change of print of item update info.
31 * Revision 1.38 2001/02/21 13:46:54 adam
34 * Revision 1.37 2001/02/20 11:25:32 adam
35 * Added ill_get_APDU and ill_get_Cancel.
37 * Revision 1.36 2001/01/30 21:34:18 adam
38 * Added step-size for Scan backend interface.
40 * Revision 1.35 2000/11/23 10:58:33 adam
41 * SSL comstack support. Separate POSIX thread support library.
43 * Revision 1.34 2000/09/04 08:58:15 adam
44 * Added prefix yaz_ for most logging utility functions.
46 * Revision 1.33 2000/08/10 08:41:26 adam
49 * Revision 1.32 2000/04/05 07:39:55 adam
50 * Added shared library support (libtool).
52 * Revision 1.31 2000/01/31 13:15:21 adam
53 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
54 * that some characters are not surrounded by spaces in resulting term.
57 * Revision 1.30 2000/01/13 23:05:50 adam
58 * Fixed tagging for member requester-CHECKED-IN in ILL ASN.1 spec.
60 * Revision 1.29 2000/01/12 14:36:07 adam
61 * Added printing stream (ODR) for backend functions.
63 * Revision 1.28 1999/12/16 23:36:19 adam
64 * Implemented ILL protocol. Minor updates ASN.1 compiler.
66 * Revision 1.27 1999/11/30 13:47:12 adam
67 * Improved installation. Moved header files to include/yaz.
69 * Revision 1.26 1999/08/27 09:40:32 adam
70 * Renamed logf function to yaz_log. Removed VC++ project files.
72 * Revision 1.25 1999/06/01 14:29:12 adam
73 * Work on Extended Services.
75 * Revision 1.24 1999/05/27 13:07:54 adam
78 * Revision 1.23 1999/05/27 13:02:20 adam
79 * Assigned OID for old DB Update (VAL_DBUPDATE0).
81 * Revision 1.22 1999/05/26 13:49:12 adam
82 * DB Update implemented in client (very basic).
84 * Revision 1.21 1998/12/15 12:45:42 adam
87 * Revision 1.20 1998/12/14 14:48:05 adam
88 * Fixed memory leak - happened when fetching MARC records.
90 * Revision 1.19 1998/10/20 15:16:22 adam
91 * Minor change to prevent warning.
93 * Revision 1.18 1998/10/20 15:13:45 adam
94 * Minor fix regarding output for Item Order.
96 * Revision 1.17 1998/10/18 22:33:35 quinn
97 * Added diagnostic dump of Item Order Eservice.
99 * Revision 1.16 1998/10/15 08:26:23 adam
100 * Added type cast to make C++ happy.
102 * Revision 1.15 1998/10/13 20:05:57 adam
105 * Revision 1.14 1998/10/13 16:12:25 adam
106 * Added support for Surrogate Diagnostics for Scan Term entries.
108 * Revision 1.13 1998/08/19 16:10:09 adam
109 * Changed som member names of DeleteResultSetRequest/Response.
111 * Revision 1.12 1998/07/20 12:38:44 adam
112 * Implemented delete result set service to server API.
114 * Revision 1.11 1998/06/09 13:55:08 adam
117 * Revision 1.10 1998/05/27 16:55:54 adam
120 * Revision 1.9 1998/03/31 11:07:45 adam
121 * Furhter work on UNIverse resource report.
122 * Added Extended Services handling in frontend server.
124 * Revision 1.8 1998/02/11 11:53:36 adam
125 * Changed code so that it compiles as C++.
127 * Revision 1.7 1998/02/10 11:03:57 adam
128 * Added support for extended handlers in backend server interface.
130 * Revision 1.6 1998/01/29 13:16:02 adam
131 * Added dummy sort in test server.
133 * Revision 1.5 1997/11/07 13:31:58 adam
134 * Added NT Service name part of statserv_options_block. Moved NT
135 * service utility to server library.
137 * Revision 1.4 1997/09/17 12:10:43 adam
140 * Revision 1.3 1997/09/09 10:10:20 adam
141 * Another MSV5.0 port. Changed projects to include proper
142 * library/include paths.
143 * Server starts server in test-mode when no options are given.
145 * Revision 1.2 1997/09/04 13:50:31 adam
151 * Demonstration of simple server
158 #include <yaz/backend.h>
165 Z_GenericRecord *read_grs1(FILE *f, ODR o);
167 int ztest_search (void *handle, bend_search_rr *rr);
168 int ztest_sort (void *handle, bend_sort_rr *rr);
169 int ztest_present (void *handle, bend_present_rr *rr);
170 int ztest_esrequest (void *handle, bend_esrequest_rr *rr);
171 int ztest_delete (void *handle, bend_delete_rr *rr);
173 int ztest_search (void *handle, bend_search_rr *rr)
175 rr->hits = rand() % 22;
179 int ztest_present (void *handle, bend_present_rr *rr)
184 int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
186 /* user-defined handle - created in bend_init */
187 int *counter = (int*) handle;
189 yaz_log(LOG_LOG, "ESRequest no %d", *counter);
193 if (rr->esr->packageName)
194 yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
195 yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
198 yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
200 if (!rr->esr->taskSpecificParameters)
202 yaz_log (LOG_WARN, "No task specific parameters");
204 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
206 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
207 yaz_log (LOG_LOG, "Received ItemOrder");
211 case Z_IOItemOrder_esRequest:
213 case Z_ItemOrder_esRequest:
216 Z_IORequest *ir = it->u.esRequest;
217 Z_IOOriginPartToKeep *k = ir->toKeep;
218 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
222 if (k->contact->name)
223 yaz_log(LOG_LOG, "contact name %s", k->contact->name);
224 if (k->contact->phone)
225 yaz_log(LOG_LOG, "contact phone %s", k->contact->phone);
226 if (k->contact->email)
227 yaz_log(LOG_LOG, "contact email %s", k->contact->email);
231 yaz_log(LOG_LOG, "Billing info (not shown)");
234 if (n->resultSetItem)
236 yaz_log(LOG_LOG, "resultsetItem");
237 yaz_log(LOG_LOG, "setId: %s", n->resultSetItem->resultSetId);
238 yaz_log(LOG_LOG, "item: %d", *n->resultSetItem->item);
243 Z_External *r = (Z_External*) n->itemRequest;
244 ILL_ItemRequest *item_req = 0;
245 ILL_APDU *ill_apdu = 0;
246 if (r->direct_reference)
248 oident *ent = oid_getentbyoid(r->direct_reference);
250 yaz_log(LOG_LOG, "OID %s", ent->desc);
251 if (ent && ent->value == VAL_TEXT_XML)
253 yaz_log (LOG_LOG, "ILL XML request");
254 if (r->which == Z_External_octet)
255 yaz_log (LOG_LOG, "%.*s", r->u.octet_aligned->len,
256 r->u.octet_aligned->buf);
258 if (ent && ent->value == VAL_ISO_ILL_1)
260 yaz_log (LOG_LOG, "Decode ItemRequest begin");
261 if (r->which == ODR_EXTERNAL_single)
263 odr_setbuf(rr->decode,
264 (char *) r->u.single_ASN1_type->buf,
265 r->u.single_ASN1_type->len, 0);
267 if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
270 "Couldn't decode ItemRequest %s near %d",
271 odr_errmsg(odr_geterror(rr->decode)),
272 odr_offset(rr->decode));
274 yaz_log(LOG_LOG, "PDU dump:");
275 odr_dumpBER(yaz_log_file(),
276 r->u.single_ASN1_type->buf,
277 r->u.single_ASN1_type->len);
281 yaz_log(LOG_LOG, "Decode ItemRequest OK");
284 ill_ItemRequest (rr->print, &item_req, 0,
286 odr_reset (rr->print);
289 if (!item_req && r->which == ODR_EXTERNAL_single)
291 yaz_log (LOG_LOG, "Decode ILL APDU begin");
292 odr_setbuf(rr->decode,
293 (char*) r->u.single_ASN1_type->buf,
294 r->u.single_ASN1_type->len, 0);
296 if (!ill_APDU (rr->decode, &ill_apdu, 0, 0))
299 "Couldn't decode ILL APDU %s near %d",
300 odr_errmsg(odr_geterror(rr->decode)),
301 odr_offset(rr->decode));
302 yaz_log(LOG_LOG, "PDU dump:");
303 odr_dumpBER(yaz_log_file(),
304 (char *) r->u.single_ASN1_type->buf,
305 r->u.single_ASN1_type->len);
308 yaz_log(LOG_LOG, "Decode ILL APDU OK");
311 ill_APDU (rr->print, &ill_apdu, 0,
313 odr_reset (rr->print);
320 yaz_log (LOG_LOG, "ILL protocol version = %d",
321 *item_req->protocol_version_num);
329 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
331 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
332 yaz_log (LOG_LOG, "Received DB Update");
333 if (up->which == Z_IUUpdate_esRequest)
335 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
336 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
337 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
339 yaz_log (LOG_LOG, "action");
342 switch (*toKeep->action)
344 case Z_IUOriginPartToKeep_recordInsert:
345 yaz_log (LOG_LOG, " recordInsert");
347 case Z_IUOriginPartToKeep_recordReplace:
348 yaz_log (LOG_LOG, " recordReplace");
350 case Z_IUOriginPartToKeep_recordDelete:
351 yaz_log (LOG_LOG, " recordDelete");
353 case Z_IUOriginPartToKeep_elementUpdate:
354 yaz_log (LOG_LOG, " elementUpdate");
356 case Z_IUOriginPartToKeep_specialUpdate:
357 yaz_log (LOG_LOG, " specialUpdate");
360 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
363 if (toKeep->databaseName)
365 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
366 if (!strcmp(toKeep->databaseName, "fault"))
369 rr->errstring = toKeep->databaseName;
371 if (!strcmp(toKeep->databaseName, "accept"))
376 Z_External *ext = (Z_External *)
377 odr_malloc (rr->stream, sizeof(*ext));
378 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
379 odr_malloc (rr->stream, sizeof(*keep));
380 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
381 odr_malloc (rr->stream, sizeof(*targetPart));
382 rr->taskPackage = (Z_TaskPackage *)
383 odr_malloc (rr->stream, sizeof(*rr->taskPackage));
384 rr->taskPackage->packageType =
385 odr_oiddup (rr->stream, rr->esr->packageType);
386 rr->taskPackage->packageName = 0;
387 rr->taskPackage->userId = 0;
388 rr->taskPackage->retentionTime = 0;
389 rr->taskPackage->permissions = 0;
390 rr->taskPackage->description = 0;
391 rr->taskPackage->targetReference = (Odr_oct *)
392 odr_malloc (rr->stream, sizeof(Odr_oct));
393 rr->taskPackage->targetReference->buf =
394 (unsigned char *) odr_strdup (rr->stream, "123");
395 rr->taskPackage->targetReference->len =
396 rr->taskPackage->targetReference->size =
397 strlen((char *) (rr->taskPackage->targetReference->buf));
398 rr->taskPackage->creationDateTime = 0;
399 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
400 rr->taskPackage->packageDiagnostics = 0;
401 rr->taskPackage->taskSpecificParameters = ext;
403 ext->direct_reference =
404 odr_oiddup (rr->stream, rr->esr->packageType);
405 ext->indirect_reference = 0;
407 ext->which = Z_External_update;
408 ext->u.update = (Z_IUUpdate *)
409 odr_malloc (rr->stream, sizeof(*ext->u.update));
410 ext->u.update->which = Z_IUUpdate_taskPackage;
411 ext->u.update->u.taskPackage = (Z_IUUpdateTaskPackage *)
412 odr_malloc (rr->stream, sizeof(Z_IUUpdateTaskPackage));
413 ext->u.update->u.taskPackage->originPart = keep;
414 ext->u.update->u.taskPackage->targetPart = targetPart;
416 keep->action = (int *) odr_malloc (rr->stream, sizeof(int));
417 *keep->action = *toKeep->action;
419 odr_strdup (rr->stream, toKeep->databaseName);
421 keep->elementSetName = 0;
422 keep->actionQualifier = 0;
424 targetPart->updateStatus = odr_intdup (rr->stream, 1);
425 targetPart->num_globalDiagnostics = 0;
426 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
427 targetPart->num_taskPackageRecords = 0;
428 targetPart->taskPackageRecords =
429 (Z_IUTaskPackageRecordStructure **) odr_nullval();
434 for (i = 0; i < notToKeep->num; i++)
436 Z_External *rec = notToKeep->elements[i]->record;
438 if (rec->direct_reference)
440 struct oident *oident;
441 oident = oid_getentbyoid(rec->direct_reference);
443 yaz_log (LOG_LOG, "record %d type %s", i,
448 case Z_External_sutrs:
449 if (rec->u.octet_aligned->len > 170)
450 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
454 yaz_log (LOG_LOG, "%d bytes:\n%s",
458 case Z_External_octet :
459 if (rec->u.octet_aligned->len > 170)
460 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
461 rec->u.octet_aligned->len,
462 rec->u.octet_aligned->buf);
464 yaz_log (LOG_LOG, "%d bytes\n%s",
465 rec->u.octet_aligned->len,
466 rec->u.octet_aligned->buf);
474 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
475 rr->esr->taskSpecificParameters->which);
481 int ztest_delete (void *handle, bend_delete_rr *rr)
483 if (rr->num_setnames == 1 && !strcmp (rr->setnames[0], "1"))
484 rr->delete_status = Z_DeleteStatus_success;
486 rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
490 /* Our sort handler really doesn't sort... */
491 int ztest_sort (void *handle, bend_sort_rr *rr)
494 rr->sort_status = Z_SortStatus_success;
498 static int atoin (const char *buf, int n)
504 val = val*10 + (*buf - '0');
510 char *marc_read(FILE *inf, ODR odr)
516 if (fread (length_str, 1, 5, inf) != 5)
518 size = atoin (length_str, 5);
521 if (!(buf = (char*) odr_malloc (odr, size+1)))
523 if (fread (buf+5, 1, size-5, inf) != (size-5))
528 memcpy (buf, length_str, 5);
533 static char *dummy_database_record (int num, ODR odr)
535 FILE *inf = fopen ("dummy-records", "r");
541 { /* this will generate a very bad MARC record (testing only) */
542 buf = (char*) odr_malloc(odr, 2101);
543 memset(buf, '7', 2100);
548 /* OK, try to get proper MARC records from the file */
551 buf = marc_read (inf, odr);
560 static Z_GenericRecord *dummy_grs_record (int num, ODR o)
562 FILE *f = fopen("dummy-grs", "r");
564 Z_GenericRecord *r = 0;
569 while (fgets(line, 512, f))
570 if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
579 int ztest_fetch(void *handle, bend_fetch_rr *r)
584 r->basename = "DUMMY";
585 r->output_format = r->request_format;
586 if (r->request_format == VAL_SUTRS)
589 /* this section returns a huge record (for testing non-blocking write, etc) */
591 r->record = odr_malloc (r->stream, r->len);
592 memset (r->record, 'x', r->len);
594 /* this section returns a small record */
597 sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
599 r->len = strlen(buf);
600 r->record = (char *) odr_malloc (r->stream, r->len+1);
601 strcpy(r->record, buf);
604 else if (r->request_format == VAL_GRS1)
607 r->record = (char*) dummy_grs_record(r->number, r->stream);
614 else if ((cp = dummy_database_record(r->number, r->stream)))
618 r->output_format = VAL_USMARC;
630 * silly dummy-scan what reads words from a file.
632 int ztest_scan(void *handle, bend_scan_rr *q)
635 static struct scan_entry list[200];
636 static char entries[200][80];
640 int term_position_req = q->term_position;
641 int num_entries_req = q->num_entries;
646 q->status = BEND_SCAN_SUCCESS;
647 if (!f && !(f = fopen("dummy-words", "r")))
649 perror("dummy-words");
652 if (q->term->term->which != Z_Term_general)
654 q->errcode = 229; /* unsupported term type */
657 if (*q->step_size != 0)
659 q->errcode = 205; /*Only zero step size supported for Scan */
662 if (q->term->term->u.general->len >= 80)
664 q->errcode = 11; /* term too long */
667 if (q->num_entries > 200)
672 memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
673 term[q->term->term->u.general->len] = '\0';
674 for (p = term; *p; p++)
678 fseek(f, 0, SEEK_SET);
681 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
682 i++, pos < 199 ? pos++ : (pos = 0))
684 if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
686 if ((q->term_position = term_position_req) > i + 1)
688 q->term_position = i + 1;
689 q->status = BEND_SCAN_PARTIAL;
691 for (; q->num_entries < q->term_position; q->num_entries++)
695 po = pos - q->term_position + q->num_entries+1; /* find pos */
699 if (!strcmp (term, "SD") && q->num_entries == 2)
701 list[q->num_entries].term = entries[pos];
702 list[q->num_entries].occurrences = -1;
703 list[q->num_entries].errcode = 233;
704 list[q->num_entries].errstring = "SD for Scan Term";
708 list[q->num_entries].term = entries[po];
709 list[q->num_entries].occurrences = hits[po];
713 else if (q->num_entries)
715 list[q->num_entries].term = entries[pos];
716 list[q->num_entries].occurrences = hits[pos];
719 if (q->num_entries >= num_entries_req)
723 q->status = BEND_SCAN_PARTIAL;
727 bend_initresult *bend_init(bend_initrequest *q)
729 bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
730 int *counter = (int *) xmalloc (sizeof(int));
735 r->handle = counter; /* user handle, in this case a simple int */
736 q->bend_sort = ztest_sort; /* register sort handler */
737 q->bend_search = ztest_search; /* register search handler */
738 q->bend_present = ztest_present; /* register present handle */
739 q->bend_esrequest = ztest_esrequest;
740 q->bend_delete = ztest_delete;
741 q->bend_fetch = ztest_fetch;
742 q->bend_scan = ztest_scan;
746 void bend_close(void *handle)
748 xfree (handle); /* release our user-defined handle */
752 int main(int argc, char **argv)
754 return statserv_main(argc, argv, bend_init, bend_close);