2 * Copyright (C) 1995-2002, Index Data
5 * $Id: zserver.c,v 1.87 2002-04-05 08:46:26 adam Exp $
18 #include <yaz/data1.h>
25 static int bend_sort (void *handle, bend_sort_rr *rr);
26 static int bend_delete (void *handle, bend_delete_rr *rr);
27 static int bend_esrequest (void *handle, bend_esrequest_rr *rr);
28 static int bend_segment (void *handle, bend_segment_rr *rr);
29 static int bend_search (void *handle, bend_search_rr *r);
30 static int bend_fetch (void *handle, bend_fetch_rr *r);
31 static int bend_scan (void *handle, bend_scan_rr *r);
33 bend_initresult *bend_init (bend_initrequest *q)
35 bend_initresult *r = (bend_initresult *)
36 odr_malloc (q->stream, sizeof(*r));
38 struct statserv_options_block *sob;
44 q->bend_sort = bend_sort;
45 q->bend_delete = bend_delete;
46 q->bend_esrequest = bend_esrequest;
47 q->bend_segment = bend_segment;
48 q->bend_search = bend_search;
49 q->bend_fetch = bend_fetch;
50 q->bend_scan = bend_scan;
53 q->implementation_name = "Z'mbol Information Server";
54 q->implementation_version = "Z'mbol " ZEBRAVER;
56 q->implementation_name = "Zebra Information Server";
57 q->implementation_version = "Zebra " ZEBRAVER;
60 logf (LOG_DEBUG, "bend_init");
62 sob = statserv_getcontrol ();
63 if (!(zh = zebra_open (sob->handle)))
65 yaz_log (LOG_FATAL, "Failed to open config `%s'", sob->configname);
71 if (q->auth->which == Z_IdAuthentication_open)
73 char *openpass = xstrdup (q->auth->u.open);
74 char *cp = strchr (openpass, '/');
78 user = nmem_strdup (odr_getmem (q->stream), openpass);
79 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
84 if (zebra_auth (zh, user, passwd))
95 static void search_terms (ZebraHandle zh, bend_search_rr *r)
100 struct Z_External *ext;
101 Z_SearchInfoReport *sr;
103 /* get no of terms for result set */
104 zebra_resultSetTerms (zh, r->setname, -1, &count, &no_terms);
108 r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
110 r->search_info->num_elements = 1;
111 r->search_info->list =
112 odr_malloc (r->stream, sizeof(*r->search_info->list));
113 r->search_info->list[0] =
114 odr_malloc (r->stream, sizeof(**r->search_info->list));
115 r->search_info->list[0]->category = 0;
116 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
117 ext = odr_malloc (r->stream, sizeof(*ext));
118 r->search_info->list[0]->information.externallyDefinedInfo = ext;
119 ext->direct_reference =
120 yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
121 ext->indirect_reference = 0;
123 ext->which = Z_External_searchResult1;
124 sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
125 ext->u.searchResult1 = sr;
127 sr->elements = odr_malloc (r->stream, sr->num *
128 sizeof(*sr->elements));
129 for (i = 0; i<no_terms; i++)
132 const char *termz = zebra_resultSetTerms (zh, r->setname, i,
135 sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
136 sr->elements[i]->subqueryId = 0;
137 sr->elements[i]->fullQuery = odr_malloc (r->stream,
139 *sr->elements[i]->fullQuery = 0;
140 sr->elements[i]->subqueryExpression =
141 odr_malloc (r->stream, sizeof(Z_QueryExpression));
142 sr->elements[i]->subqueryExpression->which =
143 Z_QueryExpression_term;
144 sr->elements[i]->subqueryExpression->u.term =
145 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
146 term = odr_malloc (r->stream, sizeof(Z_Term));
147 sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
149 term->which = Z_Term_general;
150 term->u.general = odr_malloc (r->stream, sizeof(Odr_oct));
151 term->u.general->buf = odr_strdup (r->stream, termz);
153 term->u.general->len = strlen (termz);
154 term->u.general->size = strlen (termz);
156 sr->elements[i]->subqueryExpression->u.term->termComment = 0;
157 sr->elements[i]->subqueryInterpretation = 0;
158 sr->elements[i]->subqueryRecommendation = 0;
159 sr->elements[i]->subqueryCount = odr_intdup (r->stream, count);
160 sr->elements[i]->subqueryWeight = 0;
161 sr->elements[i]->resultsByDB = 0;
165 int bend_search (void *handle, bend_search_rr *r)
167 ZebraHandle zh = (ZebraHandle) handle;
173 if (zebra_select_databases (zh, r->num_bases,
174 (const char **) r->basenames))
176 zebra_result (zh, &r->errcode, &r->errstring);
179 logf (LOG_LOG, "ResultSet '%s'", r->setname);
180 switch (r->query->which)
182 case Z_Query_type_1: case Z_Query_type_101:
183 zebra_search_rpn (zh, r->decode, r->stream, r->query->u.type_1,
184 r->setname, &r->hits);
185 zebra_result (zh, &r->errcode, &r->errstring);
187 search_terms (zh, r);
191 r->errstring = "type-2";
200 int bend_fetch (void *handle, bend_fetch_rr *r)
202 ZebraHandle zh = (ZebraHandle) handle;
203 ZebraRetrievalRecord retrievalRecord;
205 retrievalRecord.position = r->number;
208 zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
209 r->request_format, 1, &retrievalRecord);
210 zebra_result (zh, &r->errcode, &r->errstring);
211 /* non Surrogate Diagnostic OR Surrogate Diagnostic */
212 if (r->errcode == 0 && retrievalRecord.errCode)
214 r->surrogate_flag = 1;
215 r->errcode = retrievalRecord.errCode;
216 r->errstring = retrievalRecord.errString;
217 r->basename = retrievalRecord.base;
219 else if (r->errcode == 0) /* Database Record */
222 r->basename = retrievalRecord.base;
223 r->record = retrievalRecord.buf;
224 r->len = retrievalRecord.len;
225 r->output_format = retrievalRecord.format;
230 static int bend_scan (void *handle, bend_scan_rr *r)
232 ZebraScanEntry *entries;
233 ZebraHandle zh = (ZebraHandle) handle;
236 if (zebra_select_databases (zh, r->num_bases,
237 (const char **) r->basenames))
239 zebra_result (zh, &r->errcode, &r->errstring);
242 r->entries = (struct scan_entry *)
243 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
244 zebra_scan (zh, r->stream, r->term,
247 &r->num_entries, &entries, &is_partial);
249 r->status = BEND_SCAN_PARTIAL;
251 r->status = BEND_SCAN_SUCCESS;
252 for (i = 0; i < r->num_entries; i++)
254 r->entries[i].term = entries[i].term;
255 r->entries[i].occurrences = entries[i].occurrences;
257 zebra_result (zh, &r->errcode, &r->errstring);
261 void bend_close (void *handle)
263 zebra_close ((ZebraHandle) handle);
264 xmalloc_trav("bend_close");
268 int bend_sort (void *handle, bend_sort_rr *rr)
270 ZebraHandle zh = (ZebraHandle) handle;
272 zebra_sort (zh, rr->stream,
273 rr->num_input_setnames, (const char **) rr->input_setnames,
274 rr->output_setname, rr->sort_sequence, &rr->sort_status);
275 zebra_result (zh, &rr->errcode, &rr->errstring);
279 int bend_delete (void *handle, bend_delete_rr *rr)
281 ZebraHandle zh = (ZebraHandle) handle;
283 rr->delete_status = zebra_deleleResultSet(zh, rr->function,
284 rr->num_setnames, rr->setnames,
289 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
291 switch (r->toKeep->which)
293 case Z_ESAdminOriginPartToKeep_reIndex:
294 yaz_log(LOG_LOG, "adm-reindex");
296 case Z_ESAdminOriginPartToKeep_truncate:
297 yaz_log(LOG_LOG, "adm-truncate");
299 case Z_ESAdminOriginPartToKeep_drop:
300 yaz_log(LOG_LOG, "adm-drop");
302 case Z_ESAdminOriginPartToKeep_create:
303 yaz_log(LOG_LOG, "adm-create");
304 zebra_admin_create (zh, r->toKeep->databaseName);
306 case Z_ESAdminOriginPartToKeep_import:
307 yaz_log(LOG_LOG, "adm-import");
308 zebra_admin_import_begin (zh, r->toKeep->databaseName);
310 case Z_ESAdminOriginPartToKeep_refresh:
311 yaz_log(LOG_LOG, "adm-refresh");
313 case Z_ESAdminOriginPartToKeep_commit:
314 yaz_log(LOG_LOG, "adm-commit");
316 case Z_ESAdminOriginPartToKeep_shutdown:
317 yaz_log(LOG_LOG, "shutdown");
318 zebra_admin_shutdown(zh);
320 case Z_ESAdminOriginPartToKeep_start:
321 yaz_log(LOG_LOG, "start");
322 zebra_admin_start(zh);
325 yaz_log(LOG_LOG, "unknown admin");
327 if (r->toKeep->databaseName)
329 yaz_log(LOG_LOG, "database %s", r->toKeep->databaseName);
334 static int es_admin (ZebraHandle zh, Z_Admin *r)
338 case Z_Admin_esRequest:
339 es_admin_request (zh, r->u.esRequest);
341 case Z_Admin_taskPackage:
342 yaz_log (LOG_LOG, "adm taskpackage (unhandled)");
351 int bend_segment (void *handle, bend_segment_rr *rr)
353 ZebraHandle zh = (ZebraHandle) handle;
354 Z_Segment *segment = rr->segment;
356 if (segment->num_segmentRecords)
357 zebra_admin_import_segment (zh, rr->segment);
359 zebra_admin_import_end (zh);
363 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
365 ZebraHandle zh = (ZebraHandle) handle;
367 yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
368 if (rr->esr->packageName)
369 yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
370 yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
372 if (!rr->esr->taskSpecificParameters)
374 yaz_log (LOG_WARN, "No task specific parameters");
376 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
378 es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
380 zebra_result (zh, &rr->errcode, &rr->errstring);
382 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
384 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
385 yaz_log (LOG_LOG, "Received ItemOrder");
389 case Z_IOItemOrder_esRequest:
391 case Z_ItemOrder_esRequest:
394 Z_IORequest *ir = it->u.esRequest;
395 Z_IOOriginPartToKeep *k = ir->toKeep;
396 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
400 if (k->contact->name)
401 yaz_log(LOG_LOG, "contact name %s", k->contact->name);
402 if (k->contact->phone)
403 yaz_log(LOG_LOG, "contact phone %s", k->contact->phone);
404 if (k->contact->email)
405 yaz_log(LOG_LOG, "contact email %s", k->contact->email);
409 yaz_log(LOG_LOG, "Billing info (not shown)");
412 if (n->resultSetItem)
414 yaz_log(LOG_LOG, "resultsetItem");
415 yaz_log(LOG_LOG, "setId: %s", n->resultSetItem->resultSetId);
416 yaz_log(LOG_LOG, "item: %d", *n->resultSetItem->item);
421 Z_External *r = (Z_External*) n->itemRequest;
422 ILL_ItemRequest *item_req = 0;
423 ILL_Request *ill_req = 0;
424 if (r->direct_reference)
426 oident *ent = oid_getentbyoid(r->direct_reference);
428 yaz_log(LOG_LOG, "OID %s", ent->desc);
429 if (ent && ent->value == VAL_ISO_ILL_1)
431 yaz_log (LOG_LOG, "ItemRequest");
432 if (r->which == ODR_EXTERNAL_single)
434 odr_setbuf(rr->decode,
435 r->u.single_ASN1_type->buf,
436 r->u.single_ASN1_type->len, 0);
438 if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
441 "Couldn't decode ItemRequest %s near %d",
442 odr_errmsg(odr_geterror(rr->decode)),
443 odr_offset(rr->decode));
444 yaz_log(LOG_LOG, "PDU dump:");
445 odr_dumpBER(yaz_log_file(),
446 r->u.single_ASN1_type->buf,
447 r->u.single_ASN1_type->len);
451 ill_ItemRequest (rr->print, &item_req, 0,
453 odr_reset (rr->print);
456 if (!item_req && r->which == ODR_EXTERNAL_single)
458 yaz_log (LOG_LOG, "ILLRequest");
459 odr_setbuf(rr->decode,
460 r->u.single_ASN1_type->buf,
461 r->u.single_ASN1_type->len, 0);
463 if (!ill_Request (rr->decode, &ill_req, 0, 0))
466 "Couldn't decode ILLRequest %s near %d",
467 odr_errmsg(odr_geterror(rr->decode)),
468 odr_offset(rr->decode));
469 yaz_log(LOG_LOG, "PDU dump:");
470 odr_dumpBER(yaz_log_file(),
471 r->u.single_ASN1_type->buf,
472 r->u.single_ASN1_type->len);
476 ill_Request (rr->print, &ill_req, 0,
478 odr_reset (rr->print);
485 yaz_log (LOG_LOG, "ILL protocol version = %d",
486 *item_req->protocol_version_num);
494 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
496 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
497 yaz_log (LOG_LOG, "Received DB Update");
498 if (up->which == Z_IUUpdate_esRequest)
500 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
501 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
502 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
504 yaz_log (LOG_LOG, "action");
507 switch (*toKeep->action)
509 case Z_IUOriginPartToKeep_recordInsert:
510 yaz_log (LOG_LOG, "recordInsert");
512 case Z_IUOriginPartToKeep_recordReplace:
513 yaz_log (LOG_LOG, "recordUpdate");
515 case Z_IUOriginPartToKeep_recordDelete:
516 yaz_log (LOG_LOG, "recordDelete");
518 case Z_IUOriginPartToKeep_elementUpdate:
519 yaz_log (LOG_LOG, "elementUpdate");
521 case Z_IUOriginPartToKeep_specialUpdate:
522 yaz_log (LOG_LOG, "specialUpdate");
524 case Z_ESAdminOriginPartToKeep_shutdown:
525 yaz_log (LOG_LOG, "shutDown");
527 case Z_ESAdminOriginPartToKeep_start:
528 yaz_log (LOG_LOG, "start");
531 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
534 if (toKeep->databaseName)
536 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
537 if (!strcmp(toKeep->databaseName, "fault"))
540 rr->errstring = toKeep->databaseName;
542 if (!strcmp(toKeep->databaseName, "accept"))
548 for (i = 0; i < notToKeep->num; i++)
550 Z_External *rec = notToKeep->elements[i]->record;
552 if (rec->direct_reference)
554 struct oident *oident;
555 oident = oid_getentbyoid(rec->direct_reference);
557 yaz_log (LOG_LOG, "record %d type %s", i,
562 case Z_External_sutrs:
563 if (rec->u.octet_aligned->len > 170)
564 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
568 yaz_log (LOG_LOG, "%d bytes:\n%s",
572 case Z_External_octet:
573 if (rec->u.octet_aligned->len > 170)
574 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
575 rec->u.octet_aligned->len,
576 rec->u.octet_aligned->buf);
578 yaz_log (LOG_LOG, "%d bytes\n%s",
579 rec->u.octet_aligned->len,
580 rec->u.octet_aligned->buf);
588 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
589 rr->esr->taskSpecificParameters->which);
595 static void bend_start (struct statserv_options_block *sob)
602 char *pidfile = "zebrasrv.pid";
603 int fd = creat (pidfile, 0666);
605 logf (LOG_WARN|LOG_ERRNO, "creat %s", pidfile);
610 sprintf (pidstr, "%ld", (long) getpid ());
611 write (fd, pidstr, strlen(pidstr));
617 zebra_stop((ZebraService) sob->handle);
618 sob->handle = zebra_start(sob->configname);
621 static void bend_stop(struct statserv_options_block *sob)
625 ZebraService service = sob->handle;
630 int main (int argc, char **argv)
632 struct statserv_options_block *sob;
634 sob = statserv_getcontrol ();
635 strcpy (sob->configname, "zebra.cfg");
636 sob->bend_start = bend_start;
637 sob->bend_stop = bend_stop;
639 statserv_setcontrol (sob);
641 return statserv_main (argc, argv, bend_init, bend_close);