2 * Copyright (C) 1995-2000, Index Data
5 * $Id: zserver.c,v 1.83 2002-02-20 17:30:01 adam Exp $
18 #include <yaz/data1.h>
32 static int bend_sort (void *handle, bend_sort_rr *rr);
33 static int bend_delete (void *handle, bend_delete_rr *rr);
34 static int bend_esrequest (void *handle, bend_esrequest_rr *rr);
35 static int bend_segment (void *handle, bend_segment_rr *rr);
36 static int bend_search (void *handle, bend_search_rr *r);
37 static int bend_fetch (void *handle, bend_fetch_rr *r);
38 static int bend_scan (void *handle, bend_scan_rr *r);
40 bend_initresult *bend_init (bend_initrequest *q)
42 bend_initresult *r = (bend_initresult *)
43 odr_malloc (q->stream, sizeof(*r));
45 struct statserv_options_block *sob;
51 q->bend_sort = bend_sort;
52 q->bend_delete = bend_delete;
53 q->bend_esrequest = bend_esrequest;
54 q->bend_segment = bend_segment;
55 q->bend_search = bend_search;
56 q->bend_fetch = bend_fetch;
57 q->bend_scan = bend_scan;
60 q->implementation_name = "Z'mbol Information Server";
61 q->implementation_version = "Z'mbol " ZEBRAVER;
63 q->implementation_name = "Zebra Information Server";
64 q->implementation_version = "Zebra " ZEBRAVER;
67 logf (LOG_DEBUG, "bend_init");
69 sob = statserv_getcontrol ();
70 if (!(zh = zebra_open (sob->handle)))
72 logf (LOG_FATAL, "Failed to open Zebra `%s'", sob->configname);
78 if (q->auth->which == Z_IdAuthentication_open)
80 char *openpass = xstrdup (q->auth->u.open);
81 char *cp = strchr (openpass, '/');
85 user = nmem_strdup (odr_getmem (q->stream), openpass);
86 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
91 if (zebra_auth (zh->service, user, passwd))
102 int bend_search (void *handle, bend_search_rr *r)
104 ZebraHandle zh = (ZebraHandle) handle;
110 logf (LOG_LOG, "ResultSet '%s'", r->setname);
111 switch (r->query->which)
113 case Z_Query_type_1: case Z_Query_type_101:
114 zebra_search_rpn (zh, r->decode, r->stream, r->query->u.type_1,
115 r->num_bases, r->basenames, r->setname);
116 r->errcode = zh->errCode;
117 r->errstring = zh->errString;
122 r->errstring = "type-2";
130 int bend_fetch (void *handle, bend_fetch_rr *r)
132 ZebraHandle zh = (ZebraHandle) handle;
133 ZebraRetrievalRecord retrievalRecord;
135 retrievalRecord.position = r->number;
138 zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
139 r->request_format, 1, &retrievalRecord);
140 if (zh->errCode) /* non Surrogate Diagnostic */
142 r->errcode = zh->errCode;
143 r->errstring = zh->errString;
145 else if (retrievalRecord.errCode) /* Surrogate Diagnostic */
147 r->surrogate_flag = 1;
148 r->errcode = retrievalRecord.errCode;
149 r->errstring = retrievalRecord.errString;
150 r->basename = retrievalRecord.base;
152 else /* Database Record */
155 r->basename = retrievalRecord.base;
156 r->record = retrievalRecord.buf;
157 r->len = retrievalRecord.len;
158 r->output_format = retrievalRecord.format;
163 static int bend_scan (void *handle, bend_scan_rr *r)
165 ZebraScanEntry *entries;
166 ZebraHandle zh = (ZebraHandle) handle;
169 r->entries = (struct scan_entry *)
170 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
171 zebra_scan (zh, r->stream, r->term,
173 r->num_bases, r->basenames,
175 &r->num_entries, &entries, &is_partial);
177 r->status = BEND_SCAN_PARTIAL;
179 r->status = BEND_SCAN_SUCCESS;
180 for (i = 0; i < r->num_entries; i++)
182 r->entries[i].term = entries[i].term;
183 r->entries[i].occurrences = entries[i].occurrences;
185 r->errcode = zh->errCode;
186 r->errstring = zh->errString;
190 void bend_close (void *handle)
192 zebra_close ((ZebraHandle) handle);
193 xmalloc_trav("bend_close");
197 int bend_sort (void *handle, bend_sort_rr *rr)
199 ZebraHandle zh = (ZebraHandle) handle;
201 zebra_sort (zh, rr->stream,
202 rr->num_input_setnames, (const char **) rr->input_setnames,
203 rr->output_setname, rr->sort_sequence, &rr->sort_status);
204 rr->errcode = zh->errCode;
205 rr->errstring = zh->errString;
209 int bend_delete (void *handle, bend_delete_rr *rr)
211 ZebraHandle zh = (ZebraHandle) handle;
213 rr->delete_status = zebra_deleleResultSet(zh, rr->function,
214 rr->num_setnames, rr->setnames,
219 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
221 switch (r->toKeep->which)
223 case Z_ESAdminOriginPartToKeep_reIndex:
224 yaz_log(LOG_LOG, "adm-reindex");
226 case Z_ESAdminOriginPartToKeep_truncate:
227 yaz_log(LOG_LOG, "adm-truncate");
229 case Z_ESAdminOriginPartToKeep_drop:
230 yaz_log(LOG_LOG, "adm-drop");
232 case Z_ESAdminOriginPartToKeep_create:
233 yaz_log(LOG_LOG, "adm-create");
234 zebra_admin_create (zh, r->toKeep->databaseName);
236 case Z_ESAdminOriginPartToKeep_import:
237 yaz_log(LOG_LOG, "adm-import");
238 zebra_admin_import_begin (zh, r->toKeep->databaseName);
240 case Z_ESAdminOriginPartToKeep_refresh:
241 yaz_log(LOG_LOG, "adm-refresh");
243 case Z_ESAdminOriginPartToKeep_commit:
244 yaz_log(LOG_LOG, "adm-commit");
246 case Z_ESAdminOriginPartToKeep_shutdown:
247 yaz_log(LOG_LOG, "shutdown");
248 zebra_admin_shutdown(zh);
250 case Z_ESAdminOriginPartToKeep_start:
251 yaz_log(LOG_LOG, "start");
252 zebra_admin_start(zh);
255 yaz_log(LOG_LOG, "unknown admin");
258 if (r->toKeep->databaseName)
260 yaz_log(LOG_LOG, "database %s", r->toKeep->databaseName);
265 static int es_admin (ZebraHandle zh, Z_Admin *r)
269 case Z_Admin_esRequest:
270 es_admin_request (zh, r->u.esRequest);
272 case Z_Admin_taskPackage:
273 yaz_log (LOG_LOG, "adm taskpackage (unhandled)");
283 int bend_segment (void *handle, bend_segment_rr *rr)
285 ZebraHandle zh = (ZebraHandle) handle;
286 Z_Segment *segment = rr->segment;
288 if (segment->num_segmentRecords)
289 zebra_admin_import_segment (zh, rr->segment);
291 zebra_admin_import_end (zh);
295 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
297 ZebraHandle zh = (ZebraHandle) handle;
299 yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
300 if (rr->esr->packageName)
301 yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
302 yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
304 if (!rr->esr->taskSpecificParameters)
306 yaz_log (LOG_WARN, "No task specific parameters");
308 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
310 es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
311 rr->errcode = zh->errCode;
312 rr->errstring = zh->errString;
314 else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
316 Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
317 yaz_log (LOG_LOG, "Received ItemOrder");
321 case Z_IOItemOrder_esRequest:
323 case Z_ItemOrder_esRequest:
326 Z_IORequest *ir = it->u.esRequest;
327 Z_IOOriginPartToKeep *k = ir->toKeep;
328 Z_IOOriginPartNotToKeep *n = ir->notToKeep;
332 if (k->contact->name)
333 yaz_log(LOG_LOG, "contact name %s", k->contact->name);
334 if (k->contact->phone)
335 yaz_log(LOG_LOG, "contact phone %s", k->contact->phone);
336 if (k->contact->email)
337 yaz_log(LOG_LOG, "contact email %s", k->contact->email);
341 yaz_log(LOG_LOG, "Billing info (not shown)");
344 if (n->resultSetItem)
346 yaz_log(LOG_LOG, "resultsetItem");
347 yaz_log(LOG_LOG, "setId: %s", n->resultSetItem->resultSetId);
348 yaz_log(LOG_LOG, "item: %d", *n->resultSetItem->item);
353 Z_External *r = (Z_External*) n->itemRequest;
354 ILL_ItemRequest *item_req = 0;
355 ILL_Request *ill_req = 0;
356 if (r->direct_reference)
358 oident *ent = oid_getentbyoid(r->direct_reference);
360 yaz_log(LOG_LOG, "OID %s", ent->desc);
361 if (ent && ent->value == VAL_ISO_ILL_1)
363 yaz_log (LOG_LOG, "ItemRequest");
364 if (r->which == ODR_EXTERNAL_single)
366 odr_setbuf(rr->decode,
367 r->u.single_ASN1_type->buf,
368 r->u.single_ASN1_type->len, 0);
370 if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
373 "Couldn't decode ItemRequest %s near %d",
374 odr_errmsg(odr_geterror(rr->decode)),
375 odr_offset(rr->decode));
376 yaz_log(LOG_LOG, "PDU dump:");
377 odr_dumpBER(yaz_log_file(),
378 r->u.single_ASN1_type->buf,
379 r->u.single_ASN1_type->len);
383 ill_ItemRequest (rr->print, &item_req, 0,
385 odr_reset (rr->print);
388 if (!item_req && r->which == ODR_EXTERNAL_single)
390 yaz_log (LOG_LOG, "ILLRequest");
391 odr_setbuf(rr->decode,
392 r->u.single_ASN1_type->buf,
393 r->u.single_ASN1_type->len, 0);
395 if (!ill_Request (rr->decode, &ill_req, 0, 0))
398 "Couldn't decode ILLRequest %s near %d",
399 odr_errmsg(odr_geterror(rr->decode)),
400 odr_offset(rr->decode));
401 yaz_log(LOG_LOG, "PDU dump:");
402 odr_dumpBER(yaz_log_file(),
403 r->u.single_ASN1_type->buf,
404 r->u.single_ASN1_type->len);
408 ill_Request (rr->print, &ill_req, 0,
410 odr_reset (rr->print);
417 yaz_log (LOG_LOG, "ILL protocol version = %d",
418 *item_req->protocol_version_num);
426 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
428 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
429 yaz_log (LOG_LOG, "Received DB Update");
430 if (up->which == Z_IUUpdate_esRequest)
432 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
433 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
434 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
436 yaz_log (LOG_LOG, "action");
439 switch (*toKeep->action)
441 case Z_IUOriginPartToKeep_recordInsert:
442 yaz_log (LOG_LOG, "recordInsert");
444 case Z_IUOriginPartToKeep_recordReplace:
445 yaz_log (LOG_LOG, "recordUpdate");
447 case Z_IUOriginPartToKeep_recordDelete:
448 yaz_log (LOG_LOG, "recordDelete");
450 case Z_IUOriginPartToKeep_elementUpdate:
451 yaz_log (LOG_LOG, "elementUpdate");
453 case Z_IUOriginPartToKeep_specialUpdate:
454 yaz_log (LOG_LOG, "specialUpdate");
456 case Z_ESAdminOriginPartToKeep_shutdown:
457 yaz_log (LOG_LOG, "shutDown");
459 case Z_ESAdminOriginPartToKeep_start:
460 yaz_log (LOG_LOG, "start");
463 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
466 if (toKeep->databaseName)
468 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
469 if (!strcmp(toKeep->databaseName, "fault"))
472 rr->errstring = toKeep->databaseName;
474 if (!strcmp(toKeep->databaseName, "accept"))
480 for (i = 0; i < notToKeep->num; i++)
482 Z_External *rec = notToKeep->elements[i]->record;
484 if (rec->direct_reference)
486 struct oident *oident;
487 oident = oid_getentbyoid(rec->direct_reference);
489 yaz_log (LOG_LOG, "record %d type %s", i,
494 case Z_External_sutrs:
495 if (rec->u.octet_aligned->len > 170)
496 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
500 yaz_log (LOG_LOG, "%d bytes:\n%s",
504 case Z_External_octet:
505 if (rec->u.octet_aligned->len > 170)
506 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
507 rec->u.octet_aligned->len,
508 rec->u.octet_aligned->buf);
510 yaz_log (LOG_LOG, "%d bytes\n%s",
511 rec->u.octet_aligned->len,
512 rec->u.octet_aligned->buf);
520 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
521 rr->esr->taskSpecificParameters->which);
527 static void bend_start (struct statserv_options_block *sob)
534 char *pidfile = "zebrasrv.pid";
535 int fd = creat (pidfile, 0666);
537 logf (LOG_WARN|LOG_ERRNO, "creat %s", pidfile);
542 sprintf (pidstr, "%ld", (long) getpid ());
543 write (fd, pidstr, strlen(pidstr));
549 zebra_stop((ZebraService) sob->handle);
550 sob->handle = zebra_start(sob->configname);
553 static void bend_stop(struct statserv_options_block *sob)
557 ZebraService service = sob->handle;
562 int main (int argc, char **argv)
564 struct statserv_options_block *sob;
566 sob = statserv_getcontrol ();
567 strcpy (sob->configname, FNAME_CONFIG);
568 sob->bend_start = bend_start;
569 sob->bend_stop = bend_stop;
571 statserv_setcontrol (sob);
573 return statserv_main (argc, argv, bend_init, bend_close);