X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=zoom%2Fzoom-c.c;h=1b2480bde9e64a82a59618772169429b15f3a87a;hp=436fae92fb8fdd6bb038b71453c1372bb94442c3;hb=a0ff2572c63e4be9254b12d08b0166b6232477de;hpb=15f2d80b134fa41e213f6328440d1a25f80efd97 diff --git a/zoom/zoom-c.c b/zoom/zoom-c.c index 436fae9..1b2480b 100644 --- a/zoom/zoom-c.c +++ b/zoom/zoom-c.c @@ -1,5 +1,5 @@ /* - * $Id: zoom-c.c,v 1.8 2001-11-15 21:59:40 adam Exp $ + * $Id: zoom-c.c,v 1.37 2002-07-25 12:51:48 adam Exp $ * * ZOOM layer for C, connections, result sets, queries. */ @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include "zoom-p.h" @@ -16,23 +18,25 @@ #include #endif -static Z3950_Event Z3950_Event_create (int kind) +static int ZOOM_connection_send_init (ZOOM_connection c); +static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out); + +static ZOOM_Event ZOOM_Event_create (int kind) { - Z3950_Event event = xmalloc (sizeof(*event)); + ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event)); event->kind = kind; event->next = 0; event->prev = 0; return event; } -static void Z3950_Event_destroy (Z3950_Event event) +static void ZOOM_Event_destroy (ZOOM_Event event) { xfree (event); } -static void Z3950_connection_put_event (Z3950_connection c, Z3950_Event event) +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; @@ -48,10 +52,9 @@ static void Z3950_connection_put_event (Z3950_connection c, Z3950_Event event) c->m_queue_back = event; } -static Z3950_Event Z3950_connection_get_event(Z3950_connection c) +static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c) { - // get from front of queue - Z3950_Event event = c->m_queue_front; + ZOOM_Event event = c->m_queue_front; if (!event) return 0; assert (c->m_queue_back); @@ -63,46 +66,77 @@ static Z3950_Event Z3950_connection_get_event(Z3950_connection c) } else c->m_queue_back = 0; + c->last_event = event->kind; return event; } -static void clear_error (Z3950_connection c) +static void clear_error (ZOOM_connection c) { - c->error = Z3950_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; + } } -Z3950_task Z3950_connection_add_task (Z3950_connection c, int which) +ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which) { - Z3950_task *taskp = &c->tasks; + 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)->u.resultset = 0; /* one null pointer there at least */ (*taskp)->next = 0; clear_error (c); return *taskp; } -void Z3950_connection_remove_task (Z3950_connection c) +ZOOM_task ZOOM_connection_insert_task (ZOOM_connection c, int which) +{ + ZOOM_task task = (ZOOM_task) xmalloc (sizeof(*task)); + + task->next = c->tasks; + c->tasks = task; + + task->running = 0; + task->which = which; + clear_error (c); + return task; +} + +void ZOOM_connection_remove_task (ZOOM_connection c) { - Z3950_task task = c->tasks; + ZOOM_task task = c->tasks; if (task) { c->tasks = task->next; switch (task->which) { - case Z3950_TASK_SEARCH: - Z3950_resultset_destroy (task->u.resultset); + case ZOOM_TASK_SEARCH: + ZOOM_resultset_destroy (task->u.search.resultset); break; - case Z3950_TASK_RETRIEVE: - Z3950_resultset_destroy (task->u.resultset); + case ZOOM_TASK_RETRIEVE: + ZOOM_resultset_destroy (task->u.retrieve.resultset); break; - case Z3950_TASK_CONNECT: + case ZOOM_TASK_CONNECT: + break; + case ZOOM_TASK_SCAN: + ZOOM_scanset_destroy (task->u.scan.scan); + break; + case ZOOM_TASK_PACKAGE: + ZOOM_package_destroy (task->u.package); break; default: assert (0); @@ -111,24 +145,26 @@ void Z3950_connection_remove_task (Z3950_connection c) } } -void Z3950_connection_remove_tasks (Z3950_connection c) +void ZOOM_connection_remove_tasks (ZOOM_connection c) { while (c->tasks) - Z3950_connection_remove_task(c); + ZOOM_connection_remove_task(c); } -static Z3950_record record_cache_lookup (Z3950_resultset r, +static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos, const char *elementSetName); -Z3950_connection Z3950_connection_create (Z3950_options options) +ZOOM_API(ZOOM_connection) +ZOOM_connection_create (ZOOM_options options) { - Z3950_connection c = xmalloc (sizeof(*c)); + ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c)); c->cs = 0; c->mask = 0; + c->reconnect_ok = 0; c->state = STATE_IDLE; - c->error = Z3950_ERROR_NONE; + c->error = ZOOM_ERROR_NONE; c->addinfo = 0; c->buf_in = 0; c->len_in = 0; @@ -136,19 +172,24 @@ Z3950_connection Z3950_connection_create (Z3950_options options) c->len_out = 0; c->resultsets = 0; - c->options = Z3950_options_create_with_parent(options); + c->options = ZOOM_options_create_with_parent(options); c->host_port = 0; c->proxy = 0; + + c->charset = c->lang = 0; c->cookie_out = 0; c->cookie_in = 0; + c->client_IP = 0; c->tasks = 0; c->odr_in = odr_createmem (ODR_DECODE); c->odr_out = odr_createmem (ODR_ENCODE); c->async = 0; + c->support_named_resultsets = 0; + c->last_event = ZOOM_EVENT_NONE; c->m_queue_front = 0; c->m_queue_back = 0; @@ -157,19 +198,23 @@ Z3950_connection Z3950_connection_create (Z3950_options options) /* set database names. Take local databases (if set); otherwise take databases given in ZURL (if set); otherwise use Default */ -static char **set_DatabaseNames (Z3950_connection con, int *num) +static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options, + int *num) { char **databaseNames; const char *c; int no = 2; - const char *cp = Z3950_options_get (con->options, "databaseName"); + const char *cp = ZOOM_options_get (options, "databaseName"); if (!cp || !*cp) { - cp = strchr (con->host_port, '/'); + if (strncmp (con->host_port, "unix:", 5) == 0) + cp = strchr (con->host_port+5, ':'); + else + cp = strchr (con->host_port, '/'); if (cp) cp++; - } + } if (cp) { c = cp; @@ -181,7 +226,8 @@ static char **set_DatabaseNames (Z3950_connection con, int *num) } 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) { @@ -195,7 +241,7 @@ static char **set_DatabaseNames (Z3950_connection con, int *num) } /* 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; @@ -207,26 +253,51 @@ static char **set_DatabaseNames (Z3950_connection con, int *num) return databaseNames; } -Z3950_connection Z3950_connection_new (const char *host, int portnum) +ZOOM_API(ZOOM_connection) +ZOOM_connection_new (const char *host, int portnum) { - Z3950_connection c = Z3950_connection_create (0); + ZOOM_connection c = ZOOM_connection_create (0); - Z3950_connection_connect (c, host, portnum); + ZOOM_connection_connect (c, host, portnum); return c; } -void Z3950_connection_connect(Z3950_connection c, - const char *host, int portnum) +ZOOM_API(void) +ZOOM_connection_connect(ZOOM_connection c, + const char *host, int portnum) { const char *val; - Z3950_task task; + ZOOM_task task; - val = Z3950_options_get (c->options, "proxy"); + if (c->cs) + { + yaz_log (LOG_DEBUG, "reconnect"); + c->reconnect_ok = 1; + return; + } + yaz_log(LOG_DEBUG, "connect"); + xfree (c->proxy); + val = ZOOM_options_get (c->options, "proxy"); if (val && *val) c->proxy = xstrdup (val); else c->proxy = 0; + xfree (c->charset); + val = ZOOM_options_get (c->options, "charset"); + if (val && *val) + c->charset = xstrdup (val); + else + c->charset = 0; + + xfree (c->lang); + val = ZOOM_options_get (c->options, "lang"); + if (val && *val) + c->lang = xstrdup (val); + else + c->lang = 0; + + xfree (c->host_port); if (portnum) { char hostn[128]; @@ -236,20 +307,33 @@ void Z3950_connection_connect(Z3950_connection c, else c->host_port = xstrdup(host); - c->async = Z3950_options_get_bool (c->options, "async", 0); - - task = Z3950_connection_add_task (c, Z3950_TASK_CONNECT); + ZOOM_options_set(c->options, "host", c->host_port); + + val = ZOOM_options_get (c->options, "cookie"); + if (val && *val) + c->cookie_out = xstrdup (val); + + val = ZOOM_options_get (c->options, "clientIP"); + if (val && *val) + c->client_IP = xstrdup (val); + + 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) { - while (Z3950_event (1, &c)) + while (ZOOM_event (1, &c)) ; } } -Z3950_query Z3950_query_create(void) +ZOOM_API(ZOOM_query) +ZOOM_query_create(void) { - Z3950_query s = xmalloc (sizeof(*s)); + ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s)); s->refcount = 1; s->query = 0; @@ -259,18 +343,14 @@ Z3950_query Z3950_query_create(void) return s; } -const char *Z3950_connection_host (Z3950_connection c) -{ - return c->host_port; -} - -void Z3950_query_destroy(Z3950_query s) +ZOOM_API(void) +ZOOM_query_destroy(ZOOM_query s) { if (!s) return; (s->refcount)--; - yaz_log (LOG_DEBUG, "Z3950_query_destroy count=%d", s->refcount); + yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount); if (s->refcount == 0) { odr_destroy (s->odr); @@ -278,9 +358,10 @@ void Z3950_query_destroy(Z3950_query s) } } -int Z3950_query_prefix(Z3950_query s, const char *str) +ZOOM_API(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) @@ -288,7 +369,8 @@ int Z3950_query_prefix(Z3950_query s, const char *str) return 0; } -int Z3950_query_sortby(Z3950_query s, const char *criteria) +ZOOM_API(int) +ZOOM_query_sortby(ZOOM_query s, const char *criteria) { s->sort_spec = yaz_sort_spec (s->odr, criteria); if (!s->sort_spec) @@ -296,11 +378,12 @@ int Z3950_query_sortby(Z3950_query s, const char *criteria) return 0; } -static int do_write(Z3950_connection c); +static int do_write(ZOOM_connection c); -void Z3950_connection_destroy(Z3950_connection c) +ZOOM_API(void) +ZOOM_connection_destroy(ZOOM_connection c) { - Z3950_resultset r; + ZOOM_resultset r; if (!c) return; if (c->cs) @@ -312,26 +395,33 @@ void Z3950_connection_destroy(Z3950_connection c) xfree (c->addinfo); odr_destroy (c->odr_in); odr_destroy (c->odr_out); - Z3950_options_destroy (c->options); - Z3950_connection_remove_tasks (c); + ZOOM_options_destroy (c->options); + ZOOM_connection_remove_tasks (c); xfree (c->host_port); + xfree (c->proxy); + xfree (c->charset); + xfree (c->lang); + xfree (c->cookie_out); + xfree (c->cookie_in); + xfree (c->client_IP); xfree (c); } -void Z3950_resultset_addref (Z3950_resultset r) +void ZOOM_resultset_addref (ZOOM_resultset r) { if (r) (r->refcount)++; } -Z3950_resultset Z3950_resultset_create () +ZOOM_resultset ZOOM_resultset_create () { - Z3950_resultset r = xmalloc (sizeof(*r)); + ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r)); r->refcount = 1; r->size = 0; r->odr = odr_createmem (ODR_ENCODE); r->start = 0; r->piggyback = 1; + r->setname = 0; r->count = 0; r->record_cache = 0; r->r_sort_spec = 0; @@ -342,52 +432,60 @@ Z3950_resultset Z3950_resultset_create () return r; } -Z3950_resultset Z3950_connection_search_pqf(Z3950_connection c, const char *q) +ZOOM_API(ZOOM_resultset) +ZOOM_connection_search_pqf(ZOOM_connection c, const char *q) { - Z3950_resultset r; - Z3950_query s = Z3950_query_create(); + ZOOM_resultset r; + ZOOM_query s = ZOOM_query_create(); - Z3950_query_prefix (s, q); + ZOOM_query_prefix (s, q); - r = Z3950_connection_search (c, s); - Z3950_query_destroy (s); + r = ZOOM_connection_search (c, s); + ZOOM_query_destroy (s); return r; } -Z3950_resultset Z3950_connection_search(Z3950_connection c, Z3950_query q) +ZOOM_API(ZOOM_resultset) +ZOOM_connection_search(ZOOM_connection c, ZOOM_query q) { - Z3950_resultset r = Z3950_resultset_create (); - Z3950_task task; + ZOOM_resultset r = ZOOM_resultset_create (); + ZOOM_task task; + const char *cp; r->r_sort_spec = q->sort_spec; r->r_query = q->query; r->search = q; - r->options = Z3950_options_create_with_parent(c->options); + r->options = ZOOM_options_create_with_parent(c->options); - r->start = Z3950_options_get_int(r->options, "start", 0); - r->count = Z3950_options_get_int(r->options, "count", 0); - r->piggyback = Z3950_options_get_bool (r->options, "piggyback", 1); + r->start = ZOOM_options_get_int(r->options, "start", 0); + r->count = ZOOM_options_get_int(r->options, "count", 0); + r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1); + cp = ZOOM_options_get (r->options, "setname"); + if (cp) + r->setname = xstrdup (cp); + r->connection = c; r->next = c->resultsets; c->resultsets = r; - task = Z3950_connection_add_task (c, Z3950_TASK_SEARCH); - task->u.resultset = r; - Z3950_resultset_addref (r); + task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH); + task->u.search.resultset = r; + ZOOM_resultset_addref (r); (q->refcount)++; if (!c->async) { - while (Z3950_event (1, &c)) + while (ZOOM_event (1, &c)) ; } return r; } -void Z3950_resultset_destroy(Z3950_resultset r) +ZOOM_API(void) +ZOOM_resultset_destroy(ZOOM_resultset r) { if (!r) return; @@ -395,10 +493,15 @@ void Z3950_resultset_destroy(Z3950_resultset r) 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 */ - Z3950_resultset *rp = &r->connection->resultsets; + ZOOM_resultset *rp = &r->connection->resultsets; while (1) { assert (*rp); /* we must be in this list!! */ @@ -410,19 +513,21 @@ void Z3950_resultset_destroy(Z3950_resultset r) rp = &(*rp)->next; } } - Z3950_query_destroy (r->search); - Z3950_options_destroy (r->options); + ZOOM_query_destroy (r->search); + ZOOM_options_destroy (r->options); odr_destroy (r->odr); + xfree (r->setname); xfree (r); } } -size_t Z3950_resultset_size (Z3950_resultset r) +ZOOM_API(size_t) +ZOOM_resultset_size (ZOOM_resultset r) { return r->size; } -static void do_close (Z3950_connection c) +static void do_close (ZOOM_connection c) { if (c->cs) cs_close(c->cs); @@ -431,37 +536,32 @@ static void do_close (Z3950_connection c) c->state = STATE_IDLE; } -static void Z3950_resultset_retrieve (Z3950_resultset r, - int force_sync, int start, int count) +static void ZOOM_resultset_retrieve (ZOOM_resultset r, + int force_sync, int start, int count) { - Z3950_task task; - Z3950_connection c; + ZOOM_task task; + ZOOM_connection c; if (!r) return; c = r->connection; if (!c) return; - if (start >= r->size) - return; + task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE); + task->u.retrieve.resultset = r; + task->u.retrieve.start = start; + task->u.retrieve.count = count; - if (start + count > r->size) - count = r->size - start; - - task = Z3950_connection_add_task (c, Z3950_TASK_RETRIEVE); - task->u.resultset = r; - Z3950_resultset_addref (r); - - r->start = start; - r->count = count; + ZOOM_resultset_addref (r); if (!r->connection->async || force_sync) - while (r->connection && Z3950_event (1, &r->connection)) + while (r->connection && ZOOM_event (1, &r->connection)) ; } -void Z3950_resultset_records (Z3950_resultset r, Z3950_record *recs, - size_t start, size_t count) +ZOOM_API(void) +ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs, + size_t start, size_t count) { int force_present = 0; @@ -469,16 +569,16 @@ void Z3950_resultset_records (Z3950_resultset r, Z3950_record *recs, return ; if (count && recs) force_present = 1; - Z3950_resultset_retrieve (r, force_present, start, count); + ZOOM_resultset_retrieve (r, force_present, start, count); if (force_present) { size_t i; for (i = 0; i< count; i++) - recs[i] = Z3950_resultset_record_immediate (r, i+start); + recs[i] = ZOOM_resultset_record_immediate (r, i+start); } } -static int do_connect (Z3950_connection c) +static int do_connect (ZOOM_connection c) { void *add; const char *effective_host; @@ -496,102 +596,151 @@ static int do_connect (Z3950_connection c) if (c->cs) { int ret = cs_connect (c->cs, add); - yaz_log (LOG_DEBUG, "cs_connect returned %d", ret); - if (ret >= 0) + if (ret == 0) + { + ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT); + ZOOM_connection_put_event(c, event); + ZOOM_connection_send_init(c); + c->state = STATE_ESTABLISHED; + return 1; + } + else if (ret > 0) { c->state = STATE_CONNECTING; - c->mask = Z3950_SELECT_READ | Z3950_SELECT_WRITE | - Z3950_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; } } c->state = STATE_IDLE; - c->error = Z3950_ERROR_CONNECT; + c->error = ZOOM_ERROR_CONNECT; return 0; } -int z3950_connection_socket(Z3950_connection c) +int z3950_connection_socket(ZOOM_connection c) { if (c->cs) return cs_fileno(c->cs); return -1; } -int z3950_connection_mask(Z3950_connection c) +int z3950_connection_mask(ZOOM_connection c) { if (c->cs) return c->mask; return 0; } -static int encode_APDU(Z3950_connection c, Z_APDU *a, ODR out) +static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out) { - char str[120]; + int i; + for (i = 0; i<200; i++) + { + size_t len; + Z_OtherInformation **oi; + char buf[20]; + const char *val; + const char *cp; + int oidval; + + sprintf (buf, "otherInfo%d", i); + val = ZOOM_options_get (c->options, buf); + if (!val) + break; + cp = strchr (val, ':'); + if (!cp) + continue; + len = cp - val; + if (len >= sizeof(buf)) + len = sizeof(buf)-1; + memcpy (buf, val, len); + buf[len] = '\0'; + oidval = oid_getvalbyname (buf); + if (oidval == VAL_NONE) + continue; + + yaz_oi_APDU(a, &oi); + yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1); + } +} +static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out) +{ assert (a); - sprintf (str, "send_APDU t=%p type=%d", c, a->which); if (c->cookie_out) { Z_OtherInformation **oi; yaz_oi_APDU(a, &oi); yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out); } + if (c->client_IP) + { + Z_OtherInformation **oi; + yaz_oi_APDU(a, &oi); + yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP); + } + otherInfo_attach (c, a, out); if (!z_APDU(out, &a, 0, 0)) { - FILE *outf = fopen("/tmp/apdu.txt", "w"); - if (outf) + FILE *outf = fopen("/tmp/apdu.txt", "a"); + if (a && outf) { ODR odr_pr = odr_createmem(ODR_PRINT); fprintf (outf, "a=%p\n", a); odr_setprint(odr_pr, outf); z_APDU(odr_pr, &a, 0, 0); odr_destroy(odr_pr); - fclose (outf); } - c->error = Z3950_ERROR_ENCODE; + c->error = ZOOM_ERROR_ENCODE; do_close (c); return -1; } + return 0; } -static int send_APDU (Z3950_connection c, Z_APDU *a) +static int send_APDU (ZOOM_connection c, Z_APDU *a) { + ZOOM_Event event; assert (a); if (encode_APDU(c, a, c->odr_out)) return -1; 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); odr_reset(c->odr_out); do_write (c); - return 0; + return 0; } -static int Z3950_connection_send_init (Z3950_connection c) +static int ZOOM_connection_send_init (ZOOM_connection c) { 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)); - const char *auth_groupId = Z3950_options_get (c->options, "group"); - const char *auth_userId = Z3950_options_get (c->options, "user"); - const char *auth_password = Z3950_options_get (c->options, "pass"); + 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"); ODR_MASK_SET(ireq->options, Z_Options_search); ODR_MASK_SET(ireq->options, Z_Options_present); ODR_MASK_SET(ireq->options, Z_Options_scan); ODR_MASK_SET(ireq->options, Z_Options_sort); -#if 0 ODR_MASK_SET(ireq->options, Z_Options_extendedServices); ODR_MASK_SET(ireq->options, Z_Options_namedResultSets); -#endif ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1); ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2); ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3); - impname = Z3950_options_get (c->options, "implementationName"); + 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) { @@ -601,32 +750,35 @@ static int Z3950_connection_send_init (Z3950_connection c) strcat (ireq->implementationName, "ZOOM-C/YAZ"); *ireq->maximumRecordSize = - Z3950_options_get_int (c->options, "maximumRecordSize", 1024*1024); + ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024); *ireq->preferredMessageSize = - Z3950_options_get_int (c->options, "preferredMessageSize", 1024*1024); + ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024); 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++; } @@ -640,22 +792,42 @@ static int Z3950_connection_send_init (Z3950_connection c) 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 (c->proxy) yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out, VAL_PROXY, 1, c->host_port); + if (c->charset||c->lang) + { + Z_OtherInformation **oi; + Z_OtherInformationUnit *oi_unit; + + yaz_oi_APDU(apdu, &oi); + + if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0))) + { + ODR_MASK_SET(ireq->options, Z_Options_negotiationModel); + + oi_unit->which = Z_OtherInfo_externallyDefinedInfo; + oi_unit->information.externallyDefinedInfo = + yaz_set_proposal_charneg + (c->odr_out, + (const char **)&c->charset, (c->charset) ? 1:0, + (const char **)&c->lang, (c->lang) ? 1:0, 1); + } + } assert (apdu); send_APDU (c, apdu); return 0; } -static int Z3950_connection_send_search (Z3950_connection c) +static int ZOOM_connection_send_search (ZOOM_connection c) { - Z3950_resultset r; + ZOOM_resultset r; int lslb, ssub, mspn; const char *syntax; Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest); @@ -663,18 +835,21 @@ static int Z3950_connection_send_search (Z3950_connection c) const char *elementSetName; const char *smallSetElementSetName; const char *mediumSetElementSetName; + const char *schema; assert (c->tasks); - assert (c->tasks->which == Z3950_TASK_SEARCH); + assert (c->tasks->which == ZOOM_TASK_SEARCH); - r = c->tasks->u.resultset; + r = c->tasks->u.search.resultset; elementSetName = - Z3950_options_get (r->options, "elementSetName"); + ZOOM_options_get (r->options, "elementSetName"); smallSetElementSetName = - Z3950_options_get (r->options, "smallSetElementSetName"); + ZOOM_options_get (r->options, "smallSetElementSetName"); mediumSetElementSetName = - Z3950_options_get (r->options, "mediumSetElementSetName"); + ZOOM_options_get (r->options, "mediumSetElementSetName"); + schema = + ZOOM_options_get (r->options, "schema"); if (!smallSetElementSetName) smallSetElementSetName = elementSetName; @@ -689,14 +864,14 @@ static int Z3950_connection_send_search (Z3950_connection c) search_req->query = r->r_query; search_req->databaseNames = - set_DatabaseNames (c, &search_req->num_databaseNames); + set_DatabaseNames (c, r->options, &search_req->num_databaseNames); /* get syntax (no need to provide unless piggyback is in effect) */ - syntax = Z3950_options_get (r->options, "preferredRecordSyntax"); + syntax = ZOOM_options_get (r->options, "preferredRecordSyntax"); - lslb = Z3950_options_get_int (r->options, "largeSetLowerBound", -1); - ssub = Z3950_options_get_int (r->options, "smallSetUpperBound", -1); - mspn = Z3950_options_get_int (r->options, "mediumSetPresentNumber", -1); + lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1); + ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1); + mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1); if (lslb != -1 && ssub != -1 && mspn != -1) { /* So're a Z39.50 expert? Let's hope you don't do sort */ @@ -705,7 +880,7 @@ static int Z3950_connection_send_search (Z3950_connection c) *search_req->mediumSetPresentNumber = mspn; } else if (r->start == 0 && r->count > 0 - && r->piggyback && !r->r_sort_spec) + && r->piggyback && !r->r_sort_spec && !schema) { /* Regular piggyback - do it unless we're going to do sort */ *search_req->largeSetLowerBound = 2000000000; @@ -722,7 +897,8 @@ static int Z3950_connection_send_search (Z3950_connection c) } 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); @@ -730,7 +906,8 @@ static int Z3950_connection_send_search (Z3950_connection c) } 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); @@ -739,14 +916,39 @@ static int Z3950_connection_send_search (Z3950_connection c) if (syntax) search_req->preferredRecordSyntax = yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax); - + + if (!r->setname) + { + if (c->support_named_resultsets) + { + char setname[14]; + int ord; + /* find the lowest unused ordinal so that we re-use + result sets on the server. */ + for (ord = 1; ; ord++) + { + ZOOM_resultset rp; + sprintf (setname, "%d", ord); + for (rp = c->resultsets; rp; rp = rp->next) + if (rp->setname && !strcmp (rp->setname, setname)) + break; + if (!rp) + break; + } + r->setname = xstrdup (setname); + yaz_log (LOG_DEBUG, "allocating %s", r->setname); + } + else + r->setname = xstrdup ("default"); + ZOOM_options_set (r->options, "setname", r->setname); + } + search_req->resultSetName = odr_strdup(c->odr_out, r->setname); /* send search request */ send_APDU (c, apdu); - r->r_query = 0; return 1; } -static void response_diag (Z3950_connection c, Z_DiagRec *p) +static void response_diag (ZOOM_connection c, Z_DiagRec *p) { Z_DefaultDiagFormat *r; char *addinfo = 0; @@ -755,7 +957,7 @@ static void response_diag (Z3950_connection c, Z_DiagRec *p) c->addinfo = 0; if (p->which != Z_DiagRec_defaultFormat) { - c->error = Z3950_ERROR_DECODE; + c->error = ZOOM_ERROR_DECODE; return; } r = p->u.defaultFormat; @@ -773,19 +975,20 @@ static void response_diag (Z3950_connection c, Z_DiagRec *p) c->error = *r->condition; } -Z3950_record Z3950_record_dup (const Z3950_record srec) +ZOOM_API(ZOOM_record) +ZOOM_record_clone (ZOOM_record srec) { char *buf; int size; ODR odr_enc; - Z3950_record nrec; + ZOOM_record nrec; odr_enc = odr_createmem(ODR_ENCODE); if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0)) 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); @@ -795,18 +998,21 @@ Z3950_record Z3950_record_dup (const Z3950_record srec) return nrec; } -Z3950_record Z3950_resultset_record_immediate (Z3950_resultset s,size_t pos) +ZOOM_API(ZOOM_record) +ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos) { return record_cache_lookup (s, pos, 0); } -Z3950_record Z3950_resultset_record (Z3950_resultset r, size_t pos) +ZOOM_API(ZOOM_record) +ZOOM_resultset_record (ZOOM_resultset r, size_t pos) { - Z3950_resultset_retrieve (r, 1, pos, 1); - return Z3950_resultset_record_immediate (r, pos); + ZOOM_resultset_retrieve (r, 1, pos, 1); + return ZOOM_resultset_record_immediate (r, pos); } -void Z3950_record_destroy (Z3950_record rec) +ZOOM_API(void) +ZOOM_record_destroy (ZOOM_record rec) { if (!rec) return; @@ -816,9 +1022,14 @@ void Z3950_record_destroy (Z3950_record rec) xfree (rec); } -void *Z3950_record_get (Z3950_record rec, const char *type, size_t *len) +ZOOM_API(const char *) +ZOOM_record_get (ZOOM_record rec, const char *type, int *len) { Z_NamePlusRecord *npr; + + if (len) + *len = 0; /* default return */ + if (!rec) return 0; npr = rec->npr; @@ -826,6 +1037,8 @@ void *Z3950_record_get (Z3950_record rec, const char *type, size_t *len) return 0; if (!strcmp (type, "database")) { + if (len) + *len = strlen(npr->databaseName); return npr->databaseName; } else if (!strcmp (type, "syntax")) @@ -835,73 +1048,121 @@ void *Z3950_record_get (Z3950_record rec, const char *type, size_t *len) Z_External *r = (Z_External *) npr->u.databaseRecord; oident *ent = oid_getentbyoid(r->direct_reference); if (ent) + { + if (len) + *len = strlen(ent->desc); return ent->desc; + } } return "none"; } - else if (!strcmp (type, "render")) + else if (!strcmp (type, "render") && + npr->which == Z_NamePlusRecord_databaseRecord) { - 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; - } - 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; - } - else if (r->which == Z_External_grs1) - { - *len = 5; - return "GRS-1"; - } - } + Z_External *r = (Z_External *) npr->u.databaseRecord; + oident *ent = oid_getentbyoid(r->direct_reference); + + if (r->which == Z_External_sutrs) + { + if (len) *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) + { + if (len) *len = wrbuf_len(rec->wrbuf_marc); + return wrbuf_buf(rec->wrbuf_marc); + } + } + if (len) *len = r->u.octet_aligned->len; + return (const char *) r->u.octet_aligned->buf; + } + else if (r->which == Z_External_grs1) + { + if (len) *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) + { + if (len) *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) + { + if (len) *len = wrbuf_len(rec->wrbuf_marc); + return wrbuf_buf(rec->wrbuf_marc); + } + } + if (len) *len = r->u.octet_aligned->len; + return (const char *) r->u.octet_aligned->buf; + } + else if (r->which == Z_External_grs1) + { + if (len) *len = 5; + return "GRS-1"; + } return 0; } else if (!strcmp (type, "raw")) { if (npr->which == Z_NamePlusRecord_databaseRecord) - { - *len = -1; - return (Z_External *) npr->u.databaseRecord; - } + return (const char *) npr->u.databaseRecord; return 0; } return 0; } -static void record_cache_add (Z3950_resultset r, +static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, int pos, const char *elementSetName) { - Z3950_record_cache rc; + ZOOM_record_cache rc; for (rc = r->record_cache; rc; rc = rc->next) { @@ -917,9 +1178,8 @@ static void record_cache_add (Z3950_resultset r, 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; @@ -932,11 +1192,11 @@ static void record_cache_add (Z3950_resultset r, r->record_cache = rc; } -static Z3950_record record_cache_lookup (Z3950_resultset r, +static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos, const char *elementSetName) { - Z3950_record_cache rc; + ZOOM_record_cache rc; for (rc = r->record_cache; rc; rc = rc->next) { @@ -951,19 +1211,24 @@ static Z3950_record record_cache_lookup (Z3950_resultset r, return 0; } -static void handle_records (Z3950_connection c, Z_Records *sr, +static void handle_records (ZOOM_connection c, Z_Records *sr, int present_phase) { - Z3950_resultset resultset; + ZOOM_resultset resultset; if (!c->tasks) return ; - if (c->tasks->which != Z3950_TASK_SEARCH && - c->tasks->which != Z3950_TASK_RETRIEVE) - return ; - - resultset = c->tasks->u.resultset; - + switch (c->tasks->which) + { + case ZOOM_TASK_SEARCH: + resultset = c->tasks->u.search.resultset; + break; + case ZOOM_TASK_RETRIEVE: + resultset = c->tasks->u.retrieve.resultset; + break; + default: + return; + } if (sr && sr->which == Z_Records_NSD) { Z_DiagRec dr, *dr_p = &dr; @@ -977,7 +1242,7 @@ static void handle_records (Z3950_connection c, Z_Records *sr, if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1) response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]); else - c->error = Z3950_ERROR_DECODE; + c->error = ZOOM_ERROR_DECODE; } else { @@ -1002,51 +1267,77 @@ static void handle_records (Z3950_connection c, Z_Records *sr, if (present_phase && p->num_records == 0) { /* present response and we didn't get any records! */ - c->error = Z3950_ERROR_DECODE; + c->error = ZOOM_ERROR_DECODE; } } else if (present_phase) { /* present response and we didn't get any records! */ - c->error = Z3950_ERROR_DECODE; + c->error = ZOOM_ERROR_DECODE; } } } -static void handle_present_response (Z3950_connection c, Z_PresentResponse *pr) +static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr) { handle_records (c, pr->records, 1); } -static void handle_search_response (Z3950_connection c, Z_SearchResponse *sr) +static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr) { - Z3950_resultset resultset; + ZOOM_resultset resultset; yaz_log (LOG_DEBUG, "got search response"); - if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH) + if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH) return ; - resultset = c->tasks->u.resultset; + resultset = c->tasks->u.search.resultset; resultset->size = *sr->resultCount; handle_records (c, sr->records, 0); } -static void sort_response (Z3950_connection c, Z_SortResponse *res) +static void sort_response (ZOOM_connection c, Z_SortResponse *res) { if (res->diagnostics && res->num_diagnostics > 0) response_diag (c, res->diagnostics[0]); } -static int send_sort (Z3950_connection c) +static int scan_response (ZOOM_connection c, Z_ScanResponse *res) +{ + NMEM nmem = odr_extract_mem (c->odr_in); + ZOOM_scanset scan; + + if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN) + return 0; + scan = c->tasks->u.scan.scan; + + if (res->entries && res->entries->nonsurrogateDiagnostics) + response_diag(c, res->entries->nonsurrogateDiagnostics[0]); + scan->scan_response = res; + nmem_transfer (scan->odr->mem, nmem); + if (res->stepSize) + ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize); + if (res->positionOfTerm) + ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm); + if (res->scanStatus) + ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus); + if (res->numberOfEntriesReturned) + ZOOM_options_set_int (scan->options, "number", + *res->numberOfEntriesReturned); + nmem_destroy (nmem); + return 1; +} + +static int send_sort (ZOOM_connection c) { - Z3950_resultset resultset; + ZOOM_resultset resultset; - if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH) + if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH) return 0; - resultset = c->tasks->u.resultset; + resultset = c->tasks->u.search.resultset; if (c->error) { @@ -1061,8 +1352,9 @@ static int send_sort (Z3950_connection c) req->num_inputResultSetNames = 1; req->inputResultSetNames = (Z_InternationalString **) odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames)); - req->inputResultSetNames[0] = odr_strdup (c->odr_out, "default"); - req->sortedResultSetName = odr_strdup (c->odr_out, "default"); + req->inputResultSetNames[0] = + odr_strdup (c->odr_out, resultset->setname); + req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname); req->sortSequence = resultset->r_sort_spec; resultset->r_sort_spec = 0; send_APDU (c, apdu); @@ -1071,32 +1363,48 @@ static int send_sort (Z3950_connection c) return 0; } -static int send_present (Z3950_connection c) +static int send_present (ZOOM_connection c) { Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest); Z_PresentRequest *req = apdu->u.presentRequest; int i = 0; const char *syntax = - Z3950_options_get (c->options, "preferredRecordSyntax"); + ZOOM_options_get (c->options, "preferredRecordSyntax"); const char *element = - Z3950_options_get (c->options, "elementSetName"); - Z3950_resultset resultset; + ZOOM_options_get (c->options, "elementSetName"); + const char *schema = + ZOOM_options_get (c->options, "schema"); + ZOOM_resultset resultset; if (!c->tasks) return 0; - if (c->tasks->which != Z3950_TASK_SEARCH && - c->tasks->which != Z3950_TASK_RETRIEVE) - return 0; - resultset = c->tasks->u.resultset; - + switch (c->tasks->which) + { + case ZOOM_TASK_SEARCH: + resultset = c->tasks->u.search.resultset; + break; + case ZOOM_TASK_RETRIEVE: + resultset = c->tasks->u.retrieve.resultset; + resultset->start = c->tasks->u.retrieve.start; + resultset->count = c->tasks->u.retrieve.count; + + if (resultset->start >= resultset->size) + return 0; + if (resultset->start + resultset->count > resultset->size) + resultset->count = resultset->size - resultset->start; + break; + default: + return 0; + } + if (c->error) /* don't continue on error */ return 0; if (resultset->start < 0) return 0; for (i = 0; icount; i++) { - Z3950_record rec = + ZOOM_record rec = record_cache_lookup (resultset, i + resultset->start, 0); if (!rec) break; @@ -1114,10 +1422,54 @@ static int send_present (Z3950_connection c) req->preferredRecordSyntax = yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax); - if (element && *element) + if (schema && *schema) { - Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn)); - 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; + compo->u.complex = (Z_CompSpec *) + odr_malloc(c->odr_out, sizeof(*compo->u.complex)); + compo->u.complex->selectAlternativeSyntax = (bool_t *) + odr_malloc(c->odr_out, sizeof(bool_t)); + *compo->u.complex->selectAlternativeSyntax = 0; + + compo->u.complex->generic = (Z_Specification *) + odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic)); + + compo->u.complex->generic->schema = (Odr_oid *) + yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema); + + if (!compo->u.complex->generic->schema) + { + /* OID wasn't a schema! Try record syntax instead. */ + + compo->u.complex->generic->schema = (Odr_oid *) + yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema); + } + if (element && *element) + { + compo->u.complex->generic->elementSpec = (Z_ElementSpec *) + odr_malloc(c->odr_out, sizeof(Z_ElementSpec)); + compo->u.complex->generic->elementSpec->which = + Z_ElementSpec_elementSetName; + compo->u.complex->generic->elementSpec->u.elementSetName = + odr_strdup (c->odr_out, element); + } + else + compo->u.complex->generic->elementSpec = 0; + compo->u.complex->num_dbSpecific = 0; + compo->u.complex->dbSpecific = 0; + compo->u.complex->num_recordSyntax = 0; + compo->u.complex->recordSyntax = 0; + } + else if (element && *element) + { + 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); @@ -1125,47 +1477,430 @@ static int send_present (Z3950_connection c) compo->u.simple = esn; req->recordComposition = compo; } + req->resultSetId = odr_strdup(c->odr_out, resultset->setname); send_APDU (c, apdu); return 1; } -static int Z3950_connection_exec_task (Z3950_connection c) +ZOOM_API(ZOOM_scanset) +ZOOM_connection_scan (ZOOM_connection c, const char *start) +{ + ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan)); + + scan->connection = c; + scan->odr = odr_createmem (ODR_DECODE); + scan->options = ZOOM_options_create_with_parent (c->options); + scan->refcount = 1; + scan->scan_response = 0; + + if ((scan->termListAndStartPoint = + p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet, + start))) + { + ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN); + task->u.scan.scan = scan; + + (scan->refcount)++; + if (!c->async) + { + while (ZOOM_event (1, &c)) + ; + } + } + return scan; +} + +ZOOM_API(void) +ZOOM_scanset_destroy (ZOOM_scanset scan) +{ + if (!scan) + return; + (scan->refcount)--; + if (scan->refcount == 0) + { + odr_destroy (scan->odr); + + ZOOM_options_destroy (scan->options); + xfree (scan); + } +} + +static int send_package (ZOOM_connection c) +{ + ZOOM_Event event; + if (!c->tasks) + return 0; + assert (c->tasks->which == ZOOM_TASK_PACKAGE); + + event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU); + ZOOM_connection_put_event (c, event); + + do_write_ex (c, c->tasks->u.package->buf_out, + c->tasks->u.package->len_out); + return 1; +} + +static int send_scan (ZOOM_connection c) { - Z3950_task task = c->tasks; + ZOOM_scanset scan; + Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest); + Z_ScanRequest *req = apdu->u.scanRequest; + if (!c->tasks) + return 0; + assert (c->tasks->which == ZOOM_TASK_SCAN); + scan = c->tasks->u.scan.scan; + + req->termListAndStartPoint = scan->termListAndStartPoint; + req->attributeSet = scan->attributeSet; - yaz_log (LOG_LOG, "Z3950_connection_exec_task"); + *req->numberOfTermsRequested = + ZOOM_options_get_int(scan->options, "number", 10); + + req->preferredPositionInResponse = + odr_intdup (c->odr_out, + ZOOM_options_get_int(scan->options, "position", 1)); + + req->stepSize = + odr_intdup (c->odr_out, + ZOOM_options_get_int(scan->options, "stepSize", 0)); + + req->databaseNames = set_DatabaseNames (c, scan->options, + &req->num_databaseNames); + + send_APDU (c, apdu); + + return 1; +} + +ZOOM_API(size_t) +ZOOM_scanset_size (ZOOM_scanset scan) +{ + if (!scan || !scan->scan_response || !scan->scan_response->entries) + return 0; + return scan->scan_response->entries->num_entries; +} + +ZOOM_API(const char *) +ZOOM_scanset_term (ZOOM_scanset scan, size_t pos, + int *occ, int *len) +{ + const char *term = 0; + size_t noent = ZOOM_scanset_size (scan); + Z_ScanResponse *res = scan->scan_response; + + *len = 0; + *occ = 0; + if (pos >= noent) + return 0; + if (res->entries->entries[pos]->which == Z_Entry_termInfo) + { + Z_TermInfo *t = res->entries->entries[pos]->u.termInfo; + + if (t->term->which == Z_Term_general) + { + term = (const char *) t->term->u.general->buf; + *len = t->term->u.general->len; + } + *occ = t->globalOccurrences ? *t->globalOccurrences : 0; + } + return term; +} + +ZOOM_API(const char *) +ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key) +{ + return ZOOM_options_get (scan->options, key); +} + +ZOOM_API(void) +ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key, + const char *val) +{ + ZOOM_options_set (scan->options, key, val); +} + + + +static Z_APDU *create_es_package (ZOOM_package p, int type) +{ + const char *str; + Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest); + Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest; + + *req->function = Z_ExtendedServicesRequest_create; + + str = ZOOM_options_get(p->options, "package-name"); + if (str && *str) + req->packageName = nmem_strdup (p->odr_out->mem, str); + + str = ZOOM_options_get(p->options, "user-id"); + if (str) + req->userId = nmem_strdup (p->odr_out->mem, str); + + req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV, + type); + + str = ZOOM_options_get(p->options, "function"); + if (str) + { + if (!strcmp (str, "create")) + *req->function = 1; + if (!strcmp (str, "delete")) + *req->function = 2; + if (!strcmp (str, "modify")) + *req->function = 3; + } + return apdu; +} + +static const char *ill_array_lookup (void *clientData, const char *idx) +{ + ZOOM_package p = (ZOOM_package) clientData; + return ZOOM_options_get (p->options, idx+4); +} + +static Z_External *encode_ill_request (ZOOM_package p) +{ + ODR out = p->odr_out; + ILL_Request *req; + Z_External *r = 0; + struct ill_get_ctl ctl; + + ctl.odr = p->odr_out; + ctl.clientData = p; + ctl.f = ill_array_lookup; + + req = ill_get_ILLRequest(&ctl, "ill", 0); + + if (!ill_Request (out, &req, 0, 0)) + { + int ill_request_size; + char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0); + if (ill_request_buf) + odr_setbuf (out, ill_request_buf, ill_request_size, 1); + return 0; + } + else + { + oident oid; + int illRequest_size = 0; + char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0); + + oid.proto = PROTO_GENERAL; + oid.oclass = CLASS_GENERAL; + oid.value = VAL_ISO_ILL_1; + + r = (Z_External *) odr_malloc (out, sizeof(*r)); + r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid)); + r->indirect_reference = 0; + r->descriptor = 0; + r->which = Z_External_single; + + r->u.single_ASN1_type = (Odr_oct *) + odr_malloc (out, sizeof(*r->u.single_ASN1_type)); + r->u.single_ASN1_type->buf = odr_malloc (out, illRequest_size); + r->u.single_ASN1_type->len = illRequest_size; + r->u.single_ASN1_type->size = illRequest_size; + memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size); + } + return r; +} + +static Z_ItemOrder *encode_item_order(ZOOM_package p) +{ + Z_ItemOrder *req = odr_malloc (p->odr_out, sizeof(*req)); + const char *str; + + req->which=Z_IOItemOrder_esRequest; + req->u.esRequest = (Z_IORequest *) + odr_malloc(p->odr_out,sizeof(Z_IORequest)); + + /* to keep part ... */ + req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *) + odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep)); + req->u.esRequest->toKeep->supplDescription = 0; + req->u.esRequest->toKeep->contact = + odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact)); + + str = ZOOM_options_get(p->options, "contact-name"); + req->u.esRequest->toKeep->contact->name = str ? + nmem_strdup (p->odr_out->mem, str) : 0; + + str = ZOOM_options_get(p->options, "contact-phone"); + req->u.esRequest->toKeep->contact->phone = str ? + nmem_strdup (p->odr_out->mem, str) : 0; + + str = ZOOM_options_get(p->options, "contact-email"); + req->u.esRequest->toKeep->contact->email = str ? + nmem_strdup (p->odr_out->mem, str) : 0; + + req->u.esRequest->toKeep->addlBilling = 0; + + /* not to keep part ... */ + req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *) + odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep)); + + req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *) + odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem)); + + str = ZOOM_options_get(p->options, "itemorder-setname"); + if (!str) + str = "default"; + req->u.esRequest->notToKeep->resultSetItem->resultSetId = + nmem_strdup (p->odr_out->mem, str); + req->u.esRequest->notToKeep->resultSetItem->item = + (int *) odr_malloc(p->odr_out, sizeof(int)); + + str = ZOOM_options_get(p->options, "itemorder-item"); + *req->u.esRequest->notToKeep->resultSetItem->item = + (str ? atoi(str) : 1); + + req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p); + + return req; +} + +ZOOM_API(void) + ZOOM_package_send (ZOOM_package p, const char *type) +{ + Z_APDU *apdu = 0; + ZOOM_connection c; + if (!p) + return; + c = p->connection; + odr_reset (p->odr_out); + xfree (p->buf_out); + p->buf_out = 0; + if (!strcmp(type, "itemorder")) + { + Z_External *r; + apdu = create_es_package (p, VAL_ITEMORDER); + if (apdu) + { + r = odr_malloc (p->odr_out, sizeof(*r)); + + r->direct_reference = + yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV, + VAL_ITEMORDER); + r->descriptor = 0; + r->which = Z_External_itemOrder; + r->indirect_reference = 0; + r->u.itemOrder = encode_item_order (p); + + apdu->u.extendedServicesRequest->taskSpecificParameters = r; + } + } + if (apdu) + { + if (encode_APDU(p->connection, apdu, p->odr_out) == 0) + { + char *buf; + + ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE); + task->u.package = p; + buf = odr_getbuf(p->odr_out, &p->len_out, 0); + p->buf_out = xmalloc (p->len_out); + memcpy (p->buf_out, buf, p->len_out); + + (p->refcount)++; + if (!c->async) + { + while (ZOOM_event (1, &c)) + ; + } + } + } +} + +ZOOM_API(ZOOM_package) + ZOOM_connection_package (ZOOM_connection c, ZOOM_options options) +{ + ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p)); + + p->connection = c; + p->odr_out = odr_createmem (ODR_ENCODE); + p->options = ZOOM_options_create_with_parent2 (options, c->options); + p->refcount = 1; + p->buf_out = 0; + p->len_out = 0; + return p; +} + +ZOOM_API(void) + ZOOM_package_destroy(ZOOM_package p) +{ + if (!p) + return; + (p->refcount)--; + if (p->refcount == 0) + { + odr_destroy (p->odr_out); + xfree (p->buf_out); + + ZOOM_options_destroy (p->options); + xfree (p); + } +} + +ZOOM_API(const char *) +ZOOM_package_option_get (ZOOM_package p, const char *key) +{ + return ZOOM_options_get (p->options, key); +} + +ZOOM_API(void) +ZOOM_package_option_set (ZOOM_package p, const char *key, + const char *val) +{ + ZOOM_options_set (p->options, key, val); +} + +static int ZOOM_connection_exec_task (ZOOM_connection c) +{ + ZOOM_task task = c->tasks; + + yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task"); if (!task) return 0; - if (c->error != Z3950_ERROR_NONE || - (!c->cs && task->which != Z3950_TASK_CONNECT)) + if (c->error != ZOOM_ERROR_NONE || + (!c->cs && task->which != ZOOM_TASK_CONNECT)) { - Z3950_connection_remove_tasks (c); + ZOOM_connection_remove_tasks (c); return 0; } - yaz_log (LOG_DEBUG, "Z3950_connection_exec_task type=%d", task->which); + yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d", + task->which, task->running); if (task->running) return 0; task->running = 1; switch (task->which) { - case Z3950_TASK_SEARCH: + case ZOOM_TASK_SEARCH: /* see if search hasn't been sent yet. */ - if (Z3950_connection_send_search (c)) + if (ZOOM_connection_send_search (c)) return 1; break; - case Z3950_TASK_RETRIEVE: + case ZOOM_TASK_RETRIEVE: if (send_present (c)) return 1; break; - case Z3950_TASK_CONNECT: + case ZOOM_TASK_CONNECT: if (do_connect(c)) return 1; + break; + case ZOOM_TASK_SCAN: + if (send_scan(c)) + return 1; + break; + case ZOOM_TASK_PACKAGE: + if (send_package(c)) + return 1; } - Z3950_connection_remove_task (c); + ZOOM_connection_remove_task (c); return 0; } -static int send_sort_present (Z3950_connection c) +static int send_sort_present (ZOOM_connection c) { int r = send_sort (c); if (!r) @@ -1173,19 +1908,40 @@ static int send_sort_present (Z3950_connection c) return r; } -static void handle_apdu (Z3950_connection c, Z_APDU *apdu) +static int es_response (ZOOM_connection c, + Z_ExtendedServicesResponse *res) +{ + if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE) + return 0; + if (res->diagnostics && res->num_diagnostics > 0) + response_diag(c, res->diagnostics[0]); + if (res->taskPackage && + res->taskPackage->which == Z_External_extendedService) + { + Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService; + Odr_oct *id = taskPackage->targetReference; + + if (id) + ZOOM_options_setl (c->tasks->u.package->options, + "targetReference", id->buf, id->len); + } + return 1; +} + + +static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) { Z_InitResponse *initrs; - yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which); c->mask = 0; + yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which); switch(apdu->which) { case Z_APDU_initResponse: initrs = apdu->u.initResponse; if (!*initrs->result) { - c->error = Z3950_ERROR_INIT; + c->error = ZOOM_ERROR_INIT; } else { @@ -1196,136 +1952,229 @@ static void handle_apdu (Z3950_connection c, Z_APDU *apdu) c->cookie_in = 0; if (cookie) c->cookie_in = xstrdup(cookie); + if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) && + ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3)) + c->support_named_resultsets = 1; if (c->tasks) { - assert (c->tasks->which == Z3950_TASK_CONNECT); - Z3950_connection_remove_task (c); + assert (c->tasks->which == ZOOM_TASK_CONNECT); + ZOOM_connection_remove_task (c); } - Z3950_connection_exec_task (c); + ZOOM_connection_exec_task (c); } + if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel)) + { + NMEM tmpmem = nmem_create(); + Z_CharSetandLanguageNegotiation *p = + yaz_get_charneg_record(initrs->otherInfo); + + if (p) + { + char *charset=NULL, *lang=NULL; + int selected; + + yaz_get_response_charneg(tmpmem, p, &charset, &lang, &selected); + yaz_log(LOG_DEBUG, "Target accepted: charset - %s," + "language - %s, select - %d", + charset, lang, selected); + + nmem_destroy(tmpmem); + } + } break; case Z_APDU_searchResponse: handle_search_response (c, apdu->u.searchResponse); if (!send_sort_present (c)) - Z3950_connection_remove_task (c); + ZOOM_connection_remove_task (c); break; case Z_APDU_presentResponse: handle_present_response (c, apdu->u.presentResponse); if (!send_present (c)) - Z3950_connection_remove_task (c); + ZOOM_connection_remove_task (c); break; case Z_APDU_sortResponse: sort_response (c, apdu->u.sortResponse); if (!send_present (c)) - Z3950_connection_remove_task (c); + ZOOM_connection_remove_task (c); + break; + case Z_APDU_scanResponse: + scan_response (c, apdu->u.scanResponse); + ZOOM_connection_remove_task (c); + break; + case Z_APDU_extendedServicesResponse: + es_response (c, apdu->u.extendedServicesResponse); + ZOOM_connection_remove_task (c); + break; + case Z_APDU_close: + if (c->reconnect_ok) + { + do_close(c); + c->tasks->running = 0; + ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); + } + else + { + c->error = ZOOM_ERROR_CONNECTION_LOST; + do_close(c); + } + break; + default: + c->error = ZOOM_ERROR_DECODE; + do_close(c); } } -static int do_read (Z3950_connection c) +static int do_read (ZOOM_connection c) { int r; Z_APDU *apdu; + ZOOM_Event event; + event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA); + ZOOM_connection_put_event (c, event); + + yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in); + r = cs_get (c->cs, &c->buf_in, &c->len_in); if (r == 1) return 0; if (r <= 0) { - c->error= Z3950_ERROR_CONNECTION_LOST; - do_close (c); + if (c->reconnect_ok) + { + do_close (c); + c->reconnect_ok = 0; + yaz_log (LOG_DEBUG, "reconnect read"); + c->tasks->running = 0; + ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); + } + else + { + c->error= ZOOM_ERROR_CONNECTION_LOST; + do_close (c); + } } else { + ZOOM_Event event; odr_reset (c->odr_in); odr_setbuf (c->odr_in, c->buf_in, r, 0); + event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU); + ZOOM_connection_put_event (c, event); if (!z_APDU (c->odr_in, &apdu, 0, 0)) { - c->error = Z3950_ERROR_DECODE; + c->error = ZOOM_ERROR_DECODE; do_close (c); } else - { handle_apdu (c, apdu); - } + c->reconnect_ok = 0; } return 1; } -static int do_write_ex (Z3950_connection c, char *buf_out, int len_out) +static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out) { int r; + ZOOM_Event event; + 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) { + if (c->reconnect_ok) + { + do_close (c); + c->reconnect_ok = 0; + yaz_log (LOG_DEBUG, "reconnect write"); + c->tasks->running = 0; + ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); + return 0; + } if (c->state == STATE_CONNECTING) - c->error = Z3950_ERROR_CONNECT; + c->error = ZOOM_ERROR_CONNECT; else - c->error = Z3950_ERROR_CONNECTION_LOST; + c->error = ZOOM_ERROR_CONNECTION_LOST; do_close (c); return 1; } else if (r == 1) - { - c->state = STATE_ESTABLISHED; - c->mask = Z3950_SELECT_READ|Z3950_SELECT_WRITE|Z3950_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 = Z3950_SELECT_READ|Z3950_SELECT_EXCEPT; + // c->reconnect_ok = 0; + c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT; + yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask); } return 0; } -static int do_write(Z3950_connection c) +static int do_write(ZOOM_connection c) { return do_write_ex (c, c->buf_out, c->len_out); } -const char *Z3950_connection_option_get (Z3950_connection c, const char *key) +ZOOM_API(const char *) +ZOOM_connection_option_get (ZOOM_connection c, const char *key) { - return Z3950_options_get (c->options, key); + return ZOOM_options_get (c->options, key); } -void Z3950_connection_option_set (Z3950_connection c, const char *key, +ZOOM_API(void) +ZOOM_connection_option_set (ZOOM_connection c, const char *key, const char *val) { - Z3950_options_set (c->options, key, val); + ZOOM_options_set (c->options, key, val); } -const char *Z3950_resultset_option_get (Z3950_resultset r, const char *key) +ZOOM_API(const char *) +ZOOM_resultset_option_get (ZOOM_resultset r, const char *key) { - return Z3950_options_get (r->options, key); + return ZOOM_options_get (r->options, key); } -void Z3950_resultset_option_set (Z3950_resultset r, const char *key, +ZOOM_API(void) +ZOOM_resultset_option_set (ZOOM_resultset r, const char *key, const char *val) { - Z3950_options_set (r->options, key, val); + ZOOM_options_set (r->options, key, val); } -int Z3950_connection_errcode (Z3950_connection c) +ZOOM_API(int) +ZOOM_connection_errcode (ZOOM_connection c) { - return Z3950_connection_error (c, 0, 0); + return ZOOM_connection_error (c, 0, 0); } -const char *Z3950_connection_errmsg (Z3950_connection c) +ZOOM_API(const char *) +ZOOM_connection_errmsg (ZOOM_connection c) { const char *msg; - Z3950_connection_error (c, &msg, 0); + ZOOM_connection_error (c, &msg, 0); return msg; } -const char *Z3950_connection_addinfo (Z3950_connection c) +ZOOM_API(const char *) +ZOOM_connection_addinfo (ZOOM_connection c) { const char *addinfo; - Z3950_connection_error (c, 0, &addinfo); + ZOOM_connection_error (c, 0, &addinfo); return addinfo; } -int Z3950_connection_error (Z3950_connection c, const char **cp, +ZOOM_API(int) +ZOOM_connection_error (ZOOM_connection c, const char **cp, const char **addinfo) { int error = c->error; @@ -1333,23 +2182,23 @@ int Z3950_connection_error (Z3950_connection c, const char **cp, { switch (error) { - case Z3950_ERROR_NONE: + case ZOOM_ERROR_NONE: *cp = "No error"; break; - case Z3950_ERROR_CONNECT: + case ZOOM_ERROR_CONNECT: *cp = "Connect failed"; break; - case Z3950_ERROR_MEMORY: + case ZOOM_ERROR_MEMORY: *cp = "Out of memory"; break; - case Z3950_ERROR_ENCODE: + case ZOOM_ERROR_ENCODE: *cp = "Encoding failed"; break; - case Z3950_ERROR_DECODE: + case ZOOM_ERROR_DECODE: *cp = "Decoding failed"; break; - case Z3950_ERROR_CONNECTION_LOST: + case ZOOM_ERROR_CONNECTION_LOST: *cp = "Connection lost"; break; - case Z3950_ERROR_INIT: + case ZOOM_ERROR_INIT: *cp = "Init rejected"; break; - case Z3950_ERROR_INTERNAL: + case ZOOM_ERROR_INTERNAL: *cp = "Internal failure"; break; - case Z3950_ERROR_TIMEOUT: + case ZOOM_ERROR_TIMEOUT: *cp = "Timeout"; break; default: *cp = diagbib1_str (error); @@ -1365,73 +2214,73 @@ int Z3950_connection_error (Z3950_connection c, const char **cp, return c->error; } -int Z3950_connection_do_io(Z3950_connection c, int mask) +static int ZOOM_connection_do_io(ZOOM_connection c, int mask) { - Z3950_Event event; -#if 0 + ZOOM_Event event = 0; int r = cs_look(c->cs); - yaz_log (LOG_LOG, "Z3950_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) { - c->error = Z3950_ERROR_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) { - yaz_log (LOG_LOG, "calling rcvconnect"); - if (cs_rcvconnect (c->cs) < 0) - { - c->error = Z3950_ERROR_CONNECT; - do_close (c); - } - else - Z3950_connection_send_init (c); - } - else - { - if (mask & Z3950_SELECT_READ) - do_read (c); - if (c->cs && (mask & Z3950_SELECT_WRITE)) - do_write (c); - } -#else - yaz_log (LOG_DEBUG, "Z3950_connection_do_io c=%p mask=%d", c, mask); - if (c->state == STATE_CONNECTING) - { - if (mask & Z3950_SELECT_WRITE) - Z3950_connection_send_init (c); - else - { - c->error = Z3950_ERROR_CONNECT; - do_close (c); - } - } - else if (c->state == STATE_ESTABLISHED) - { - if (mask & Z3950_SELECT_READ) - do_read (c); - if (c->cs && (mask & Z3950_SELECT_WRITE)) - do_write (c); + 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); + } + else if (ret == 0) + { + ZOOM_connection_put_event (c, event); + ZOOM_connection_send_init (c); + c->state = STATE_ESTABLISHED; + } + else + { + c->error = ZOOM_ERROR_CONNECT; + do_close (c); + ZOOM_connection_put_event (c, event); + } } else { - c->error = Z3950_ERROR_INTERNAL; - do_close (c); + if (mask & ZOOM_SELECT_READ) + do_read (c); + if (c->cs && (mask & ZOOM_SELECT_WRITE)) + do_write (c); } -#endif - event = Z3950_Event_create (1); - Z3950_connection_put_event (c, event); return 1; } +ZOOM_API(int) +ZOOM_connection_last_event(ZOOM_connection cs) +{ + if (!cs) + return ZOOM_EVENT_NONE; + return cs->last_event; +} -int Z3950_event (int no, Z3950_connection *cs) +ZOOM_API(int) +ZOOM_event (int no, ZOOM_connection *cs) { #if HAVE_SYS_POLL_H struct pollfd pollfds[1024]; - Z3950_connection poll_cs[1024]; + ZOOM_connection poll_cs[1024]; #else struct timeval tv; fd_set input, output, except; @@ -1441,24 +2290,31 @@ int Z3950_event (int no, Z3950_connection *cs) for (i = 0; imask) { int mask = 0; if (pollfds[i].revents & POLLIN) - mask += Z3950_SELECT_READ; + mask += ZOOM_SELECT_READ; if (pollfds[i].revents & POLLOUT) - mask += Z3950_SELECT_WRITE; + mask += ZOOM_SELECT_WRITE; if (pollfds[i].revents & POLLERR) - mask += Z3950_SELECT_EXCEPT; + mask += ZOOM_SELECT_EXCEPT; if (mask) - Z3950_connection_do_io(c, mask); + ZOOM_connection_do_io(c, mask); } else if (r == 0 && c->mask) { - Z3950_Event event = Z3950_Event_create(0); + ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT); /* timeout and this connection was waiting */ - c->error = Z3950_ERROR_TIMEOUT; + c->error = ZOOM_ERROR_TIMEOUT; do_close (c); - Z3950_connection_put_event(c, event); + ZOOM_connection_put_event(c, event); } } #else @@ -1552,7 +2406,7 @@ int Z3950_event (int no, Z3950_connection *cs) yaz_log (LOG_DEBUG, "select stop, returned r=%d", r); for (i = 0; imask) { - Z3950_Event event = Z3950_Event_create(0); + ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT); /* timeout and this connection was waiting */ - c->error = Z3950_ERROR_TIMEOUT; + c->error = ZOOM_ERROR_TIMEOUT; do_close (c); - yaz_log (LOG_LOG, "timeout"); - Z3950_connection_put_event(c, event); + yaz_log (LOG_DEBUG, "timeout"); + ZOOM_connection_put_event(c, event); } } #endif for (i = 0; i