/*
- * Copyright (c) 2000-2004, Index Data
+ * Copyright (C) 1995-2005, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: zoom-c.c,v 1.27 2004-04-28 22:44:59 adam Exp $
- *
- * ZOOM layer for C, connections, result sets, queries.
+ * $Id: zoom-c.c,v 1.37 2005-01-16 22:01:13 adam Exp $
+ */
+/**
+ * \file zoom-c.c
+ * \brief Implements ZOOM C interface.
*/
+
#include <assert.h>
#include <string.h>
#include "zoom-p.h"
#include <yaz/ill.h>
#include <yaz/srw.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
+#include <winsock.h>
+#endif
+
+static int log_level = 0;
+static int log_level_initialized = 0;
typedef enum {
zoom_pending,
const char *addinfo, const char *addinfo2)
{
char *cp;
+ if (!log_level_initialized)
+ {
+ log_level = yaz_log_module_level("zoom");
+ log_level_initialized = 1;
+ }
+
xfree (c->addinfo);
c->addinfo = 0;
c->error = error;
}
if (addinfo && addinfo2)
{
- c->addinfo = xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
+ c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
strcpy(c->addinfo, addinfo);
strcat(c->addinfo, addinfo2);
}
else if (addinfo)
c->addinfo = xstrdup(addinfo);
if (error)
- yaz_log(LOG_DEBUG, "Error %s %s:%d %s %s",
+ yaz_log(log_level, "Error %s %s:%d %s %s",
c->host_port ? c->host_port : "<>", dset, error,
addinfo ? addinfo : "",
addinfo2 ? addinfo2 : "");
{
ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
+ if (!log_level_initialized)
+ {
+ log_level = yaz_log_module_level("zoom");
+ log_level_initialized = 1;
+ }
+
c->proto = PROTO_Z3950;
c->cs = 0;
c->mask = 0;
const char *val;
ZOOM_task task;
+ if (!log_level_initialized)
+ {
+ log_level = yaz_log_module_level("zoom");
+ log_level_initialized = 1;
+ }
+
if (c->cs)
{
- yaz_log (LOG_DEBUG, "reconnect");
+ yaz_log (log_level, "reconnect");
c->reconnect_ok = 1;
return;
}
- yaz_log(LOG_DEBUG, "connect");
+ yaz_log(log_level, "connect");
xfree (c->proxy);
val = ZOOM_options_get (c->options, "proxy");
if (val && *val)
return;
(s->refcount)--;
- yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
+ yaz_log (log_level, "ZOOM_query_destroy count=%d", s->refcount);
if (s->refcount == 0)
{
odr_destroy (s->odr);
if (r)
{
(r->refcount)++;
- yaz_log (LOG_DEBUG, "ZOOM_resultset_addref r=%p count=%d",
+ yaz_log (log_level, "ZOOM_resultset_addref r=%p count=%d",
r, r->refcount);
}
}
{
ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
- yaz_log (LOG_DEBUG, "ZOOM_resultset_create r = %p", r);
+ if (!log_level_initialized)
+ {
+ log_level = yaz_log_module_level("zoom");
+ log_level_initialized = 1;
+ }
+
+ yaz_log (log_level, "ZOOM_resultset_create r = %p", r);
r->refcount = 1;
r->size = 0;
r->odr = odr_createmem (ODR_ENCODE);
{
if (!c->cs)
{
- yaz_log(LOG_DEBUG, "NO COMSTACK");
+ yaz_log(log_level, "NO COMSTACK");
ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
}
else
{
- yaz_log(LOG_DEBUG, "PREPARE FOR RECONNECT");
+ yaz_log(log_level, "PREPARE FOR RECONNECT");
c->reconnect_ok = 1;
}
}
{
if (!c->cs)
{
- yaz_log(LOG_DEBUG, "NO COMSTACK");
+ yaz_log(log_level, "NO COMSTACK");
ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
}
else
{
- yaz_log(LOG_DEBUG, "PREPARE FOR RECONNECT");
+ yaz_log(log_level, "PREPARE FOR RECONNECT");
c->reconnect_ok = 1;
}
}
if (!r)
return;
(r->refcount)--;
- yaz_log (LOG_DEBUG, "ZOOM_resultset_destroy r = %p count=%d",
+ yaz_log (log_level, "ZOOM_resultset_destroy r = %p count=%d",
r, r->refcount);
if (r->refcount == 0)
{
{
if (!c->cs)
{
- yaz_log(LOG_DEBUG, "NO COMSTACK");
+ yaz_log(log_level, "NO COMSTACK");
ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
}
else
{
- yaz_log(LOG_DEBUG, "PREPARE FOR RECONNECT");
+ yaz_log(log_level, "PREPARE FOR RECONNECT");
c->reconnect_ok = 1;
}
}
{
void *add;
const char *effective_host;
+ if (!log_level_initialized)
+ {
+ log_level = yaz_log_module_level("zoom");
+ log_level_initialized = 1;
+ }
+
if (c->proxy)
effective_host = c->proxy;
else
effective_host = c->host_port;
- yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
+ yaz_log (log_level, "do_connect host=%s", effective_host);
- assert (!c->cs);
+ if (c->cs)
+ cs_close(c->cs);
c->cs = cs_create_host (effective_host, 0, &add);
if (c->cs && c->cs->protocol == PROTO_HTTP)
c->proto = PROTO_HTTP;
cs_get_host_args(c->host_port, &path);
xfree(c->path);
- c->path = xmalloc(strlen(path)+2);
+ c->path = (char*) xmalloc(strlen(path)+2);
c->path[0] = '/';
strcpy (c->path+1, path);
#else
z_APDU(odr_pr, &a, 0, 0);
odr_destroy(odr_pr);
}
- yaz_log (LOG_DEBUG, "encoding failed");
+ yaz_log (log_level, "encoding failed");
set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
odr_reset(out);
return -1;
assert (a);
if (encode_APDU(c, a, c->odr_out))
return zoom_complete;
- yaz_log(LOG_DEBUG, "send APDU type=%d", a->which);
+ yaz_log(log_level, "send APDU type=%d", a->which);
c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
ZOOM_connection_put_event (c, event);
ZOOM_options_get(c->options, "implementationName"),
odr_prepend(c->odr_out, "ZOOM-C", ireq->implementationName));
- version = odr_strdup(c->odr_out, "$Revision: 1.27 $");
+ version = odr_strdup(c->odr_out, "$Revision: 1.37 $");
if (strlen(version) > 10) /* check for unexpanded CVS strings */
version[strlen(version)-2] = '\0';
ireq->implementationVersion = odr_prepend(c->odr_out,
};
ODR o = odr_createmem(ODR_ENCODE);
int ret;
- Z_SOAP *p = odr_malloc(o, sizeof(*p));
+ Z_SOAP *p = (Z_SOAP*) odr_malloc(o, sizeof(*p));
Z_GDU *gdu;
ZOOM_Event event;
if (cp0 && cp1)
{
- char *h = odr_malloc(c->odr_out, cp1 - cp0 + 1);
+ char *h = (char*) odr_malloc(c->odr_out, cp1 - cp0 + 1);
memcpy (h, cp0, cp1 - cp0);
h[cp1-cp0] = '\0';
z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
"SOAPAction", "\"\"");
p->which = Z_SOAP_generic;
- p->u.generic = odr_malloc(o, sizeof(*p->u.generic));
+ p->u.generic = (Z_SOAP_Generic *) odr_malloc(o, sizeof(*p->u.generic));
p->u.generic->no = 0;
p->u.generic->ns = 0;
p->u.generic->p = sr;
break;
}
r->setname = xstrdup (setname);
- yaz_log (LOG_DEBUG, "allocating set %s", r->setname);
+ yaz_log (log_level, "allocating set %s", r->setname);
}
else
r->setname = xstrdup ("default");
}
if (r->which == Z_External_sutrs)
return record_iconv_return(rec, len,
- r->u.sutrs->buf, r->u.sutrs->len,
+ (char*) r->u.sutrs->buf,
+ r->u.sutrs->len,
charset);
else if (r->which == Z_External_octet)
{
if (present_phase && p->num_records == 0)
{
/* present response and we didn't get any records! */
- set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
+ Z_NamePlusRecord *myrec =
+ zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
+ record_cache_add(resultset, myrec, resultset->start);
+ yaz_log(YLOG_LOG, "pseudo record 1, at pos %d",
+ resultset->start);
}
}
else if (present_phase)
{
/* present response and we didn't get any records! */
- set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
+ Z_NamePlusRecord *myrec =
+ zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
+ record_cache_add(resultset, myrec, resultset->start);
+ yaz_log(YLOG_LOG, "pseudo record 1, at pos %d",
+ resultset->start);
}
}
}
ZOOM_resultset resultset;
ZOOM_Event event;
- yaz_log (LOG_DEBUG, "got search response");
+ yaz_log (log_level, "got search response");
if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
return ;
Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc (p->odr_out, sizeof(*req));
const char *str;
- req->which=Z_IOItemOrder_esRequest;
+ req->which = Z_IOItemOrder_esRequest;
req->u.esRequest = (Z_IORequest *)
odr_malloc(p->odr_out,sizeof(Z_IORequest));
toKeep = r->u.adminService->u.esRequest->toKeep =
(Z_ESAdminOriginPartToKeep *)
odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
- toKeep->which=type;
+ toKeep->which = type;
toKeep->databaseName = odr_strdup(p->odr_out, first_db);
- toKeep->u.create=odr_nullval();
+ toKeep->u.create = odr_nullval();
apdu->u.extendedServicesRequest->taskSpecificParameters = r;
r->u.adminService->u.esRequest->notToKeep = notToKeep =
(Z_ESAdminOriginPartNotToKeep *)
odr_malloc(p->odr_out,
sizeof(*r->u.adminService->u.esRequest->notToKeep));
- notToKeep->which=Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
- notToKeep->u.recordsWillFollow=odr_nullval();
+ notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
+ notToKeep->u.recordsWillFollow = odr_nullval();
if (toKeepP)
*toKeepP = toKeep;
if (notToKeepP)
odr_malloc (p->odr_out, sizeof(Odr_oct));
notToKeep->elements[0]->u.opaque->size =
notToKeep->elements[0]->u.opaque->len = strlen(recordIdOpaque);
- notToKeep->elements[0]->u.opaque->buf =
+ notToKeep->elements[0]->u.opaque->buf = (unsigned char*)
odr_strdup(p->odr_out, recordIdOpaque);
}
else if (recordIdNumber)
apdu->u.extendedServicesRequest->taskSpecificParameters = r;
}
}
- if (!strcmp(type, "create")) /* create database */
+ else if (!strcmp(type, "create")) /* create database */
{
apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
0, 0);
}
- if (!strcmp(type, "drop")) /* drop database */
+ else if (!strcmp(type, "drop")) /* drop database */
{
apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
0, 0);
}
- if (!strcmp(type, "update")) /* update record(s) */
+ else if (!strcmp(type, "commit")) /* commit changes */
+ {
+ apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
+ 0, 0);
+ }
+ else if (!strcmp(type, "update")) /* update record(s) */
{
apdu = create_update_package(p);
}
if (!task)
{
- yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task task=<null>");
+ yaz_log (log_level, "ZOOM_connection_exec_task task=<null>");
return 0;
}
- yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
+ yaz_log (log_level, "ZOOM_connection_exec_task type=%d run=%d",
task->which, task->running);
if (c->error != ZOOM_ERROR_NONE)
{
- yaz_log (LOG_DEBUG, "remove tasks because of error = %d", c->error);
+ yaz_log (log_level, "remove tasks because of error = %d", c->error);
ZOOM_connection_remove_tasks (c);
return 0;
}
if (task->running)
{
- yaz_log (LOG_DEBUG, "task already running");
+ yaz_log (log_level, "task already running");
return 0;
}
task->running = 1;
}
else
{
- yaz_log (LOG_DEBUG, "remove tasks because no connection exist");
+ yaz_log (log_level, "remove tasks because no connection exist");
ZOOM_connection_remove_tasks (c);
}
if (ret == zoom_complete)
{
- yaz_log (LOG_DEBUG, "task removed (complete)");
+ yaz_log (log_level, "task removed (complete)");
ZOOM_connection_remove_task (c);
return 0;
}
- yaz_log (LOG_DEBUG, "task pending");
+ yaz_log (log_level, "task pending");
return 1;
}
Z_InitResponse *initrs;
c->mask = 0;
- yaz_log (LOG_DEBUG, "recv APDU type=%d", apdu->which);
+ yaz_log (log_level, "recv APDU type=%d", apdu->which);
switch(apdu->which)
{
case Z_APDU_initResponse:
if (p)
{
- char *charset=NULL, *lang=NULL;
+ char *charset = NULL, *lang = NULL;
int sel;
yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
- yaz_log(LOG_DEBUG, "Target accepted: charset %s, "
+ yaz_log(log_level, "Target accepted: charset %s, "
"language %s, select %d",
charset ? charset : "none", lang ? lang : "none", sel);
if (charset)
resultset->size = 0;
- yaz_log(LOG_DEBUG, "got SRW response OK");
+ yaz_log(log_level, "got SRW response OK");
if (res->numberOfRecords)
resultset->size = *res->numberOfRecords;
npr->u.databaseRecord->which = Z_External_octet;
npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
odr_malloc(c->odr_in, sizeof(Odr_oct));
- npr->u.databaseRecord->u.octet_aligned->buf =
+ npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
res->records[i].recordData_buf;
npr->u.databaseRecord->u.octet_aligned->len =
npr->u.databaseRecord->u.octet_aligned->size =
const char *connection_head = z_HTTP_header_lookup(hres->headers,
"Connection");
c->mask = 0;
- yaz_log (LOG_DEBUG, "handle_http");
+ yaz_log (log_level, "handle_http");
if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
{
if (!ret && soap_package->which == Z_SOAP_generic &&
soap_package->u.generic->no == 0)
{
- Z_SRW_PDU *sr = soap_package->u.generic->p;
+ Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
if (sr->which == Z_SRW_searchRetrieve_response)
handle_srw_response(c, sr->u.response);
else
r = cs_get (c->cs, &c->buf_in, &c->len_in);
more = cs_more(c->cs);
- yaz_log (LOG_DEBUG, "do_read len=%d more=%d", r, more);
+ yaz_log (log_level, "do_read len=%d more=%d", r, more);
if (r == 1)
return 0;
if (r <= 0)
{
do_close (c);
c->reconnect_ok = 0;
- yaz_log (LOG_DEBUG, "reconnect read");
+ yaz_log (log_level, "reconnect read");
c->tasks->running = 0;
ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
}
event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
ZOOM_connection_put_event (c, event);
- yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
- if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
+ yaz_log (log_level, "do_write_ex len=%d", len_out);
+ if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
{
if (c->reconnect_ok)
{
do_close (c);
c->reconnect_ok = 0;
- yaz_log (LOG_DEBUG, "reconnect write");
+ yaz_log (log_level, "reconnect write");
c->tasks->running = 0;
ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
return zoom_pending;
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);
+ yaz_log (log_level, "do_write_ex 1 mask=%d", c->mask);
}
else
{
c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
- yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
+ yaz_log (log_level, "do_write_ex 2 mask=%d", c->mask);
}
return zoom_pending;
}
{
ZOOM_Event event = 0;
int r = cs_look(c->cs);
- yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
+ yaz_log (log_level, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
c, mask, r);
if (r == CS_NONE)
event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
ret = cs_rcvconnect (c->cs);
- yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
+ yaz_log (log_level, "cs_rcvconnect returned %d", ret);
if (ret == 1)
{
c->mask = ZOOM_SELECT_EXCEPT;
#else
tv.tv_sec = timeout;
tv.tv_usec = 0;
- yaz_log (LOG_DEBUG, "select start");
+ yaz_log (log_level, "select start");
r = select (max_fd+1, &input, &output, &except, &tv);
- yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
+ yaz_log (log_level, "select stop, returned r=%d", r);
for (i = 0; i<no; i++)
{
ZOOM_connection c = cs[i];
/* timeout and this connection was waiting */
set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
do_close (c);
- yaz_log (LOG_DEBUG, "timeout");
+ yaz_log (log_level, "timeout");
ZOOM_connection_put_event(c, event);
}
}