/*
- * $Id: zoom-c.c,v 1.11 2001-11-18 21:14:23 adam Exp $
+ * $Id: zoom-c.c,v 1.22 2002-01-24 19:33:09 adam Exp $
*
* ZOOM layer for C, connections, result sets, queries.
*/
static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
{
- // put in back of queue
if (c->m_queue_back)
{
c->m_queue_back->prev = event;
static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
{
- // get from front of queue
ZOOM_Event event = c->m_queue_front;
if (!event)
return 0;
}
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)
*taskp = 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;
switch (task->which)
{
case ZOOM_TASK_SEARCH:
- ZOOM_resultset_destroy (task->u.resultset);
+ ZOOM_resultset_destroy (task->u.search.resultset);
break;
case ZOOM_TASK_RETRIEVE:
- ZOOM_resultset_destroy (task->u.resultset);
+ ZOOM_resultset_destroy (task->u.retrieve.resultset);
break;
case ZOOM_TASK_CONNECT:
break;
+ case ZOOM_TASK_SCAN:
+ ZOOM_scanset_destroy (task->u.scan.scan);
+ break;
default:
assert (0);
}
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;
/* 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)
{
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)
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;
{
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->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 = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
- task->u.resultset = r;
+ task->u.search.resultset = r;
ZOOM_resultset_addref (r);
(q->refcount)++;
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 */
ZOOM_query_destroy (r->search);
ZOOM_options_destroy (r->options);
odr_destroy (r->odr);
+ xfree (r->setname);
xfree (r);
}
}
c = r->connection;
if (!c)
return;
- if (start >= r->size)
- return;
-
- if (start + count > r->size)
- count = r->size - start;
-
task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
- task->u.resultset = r;
- ZOOM_resultset_addref (r);
+ task->u.retrieve.resultset = r;
+ task->u.retrieve.start = start;
+ task->u.retrieve.count = count;
- r->start = start;
- r->count = count;
+ ZOOM_resultset_addref (r);
if (!r->connection->async || force_sync)
while (r->connection && ZOOM_event (1, &r->connection))
if (ret >= 0)
{
c->state = STATE_CONNECTING;
- c->mask = ZOOM_SELECT_READ | ZOOM_SELECT_WRITE |
- ZOOM_SELECT_EXCEPT;
+ c->mask = ZOOM_SELECT_EXCEPT;
+ if (c->cs->io_pending & CS_WANT_WRITE)
+ c->mask += ZOOM_SELECT_WRITE;
+ if (c->cs->io_pending & CS_WANT_READ)
+ c->mask += ZOOM_SELECT_READ;
return 1;
}
}
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;
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);
const char *elementSetName;
const char *smallSetElementSetName;
const char *mediumSetElementSetName;
+ const char *schema;
assert (c->tasks);
assert (c->tasks->which == ZOOM_TASK_SEARCH);
- r = c->tasks->u.resultset;
+ r = c->tasks->u.search.resultset;
elementSetName =
ZOOM_options_get (r->options, "elementSetName");
ZOOM_options_get (r->options, "smallSetElementSetName");
mediumSetElementSetName =
ZOOM_options_get (r->options, "mediumSetElementSetName");
+ schema =
+ ZOOM_options_get (r->options, "schema");
if (!smallSetElementSetName)
smallSetElementSetName = elementSetName;
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");
*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;
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;
default:
if (!rec->wrbuf_marc)
rec->wrbuf_marc = wrbuf_alloc();
+ wrbuf_rewind (rec->wrbuf_marc);
if (marc_display_wrbuf (r->u.octet_aligned->buf,
rec->wrbuf_marc, 0,
r->u.octet_aligned->len) > 0)
{
if (npr->which == Z_NamePlusRecord_databaseRecord)
{
- *len = -1;
- return (Z_External *) npr->u.databaseRecord;
+ Z_External *r = (Z_External *) npr->u.databaseRecord;
+
+ if (r->which == Z_External_sutrs)
+ {
+ *len = r->u.sutrs->len;
+ return r->u.sutrs->buf;
+ }
+ else if (r->which == Z_External_octet)
+ {
+ *len = r->u.octet_aligned->len;
+ return r->u.octet_aligned->buf;
+ }
+ else /* grs-1, explain, ... */
+ {
+ *len = -1;
+ return (Z_External *) npr->u.databaseRecord;
+ }
}
return 0;
}
return;
}
}
-
}
rc = odr_malloc (r->odr, sizeof(*rc));
rc->rec.npr = npr;
if (!c->tasks)
return ;
- if (c->tasks->which != ZOOM_TASK_SEARCH &&
- c->tasks->which != ZOOM_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;
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);
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;
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)
{
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);
ZOOM_options_get (c->options, "preferredRecordSyntax");
const char *element =
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 != ZOOM_TASK_SEARCH &&
- c->tasks->which != ZOOM_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)
req->preferredRecordSyntax =
yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
- if (element && *element)
+ if (schema && *schema)
+ {
+ Z_RecordComposition *compo = 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 = odr_malloc (c->odr_out, sizeof(*esn));
Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
compo->u.simple = esn;
req->recordComposition = compo;
}
+ req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
send_APDU (c, apdu);
return 1;
}
+ZOOM_scanset ZOOM_connection_scan (ZOOM_connection c, const char *start)
+{
+ ZOOM_scanset scan = 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, size_t *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 = 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;
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;
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 == ZOOM_TASK_CONNECT);
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);
}
}
{
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)
}
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;
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)
return 1;
}
else if (r == 1)
- {
- c->state = STATE_ESTABLISHED;
- c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_WRITE|ZOOM_SELECT_EXCEPT;
+ {
+ c->mask = ZOOM_SELECT_EXCEPT;
+ if (c->cs->io_pending & CS_WANT_WRITE)
+ c->mask += ZOOM_SELECT_WRITE;
+ if (c->cs->io_pending & CS_WANT_READ)
+ c->mask += ZOOM_SELECT_READ;
+ yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
}
else
{
- c->state = STATE_ESTABLISHED;
- c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
+ c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
+ yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
}
return 0;
}
int ZOOM_connection_do_io(ZOOM_connection c, int mask)
{
- ZOOM_Event event;
-#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)
{
for (i = 0; i<no; i++)
{
ZOOM_connection c = cs[i];
+ ZOOM_Event event;
if (c && ZOOM_connection_exec_task (c))
- return i+1;
+ {
+ if ((event = ZOOM_connection_get_event(c)))
+ {
+ ZOOM_Event_destroy (event);
+ return i+1;
+ }
+ }
}
#if HAVE_SYS_POLL_H
if (!nfds)
return 0;
#if HAVE_SYS_POLL_H
- yaz_log (LOG_DEBUG, "poll start");
r = poll (pollfds, nfds, 15000);
- yaz_log (LOG_DEBUG, "poll stop, returned r=%d", r);
for (i = 0; i<nfds; i++)
{
ZOOM_connection c = poll_cs[i];
}
else 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);
}
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);