/*
- * $Id: zoom-c.c,v 1.18 2002-01-03 12:18:38 adam Exp $
+ * $Id: zoom-c.c,v 1.24 2002-02-28 13:21:16 adam Exp $
*
* ZOOM layer for C, connections, result sets, queries.
*/
static ZOOM_Event ZOOM_Event_create (int kind)
{
- ZOOM_Event event = xmalloc (sizeof(*event));
+ ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event));
event->kind = kind;
event->next = 0;
event->prev = 0;
static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
{
- // put in back of queue
if (c->m_queue_back)
{
c->m_queue_back->prev = event;
static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
{
- // get from front of queue
ZOOM_Event event = c->m_queue_front;
if (!event)
return 0;
static void clear_error (ZOOM_connection c)
{
- c->error = ZOOM_ERROR_NONE;
- xfree (c->addinfo);
- c->addinfo = 0;
+
+ switch (c->error)
+ {
+ case ZOOM_ERROR_CONNECT:
+ case ZOOM_ERROR_MEMORY:
+ case ZOOM_ERROR_DECODE:
+ case ZOOM_ERROR_CONNECTION_LOST:
+ case ZOOM_ERROR_INIT:
+ case ZOOM_ERROR_INTERNAL:
+ break;
+ default:
+ c->error = ZOOM_ERROR_NONE;
+ xfree (c->addinfo);
+ c->addinfo = 0;
+ }
}
ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
ZOOM_task *taskp = &c->tasks;
while (*taskp)
taskp = &(*taskp)->next;
- *taskp = xmalloc (sizeof(**taskp));
+ *taskp = (ZOOM_task) xmalloc (sizeof(**taskp));
(*taskp)->running = 0;
(*taskp)->which = which;
(*taskp)->next = 0;
ZOOM_connection ZOOM_connection_create (ZOOM_options options)
{
- ZOOM_connection c = xmalloc (sizeof(*c));
+ ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
c->cs = 0;
c->mask = 0;
}
else
cp = "Default";
- databaseNames = odr_malloc (con->odr_out, no * sizeof(*databaseNames));
+ databaseNames = (char**)
+ odr_malloc (con->odr_out, no * sizeof(*databaseNames));
no = 0;
while (*cp)
{
}
/* cp ptr to first char of db name, c is char
following db name */
- databaseNames[no] = odr_malloc (con->odr_out, 1+c-cp);
+ databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
memcpy (databaseNames[no], cp, c-cp);
databaseNames[no++][c-cp] = '\0';
cp = c;
ZOOM_options_set(c->options, "host", c->host_port);
c->async = ZOOM_options_get_bool (c->options, "async", 0);
-
+
+ c->error = ZOOM_ERROR_NONE;
+
task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
if (!c->async)
ZOOM_query ZOOM_query_create(void)
{
- ZOOM_query s = xmalloc (sizeof(*s));
+ ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
s->refcount = 1;
s->query = 0;
int ZOOM_query_prefix(ZOOM_query s, const char *str)
{
- s->query = odr_malloc (s->odr, sizeof(*s->query));
+ s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query));
s->query->which = Z_Query_type_1;
s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
if (!s->query->u.type_1)
}
ZOOM_resultset ZOOM_resultset_create ()
{
- ZOOM_resultset r = xmalloc (sizeof(*r));
+ ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
r->refcount = 1;
r->size = 0;
yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
if (r->refcount == 0)
{
+ ZOOM_record_cache rc;
+
+ for (rc = r->record_cache; rc; rc = rc->next)
+ if (rc->rec.wrbuf_marc)
+ wrbuf_free (rc->rec.wrbuf_marc, 1);
if (r->connection)
{
/* remove ourselves from the resultsets in connection */
if (ret >= 0)
{
c->state = STATE_CONNECTING;
- c->mask = ZOOM_SELECT_READ | ZOOM_SELECT_WRITE |
- ZOOM_SELECT_EXCEPT;
+ c->mask = ZOOM_SELECT_EXCEPT;
+ if (c->cs->io_pending & CS_WANT_WRITE)
+ c->mask += ZOOM_SELECT_WRITE;
+ if (c->cs->io_pending & CS_WANT_READ)
+ c->mask += ZOOM_SELECT_READ;
return 1;
}
}
const char *impname;
Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
Z_InitRequest *ireq = apdu->u.initRequest;
- Z_IdAuthentication *auth = odr_malloc(c->odr_out, sizeof(*auth));
+ Z_IdAuthentication *auth = (Z_IdAuthentication *)
+ odr_malloc(c->odr_out, sizeof(*auth));
const char *auth_groupId = ZOOM_options_get (c->options, "group");
const char *auth_userId = ZOOM_options_get (c->options, "user");
const char *auth_password = ZOOM_options_get (c->options, "pass");
impname = ZOOM_options_get (c->options, "implementationName");
ireq->implementationName =
- odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
+ (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
strcpy (ireq->implementationName, "");
if (impname)
{
if (auth_groupId || auth_password)
{
- Z_IdPass *pass = odr_malloc(c->odr_out, sizeof(*pass));
+ Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
int i = 0;
pass->groupId = 0;
if (auth_groupId && *auth_groupId)
{
- pass->groupId = odr_malloc(c->odr_out, strlen(auth_groupId)+1);
+ pass->groupId = (char *)
+ odr_malloc(c->odr_out, strlen(auth_groupId)+1);
strcpy(pass->groupId, auth_groupId);
i++;
}
pass->userId = 0;
if (auth_userId && *auth_userId)
{
- pass->userId = odr_malloc(c->odr_out, strlen(auth_userId)+1);
+ pass->userId = (char *)
+ odr_malloc(c->odr_out, strlen(auth_userId)+1);
strcpy(pass->userId, auth_userId);
i++;
}
pass->password = 0;
if (auth_password && *auth_password)
{
- pass->password = odr_malloc(c->odr_out, strlen(auth_password)+1);
+ pass->password = (char *)
+ odr_malloc(c->odr_out, strlen(auth_password)+1);
strcpy(pass->password, auth_password);
i++;
}
else if (auth_userId)
{
auth->which = Z_IdAuthentication_open;
- auth->u.open = odr_malloc(c->odr_out, strlen(auth_userId)+1);
+ auth->u.open = (char *)
+ odr_malloc(c->odr_out, strlen(auth_userId)+1);
strcpy(auth->u.open, auth_userId);
ireq->idAuthentication = auth;
}
}
if (smallSetElementSetName && *smallSetElementSetName)
{
- Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
+ Z_ElementSetNames *esn = (Z_ElementSetNames *)
+ odr_malloc (c->odr_out, sizeof(*esn));
esn->which = Z_ElementSetNames_generic;
esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
}
if (mediumSetElementSetName && *mediumSetElementSetName)
{
- Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
+ Z_ElementSetNames *esn = (Z_ElementSetNames *)
+ odr_malloc (c->odr_out, sizeof(*esn));
esn->which = Z_ElementSetNames_generic;
esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
return 0;
buf = odr_getbuf (odr_enc, &size, 0);
- nrec = xmalloc (sizeof(*nrec));
+ nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
nrec->odr = odr_createmem(ODR_DECODE);
nrec->wrbuf_marc = 0;
odr_setbuf (nrec->odr, buf, size, 0);
xfree (rec);
}
-void *ZOOM_record_get (ZOOM_record rec, const char *type, size_t *len)
+const char *ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
{
Z_NamePlusRecord *npr;
if (!rec)
}
return "none";
}
- else if (!strcmp (type, "render"))
+ else if (!strcmp (type, "render") &&
+ npr->which == Z_NamePlusRecord_databaseRecord)
+ {
+ Z_External *r = (Z_External *) npr->u.databaseRecord;
+ oident *ent = oid_getentbyoid(r->direct_reference);
+
+ if (r->which == Z_External_sutrs)
+ {
+ *len = r->u.sutrs->len;
+ return (const char *) r->u.sutrs->buf;
+ }
+ else if (r->which == Z_External_octet)
+ {
+ switch (ent->value)
+ {
+ case VAL_SOIF:
+ case VAL_HTML:
+ case VAL_SUTRS:
+ break;
+ case VAL_TEXT_XML:
+ case VAL_APPLICATION_XML:
+ break;
+ default:
+ if (!rec->wrbuf_marc)
+ rec->wrbuf_marc = wrbuf_alloc();
+ wrbuf_rewind (rec->wrbuf_marc);
+ if (yaz_marc_decode ((const char *)
+ r->u.octet_aligned->buf,
+ rec->wrbuf_marc, 0,
+ r->u.octet_aligned->len,
+ 0) > 0)
+ {
+ *len = wrbuf_len(rec->wrbuf_marc);
+ return wrbuf_buf(rec->wrbuf_marc);
+ }
+ }
+ *len = r->u.octet_aligned->len;
+ return (const char *) r->u.octet_aligned->buf;
+ }
+ else if (r->which == Z_External_grs1)
+ {
+ *len = 5;
+ return "GRS-1";
+ }
+ return 0;
+ }
+ else if (!strcmp (type, "xml") &&
+ npr->which == Z_NamePlusRecord_databaseRecord)
+ {
+ Z_External *r = (Z_External *) npr->u.databaseRecord;
+ oident *ent = oid_getentbyoid(r->direct_reference);
+
+ if (r->which == Z_External_sutrs)
+ {
+ *len = r->u.sutrs->len;
+ return (const char *) r->u.sutrs->buf;
+ }
+ else if (r->which == Z_External_octet)
+ {
+ switch (ent->value)
+ {
+ case VAL_SOIF:
+ case VAL_HTML:
+ case VAL_SUTRS:
+ break;
+ case VAL_TEXT_XML:
+ case VAL_APPLICATION_XML:
+ break;
+ default:
+ if (!rec->wrbuf_marc)
+ rec->wrbuf_marc = wrbuf_alloc();
+ wrbuf_rewind (rec->wrbuf_marc);
+ if (yaz_marc_decode ((const char *)
+ r->u.octet_aligned->buf,
+ rec->wrbuf_marc, 0,
+ r->u.octet_aligned->len,
+ 1) > 0)
+ {
+ *len = wrbuf_len(rec->wrbuf_marc);
+ return wrbuf_buf(rec->wrbuf_marc);
+ }
+ }
+ *len = r->u.octet_aligned->len;
+ return (const char *) r->u.octet_aligned->buf;
+ }
+ else if (r->which == Z_External_grs1)
+ {
+ *len = 5;
+ return "GRS-1";
+ }
+ return 0;
+ }
+ else if (!strcmp (type, "raw"))
{
if (npr->which == Z_NamePlusRecord_databaseRecord)
{
Z_External *r = (Z_External *) npr->u.databaseRecord;
- oident *ent = oid_getentbyoid(r->direct_reference);
if (r->which == Z_External_sutrs)
{
*len = r->u.sutrs->len;
- return r->u.sutrs->buf;
+ return (const char *) r->u.sutrs->buf;
}
else if (r->which == Z_External_octet)
{
- switch (ent->value)
- {
- case VAL_SOIF:
- case VAL_HTML:
- case VAL_SUTRS:
- break;
- case VAL_TEXT_XML:
- case VAL_APPLICATION_XML:
- break;
- default:
- if (!rec->wrbuf_marc)
- rec->wrbuf_marc = wrbuf_alloc();
- if (marc_display_wrbuf (r->u.octet_aligned->buf,
- rec->wrbuf_marc, 0,
- r->u.octet_aligned->len) > 0)
- {
- *len = wrbuf_len(rec->wrbuf_marc);
- return wrbuf_buf(rec->wrbuf_marc);
- }
- }
*len = r->u.octet_aligned->len;
- return r->u.octet_aligned->buf;
+ return (const char *) r->u.octet_aligned->buf;
}
- else if (r->which == Z_External_grs1)
+ else /* grs-1, explain, ... */
{
- *len = 5;
- return "GRS-1";
+ *len = -1;
+ return (const char *) npr->u.databaseRecord;
}
}
return 0;
}
- else if (!strcmp (type, "raw"))
- {
- if (npr->which == Z_NamePlusRecord_databaseRecord)
- {
- *len = -1;
- return (Z_External *) npr->u.databaseRecord;
- }
- return 0;
- }
return 0;
}
return;
}
}
-
}
- rc = odr_malloc (r->odr, sizeof(*rc));
+ rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
rc->rec.npr = npr;
rc->rec.odr = 0;
rc->rec.wrbuf_marc = 0;
if (schema && *schema)
{
- Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
+ Z_RecordComposition *compo = (Z_RecordComposition *)
+ odr_malloc (c->odr_out, sizeof(*compo));
req->recordComposition = compo;
compo->which = Z_RecordComp_complex;
}
else if (element && *element)
{
- Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
- Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
+ Z_ElementSetNames *esn = (Z_ElementSetNames *)
+ odr_malloc (c->odr_out, sizeof(*esn));
+ Z_RecordComposition *compo = (Z_RecordComposition *)
+ odr_malloc (c->odr_out, sizeof(*compo));
esn->which = Z_ElementSetNames_generic;
esn->u.generic = odr_strdup (c->odr_out, element);
ZOOM_scanset ZOOM_connection_scan (ZOOM_connection c, const char *start)
{
- ZOOM_scanset scan = xmalloc (sizeof(*scan));
+ ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
scan->connection = c;
scan->odr = odr_createmem (ODR_DECODE);
}
const char *ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
- int *occ, size_t *len)
+ int *occ, int *len)
{
const char *term = 0;
size_t noent = ZOOM_scanset_size (scan);
if (t->term->which == Z_Term_general)
{
- term = t->term->u.general->buf;
+ term = (const char *) t->term->u.general->buf;
*len = t->term->u.general->len;
}
*occ = t->globalOccurrences ? *t->globalOccurrences : 0;
return 1;
}
else if (r == 1)
- {
- c->state = STATE_ESTABLISHED;
- c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_WRITE|ZOOM_SELECT_EXCEPT;
+ {
+ c->mask = ZOOM_SELECT_EXCEPT;
+ if (c->cs->io_pending & CS_WANT_WRITE)
+ c->mask += ZOOM_SELECT_WRITE;
+ if (c->cs->io_pending & CS_WANT_READ)
+ c->mask += ZOOM_SELECT_READ;
+ yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
}
else
{
- c->state = STATE_ESTABLISHED;
- c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
+ c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
+ yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
}
return 0;
}
int ZOOM_connection_do_io(ZOOM_connection c, int mask)
{
ZOOM_Event event = 0;
-#if 0
int r = cs_look(c->cs);
- yaz_log (LOG_LOG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
+ yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
c, mask, r);
if (r == CS_NONE)
{
- event = ZOOM_Event_create (ZOOM_EVENT_IO_CONNECT);
+ event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
c->error = ZOOM_ERROR_CONNECT;
do_close (c);
ZOOM_connection_put_event (c, event);
}
else if (r == CS_CONNECT)
{
- event = ZOOM_Event_create (ZOOM_EVENT_IO_CONNECT);
- yaz_log (LOG_LOG, "calling rcvconnect");
- if (cs_rcvconnect (c->cs) < 0)
- {
- c->error = ZOOM_ERROR_CONNECT;
- do_close (c);
- ZOOM_connection_put_event (c, event);
- }
- else
+ int ret;
+ event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
+
+ ret = cs_rcvconnect (c->cs);
+ yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
+ if (ret == 1)
{
+ c->mask = ZOOM_SELECT_EXCEPT;
+ if (c->cs->io_pending & CS_WANT_WRITE)
+ c->mask += ZOOM_SELECT_WRITE;
+ if (c->cs->io_pending & CS_WANT_READ)
+ c->mask += ZOOM_SELECT_READ;
ZOOM_connection_put_event (c, event);
- ZOOM_connection_send_init (c);
}
- }
- else
- {
- if (mask & ZOOM_SELECT_READ)
- do_read (c);
- if (c->cs && (mask & ZOOM_SELECT_WRITE))
- do_write (c);
- }
-#else
- yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d", c, mask);
- if (c->state == STATE_CONNECTING)
- {
- event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
- if (mask & ZOOM_SELECT_WRITE)
+ else if (ret == 0)
{
ZOOM_connection_put_event (c, event);
- ZOOM_connection_send_init (c);
+ ZOOM_connection_send_init (c);
+ c->state = STATE_ESTABLISHED;
}
- else
- {
- c->error = ZOOM_ERROR_CONNECT;
- do_close (c);
+ else
+ {
+ c->error = ZOOM_ERROR_CONNECT;
+ do_close (c);
ZOOM_connection_put_event (c, event);
- }
- }
- else if (c->state == STATE_ESTABLISHED)
- {
- if (mask & ZOOM_SELECT_READ)
- do_read (c);
- if (c->cs && (mask & ZOOM_SELECT_WRITE))
- do_write (c);
+ }
}
else
{
- event = ZOOM_Event_create (ZOOM_EVENT_UNKNOWN);
- ZOOM_connection_put_event (c, event);
- c->error = ZOOM_ERROR_INTERNAL;
- do_close (c);
+ if (mask & ZOOM_SELECT_READ)
+ do_read (c);
+ if (c->cs && (mask & ZOOM_SELECT_WRITE))
+ do_write (c);
}
-#endif
return 1;
}
if (!nfds)
return 0;
#if HAVE_SYS_POLL_H
- yaz_log (LOG_DEBUG, "poll start");
r = poll (pollfds, nfds, 15000);
- yaz_log (LOG_DEBUG, "poll stop, returned r=%d", r);
for (i = 0; i<nfds; i++)
{
ZOOM_connection c = poll_cs[i];