X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=zoom%2Fzoom-c.c;h=481f90405192d2602a5c3c96a39a100c8914f81b;hb=47f8bbd041e610b13dd25153872dfef2aa288c52;hp=862811bd8b95572a6bc16ab7618077be863b0d04;hpb=bd67bd91a1ef6340ba08db039aa04b6fc424fc8d;p=yaz-moved-to-github.git diff --git a/zoom/zoom-c.c b/zoom/zoom-c.c index 862811b..481f904 100644 --- a/zoom/zoom-c.c +++ b/zoom/zoom-c.c @@ -1,5 +1,5 @@ /* - * $Id: zoom-c.c,v 1.14 2001-11-30 08:24:06 adam Exp $ + * $Id: zoom-c.c,v 1.24 2002-02-28 13:21:16 adam Exp $ * * ZOOM layer for C, connections, result sets, queries. */ @@ -18,7 +18,7 @@ static ZOOM_Event ZOOM_Event_create (int kind) { - ZOOM_Event event = xmalloc (sizeof(*event)); + ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event)); event->kind = kind; event->next = 0; event->prev = 0; @@ -32,7 +32,6 @@ static void ZOOM_Event_destroy (ZOOM_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; @@ -50,7 +49,6 @@ static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event) static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c) { - // get from front of queue ZOOM_Event event = c->m_queue_front; if (!event) return 0; @@ -63,14 +61,27 @@ static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c) } else c->m_queue_back = 0; + c->last_event = event->kind; return event; } static void clear_error (ZOOM_connection c) { - c->error = ZOOM_ERROR_NONE; - xfree (c->addinfo); - c->addinfo = 0; + + switch (c->error) + { + case ZOOM_ERROR_CONNECT: + case ZOOM_ERROR_MEMORY: + case ZOOM_ERROR_DECODE: + case ZOOM_ERROR_CONNECTION_LOST: + case ZOOM_ERROR_INIT: + case ZOOM_ERROR_INTERNAL: + break; + default: + c->error = ZOOM_ERROR_NONE; + xfree (c->addinfo); + c->addinfo = 0; + } } ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which) @@ -78,7 +89,7 @@ ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which) ZOOM_task *taskp = &c->tasks; while (*taskp) taskp = &(*taskp)->next; - *taskp = xmalloc (sizeof(**taskp)); + *taskp = (ZOOM_task) xmalloc (sizeof(**taskp)); (*taskp)->running = 0; (*taskp)->which = which; (*taskp)->next = 0; @@ -103,6 +114,9 @@ void ZOOM_connection_remove_task (ZOOM_connection c) break; case ZOOM_TASK_CONNECT: break; + case ZOOM_TASK_SCAN: + ZOOM_scanset_destroy (task->u.scan.scan); + break; default: assert (0); } @@ -122,7 +136,7 @@ static ZOOM_record record_cache_lookup (ZOOM_resultset r, ZOOM_connection ZOOM_connection_create (ZOOM_options options) { - ZOOM_connection c = xmalloc (sizeof(*c)); + ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c)); c->cs = 0; c->mask = 0; @@ -149,6 +163,7 @@ ZOOM_connection ZOOM_connection_create (ZOOM_options options) 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,12 +172,13 @@ ZOOM_connection ZOOM_connection_create (ZOOM_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 (ZOOM_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 = ZOOM_options_get (con->options, "databaseName"); + const char *cp = ZOOM_options_get (options, "databaseName"); if (!cp || !*cp) { @@ -181,7 +197,8 @@ static char **set_DatabaseNames (ZOOM_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 +212,7 @@ static char **set_DatabaseNames (ZOOM_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; @@ -239,7 +256,9 @@ void ZOOM_connection_connect(ZOOM_connection c, ZOOM_options_set(c->options, "host", c->host_port); c->async = ZOOM_options_get_bool (c->options, "async", 0); - + + c->error = ZOOM_ERROR_NONE; + task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT); if (!c->async) @@ -251,7 +270,7 @@ void ZOOM_connection_connect(ZOOM_connection c, ZOOM_query ZOOM_query_create(void) { - ZOOM_query s = xmalloc (sizeof(*s)); + ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s)); s->refcount = 1; s->query = 0; @@ -277,7 +296,7 @@ void ZOOM_query_destroy(ZOOM_query s) 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) @@ -322,7 +341,7 @@ void ZOOM_resultset_addref (ZOOM_resultset r) } ZOOM_resultset ZOOM_resultset_create () { - ZOOM_resultset r = xmalloc (sizeof(*r)); + ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r)); r->refcount = 1; r->size = 0; @@ -398,6 +417,11 @@ void ZOOM_resultset_destroy(ZOOM_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 */ @@ -440,7 +464,6 @@ static void ZOOM_resultset_retrieve (ZOOM_resultset r, { ZOOM_task task; ZOOM_connection c; - const char *cp; if (!r) return; @@ -499,8 +522,11 @@ static int do_connect (ZOOM_connection c) if (ret >= 0) { c->state = STATE_CONNECTING; - c->mask = ZOOM_SELECT_READ | ZOOM_SELECT_WRITE | - ZOOM_SELECT_EXCEPT; + c->mask = ZOOM_SELECT_EXCEPT; + if (c->cs->io_pending & CS_WANT_WRITE) + c->mask += ZOOM_SELECT_WRITE; + if (c->cs->io_pending & CS_WANT_READ) + c->mask += ZOOM_SELECT_READ; return 1; } } @@ -551,15 +577,19 @@ static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out) do_close (c); return -1; } + return 0; } 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; @@ -570,7 +600,8 @@ 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)); + 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"); @@ -588,7 +619,7 @@ static int ZOOM_connection_send_init (ZOOM_connection c) 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) { @@ -604,26 +635,29 @@ static int ZOOM_connection_send_init (ZOOM_connection c) 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++; } @@ -637,7 +671,8 @@ static int ZOOM_connection_send_init (ZOOM_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; } @@ -689,7 +724,7 @@ static int ZOOM_connection_send_search (ZOOM_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 = ZOOM_options_get (r->options, "preferredRecordSyntax"); @@ -722,7 +757,8 @@ static int ZOOM_connection_send_search (ZOOM_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 +766,8 @@ static int ZOOM_connection_send_search (ZOOM_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); @@ -811,7 +848,7 @@ ZOOM_record ZOOM_record_clone (ZOOM_record srec) 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); @@ -842,7 +879,7 @@ void ZOOM_record_destroy (ZOOM_record rec) xfree (rec); } -void *ZOOM_record_get (ZOOM_record rec, const char *type, size_t *len) +const char *ZOOM_record_get (ZOOM_record rec, const char *type, int *len) { Z_NamePlusRecord *npr; if (!rec) @@ -865,60 +902,122 @@ void *ZOOM_record_get (ZOOM_record rec, const char *type, size_t *len) } return "none"; } - else if (!strcmp (type, "render")) + else if (!strcmp (type, "render") && + npr->which == Z_NamePlusRecord_databaseRecord) + { + Z_External *r = (Z_External *) npr->u.databaseRecord; + oident *ent = oid_getentbyoid(r->direct_reference); + + if (r->which == Z_External_sutrs) + { + *len = r->u.sutrs->len; + return (const char *) r->u.sutrs->buf; + } + else if (r->which == Z_External_octet) + { + switch (ent->value) + { + case VAL_SOIF: + case VAL_HTML: + case VAL_SUTRS: + break; + case VAL_TEXT_XML: + case VAL_APPLICATION_XML: + break; + default: + if (!rec->wrbuf_marc) + rec->wrbuf_marc = wrbuf_alloc(); + wrbuf_rewind (rec->wrbuf_marc); + if (yaz_marc_decode ((const char *) + r->u.octet_aligned->buf, + rec->wrbuf_marc, 0, + r->u.octet_aligned->len, + 0) > 0) + { + *len = wrbuf_len(rec->wrbuf_marc); + return wrbuf_buf(rec->wrbuf_marc); + } + } + *len = r->u.octet_aligned->len; + return (const char *) r->u.octet_aligned->buf; + } + else if (r->which == Z_External_grs1) + { + *len = 5; + return "GRS-1"; + } + return 0; + } + else if (!strcmp (type, "xml") && + npr->which == Z_NamePlusRecord_databaseRecord) + { + Z_External *r = (Z_External *) npr->u.databaseRecord; + oident *ent = oid_getentbyoid(r->direct_reference); + + if (r->which == Z_External_sutrs) + { + *len = r->u.sutrs->len; + return (const char *) r->u.sutrs->buf; + } + else if (r->which == Z_External_octet) + { + switch (ent->value) + { + case VAL_SOIF: + case VAL_HTML: + case VAL_SUTRS: + break; + case VAL_TEXT_XML: + case VAL_APPLICATION_XML: + break; + default: + if (!rec->wrbuf_marc) + rec->wrbuf_marc = wrbuf_alloc(); + wrbuf_rewind (rec->wrbuf_marc); + if (yaz_marc_decode ((const char *) + r->u.octet_aligned->buf, + rec->wrbuf_marc, 0, + r->u.octet_aligned->len, + 1) > 0) + { + *len = wrbuf_len(rec->wrbuf_marc); + return wrbuf_buf(rec->wrbuf_marc); + } + } + *len = r->u.octet_aligned->len; + return (const char *) r->u.octet_aligned->buf; + } + else if (r->which == Z_External_grs1) + { + *len = 5; + return "GRS-1"; + } + return 0; + } + else if (!strcmp (type, "raw")) { if (npr->which == Z_NamePlusRecord_databaseRecord) { Z_External *r = (Z_External *) npr->u.databaseRecord; - oident *ent = oid_getentbyoid(r->direct_reference); if (r->which == Z_External_sutrs) { *len = r->u.sutrs->len; - return r->u.sutrs->buf; + return (const char *) r->u.sutrs->buf; } else if (r->which == Z_External_octet) { - switch (ent->value) - { - case VAL_SOIF: - case VAL_HTML: - case VAL_SUTRS: - break; - case VAL_TEXT_XML: - case VAL_APPLICATION_XML: - break; - default: - if (!rec->wrbuf_marc) - rec->wrbuf_marc = wrbuf_alloc(); - if (marc_display_wrbuf (r->u.octet_aligned->buf, - rec->wrbuf_marc, 0, - r->u.octet_aligned->len) > 0) - { - *len = wrbuf_len(rec->wrbuf_marc); - return wrbuf_buf(rec->wrbuf_marc); - } - } *len = r->u.octet_aligned->len; - return r->u.octet_aligned->buf; + return (const char *) r->u.octet_aligned->buf; } - else if (r->which == Z_External_grs1) + else /* grs-1, explain, ... */ { - *len = 5; - return "GRS-1"; + *len = -1; + return (const char *) npr->u.databaseRecord; } } return 0; } - else if (!strcmp (type, "raw")) - { - if (npr->which == Z_NamePlusRecord_databaseRecord) - { - *len = -1; - return (Z_External *) npr->u.databaseRecord; - } - return 0; - } return 0; } @@ -943,9 +1042,8 @@ static void record_cache_add (ZOOM_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; @@ -1070,6 +1168,32 @@ static void sort_response (ZOOM_connection c, Z_SortResponse *res) response_diag (c, res->diagnostics[0]); } +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) { ZOOM_resultset resultset; @@ -1164,7 +1288,8 @@ static int send_present (ZOOM_connection c) if (schema && *schema) { - Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo)); + Z_RecordComposition *compo = (Z_RecordComposition *) + odr_malloc (c->odr_out, sizeof(*compo)); req->recordComposition = compo; compo->which = Z_RecordComp_complex; @@ -1205,8 +1330,10 @@ static int send_present (ZOOM_connection c) } else if (element && *element) { - Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn)); - Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo)); + Z_ElementSetNames *esn = (Z_ElementSetNames *) + odr_malloc (c->odr_out, sizeof(*esn)); + Z_RecordComposition *compo = (Z_RecordComposition *) + odr_malloc (c->odr_out, sizeof(*compo)); esn->which = Z_ElementSetNames_generic; esn->u.generic = odr_strdup (c->odr_out, element); @@ -1219,6 +1346,122 @@ static int send_present (ZOOM_connection c) return 1; } +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; +} + +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); + } +} + +int 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; + assert (c->tasks->which == ZOOM_TASK_SCAN); + scan = c->tasks->u.scan.scan; + + req->termListAndStartPoint = scan->termListAndStartPoint; + req->attributeSet = scan->attributeSet; + + *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; +} + +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; +} + +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; +} + +const char *ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key) +{ + return ZOOM_options_get (scan->options, key); +} + +void ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key, + const char *val) +{ + ZOOM_options_set (scan->options, key, val); +} + static int ZOOM_connection_exec_task (ZOOM_connection c) { ZOOM_task task = c->tasks; @@ -1250,6 +1493,10 @@ static int ZOOM_connection_exec_task (ZOOM_connection c) case ZOOM_TASK_CONNECT: if (do_connect(c)) return 1; + break; + case ZOOM_TASK_SCAN: + if (send_scan(c)) + return 1; } ZOOM_connection_remove_task (c); return 0; @@ -1286,7 +1533,8 @@ static void handle_apdu (ZOOM_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)) + 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) { @@ -1310,6 +1558,10 @@ static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) sort_response (c, apdu->u.sortResponse); if (!send_present (c)) ZOOM_connection_remove_task (c); + break; + case Z_APDU_scanResponse: + scan_response (c, apdu->u.scanResponse); + ZOOM_connection_remove_task (c); } } @@ -1317,6 +1569,10 @@ 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); r = cs_get (c->cs, &c->buf_in, &c->len_in); if (r == 1) @@ -1328,8 +1584,11 @@ static int do_read (ZOOM_connection 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 = ZOOM_ERROR_DECODE; @@ -1346,7 +1605,11 @@ static int do_read (ZOOM_connection c) 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); + if ((r=cs_put (c->cs, buf_out, len_out)) < 0) { if (c->state == STATE_CONNECTING) @@ -1357,14 +1620,18 @@ static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out) return 1; } else if (r == 1) - { - c->state = STATE_ESTABLISHED; - c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_WRITE|ZOOM_SELECT_EXCEPT; + { + c->mask = ZOOM_SELECT_EXCEPT; + if (c->cs->io_pending & CS_WANT_WRITE) + c->mask += ZOOM_SELECT_WRITE; + if (c->cs->io_pending & CS_WANT_READ) + c->mask += ZOOM_SELECT_READ; + yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask); } else { - c->state = STATE_ESTABLISHED; - c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT; + c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT; + yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask); } return 0; } @@ -1459,65 +1726,63 @@ int ZOOM_connection_error (ZOOM_connection c, const char **cp, int ZOOM_connection_do_io(ZOOM_connection c, int mask) { - ZOOM_Event event; -#if 0 + ZOOM_Event event = 0; int r = cs_look(c->cs); - yaz_log (LOG_LOG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d", + yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d", c, mask, r); if (r == CS_NONE) { + event = ZOOM_Event_create (ZOOM_EVENT_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 = ZOOM_ERROR_CONNECT; - do_close (c); - } - else - ZOOM_connection_send_init (c); - } - else - { - if (mask & ZOOM_SELECT_READ) - do_read (c); - if (c->cs && (mask & ZOOM_SELECT_WRITE)) - do_write (c); - } -#else - yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d", c, mask); - if (c->state == STATE_CONNECTING) - { - if (mask & ZOOM_SELECT_WRITE) - ZOOM_connection_send_init (c); - else - { - c->error = ZOOM_ERROR_CONNECT; - do_close (c); - } - } - else if (c->state == STATE_ESTABLISHED) - { - if (mask & ZOOM_SELECT_READ) - do_read (c); - if (c->cs && (mask & ZOOM_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 = ZOOM_ERROR_INTERNAL; - do_close (c); + if (mask & ZOOM_SELECT_READ) + do_read (c); + if (c->cs && (mask & ZOOM_SELECT_WRITE)) + do_write (c); } -#endif - event = ZOOM_Event_create (1); - ZOOM_connection_put_event (c, event); return 1; } +int ZOOM_connection_last_event(ZOOM_connection cs) +{ + if (!cs) + return ZOOM_EVENT_NONE; + return cs->last_event; +} int ZOOM_event (int no, ZOOM_connection *cs) { @@ -1544,8 +1809,15 @@ int ZOOM_event (int no, ZOOM_connection *cs) for (i = 0; imask) { - ZOOM_Event event = ZOOM_Event_create(0); + ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT); /* timeout and this connection was waiting */ c->error = ZOOM_ERROR_TIMEOUT; do_close (c); @@ -1665,7 +1935,7 @@ int ZOOM_event (int no, ZOOM_connection *cs) } if (r == 0 && c->mask) { - ZOOM_Event event = ZOOM_Event_create(0); + ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT); /* timeout and this connection was waiting */ c->error = ZOOM_ERROR_TIMEOUT; do_close (c);