X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=zutil%2Fzoom-c.c;h=a414e45abdeeffe64c472f6b5552726e5d81d7c2;hb=c620a713f1418315efcd4e6fab225e036775d365;hp=58a51bfd23fb01553044efa0592ec10cb90eb172;hpb=141269ce5f566fcd7ed188ebe9bb878bf4b32018;p=yaz-moved-to-github.git diff --git a/zutil/zoom-c.c b/zutil/zoom-c.c index 58a51bf..a414e45 100644 --- a/zutil/zoom-c.c +++ b/zutil/zoom-c.c @@ -1,25 +1,36 @@ /* - * $Id: zoom-c.c,v 1.1 2002-09-16 18:45:14 adam Exp $ + * Copyright (c) 2000-2003, Index Data + * See the file LICENSE for details. + * + * $Id: zoom-c.c,v 1.17 2003-02-12 15:06:44 adam Exp $ * * ZOOM layer for C, connections, result sets, queries. */ #include +#include +#include "zoom-p.h" + #include #include #include #include +#include #include #include #include -#include "zoom-p.h" #if HAVE_SYS_POLL_H #include #endif -static int ZOOM_connection_send_init (ZOOM_connection c); -static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out); +typedef enum { + zoom_pending, + zoom_complete +} zoom_ret; + +static zoom_ret ZOOM_connection_send_init (ZOOM_connection c); +static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out); static ZOOM_Event ZOOM_Event_create (int kind) { @@ -70,6 +81,25 @@ static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c) return event; } +static void set_bib1_error (ZOOM_connection c, int error) +{ + xfree (c->addinfo); + c->addinfo = 0; + c->error = error; + c->diagset = "Bib-1"; +} + +static void set_ZOOM_error (ZOOM_connection c, int error, + const char *addinfo) +{ + xfree (c->addinfo); + c->addinfo = 0; + c->error = error; + c->diagset = "ZOOM"; + if (addinfo) + c->addinfo = xstrdup(addinfo); +} + static void clear_error (ZOOM_connection c) { @@ -81,11 +111,10 @@ static void clear_error (ZOOM_connection c) case ZOOM_ERROR_CONNECTION_LOST: case ZOOM_ERROR_INIT: case ZOOM_ERROR_INTERNAL: + case ZOOM_ERROR_UNSUPPORTED_PROTOCOL: break; default: - c->error = ZOOM_ERROR_NONE; - xfree (c->addinfo); - c->addinfo = 0; + set_ZOOM_error(c, ZOOM_ERROR_NONE, 0); } } @@ -158,12 +187,13 @@ ZOOM_connection_create (ZOOM_options options) { ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c)); + c->soap = 0; c->cs = 0; c->mask = 0; c->reconnect_ok = 0; c->state = STATE_IDLE; - c->error = ZOOM_ERROR_NONE; c->addinfo = 0; + set_ZOOM_error(c, ZOOM_ERROR_NONE, 0); c->buf_in = 0; c->len_in = 0; c->buf_out = 0; @@ -317,7 +347,7 @@ ZOOM_connection_connect(ZOOM_connection c, c->async = ZOOM_options_get_bool (c->options, "async", 0); - c->error = ZOOM_ERROR_NONE; + set_ZOOM_error(c, ZOOM_ERROR_NONE, 0); task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT); @@ -334,7 +364,7 @@ ZOOM_query_create(void) ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s)); s->refcount = 1; - s->query = 0; + s->z_query = 0; s->sort_spec = 0; s->odr = odr_createmem (ODR_ENCODE); @@ -359,15 +389,33 @@ ZOOM_query_destroy(ZOOM_query s) ZOOM_API(int) ZOOM_query_prefix(ZOOM_query s, const char *str) { - 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) + s->z_query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->z_query)); + s->z_query->which = Z_Query_type_1; + s->z_query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str); + if (!s->z_query->u.type_1) return -1; return 0; } ZOOM_API(int) +ZOOM_query_cql(ZOOM_query s, const char *str) +{ + Z_External *ext; + + ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext)); + ext->direct_reference = odr_getoidbystr(s->odr, "1.2.840.10003.16.2"); + ext->indirect_reference = 0; + ext->descriptor = 0; + ext->which = Z_External_CQL; + ext->u.cql = odr_strdup(s->odr, str); + + s->z_query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->z_query)); + s->z_query->which = Z_Query_type_104; + s->z_query->u.type_104 = ext; + return 0; +} + +ZOOM_API(int) ZOOM_query_sortby(ZOOM_query s, const char *criteria) { s->sort_spec = yaz_sort_spec (s->odr, criteria); @@ -376,7 +424,7 @@ ZOOM_query_sortby(ZOOM_query s, const char *criteria) return 0; } -static int do_write(ZOOM_connection c); +static zoom_ret do_write(ZOOM_connection c); ZOOM_API(void) ZOOM_connection_destroy(ZOOM_connection c) @@ -384,6 +432,10 @@ ZOOM_connection_destroy(ZOOM_connection c) ZOOM_resultset r; if (!c) return; +#if HAVE_GSOAP + if (c->soap) + soap_end(c->soap); +#endif if (c->cs) cs_close (c->cs); for (r = c->resultsets; r; r = r->next) @@ -408,12 +460,17 @@ ZOOM_connection_destroy(ZOOM_connection c) void ZOOM_resultset_addref (ZOOM_resultset r) { if (r) + { (r->refcount)++; + yaz_log (LOG_DEBUG, "ZOOM_resultset_addref r=%p count=%d", + r, r->refcount); + } } ZOOM_resultset ZOOM_resultset_create () { ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r)); + yaz_log (LOG_DEBUG, "ZOOM_resultset_create r = %p", r); r->refcount = 1; r->size = 0; r->odr = odr_createmem (ODR_ENCODE); @@ -423,7 +480,7 @@ ZOOM_resultset ZOOM_resultset_create () r->count = 0; r->record_cache = 0; r->r_sort_spec = 0; - r->r_query = 0; + r->z_query = 0; r->search = 0; r->connection = 0; r->next = 0; @@ -451,7 +508,7 @@ ZOOM_connection_search(ZOOM_connection c, ZOOM_query q) const char *cp; r->r_sort_spec = q->sort_spec; - r->r_query = q->query; + r->z_query = q->z_query; r->search = q; r->options = ZOOM_options_create_with_parent(c->options); @@ -488,7 +545,8 @@ ZOOM_resultset_destroy(ZOOM_resultset r) if (!r) return; (r->refcount)--; - yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount); + yaz_log (LOG_DEBUG, "ZOOM_resultset_destroy r = %p count=%d", + r, r->refcount); if (r->refcount == 0) { ZOOM_record_cache rc; @@ -576,7 +634,7 @@ ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs, } } -static int do_connect (ZOOM_connection c) +static zoom_ret do_connect (ZOOM_connection c) { void *add; const char *effective_host; @@ -589,33 +647,47 @@ static int do_connect (ZOOM_connection c) yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host); assert (!c->cs); - c->cs = cs_create_host (effective_host, 0, &add); - if (c->cs) + if (memcmp(c->host_port, "http:", 5) == 0) { - int ret = cs_connect (c->cs, add); - if (ret == 0) +#if HAVE_GSOAP + c->soap = soap_new(); + c->soap->namespaces = srw_namespaces; +#else + c->state = STATE_IDLE; + set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW"); +#endif + } + else + { + c->cs = cs_create_host (effective_host, 0, &add); + + if (c->cs) { - 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; + int ret = cs_connect (c->cs, add); + 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 zoom_pending; + } + else if (ret > 0) + { + c->state = STATE_CONNECTING; + 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 zoom_pending; + } } - else if (ret > 0) - { - c->state = STATE_CONNECTING; - 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; + set_ZOOM_error(c, ZOOM_ERROR_CONNECT, effective_host); } - c->state = STATE_IDLE; - c->error = ZOOM_ERROR_CONNECT; - return 0; + return zoom_complete; } int z3950_connection_socket(ZOOM_connection c) @@ -692,29 +764,33 @@ static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out) z_APDU(odr_pr, &a, 0, 0); odr_destroy(odr_pr); } - c->error = ZOOM_ERROR_ENCODE; - do_close (c); + yaz_log (LOG_DEBUG, "encoding failed"); + set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0); + odr_reset(out); return -1; } return 0; } -static int send_APDU (ZOOM_connection c, Z_APDU *a) +static zoom_ret send_APDU (ZOOM_connection c, Z_APDU *a) { ZOOM_Event event; assert (a); if (encode_APDU(c, a, c->odr_out)) - return -1; + return zoom_complete; 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 do_write (c); } -static int ZOOM_connection_send_init (ZOOM_connection c) +/* returns 1 if PDU was sent OK (still pending ) + 0 if PDU was not sent OK (nothing to wait for) +*/ + +static zoom_ret ZOOM_connection_send_init (ZOOM_connection c) { const char *impname; Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest); @@ -818,12 +894,10 @@ static int ZOOM_connection_send_init (ZOOM_connection c) } } assert (apdu); - send_APDU (c, apdu); - - return 0; + return send_APDU (c, apdu); } -static int ZOOM_connection_send_search (ZOOM_connection c) +static zoom_ret ZOOM_connection_send_search (ZOOM_connection c) { ZOOM_resultset r; int lslb, ssub, mspn; @@ -856,10 +930,10 @@ static int ZOOM_connection_send_search (ZOOM_connection c) mediumSetElementSetName = elementSetName; assert (r); - assert (r->r_query); + assert (r->z_query); /* prepare query for the search request */ - search_req->query = r->r_query; + search_req->query = r->z_query; search_req->databaseNames = set_DatabaseNames (c, r->options, &search_req->num_databaseNames); @@ -882,9 +956,9 @@ static int ZOOM_connection_send_search (ZOOM_connection c) { /* Regular piggyback - do it unless we're going to do sort */ *search_req->largeSetLowerBound = 2000000000; - *search_req->smallSetUpperBound = r->count; + *search_req->smallSetUpperBound = 0; *search_req->mediumSetPresentNumber = r->count; - smallSetElementSetName = 0; /* no need to provide this */ + smallSetElementSetName = 0; } else { @@ -934,7 +1008,7 @@ static int ZOOM_connection_send_search (ZOOM_connection c) break; } r->setname = xstrdup (setname); - yaz_log (LOG_DEBUG, "allocating %s", r->setname); + yaz_log (LOG_DEBUG, "allocating set %s", r->setname); } else r->setname = xstrdup ("default"); @@ -942,12 +1016,12 @@ static int ZOOM_connection_send_search (ZOOM_connection c) } search_req->resultSetName = odr_strdup(c->odr_out, r->setname); /* send search request */ - send_APDU (c, apdu); - return 1; + return send_APDU (c, apdu); } static void response_diag (ZOOM_connection c, Z_DiagRec *p) { + int oclass; Z_DefaultDiagFormat *r; char *addinfo = 0; @@ -955,10 +1029,12 @@ static void response_diag (ZOOM_connection c, Z_DiagRec *p) c->addinfo = 0; if (p->which != Z_DiagRec_defaultFormat) { - c->error = ZOOM_ERROR_DECODE; + set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0); return; } r = p->u.defaultFormat; + c->diagset = yaz_z3950oid_to_str(r->diagnosticSetId, &oclass); + switch (r->which) { case Z_DefaultDiagFormat_v2Addinfo: @@ -1041,18 +1117,19 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len) } else if (!strcmp (type, "syntax")) { + const char *desc = 0; if (npr->which == Z_NamePlusRecord_databaseRecord) { 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; - } + desc = ent->desc; } - return "none"; + if (!desc) + desc = "none"; + if (len) + *len = strlen(desc); + return desc; } else if (!strcmp (type, "render") && npr->which == Z_NamePlusRecord_databaseRecord) @@ -1067,6 +1144,7 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len) } else if (r->which == Z_External_octet) { + yaz_marc_t mt; switch (ent->value) { case VAL_SOIF: @@ -1079,29 +1157,39 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len) default: if (!rec->wrbuf_marc) rec->wrbuf_marc = wrbuf_alloc(); + + mt = yaz_marc_create(); 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 (yaz_marc_decode_wrbuf ( + mt, (const char *) r->u.octet_aligned->buf, + r->u.octet_aligned->len, + rec->wrbuf_marc) > 0) { - if (len) *len = wrbuf_len(rec->wrbuf_marc); + if (len) + *len = wrbuf_len(rec->wrbuf_marc); + yaz_marc_destroy(mt); return wrbuf_buf(rec->wrbuf_marc); } + yaz_marc_destroy(mt); } - if (len) *len = r->u.octet_aligned->len; + 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"; + if (!rec->wrbuf_marc) + rec->wrbuf_marc = wrbuf_alloc(); + wrbuf_rewind (rec->wrbuf_marc); + yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0); + if (len) + *len = wrbuf_len(rec->wrbuf_marc); + return wrbuf_buf(rec->wrbuf_marc); } return 0; } - else if (!strcmp (type, "xml") && - npr->which == Z_NamePlusRecord_databaseRecord) + else if (npr->which == Z_NamePlusRecord_databaseRecord && + (!strcmp (type, "xml") || !strcmp(type, "oai"))) { Z_External *r = (Z_External *) npr->u.databaseRecord; oident *ent = oid_getentbyoid(r->direct_reference); @@ -1113,6 +1201,11 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len) } else if (r->which == Z_External_octet) { + yaz_marc_t mt; + int marc_decode_type = YAZ_MARC_MARCXML; + + if (!strcmp(type, "oai")) + marc_decode_type = YAZ_MARC_OAIMARC; switch (ent->value) { case VAL_SOIF: @@ -1126,15 +1219,20 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len) 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) + mt = yaz_marc_create(); + + yaz_marc_xml(mt, YAZ_MARC_MARCXML); + if (yaz_marc_decode_wrbuf ( + mt, (const char *) r->u.octet_aligned->buf, + r->u.octet_aligned->len, + rec->wrbuf_marc) > 0) { - if (len) *len = wrbuf_len(rec->wrbuf_marc); + if (len) + *len = wrbuf_len(rec->wrbuf_marc); + yaz_marc_destroy(mt); return wrbuf_buf(rec->wrbuf_marc); } + yaz_marc_destroy(mt); } if (len) *len = r->u.octet_aligned->len; return (const char *) r->u.octet_aligned->buf; @@ -1187,7 +1285,6 @@ static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, int pos) const char *syntax = ZOOM_resultset_option_get (r, "preferredRecordSyntax"); - for (rc = r->record_cache; rc; rc = rc->next) { if (pos == rc->pos) @@ -1284,7 +1381,7 @@ static void handle_records (ZOOM_connection c, Z_Records *sr, if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1) response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]); else - c->error = ZOOM_ERROR_DECODE; + set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0); } else { @@ -1309,13 +1406,13 @@ static void handle_records (ZOOM_connection c, Z_Records *sr, if (present_phase && p->num_records == 0) { /* present response and we didn't get any records! */ - c->error = ZOOM_ERROR_DECODE; + set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0); } } else if (present_phase) { /* present response and we didn't get any records! */ - c->error = ZOOM_ERROR_DECODE; + set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0); } } } @@ -1372,19 +1469,19 @@ static int scan_response (ZOOM_connection c, Z_ScanResponse *res) return 1; } -static int send_sort (ZOOM_connection c) +static zoom_ret send_sort (ZOOM_connection c) { ZOOM_resultset resultset; if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH) - return 0; + return zoom_complete; resultset = c->tasks->u.search.resultset; if (c->error) { resultset->r_sort_spec = 0; - return 0; + return zoom_complete; } if (resultset->r_sort_spec) { @@ -1399,13 +1496,12 @@ static int send_sort (ZOOM_connection c) req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname); req->sortSequence = resultset->r_sort_spec; resultset->r_sort_spec = 0; - send_APDU (c, apdu); - return 1; + return send_APDU (c, apdu); } - return 0; + return zoom_complete; } -static int send_present (ZOOM_connection c) +static zoom_ret send_present (ZOOM_connection c) { Z_APDU *apdu = 0; Z_PresentRequest *req = 0; @@ -1416,7 +1512,7 @@ static int send_present (ZOOM_connection c) ZOOM_resultset resultset; if (!c->tasks) - return 0; + return zoom_complete; switch (c->tasks->which) { @@ -1429,12 +1525,12 @@ static int send_present (ZOOM_connection c) resultset->count = c->tasks->u.retrieve.count; if (resultset->start >= resultset->size) - return 0; + return zoom_complete; if (resultset->start + resultset->count > resultset->size) resultset->count = resultset->size - resultset->start; break; default: - return 0; + return zoom_complete; } syntax = ZOOM_resultset_option_get (resultset, "preferredRecordSyntax"); @@ -1442,9 +1538,9 @@ static int send_present (ZOOM_connection c) schema = ZOOM_resultset_option_get (resultset, "schema"); if (c->error) /* don't continue on error */ - return 0; + return zoom_complete; if (resultset->start < 0) - return 0; + return zoom_complete; for (i = 0; icount; i++) { ZOOM_record rec = @@ -1453,7 +1549,7 @@ static int send_present (ZOOM_connection c) break; } if (i == resultset->count) - return 0; + return zoom_complete; apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest); req = apdu->u.presentRequest; @@ -1484,14 +1580,15 @@ static int send_present (ZOOM_connection c) compo->u.complex->generic = (Z_Specification *) odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic)); - compo->u.complex->generic->schema = (Odr_oid *) + compo->u.complex->generic->which = Z_Specification_oid; + compo->u.complex->generic->u.oid = (Odr_oid *) yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema); - if (!compo->u.complex->generic->schema) + if (!compo->u.complex->generic->u.oid) { /* OID wasn't a schema! Try record syntax instead. */ - compo->u.complex->generic->schema = (Odr_oid *) + compo->u.complex->generic->u.oid = (Odr_oid *) yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema); } if (elementSetName && *elementSetName) @@ -1524,8 +1621,7 @@ static int send_present (ZOOM_connection c) req->recordComposition = compo; } req->resultSetId = odr_strdup(c->odr_out, resultset->setname); - send_APDU (c, apdu); - return 1; + return send_APDU (c, apdu); } ZOOM_API(ZOOM_scanset) @@ -1571,28 +1667,27 @@ ZOOM_scanset_destroy (ZOOM_scanset scan) } } -static int send_package (ZOOM_connection c) +static zoom_ret send_package (ZOOM_connection c) { ZOOM_Event event; if (!c->tasks) - return 0; + return zoom_complete; 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; + + return do_write_ex (c, c->tasks->u.package->buf_out, + c->tasks->u.package->len_out); } -static int send_scan (ZOOM_connection c) +static zoom_ret send_scan (ZOOM_connection c) { 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; + return zoom_complete; assert (c->tasks->which == ZOOM_TASK_SCAN); scan = c->tasks->u.scan.scan; @@ -1613,9 +1708,7 @@ static int send_scan (ZOOM_connection c) req->databaseNames = set_DatabaseNames (c, scan->options, &req->num_databaseNames); - send_APDU (c, apdu); - - return 1; + return send_APDU (c, apdu); } ZOOM_API(size_t) @@ -1665,8 +1758,6 @@ ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key, ZOOM_options_set (scan->options, key, val); } - - static Z_APDU *create_es_package (ZOOM_package p, int type) { const char *str; @@ -1744,7 +1835,8 @@ static Z_External *encode_ill_request (ZOOM_package p) 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->buf = (unsigned char*) + 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); @@ -1754,7 +1846,7 @@ static Z_External *encode_ill_request (ZOOM_package p) static Z_ItemOrder *encode_item_order(ZOOM_package p) { - Z_ItemOrder *req = odr_malloc (p->odr_out, sizeof(*req)); + Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc (p->odr_out, sizeof(*req)); const char *str; req->which=Z_IOItemOrder_esRequest; @@ -1765,7 +1857,7 @@ static Z_ItemOrder *encode_item_order(ZOOM_package p) 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 = + req->u.esRequest->toKeep->contact = (Z_IOContact *) odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact)); str = ZOOM_options_get(p->options, "contact-name"); @@ -1823,7 +1915,7 @@ ZOOM_API(void) apdu = create_es_package (p, VAL_ITEMORDER); if (apdu) { - r = odr_malloc (p->odr_out, sizeof(*r)); + r = (Z_External *) odr_malloc (p->odr_out, sizeof(*r)); r->direct_reference = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV, @@ -1845,7 +1937,7 @@ ZOOM_API(void) 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); + p->buf_out = (char *) xmalloc (p->len_out); memcpy (p->buf_out, buf, p->len_out); (p->refcount)++; @@ -1894,6 +1986,84 @@ ZOOM_package_option_get (ZOOM_package p, const char *key) return ZOOM_options_get (p->options, key); } +#if HAVE_GSOAP +static zoom_ret ZOOM_srw_search(ZOOM_connection c, ZOOM_resultset r, + const char *cql) +{ + int ret; + struct xcql__operandType *xQuery = 0; + char *action = 0; + xsd__integer startRecord = r->start + 1; + xsd__integer maximumRecord = r->count; + const char *schema = ZOOM_resultset_option_get (r, "schema"); + struct zs__searchRetrieveResponse res; + xsd__string recordPacking = 0; + + + if (!schema) + schema = "http://www.loc.gov/marcxml/"; + + ret = soap_call_zs__searchRetrieveRequest(c->soap, c->host_port, + action, + &r->z_query->u.type_104->u.cql, + xQuery, + 0, 0, + &startRecord, &maximumRecord, + (char **) &schema, + &recordPacking, + &res); + if (ret != SOAP_OK) + { + const char **s = soap_faultdetail(c->soap); + xfree (c->addinfo); + c->addinfo = 0; + if (s && *s) + c->addinfo = xstrdup(*s); + c->diagset = "SOAP"; + c->error = ret; + } + else + { + if (res.diagnostics.__sizeDiagnostics > 0) + { + int i = 0; + xfree (c->addinfo); + c->addinfo = 0; + c->diagset = "SRW"; + c->error = res.diagnostics.diagnostic[i]->code; + if (res.diagnostics.diagnostic[i]->details) + c->addinfo = + xstrdup(res.diagnostics.diagnostic[i]->details); + + } + else + { + int i; + r->size = res.numberOfRecords; + if (res.resultSetId) + r->setname = xstrdup(res.resultSetId); + for (i = 0; i < res.records.__sizeRecords; i++) + { + char *rdata = res.records.record[i]->recordData; + if (rdata) + { + Z_NamePlusRecord *npr = + odr_malloc(r->odr, sizeof(*npr)); + Z_External *ext = + z_ext_record(r->odr, VAL_TEXT_XML, + rdata, strlen(rdata)); + npr->databaseName = 0; + npr->which = Z_NamePlusRecord_databaseRecord; + npr->u.databaseRecord = ext; + record_cache_add (r, npr, r->start + i); + } + } + } + } + return zoom_complete; +} +#endif + ZOOM_API(void) ZOOM_package_option_set (ZOOM_package p, const char *key, const char *val) @@ -1904,52 +2074,103 @@ ZOOM_package_option_set (ZOOM_package p, const char *key, static int ZOOM_connection_exec_task (ZOOM_connection c) { ZOOM_task task = c->tasks; + zoom_ret ret = zoom_complete; - yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task"); if (!task) - return 0; - if (c->error != ZOOM_ERROR_NONE || - (!c->cs && task->which != ZOOM_TASK_CONNECT)) { - ZOOM_connection_remove_tasks (c); + yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task task="); return 0; } yaz_log (LOG_DEBUG, "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); + ZOOM_connection_remove_tasks (c); + return 0; + } if (task->running) + { + yaz_log (LOG_DEBUG, "task already running"); return 0; + } task->running = 1; - switch (task->which) + ret = zoom_complete; + if (c->soap) { - case ZOOM_TASK_SEARCH: - /* see if search hasn't been sent yet. */ - if (ZOOM_connection_send_search (c)) - return 1; - break; - case ZOOM_TASK_RETRIEVE: - if (send_present (c)) - return 1; - break; - 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; +#if HAVE_GSOAP + ZOOM_resultset resultset; + switch (task->which) + { + case ZOOM_TASK_SEARCH: + resultset = c->tasks->u.search.resultset; + if (resultset->z_query && + resultset->z_query->which == Z_Query_type_104 + && resultset->z_query->u.type_104->which == Z_External_CQL) + ret = ZOOM_srw_search(c, resultset, + resultset->z_query->u.type_104->u.cql); + 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 zoom_complete; + if (resultset->start + resultset->count > resultset->size) + resultset->count = resultset->size - resultset->start; + + if (resultset->z_query && + resultset->z_query->which == Z_Query_type_104 + && resultset->z_query->u.type_104->which == Z_External_CQL) + ret = ZOOM_srw_search(c, resultset, + resultset->z_query->u.type_104->u.cql); + break; + } +#else + ; +#endif } - ZOOM_connection_remove_task (c); - return 0; + else if (c->cs || task->which == ZOOM_TASK_CONNECT) + { + switch (task->which) + { + case ZOOM_TASK_SEARCH: + ret = ZOOM_connection_send_search(c); + break; + case ZOOM_TASK_RETRIEVE: + ret = send_present (c); + break; + case ZOOM_TASK_CONNECT: + ret = do_connect(c); + break; + case ZOOM_TASK_SCAN: + ret = send_scan(c); + break; + case ZOOM_TASK_PACKAGE: + ret = send_package(c); + break; + } + } + else + { + yaz_log (LOG_DEBUG, "remove tasks because no connection exist"); + ZOOM_connection_remove_tasks (c); + } + if (ret == zoom_complete) + { + yaz_log (LOG_DEBUG, "task removed (complete)"); + ZOOM_connection_remove_task (c); + return 0; + } + yaz_log (LOG_DEBUG, "task pending"); + return 1; } -static int send_sort_present (ZOOM_connection c) +static zoom_ret send_sort_present (ZOOM_connection c) { - int r = send_sort (c); - if (!r) + zoom_ret r = send_sort (c); + if (r == zoom_complete) r = send_present (c); return r; } @@ -1969,7 +2190,7 @@ static int es_response (ZOOM_connection c, if (id) ZOOM_options_setl (c->tasks->u.package->options, - "targetReference", id->buf, id->len); + "targetReference", (char*) id->buf, id->len); } return 1; } @@ -1980,14 +2201,23 @@ static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) Z_InitResponse *initrs; c->mask = 0; - yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which); + yaz_log (LOG_DEBUG, "handle_apdu type=%d", apdu->which); switch(apdu->which) { case Z_APDU_initResponse: initrs = apdu->u.initResponse; + ZOOM_connection_option_set(c, "targetImplementationId", + initrs->implementationId ? + initrs->implementationId : ""); + ZOOM_connection_option_set(c, "targetImplementationName", + initrs->implementationName ? + initrs->implementationName : ""); + ZOOM_connection_option_set(c, "targetImplementationVersion", + initrs->implementationVersion ? + initrs->implementationVersion : ""); if (!*initrs->result) { - c->error = ZOOM_ERROR_INIT; + set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); } else { @@ -2017,30 +2247,35 @@ static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) 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); + int sel; + yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel); + yaz_log(LOG_DEBUG, "Target accepted: charset %s, " + "language %s, select %d", + charset ? charset : "none", lang ? lang : "none", sel); + if (charset) + ZOOM_connection_option_set (c, "negotiation-charset", + charset); + if (lang) + ZOOM_connection_option_set (c, "negotiation-lang", + lang); nmem_destroy(tmpmem); } } break; case Z_APDU_searchResponse: handle_search_response (c, apdu->u.searchResponse); - if (!send_sort_present (c)) + if (send_sort_present (c) == zoom_complete) ZOOM_connection_remove_task (c); break; case Z_APDU_presentResponse: handle_present_response (c, apdu->u.presentResponse); - if (!send_present (c)) + if (send_present (c) == zoom_complete) ZOOM_connection_remove_task (c); break; case Z_APDU_sortResponse: sort_response (c, apdu->u.sortResponse); - if (!send_present (c)) + if (send_present (c) == zoom_complete) ZOOM_connection_remove_task (c); break; case Z_APDU_scanResponse: @@ -2060,12 +2295,12 @@ static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) } else { - c->error = ZOOM_ERROR_CONNECTION_LOST; + set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0); do_close(c); } break; default: - c->error = ZOOM_ERROR_DECODE; + set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0); do_close(c); } } @@ -2096,7 +2331,7 @@ static int do_read (ZOOM_connection c) } else { - c->error= ZOOM_ERROR_CONNECTION_LOST; + set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0); do_close (c); } } @@ -2109,7 +2344,7 @@ static int do_read (ZOOM_connection c) ZOOM_connection_put_event (c, event); if (!z_APDU (c->odr_in, &apdu, 0, 0)) { - c->error = ZOOM_ERROR_DECODE; + set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0); do_close (c); } else @@ -2119,7 +2354,7 @@ static int do_read (ZOOM_connection c) return 1; } -static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out) +static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out) { int r; ZOOM_Event event; @@ -2137,14 +2372,14 @@ static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out) yaz_log (LOG_DEBUG, "reconnect write"); c->tasks->running = 0; ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); - return 0; + return zoom_complete; } if (c->state == STATE_CONNECTING) - c->error = ZOOM_ERROR_CONNECT; + set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0); else - c->error = ZOOM_ERROR_CONNECTION_LOST; + set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0); do_close (c); - return 1; + return zoom_complete; } else if (r == 1) { @@ -2157,14 +2392,13 @@ static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out) } else { - // 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; + return zoom_pending; } -static int do_write(ZOOM_connection c) +static zoom_ret do_write(ZOOM_connection c) { return do_write_ex (c, c->buf_out, c->len_out); } @@ -2219,47 +2453,74 @@ ZOOM_connection_addinfo (ZOOM_connection c) return addinfo; } +ZOOM_API(const char *) +ZOOM_diag_str (int error) +{ + switch (error) + { + case ZOOM_ERROR_NONE: + return "No error"; + case ZOOM_ERROR_CONNECT: + return "Connect failed"; + case ZOOM_ERROR_MEMORY: + return "Out of memory"; + case ZOOM_ERROR_ENCODE: + return "Encoding failed"; + case ZOOM_ERROR_DECODE: + return "Decoding failed"; + case ZOOM_ERROR_CONNECTION_LOST: + return "Connection lost"; + case ZOOM_ERROR_INIT: + return "Init rejected"; + case ZOOM_ERROR_INTERNAL: + return "Internal failure"; + case ZOOM_ERROR_TIMEOUT: + return "Timeout"; + case ZOOM_ERROR_UNSUPPORTED_PROTOCOL: + return "Unsupported protocol"; + default: + return diagbib1_str (error); + } +} + ZOOM_API(int) -ZOOM_connection_error (ZOOM_connection c, const char **cp, - const char **addinfo) +ZOOM_connection_error_x (ZOOM_connection c, const char **cp, + const char **addinfo, const char **diagset) { int error = c->error; if (cp) { - switch (error) - { - case ZOOM_ERROR_NONE: - *cp = "No error"; break; - case ZOOM_ERROR_CONNECT: - *cp = "Connect failed"; break; - case ZOOM_ERROR_MEMORY: - *cp = "Out of memory"; break; - case ZOOM_ERROR_ENCODE: - *cp = "Encoding failed"; break; - case ZOOM_ERROR_DECODE: - *cp = "Decoding failed"; break; - case ZOOM_ERROR_CONNECTION_LOST: - *cp = "Connection lost"; break; - case ZOOM_ERROR_INIT: - *cp = "Init rejected"; break; - case ZOOM_ERROR_INTERNAL: - *cp = "Internal failure"; break; - case ZOOM_ERROR_TIMEOUT: - *cp = "Timeout"; break; - default: - *cp = diagbib1_str (error); - } + if (!c->diagset) + *cp = ZOOM_diag_str(error); +#if HAVE_GSOAP + else if (!strcmp(c->diagset, "SRW")) + *cp = yaz_srw_diag_str(error); + else if (c->soap && !strcmp(c->diagset, "SOAP")) + { + const char **s = soap_faultstring(c->soap); + if (s && *s) + *cp = *s; + else + *cp = "unknown"; + } +#endif + else + *cp = ZOOM_diag_str(error); } if (addinfo) - { - if (c->addinfo) - *addinfo = c->addinfo; - else - *addinfo = ""; - } + *addinfo = c->addinfo ? c->addinfo : ""; + if (diagset) + *diagset = c->diagset ? c->diagset : ""; return c->error; } +ZOOM_API(int) +ZOOM_connection_error (ZOOM_connection c, const char **cp, + const char **addinfo) +{ + return ZOOM_connection_error_x(c, cp, addinfo, 0); +} + static int ZOOM_connection_do_io(ZOOM_connection c, int mask) { ZOOM_Event event = 0; @@ -2270,7 +2531,7 @@ static int ZOOM_connection_do_io(ZOOM_connection c, int mask) if (r == CS_NONE) { event = ZOOM_Event_create (ZOOM_EVENT_CONNECT); - c->error = ZOOM_ERROR_CONNECT; + set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0); do_close (c); ZOOM_connection_put_event (c, event); } @@ -2298,7 +2559,7 @@ static int ZOOM_connection_do_io(ZOOM_connection c, int mask) } else { - c->error = ZOOM_ERROR_CONNECT; + set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0); do_close (c); ZOOM_connection_put_event (c, event); } @@ -2447,7 +2708,7 @@ ZOOM_event (int no, ZOOM_connection *cs) { ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT); /* timeout and this connection was waiting */ - c->error = ZOOM_ERROR_TIMEOUT; + set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0); do_close (c); ZOOM_connection_put_event(c, event); } @@ -2483,7 +2744,7 @@ ZOOM_event (int no, ZOOM_connection *cs) { ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT); /* timeout and this connection was waiting */ - c->error = ZOOM_ERROR_TIMEOUT; + set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0); do_close (c); yaz_log (LOG_DEBUG, "timeout"); ZOOM_connection_put_event(c, event);