/*
- * Copyright (C) 1995-2006, Index Data ApS
+ * Copyright (C) 1995-2007, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: zoom-c.c,v 1.93 2006-10-26 15:34:46 adam Exp $
+ * $Id: zoom-c.c,v 1.110 2007-01-23 19:25:21 adam Exp $
*/
/**
* \file zoom-c.c
#include <yaz/cql.h>
#include <yaz/ccl.h>
-#if HAVE_SYS_TYPES_H
-#include <sys/types.h>
-#endif
-#if HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#if HAVE_SYS_POLL_H
-#include <sys/poll.h>
-#endif
-#if HAVE_SYS_SELECT_H
-#include <sys/select.h>
-#endif
-#ifdef WIN32
-#if FD_SETSIZE < 512
-#define FD_SETSIZE 512
-#endif
-#include <winsock.h>
-#endif
-
static int log_api = 0;
static int log_details = 0;
return event;
}
+ZOOM_API(int) ZOOM_connection_peek_event(ZOOM_connection c)
+{
+ ZOOM_Event event = c->m_queue_front;
+
+ return event ? event->kind : ZOOM_EVENT_NONE;
+}
static void set_dset_error(ZOOM_connection c, int error,
const char *dset,
}
}
+void ZOOM_connection_show_task(ZOOM_task task)
+{
+ switch(task->which)
+ {
+ case ZOOM_TASK_SEARCH:
+ yaz_log(YLOG_LOG, "search p=%p", task);
+ break;
+ case ZOOM_TASK_RETRIEVE:
+ yaz_log(YLOG_LOG, "retrieve p=%p", task);
+ break;
+ case ZOOM_TASK_CONNECT:
+ yaz_log(YLOG_LOG, "connect p=%p", task);
+ break;
+ case ZOOM_TASK_SCAN:
+ yaz_log(YLOG_LOG, "scant p=%p", task);
+ break;
+ }
+}
+
+void ZOOM_connection_show_tasks(ZOOM_connection c)
+{
+ ZOOM_task task;
+ yaz_log(YLOG_LOG, "connection p=%p tasks", c);
+ for (task = c->tasks; task; task = task->next)
+ ZOOM_connection_show_task(task);
+}
+
ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
{
ZOOM_task *taskp = &c->tasks;
c->proto = PROTO_Z3950;
c->cs = 0;
- c->mask = 0;
+ ZOOM_connection_set_mask(c, 0);
c->reconnect_ok = 0;
c->state = STATE_IDLE;
c->addinfo = 0;
/* set database names. Take local databases (if set); otherwise
take databases given in ZURL (if set); otherwise use Default */
static char **set_DatabaseNames(ZOOM_connection con, ZOOM_options options,
- int *num)
+ int *num, ODR odr)
{
char **databaseNames;
const char *cp = ZOOM_options_get(options, "databaseName");
}
if (!cp)
cp = "Default";
- nmem_strsplit(con->odr_out->mem, "+", cp, &databaseNames, num);
+ nmem_strsplit(odr->mem, "+", cp, &databaseNames, num);
return databaseNames;
}
else
c->lang = 0;
- val = ZOOM_options_get(c->options, "sru");
- c->sru_mode = get_sru_mode_from_string(val);
-
xfree(c->host_port);
if (portnum)
{
}
}
+ val = ZOOM_options_get(c->options, "sru");
+ c->sru_mode = get_sru_mode_from_string(val);
+
ZOOM_options_set(c->options, "host", c->host_port);
val = ZOOM_options_get(c->options, "cookie");
r->query = 0;
r->connection = 0;
r->next = 0;
+ r->databaseNames = 0;
+ r->num_databaseNames = 0;
return r;
}
cp = ZOOM_options_get(r->options, "schema");
if (cp)
r->schema = xstrdup(cp);
+
+ r->databaseNames = set_DatabaseNames(c, c->options, &r->num_databaseNames,
+ r->odr);
r->connection = c;
ZOOM_API(size_t)
ZOOM_resultset_size(ZOOM_resultset r)
{
+ yaz_log(log_details, "ZOOM_resultset_size r=%p count=%d",
+ r, r->size);
return r->size;
}
if (c->cs)
cs_close(c->cs);
c->cs = 0;
- c->mask = 0;
+ ZOOM_connection_set_mask(c, 0);
c->state = STATE_IDLE;
}
/* no init request for SRW .. */
assert(c->tasks->which == ZOOM_TASK_CONNECT);
ZOOM_connection_remove_task(c);
- c->mask = 0;
+ ZOOM_connection_set_mask(c, 0);
ZOOM_connection_exec_task(c);
}
c->state = STATE_ESTABLISHED;
}
else if (ret > 0)
{
- c->state = STATE_CONNECTING;
- c->mask = ZOOM_SELECT_EXCEPT;
+ int mask = ZOOM_SELECT_EXCEPT;
if (c->cs->io_pending & CS_WANT_WRITE)
- c->mask += ZOOM_SELECT_WRITE;
+ mask += ZOOM_SELECT_WRITE;
if (c->cs->io_pending & CS_WANT_READ)
- c->mask += ZOOM_SELECT_READ;
+ mask += ZOOM_SELECT_READ;
+ ZOOM_connection_set_mask(c, mask);
+ c->state = STATE_CONNECTING;
return zoom_pending;
}
}
return zoom_complete;
}
-int z3950_connection_socket(ZOOM_connection c)
-{
- if (c->cs)
- return cs_fileno(c->cs);
- return -1;
-}
-
-int z3950_connection_mask(ZOOM_connection c)
-{
- if (c->cs)
- return c->mask;
- return 0;
-}
-
static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
{
int i;
odr_prepend(c->odr_out, "ZOOM-C",
ireq->implementationName));
- version = odr_strdup(c->odr_out, "$Revision: 1.93 $");
+ version = odr_strdup(c->odr_out, "$Revision: 1.110 $");
if (strlen(version) > 10) /* check for unexpanded CVS strings */
version[strlen(version)-2] = '\0';
ireq->implementationVersion =
return zoom_complete;
}
- search_req->databaseNames =
- set_DatabaseNames(c, r->options, &search_req->num_databaseNames);
+ search_req->databaseNames = r->databaseNames;
+ search_req->num_databaseNames = r->num_databaseNames;
/* get syntax (no need to provide unless piggyback is in effect) */
syntax = c->tasks->u.search.syntax;
return buf;
}
+ZOOM_API(int)
+ ZOOM_record_error(ZOOM_record rec, const char **cp,
+ const char **addinfo, const char **diagset)
+{
+ Z_NamePlusRecord *npr;
+
+ if (!rec)
+ return 0;
+ npr = rec->npr;
+ if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
+ {
+ Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
+ int error = YAZ_BIB1_UNSPECIFIED_ERROR;
+ const char *add = 0;
+
+ if (diag_rec->which == Z_DiagRec_defaultFormat)
+ {
+ Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
+ int oclass;
+
+ error = *ddf->condition;
+ switch (ddf->which)
+ {
+ case Z_DefaultDiagFormat_v2Addinfo:
+ add = ddf->u.v2Addinfo;
+ break;
+ case Z_DefaultDiagFormat_v3Addinfo:
+ add = ddf->u.v3Addinfo;
+ break;
+ }
+ if (diagset)
+ *diagset = yaz_z3950oid_to_str(ddf->diagnosticSetId, &oclass);
+ }
+ else
+ {
+ if (diagset)
+ *diagset = "Bib-1";
+ }
+ if (addinfo)
+ *addinfo = add ? add : "";
+ if (cp)
+ *cp = diagbib1_str(error);
+ return error;
+ }
+ return 0;
+}
+
ZOOM_API(const char *)
ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
{
syntax, elementSetName);
}
*count -= i;
+ if (*count < 0)
+ *count = 0;
*start += i;
yaz_log(log_details,
"handle_records resultset=%p start=%d count=%d",
resultset = c->tasks->u.search.resultset;
+ if (sr->resultSetStatus)
+ {
+ ZOOM_options_set_int(resultset->options, "resultSetStatus",
+ *sr->resultSetStatus);
+ }
+ if (sr->presentStatus)
+ {
+ ZOOM_options_set_int(resultset->options, "presentStatus",
+ *sr->presentStatus);
+ }
handle_searchResult(c, resultset, sr->additionalSearchInfo);
resultset->size = *sr->resultCount;
count = &c->tasks->u.retrieve.count;
syntax = c->tasks->u.retrieve.syntax;
elementSetName = c->tasks->u.retrieve.elementSetName;
-
- if (*start >= resultset->size)
- {
- yaz_log(log_details, "%p send_present start=%d >= size=%d",
- c, *start, resultset->size);
- return zoom_complete;
- }
- if (*start + *count > resultset->size)
- *count = resultset->size - *start;
break;
default:
return zoom_complete;
yaz_log(log_details, "%p send_present start=%d count=%d",
c, *start, *count);
+ if (*start < 0 || *count < 0 || *start + *count > resultset->size)
+ {
+ set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
+ "", 0);
+ }
if (c->error) /* don't continue on error */
return zoom_complete;
- if (*start < 0)
- return zoom_complete;
yaz_log(log_details, "send_present resultset=%p start=%d count=%d",
resultset, *start, *count);
c, q, q->query_string);
abort();
}
-
+
scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
scan->connection = c;
scan->odr = odr_createmem(ODR_DECODE);
scan->termListAndStartPoint =
p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet, start);
xfree(freeme);
+
+ scan->databaseNames = set_DatabaseNames(c, c->options,
+ &scan->num_databaseNames,
+ scan->odr);
if (scan->termListAndStartPoint != 0)
{
ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
odr_intdup(c->odr_out,
ZOOM_options_get_int(scan->options, "stepSize", 0));
- req->databaseNames = set_DatabaseNames(c, scan->options,
- &req->num_databaseNames);
+ req->databaseNames = scan->databaseNames;
+ req->num_databaseNames = scan->num_databaseNames;
return send_APDU(c, apdu);
}
*req->u.esRequest->notToKeep->resultSetItem->item =
(str ? atoi(str) : 1);
}
- req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
+
+ str = ZOOM_options_get(p->options, "doc");
+ if (str)
+ req->u.esRequest->notToKeep->itemRequest =
+ z_ext_record(p->odr_out, VAL_TEXT_XML, str, strlen(str));
+ else
+ req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
return req;
}
Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
const char *first_db = "Default";
int num_db;
- char **db = set_DatabaseNames(p->connection, p->options, &num_db);
+ char **db = set_DatabaseNames(p->connection, p->options, &num_db,
+ p->odr_out);
if (num_db > 0)
first_db = db[0];
Z_APDU *apdu = 0;
const char *first_db = "Default";
int num_db;
- char **db = set_DatabaseNames(p->connection, p->options, &num_db);
+ char **db = set_DatabaseNames(p->connection, p->options, &num_db, p->odr_out);
const char *action = ZOOM_options_get(p->options, "action");
const char *recordIdOpaque = ZOOM_options_get(p->options, "recordIdOpaque");
const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
}
}
+
+static void set_init_option(const char *name, void *clientData) {
+ ZOOM_connection c = clientData;
+ char buf[80];
+
+ sprintf(buf, "init_opt_%.70s", name);
+ ZOOM_connection_option_set(c, buf, "1");
+}
+
+
static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
{
Z_InitResponse *initrs;
- c->mask = 0;
+ ZOOM_connection_set_mask(c, 0);
yaz_log(log_details, "%p recv_apdu apdu->which=%d", c, apdu->which);
switch(apdu->which)
{
ZOOM_connection_option_set(c, "targetImplementationVersion",
initrs->implementationVersion ?
initrs->implementationVersion : "");
+
+ /* Make initrs->options available as ZOOM-level options */
+ yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
+
if (!*initrs->result)
{
Z_External *uif = initrs->userInformationField;
"Content-Type");
const char *connection_head = z_HTTP_header_lookup(hres->headers,
"Connection");
- c->mask = 0;
+ ZOOM_connection_set_mask(c, 0);
yaz_log(log_details, "%p handle_http", c);
if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
Z_SOAP *soap_package = 0;
ODR o = c->odr_in;
Z_SOAP_Handler soap_handlers[2] = {
- {"http://www.loc.gov/zing/srw/", 0,
- (Z_SOAP_fun) yaz_srw_codec},
+ {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
{0, 0, 0}
};
ret = z_soap_codec(o, &soap_package,
}
else if (r == 1)
{
- c->mask = ZOOM_SELECT_EXCEPT;
+ int mask = ZOOM_SELECT_EXCEPT;
if (c->cs->io_pending & CS_WANT_WRITE)
- c->mask += ZOOM_SELECT_WRITE;
+ mask += ZOOM_SELECT_WRITE;
if (c->cs->io_pending & CS_WANT_READ)
- c->mask += ZOOM_SELECT_READ;
+ mask += ZOOM_SELECT_READ;
+ ZOOM_connection_set_mask(c, mask);
yaz_log(log_details, "%p do_write_ex write incomplete mask=%d",
c, c->mask);
}
else
{
- c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
+ ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
yaz_log(log_details, "%p do_write_ex write complete mask=%d",
c, c->mask);
}
"cs_rcvconnect returned %d", c, ret);
if (ret == 1)
{
- c->mask = ZOOM_SELECT_EXCEPT;
+ int mask = ZOOM_SELECT_EXCEPT;
if (c->cs->io_pending & CS_WANT_WRITE)
- c->mask += ZOOM_SELECT_WRITE;
+ mask += ZOOM_SELECT_WRITE;
if (c->cs->io_pending & CS_WANT_READ)
- c->mask += ZOOM_SELECT_READ;
+ mask += ZOOM_SELECT_READ;
+ ZOOM_connection_set_mask(c, mask);
}
else if (ret == 0)
{
/* no init request for SRW .. */
assert(c->tasks->which == ZOOM_TASK_CONNECT);
ZOOM_connection_remove_task(c);
- c->mask = 0;
+ ZOOM_connection_set_mask(c, 0);
ZOOM_connection_exec_task(c);
}
c->state = STATE_ESTABLISHED;
return cs->last_event;
}
-ZOOM_API(int)
- ZOOM_event(int no, ZOOM_connection *cs)
-{
- int timeout = 30; /* default timeout in seconds */
- int timeout_set = 0; /* whether it was overriden at all */
-#if HAVE_SYS_POLL_H
- struct pollfd pollfds[1024];
- ZOOM_connection poll_cs[1024];
-#else
- struct timeval tv;
- fd_set input, output, except;
-#endif
- int i, r, nfds;
- int max_fd = 0;
-
- yaz_log(log_details, "ZOOM_event(no=%d,cs=%p)", no, cs);
- for (i = 0; i<no; i++)
- {
- ZOOM_connection c = cs[i];
- ZOOM_Event event;
- if (c && (event = ZOOM_connection_get_event(c)))
- {
- ZOOM_Event_destroy(event);
- return i+1;
- }
- }
- for (i = 0; i<no; i++)
- {
- ZOOM_connection c = cs[i];
- if (c)
- {
- ZOOM_Event event;
- ZOOM_connection_exec_task(c);
- if ((event = ZOOM_connection_get_event(c)))
- {
- ZOOM_Event_destroy(event);
- return i+1;
- }
- }
- }
-#if HAVE_SYS_POLL_H
-
-#else
- FD_ZERO(&input);
- FD_ZERO(&output);
- FD_ZERO(&except);
-#endif
- nfds = 0;
- for (i = 0; i<no; i++)
- {
- ZOOM_connection c = cs[i];
- int fd, mask;
- int this_timeout;
-
- if (!c)
- continue;
- fd = z3950_connection_socket(c);
- mask = z3950_connection_mask(c);
-
- if (fd == -1)
- continue;
- if (max_fd < fd)
- max_fd = fd;
-
- /* -1 is used for indefinite timeout (no timeout), so -2 here. */
- this_timeout = ZOOM_options_get_int(c->options, "timeout", -2);
- if (this_timeout != -2)
- {
- /* ensure the minimum timeout is used */
- if (!timeout_set)
- timeout = this_timeout;
- else if (this_timeout != -1 && this_timeout < timeout)
- timeout = this_timeout;
- timeout_set = 1;
- }
-#if HAVE_SYS_POLL_H
- if (mask)
- {
- short poll_events = 0;
-
- if (mask & ZOOM_SELECT_READ)
- poll_events += POLLIN;
- if (mask & ZOOM_SELECT_WRITE)
- poll_events += POLLOUT;
- if (mask & ZOOM_SELECT_EXCEPT)
- poll_events += POLLERR;
- pollfds[nfds].fd = fd;
- pollfds[nfds].events = poll_events;
- pollfds[nfds].revents = 0;
- poll_cs[nfds] = c;
- nfds++;
- }
-#else
- if (mask & ZOOM_SELECT_READ)
- {
- FD_SET(fd, &input);
- nfds++;
- }
- if (mask & ZOOM_SELECT_WRITE)
- {
- FD_SET(fd, &output);
- nfds++;
- }
- if (mask & ZOOM_SELECT_EXCEPT)
- {
- FD_SET(fd, &except);
- nfds++;
- }
-#endif
- }
- if (!nfds)
- return 0;
-
-#if HAVE_SYS_POLL_H
- r = poll(pollfds, nfds, (timeout == -1 ? -1 : timeout * 1000));
- for (i = 0; i<nfds; i++)
- {
- ZOOM_connection c = poll_cs[i];
- if (r && c->mask)
- {
- int mask = 0;
- if (pollfds[i].revents & POLLIN)
- mask += ZOOM_SELECT_READ;
- if (pollfds[i].revents & POLLOUT)
- mask += ZOOM_SELECT_WRITE;
- if (pollfds[i].revents & POLLERR)
- mask += ZOOM_SELECT_EXCEPT;
- if (mask)
- ZOOM_connection_do_io(c, mask);
- }
- else if (r == 0 && c->mask)
- {
- ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
- /* timeout and this connection was waiting */
- set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
- do_close(c);
- ZOOM_connection_put_event(c, event);
- }
- }
-#else
- tv.tv_sec = timeout;
- tv.tv_usec = 0;
- r = select(max_fd+1, &input, &output, &except, (timeout == -1 ? 0 : &tv));
- for (i = 0; i<no; i++)
- {
- ZOOM_connection c = cs[i];
- int fd, mask;
-
- if (!c)
- continue;
- fd = z3950_connection_socket(c);
- mask = 0;
- if (r && c->mask)
- {
- /* no timeout and real socket */
- if (FD_ISSET(fd, &input))
- mask += ZOOM_SELECT_READ;
- if (FD_ISSET(fd, &output))
- mask += ZOOM_SELECT_WRITE;
- if (FD_ISSET(fd, &except))
- mask += ZOOM_SELECT_EXCEPT;
- if (mask)
- ZOOM_connection_do_io(c, mask);
- }
- if (r == 0 && c->mask)
- {
- ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
- /* timeout and this connection was waiting */
- set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
- do_close(c);
- ZOOM_connection_put_event(c, event);
- }
- }
-#endif
- for (i = 0; i<no; i++)
- {
- ZOOM_connection c = cs[i];
- ZOOM_Event event;
- if (c && (event = ZOOM_connection_get_event(c)))
- {
- ZOOM_Event_destroy(event);
- return i+1;
- }
- }
- return 0;
-}
-
/*
* Returns an xmalloc()d string containing RPN that corresponds to the
return xstrdup(pqfbuf);
}
+ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
+{
+ if (c->mask)
+ {
+ ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
+ /* timeout and this connection was waiting */
+ set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
+ do_close(c);
+ ZOOM_connection_put_event(c, event);
+ }
+ return 0;
+}
+
+ZOOM_API(int)
+ ZOOM_connection_process(ZOOM_connection c)
+{
+ ZOOM_Event event;
+ if (!c)
+ return 0;
+
+ event = ZOOM_connection_get_event(c);
+ if (event)
+ {
+ ZOOM_Event_destroy(event);
+ return 1;
+ }
+ ZOOM_connection_exec_task(c);
+ event = ZOOM_connection_get_event(c);
+ if (event)
+ {
+ ZOOM_Event_destroy(event);
+ return 1;
+ }
+ return 0;
+}
+
+ZOOM_API(int)
+ ZOOM_event_nonblock(int no, ZOOM_connection *cs)
+{
+ int i;
+
+ yaz_log(log_details, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
+
+ for (i = 0; i<no; i++)
+ {
+ ZOOM_connection c = cs[i];
+
+ if (c && ZOOM_connection_process(c))
+ return i+1;
+ }
+ return 0;
+}
+
+ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
+{
+ if (c->mask && mask)
+ ZOOM_connection_do_io(c, mask);
+ return 0;
+}
+
+ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
+{
+ if (c->cs)
+ return cs_fileno(c->cs);
+ return -1;
+}
+
+ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
+{
+ c->mask = mask;
+ if (!c->cs)
+ return -1;
+ return 0;
+}
+
+ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
+{
+ if (c->cs)
+ return c->mask;
+ return 0;
+}
+
+ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
+{
+ return ZOOM_options_get_int(c->options, "timeout", 30);
+}
+
/*
* Local variables:
* c-basic-offset: 4