2 * $Id: zoom-c.c,v 1.12 2001-11-22 09:45:31 adam Exp $
4 * ZOOM layer for C, connections, result sets, queries.
7 #include <yaz/xmalloc.h>
8 #include <yaz/otherinfo.h>
10 #include <yaz/pquery.h>
11 #include <yaz/diagbib1.h>
19 static ZOOM_Event ZOOM_Event_create (int kind)
21 ZOOM_Event event = xmalloc (sizeof(*event));
28 static void ZOOM_Event_destroy (ZOOM_Event event)
33 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
35 // put in back of queue
38 c->m_queue_back->prev = event;
39 assert (c->m_queue_front);
43 assert (!c->m_queue_front);
44 c->m_queue_front = event;
46 event->next = c->m_queue_back;
48 c->m_queue_back = event;
51 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
53 // get from front of queue
54 ZOOM_Event event = c->m_queue_front;
57 assert (c->m_queue_back);
58 c->m_queue_front = event->prev;
61 assert (c->m_queue_back);
62 c->m_queue_front->next = 0;
69 static void clear_error (ZOOM_connection c)
71 c->error = ZOOM_ERROR_NONE;
76 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
78 ZOOM_task *taskp = &c->tasks;
80 taskp = &(*taskp)->next;
81 *taskp = xmalloc (sizeof(**taskp));
82 (*taskp)->running = 0;
83 (*taskp)->which = which;
84 (*taskp)->u.resultset = 0; /* one null pointer there at least */
90 void ZOOM_connection_remove_task (ZOOM_connection c)
92 ZOOM_task task = c->tasks;
96 c->tasks = task->next;
99 case ZOOM_TASK_SEARCH:
100 ZOOM_resultset_destroy (task->u.resultset);
102 case ZOOM_TASK_RETRIEVE:
103 ZOOM_resultset_destroy (task->u.resultset);
105 case ZOOM_TASK_CONNECT:
114 void ZOOM_connection_remove_tasks (ZOOM_connection c)
117 ZOOM_connection_remove_task(c);
120 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
122 const char *elementSetName);
124 ZOOM_connection ZOOM_connection_create (ZOOM_options options)
126 ZOOM_connection c = xmalloc (sizeof(*c));
130 c->state = STATE_IDLE;
131 c->error = ZOOM_ERROR_NONE;
139 c->options = ZOOM_options_create_with_parent(options);
148 c->odr_in = odr_createmem (ODR_DECODE);
149 c->odr_out = odr_createmem (ODR_ENCODE);
152 c->support_named_resultsets = 0;
154 c->m_queue_front = 0;
159 /* set database names. Take local databases (if set); otherwise
160 take databases given in ZURL (if set); otherwise use Default */
161 static char **set_DatabaseNames (ZOOM_connection con, int *num)
163 char **databaseNames;
166 const char *cp = ZOOM_options_get (con->options, "databaseName");
170 cp = strchr (con->host_port, '/');
177 while ((c = strchr(c, '+')))
185 databaseNames = odr_malloc (con->odr_out, no * sizeof(*databaseNames));
189 c = strchr (cp, '+');
197 /* cp ptr to first char of db name, c is char
199 databaseNames[no] = odr_malloc (con->odr_out, 1+c-cp);
200 memcpy (databaseNames[no], cp, c-cp);
201 databaseNames[no++][c-cp] = '\0';
206 databaseNames[no] = NULL;
208 return databaseNames;
211 ZOOM_connection ZOOM_connection_new (const char *host, int portnum)
213 ZOOM_connection c = ZOOM_connection_create (0);
215 ZOOM_connection_connect (c, host, portnum);
219 void ZOOM_connection_connect(ZOOM_connection c,
220 const char *host, int portnum)
225 val = ZOOM_options_get (c->options, "proxy");
227 c->proxy = xstrdup (val);
234 sprintf (hostn, "%.80s:%d", host, portnum);
235 c->host_port = xstrdup(hostn);
238 c->host_port = xstrdup(host);
240 ZOOM_options_set(c->options, "host", c->host_port);
242 c->async = ZOOM_options_get_bool (c->options, "async", 0);
244 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
248 while (ZOOM_event (1, &c))
253 ZOOM_query ZOOM_query_create(void)
255 ZOOM_query s = xmalloc (sizeof(*s));
260 s->odr = odr_createmem (ODR_ENCODE);
265 void ZOOM_query_destroy(ZOOM_query s)
271 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
272 if (s->refcount == 0)
274 odr_destroy (s->odr);
279 int ZOOM_query_prefix(ZOOM_query s, const char *str)
281 s->query = odr_malloc (s->odr, sizeof(*s->query));
282 s->query->which = Z_Query_type_1;
283 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
284 if (!s->query->u.type_1)
289 int ZOOM_query_sortby(ZOOM_query s, const char *criteria)
291 s->sort_spec = yaz_sort_spec (s->odr, criteria);
297 static int do_write(ZOOM_connection c);
299 void ZOOM_connection_destroy(ZOOM_connection c)
306 for (r = c->resultsets; r; r = r->next)
311 odr_destroy (c->odr_in);
312 odr_destroy (c->odr_out);
313 ZOOM_options_destroy (c->options);
314 ZOOM_connection_remove_tasks (c);
315 xfree (c->host_port);
319 void ZOOM_resultset_addref (ZOOM_resultset r)
324 ZOOM_resultset ZOOM_resultset_create ()
326 ZOOM_resultset r = xmalloc (sizeof(*r));
330 r->odr = odr_createmem (ODR_ENCODE);
344 ZOOM_resultset ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
347 ZOOM_query s = ZOOM_query_create();
349 ZOOM_query_prefix (s, q);
351 r = ZOOM_connection_search (c, s);
352 ZOOM_query_destroy (s);
356 ZOOM_resultset ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
358 ZOOM_resultset r = ZOOM_resultset_create ();
362 r->r_sort_spec = q->sort_spec;
363 r->r_query = q->query;
366 r->options = ZOOM_options_create_with_parent(c->options);
368 r->start = ZOOM_options_get_int(r->options, "start", 0);
369 r->count = ZOOM_options_get_int(r->options, "count", 0);
370 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
371 cp = ZOOM_options_get (r->options, "setname");
373 r->setname = xstrdup (cp);
377 r->next = c->resultsets;
380 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
381 task->u.resultset = r;
382 ZOOM_resultset_addref (r);
388 while (ZOOM_event (1, &c))
394 void ZOOM_resultset_destroy(ZOOM_resultset r)
399 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
400 if (r->refcount == 0)
404 /* remove ourselves from the resultsets in connection */
405 ZOOM_resultset *rp = &r->connection->resultsets;
408 assert (*rp); /* we must be in this list!! */
410 { /* OK, we're here - take us out of it */
417 ZOOM_query_destroy (r->search);
418 ZOOM_options_destroy (r->options);
419 odr_destroy (r->odr);
425 size_t ZOOM_resultset_size (ZOOM_resultset r)
430 static void do_close (ZOOM_connection c)
436 c->state = STATE_IDLE;
439 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
440 int force_sync, int start, int count)
450 if (start >= r->size)
453 if (start + count > r->size)
454 count = r->size - start;
456 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
457 task->u.resultset = r;
458 ZOOM_resultset_addref (r);
463 if (!r->connection->async || force_sync)
464 while (r->connection && ZOOM_event (1, &r->connection))
468 void ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
469 size_t start, size_t count)
471 int force_present = 0;
477 ZOOM_resultset_retrieve (r, force_present, start, count);
481 for (i = 0; i< count; i++)
482 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
486 static int do_connect (ZOOM_connection c)
489 const char *effective_host;
492 effective_host = c->proxy;
494 effective_host = c->host_port;
496 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
499 c->cs = cs_create_host (effective_host, 0, &add);
503 int ret = cs_connect (c->cs, add);
504 yaz_log (LOG_DEBUG, "cs_connect returned %d", ret);
507 c->state = STATE_CONNECTING;
508 c->mask = ZOOM_SELECT_READ | ZOOM_SELECT_WRITE |
513 c->state = STATE_IDLE;
514 c->error = ZOOM_ERROR_CONNECT;
518 int z3950_connection_socket(ZOOM_connection c)
521 return cs_fileno(c->cs);
525 int z3950_connection_mask(ZOOM_connection c)
532 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
537 sprintf (str, "send_APDU t=%p type=%d", c, a->which);
540 Z_OtherInformation **oi;
542 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
544 if (!z_APDU(out, &a, 0, 0))
546 FILE *outf = fopen("/tmp/apdu.txt", "w");
549 ODR odr_pr = odr_createmem(ODR_PRINT);
550 fprintf (outf, "a=%p\n", a);
551 odr_setprint(odr_pr, outf);
552 z_APDU(odr_pr, &a, 0, 0);
556 c->error = ZOOM_ERROR_ENCODE;
563 static int send_APDU (ZOOM_connection c, Z_APDU *a)
566 if (encode_APDU(c, a, c->odr_out))
568 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
569 odr_reset(c->odr_out);
574 static int ZOOM_connection_send_init (ZOOM_connection c)
577 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
578 Z_InitRequest *ireq = apdu->u.initRequest;
579 Z_IdAuthentication *auth = odr_malloc(c->odr_out, sizeof(*auth));
580 const char *auth_groupId = ZOOM_options_get (c->options, "group");
581 const char *auth_userId = ZOOM_options_get (c->options, "user");
582 const char *auth_password = ZOOM_options_get (c->options, "pass");
584 ODR_MASK_SET(ireq->options, Z_Options_search);
585 ODR_MASK_SET(ireq->options, Z_Options_present);
586 ODR_MASK_SET(ireq->options, Z_Options_scan);
587 ODR_MASK_SET(ireq->options, Z_Options_sort);
588 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
589 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
591 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
592 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
593 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
595 impname = ZOOM_options_get (c->options, "implementationName");
596 ireq->implementationName =
597 odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
598 strcpy (ireq->implementationName, "");
601 strcat (ireq->implementationName, impname);
602 strcat (ireq->implementationName, "/");
604 strcat (ireq->implementationName, "ZOOM-C/YAZ");
606 *ireq->maximumRecordSize =
607 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
608 *ireq->preferredMessageSize =
609 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
611 if (auth_groupId || auth_password)
613 Z_IdPass *pass = odr_malloc(c->odr_out, sizeof(*pass));
616 if (auth_groupId && *auth_groupId)
618 pass->groupId = odr_malloc(c->odr_out, strlen(auth_groupId)+1);
619 strcpy(pass->groupId, auth_groupId);
623 if (auth_userId && *auth_userId)
625 pass->userId = odr_malloc(c->odr_out, strlen(auth_userId)+1);
626 strcpy(pass->userId, auth_userId);
630 if (auth_password && *auth_password)
632 pass->password = odr_malloc(c->odr_out, strlen(auth_password)+1);
633 strcpy(pass->password, auth_password);
638 auth->which = Z_IdAuthentication_idPass;
639 auth->u.idPass = pass;
640 ireq->idAuthentication = auth;
643 else if (auth_userId)
645 auth->which = Z_IdAuthentication_open;
646 auth->u.open = odr_malloc(c->odr_out, strlen(auth_userId)+1);
647 strcpy(auth->u.open, auth_userId);
648 ireq->idAuthentication = auth;
651 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
652 VAL_PROXY, 1, c->host_port);
659 static int ZOOM_connection_send_search (ZOOM_connection c)
662 int lslb, ssub, mspn;
664 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
665 Z_SearchRequest *search_req = apdu->u.searchRequest;
666 const char *elementSetName;
667 const char *smallSetElementSetName;
668 const char *mediumSetElementSetName;
671 assert (c->tasks->which == ZOOM_TASK_SEARCH);
673 r = c->tasks->u.resultset;
676 ZOOM_options_get (r->options, "elementSetName");
677 smallSetElementSetName =
678 ZOOM_options_get (r->options, "smallSetElementSetName");
679 mediumSetElementSetName =
680 ZOOM_options_get (r->options, "mediumSetElementSetName");
682 if (!smallSetElementSetName)
683 smallSetElementSetName = elementSetName;
685 if (!mediumSetElementSetName)
686 mediumSetElementSetName = elementSetName;
691 /* prepare query for the search request */
692 search_req->query = r->r_query;
694 search_req->databaseNames =
695 set_DatabaseNames (c, &search_req->num_databaseNames);
697 /* get syntax (no need to provide unless piggyback is in effect) */
698 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
700 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
701 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
702 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
703 if (lslb != -1 && ssub != -1 && mspn != -1)
705 /* So're a Z39.50 expert? Let's hope you don't do sort */
706 *search_req->largeSetLowerBound = lslb;
707 *search_req->smallSetUpperBound = ssub;
708 *search_req->mediumSetPresentNumber = mspn;
710 else if (r->start == 0 && r->count > 0
711 && r->piggyback && !r->r_sort_spec)
713 /* Regular piggyback - do it unless we're going to do sort */
714 *search_req->largeSetLowerBound = 2000000000;
715 *search_req->smallSetUpperBound = r->count;
716 *search_req->mediumSetPresentNumber = r->count;
717 smallSetElementSetName = 0; /* no need to provide this */
721 /* non-piggyback. Need not provide elementsets or syntaxes .. */
722 smallSetElementSetName = 0;
723 mediumSetElementSetName = 0;
726 if (smallSetElementSetName && *smallSetElementSetName)
728 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
730 esn->which = Z_ElementSetNames_generic;
731 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
732 search_req->smallSetElementSetNames = esn;
734 if (mediumSetElementSetName && *mediumSetElementSetName)
736 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
738 esn->which = Z_ElementSetNames_generic;
739 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
740 search_req->mediumSetElementSetNames = esn;
743 search_req->preferredRecordSyntax =
744 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
748 if (c->support_named_resultsets)
752 /* find the lowest unused ordinal so that we re-use
753 result sets on the server. */
754 for (ord = 1; ; ord++)
757 sprintf (setname, "%d", ord);
758 for (rp = c->resultsets; rp; rp = rp->next)
759 if (rp->setname && !strcmp (rp->setname, setname))
764 r->setname = xstrdup (setname);
765 yaz_log (LOG_DEBUG, "allocating %s", r->setname);
768 r->setname = xstrdup ("default");
769 ZOOM_options_set (r->options, "setname", r->setname);
771 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
772 /* send search request */
778 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
780 Z_DefaultDiagFormat *r;
785 if (p->which != Z_DiagRec_defaultFormat)
787 c->error = ZOOM_ERROR_DECODE;
790 r = p->u.defaultFormat;
793 case Z_DefaultDiagFormat_v2Addinfo:
794 addinfo = r->u.v2Addinfo;
796 case Z_DefaultDiagFormat_v3Addinfo:
797 addinfo = r->u.v3Addinfo;
801 c->addinfo = xstrdup (addinfo);
802 c->error = *r->condition;
805 ZOOM_record ZOOM_record_clone (ZOOM_record srec)
812 odr_enc = odr_createmem(ODR_ENCODE);
813 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
815 buf = odr_getbuf (odr_enc, &size, 0);
817 nrec = xmalloc (sizeof(*nrec));
818 nrec->odr = odr_createmem(ODR_DECODE);
819 nrec->wrbuf_marc = 0;
820 odr_setbuf (nrec->odr, buf, size, 0);
821 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
823 odr_destroy (odr_enc);
827 ZOOM_record ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
829 return record_cache_lookup (s, pos, 0);
832 ZOOM_record ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
834 ZOOM_resultset_retrieve (r, 1, pos, 1);
835 return ZOOM_resultset_record_immediate (r, pos);
838 void ZOOM_record_destroy (ZOOM_record rec)
843 wrbuf_free (rec->wrbuf_marc, 1);
844 odr_destroy (rec->odr);
848 void *ZOOM_record_get (ZOOM_record rec, const char *type, size_t *len)
850 Z_NamePlusRecord *npr;
856 if (!strcmp (type, "database"))
858 return npr->databaseName;
860 else if (!strcmp (type, "syntax"))
862 if (npr->which == Z_NamePlusRecord_databaseRecord)
864 Z_External *r = (Z_External *) npr->u.databaseRecord;
865 oident *ent = oid_getentbyoid(r->direct_reference);
871 else if (!strcmp (type, "render"))
873 if (npr->which == Z_NamePlusRecord_databaseRecord)
875 Z_External *r = (Z_External *) npr->u.databaseRecord;
876 oident *ent = oid_getentbyoid(r->direct_reference);
878 if (r->which == Z_External_sutrs)
880 *len = r->u.sutrs->len;
881 return r->u.sutrs->buf;
883 else if (r->which == Z_External_octet)
892 case VAL_APPLICATION_XML:
895 if (!rec->wrbuf_marc)
896 rec->wrbuf_marc = wrbuf_alloc();
897 if (marc_display_wrbuf (r->u.octet_aligned->buf,
899 r->u.octet_aligned->len) > 0)
901 *len = wrbuf_len(rec->wrbuf_marc);
902 return wrbuf_buf(rec->wrbuf_marc);
905 *len = r->u.octet_aligned->len;
906 return r->u.octet_aligned->buf;
908 else if (r->which == Z_External_grs1)
916 else if (!strcmp (type, "raw"))
918 if (npr->which == Z_NamePlusRecord_databaseRecord)
921 return (Z_External *) npr->u.databaseRecord;
928 static void record_cache_add (ZOOM_resultset r,
929 Z_NamePlusRecord *npr,
931 const char *elementSetName)
933 ZOOM_record_cache rc;
935 for (rc = r->record_cache; rc; rc = rc->next)
939 if ((!elementSetName && !rc->elementSetName)
940 || (elementSetName && rc->elementSetName &&
941 !strcmp (elementSetName, rc->elementSetName)))
943 /* not destroying rc->npr (it's handled by nmem )*/
945 /* keeping wrbuf_marc too */
951 rc = odr_malloc (r->odr, sizeof(*rc));
954 rc->rec.wrbuf_marc = 0;
956 rc->elementSetName = odr_strdup (r->odr, elementSetName);
958 rc->elementSetName = 0;
960 rc->next = r->record_cache;
961 r->record_cache = rc;
964 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
966 const char *elementSetName)
968 ZOOM_record_cache rc;
970 for (rc = r->record_cache; rc; rc = rc->next)
974 if ((!elementSetName && !rc->elementSetName)
975 || (elementSetName && rc->elementSetName &&
976 !strcmp (elementSetName, rc->elementSetName)))
983 static void handle_records (ZOOM_connection c, Z_Records *sr,
986 ZOOM_resultset resultset;
990 if (c->tasks->which != ZOOM_TASK_SEARCH &&
991 c->tasks->which != ZOOM_TASK_RETRIEVE)
994 resultset = c->tasks->u.resultset;
996 if (sr && sr->which == Z_Records_NSD)
998 Z_DiagRec dr, *dr_p = &dr;
999 dr.which = Z_DiagRec_defaultFormat;
1000 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1002 response_diag (c, dr_p);
1004 else if (sr && sr->which == Z_Records_multipleNSD)
1006 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1007 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1009 c->error = ZOOM_ERROR_DECODE;
1013 if (resultset->count + resultset->start > resultset->size)
1014 resultset->count = resultset->size - resultset->start;
1015 if (resultset->count < 0)
1016 resultset->count = 0;
1017 if (sr && sr->which == Z_Records_DBOSD)
1020 NMEM nmem = odr_extract_mem (c->odr_in);
1021 Z_NamePlusRecordList *p =
1022 sr->u.databaseOrSurDiagnostics;
1023 for (i = 0; i<p->num_records; i++)
1025 record_cache_add (resultset, p->records[i],
1026 i+ resultset->start, 0);
1028 /* transfer our response to search_nmem .. we need it later */
1029 nmem_transfer (resultset->odr->mem, nmem);
1030 nmem_destroy (nmem);
1031 if (present_phase && p->num_records == 0)
1033 /* present response and we didn't get any records! */
1034 c->error = ZOOM_ERROR_DECODE;
1037 else if (present_phase)
1039 /* present response and we didn't get any records! */
1040 c->error = ZOOM_ERROR_DECODE;
1045 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1047 handle_records (c, pr->records, 1);
1050 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1052 ZOOM_resultset resultset;
1054 yaz_log (LOG_DEBUG, "got search response");
1056 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1059 resultset = c->tasks->u.resultset;
1061 resultset->size = *sr->resultCount;
1062 handle_records (c, sr->records, 0);
1065 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1067 if (res->diagnostics && res->num_diagnostics > 0)
1068 response_diag (c, res->diagnostics[0]);
1071 static int send_sort (ZOOM_connection c)
1073 ZOOM_resultset resultset;
1075 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1078 resultset = c->tasks->u.resultset;
1082 resultset->r_sort_spec = 0;
1085 if (resultset->r_sort_spec)
1087 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1088 Z_SortRequest *req = apdu->u.sortRequest;
1090 req->num_inputResultSetNames = 1;
1091 req->inputResultSetNames = (Z_InternationalString **)
1092 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1093 req->inputResultSetNames[0] =
1094 odr_strdup (c->odr_out, resultset->setname);
1095 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1096 req->sortSequence = resultset->r_sort_spec;
1097 resultset->r_sort_spec = 0;
1098 send_APDU (c, apdu);
1104 static int send_present (ZOOM_connection c)
1106 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1107 Z_PresentRequest *req = apdu->u.presentRequest;
1109 const char *syntax =
1110 ZOOM_options_get (c->options, "preferredRecordSyntax");
1111 const char *element =
1112 ZOOM_options_get (c->options, "elementSetName");
1113 ZOOM_resultset resultset;
1117 if (c->tasks->which != ZOOM_TASK_SEARCH &&
1118 c->tasks->which != ZOOM_TASK_RETRIEVE)
1121 resultset = c->tasks->u.resultset;
1123 if (c->error) /* don't continue on error */
1125 if (resultset->start < 0)
1127 for (i = 0; i<resultset->count; i++)
1130 record_cache_lookup (resultset, i + resultset->start, 0);
1134 if (i == resultset->count)
1137 resultset->start += i;
1138 resultset->count -= i;
1139 *req->resultSetStartPoint = resultset->start + 1;
1140 *req->numberOfRecordsRequested = resultset->count;
1141 assert (*req->numberOfRecordsRequested > 0);
1143 if (syntax && *syntax)
1144 req->preferredRecordSyntax =
1145 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1147 if (element && *element)
1149 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
1150 Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1152 esn->which = Z_ElementSetNames_generic;
1153 esn->u.generic = odr_strdup (c->odr_out, element);
1154 compo->which = Z_RecordComp_simple;
1155 compo->u.simple = esn;
1156 req->recordComposition = compo;
1158 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1159 send_APDU (c, apdu);
1163 static int ZOOM_connection_exec_task (ZOOM_connection c)
1165 ZOOM_task task = c->tasks;
1167 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1170 if (c->error != ZOOM_ERROR_NONE ||
1171 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1173 ZOOM_connection_remove_tasks (c);
1176 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d", task->which);
1180 switch (task->which)
1182 case ZOOM_TASK_SEARCH:
1183 /* see if search hasn't been sent yet. */
1184 if (ZOOM_connection_send_search (c))
1187 case ZOOM_TASK_RETRIEVE:
1188 if (send_present (c))
1191 case ZOOM_TASK_CONNECT:
1195 ZOOM_connection_remove_task (c);
1199 static int send_sort_present (ZOOM_connection c)
1201 int r = send_sort (c);
1203 r = send_present (c);
1207 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1209 Z_InitResponse *initrs;
1211 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1215 case Z_APDU_initResponse:
1216 initrs = apdu->u.initResponse;
1217 if (!*initrs->result)
1219 c->error = ZOOM_ERROR_INIT;
1224 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1226 xfree (c->cookie_in);
1229 c->cookie_in = xstrdup(cookie);
1230 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets))
1231 c->support_named_resultsets = 1;
1234 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1235 ZOOM_connection_remove_task (c);
1237 ZOOM_connection_exec_task (c);
1240 case Z_APDU_searchResponse:
1241 handle_search_response (c, apdu->u.searchResponse);
1242 if (!send_sort_present (c))
1243 ZOOM_connection_remove_task (c);
1245 case Z_APDU_presentResponse:
1246 handle_present_response (c, apdu->u.presentResponse);
1247 if (!send_present (c))
1248 ZOOM_connection_remove_task (c);
1250 case Z_APDU_sortResponse:
1251 sort_response (c, apdu->u.sortResponse);
1252 if (!send_present (c))
1253 ZOOM_connection_remove_task (c);
1257 static int do_read (ZOOM_connection c)
1262 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1267 c->error= ZOOM_ERROR_CONNECTION_LOST;
1272 odr_reset (c->odr_in);
1273 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1274 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1276 c->error = ZOOM_ERROR_DECODE;
1281 handle_apdu (c, apdu);
1287 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
1291 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1293 if (c->state == STATE_CONNECTING)
1294 c->error = ZOOM_ERROR_CONNECT;
1296 c->error = ZOOM_ERROR_CONNECTION_LOST;
1302 c->state = STATE_ESTABLISHED;
1303 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_WRITE|ZOOM_SELECT_EXCEPT;
1307 c->state = STATE_ESTABLISHED;
1308 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
1313 static int do_write(ZOOM_connection c)
1315 return do_write_ex (c, c->buf_out, c->len_out);
1319 const char *ZOOM_connection_option_get (ZOOM_connection c, const char *key)
1321 return ZOOM_options_get (c->options, key);
1324 void ZOOM_connection_option_set (ZOOM_connection c, const char *key,
1327 ZOOM_options_set (c->options, key, val);
1330 const char *ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
1332 return ZOOM_options_get (r->options, key);
1335 void ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
1338 ZOOM_options_set (r->options, key, val);
1342 int ZOOM_connection_errcode (ZOOM_connection c)
1344 return ZOOM_connection_error (c, 0, 0);
1347 const char *ZOOM_connection_errmsg (ZOOM_connection c)
1350 ZOOM_connection_error (c, &msg, 0);
1354 const char *ZOOM_connection_addinfo (ZOOM_connection c)
1356 const char *addinfo;
1357 ZOOM_connection_error (c, 0, &addinfo);
1361 int ZOOM_connection_error (ZOOM_connection c, const char **cp,
1362 const char **addinfo)
1364 int error = c->error;
1369 case ZOOM_ERROR_NONE:
1370 *cp = "No error"; break;
1371 case ZOOM_ERROR_CONNECT:
1372 *cp = "Connect failed"; break;
1373 case ZOOM_ERROR_MEMORY:
1374 *cp = "Out of memory"; break;
1375 case ZOOM_ERROR_ENCODE:
1376 *cp = "Encoding failed"; break;
1377 case ZOOM_ERROR_DECODE:
1378 *cp = "Decoding failed"; break;
1379 case ZOOM_ERROR_CONNECTION_LOST:
1380 *cp = "Connection lost"; break;
1381 case ZOOM_ERROR_INIT:
1382 *cp = "Init rejected"; break;
1383 case ZOOM_ERROR_INTERNAL:
1384 *cp = "Internal failure"; break;
1385 case ZOOM_ERROR_TIMEOUT:
1386 *cp = "Timeout"; break;
1388 *cp = diagbib1_str (error);
1394 *addinfo = c->addinfo;
1401 int ZOOM_connection_do_io(ZOOM_connection c, int mask)
1405 int r = cs_look(c->cs);
1406 yaz_log (LOG_LOG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
1411 c->error = ZOOM_ERROR_CONNECT;
1414 else if (r == CS_CONNECT)
1416 yaz_log (LOG_LOG, "calling rcvconnect");
1417 if (cs_rcvconnect (c->cs) < 0)
1419 c->error = ZOOM_ERROR_CONNECT;
1423 ZOOM_connection_send_init (c);
1427 if (mask & ZOOM_SELECT_READ)
1429 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1433 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d", c, mask);
1434 if (c->state == STATE_CONNECTING)
1436 if (mask & ZOOM_SELECT_WRITE)
1437 ZOOM_connection_send_init (c);
1440 c->error = ZOOM_ERROR_CONNECT;
1444 else if (c->state == STATE_ESTABLISHED)
1446 if (mask & ZOOM_SELECT_READ)
1448 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1453 c->error = ZOOM_ERROR_INTERNAL;
1457 event = ZOOM_Event_create (1);
1458 ZOOM_connection_put_event (c, event);
1463 int ZOOM_event (int no, ZOOM_connection *cs)
1466 struct pollfd pollfds[1024];
1467 ZOOM_connection poll_cs[1024];
1470 fd_set input, output, except;
1475 for (i = 0; i<no; i++)
1477 ZOOM_connection c = cs[i];
1479 if (c && (event = ZOOM_connection_get_event(c)))
1481 ZOOM_Event_destroy (event);
1485 for (i = 0; i<no; i++)
1487 ZOOM_connection c = cs[i];
1488 if (c && ZOOM_connection_exec_task (c))
1502 for (i = 0; i<no; i++)
1504 ZOOM_connection c = cs[i];
1509 fd = z3950_connection_socket(c);
1510 mask = z3950_connection_mask(c);
1520 short poll_events = 0;
1522 if (mask & ZOOM_SELECT_READ)
1523 poll_events += POLLIN;
1524 if (mask & ZOOM_SELECT_WRITE)
1525 poll_events += POLLOUT;
1526 if (mask & ZOOM_SELECT_EXCEPT)
1527 poll_events += POLLERR;
1528 pollfds[nfds].fd = fd;
1529 pollfds[nfds].events = poll_events;
1530 pollfds[nfds].revents = 0;
1535 if (mask & ZOOM_SELECT_READ)
1537 FD_SET (fd, &input);
1540 if (mask & ZOOM_SELECT_WRITE)
1542 FD_SET (fd, &output);
1545 if (mask & ZOOM_SELECT_EXCEPT)
1547 FD_SET (fd, &except);
1555 yaz_log (LOG_DEBUG, "poll start");
1556 r = poll (pollfds, nfds, 15000);
1557 yaz_log (LOG_DEBUG, "poll stop, returned r=%d", r);
1558 for (i = 0; i<nfds; i++)
1560 ZOOM_connection c = poll_cs[i];
1564 if (pollfds[i].revents & POLLIN)
1565 mask += ZOOM_SELECT_READ;
1566 if (pollfds[i].revents & POLLOUT)
1567 mask += ZOOM_SELECT_WRITE;
1568 if (pollfds[i].revents & POLLERR)
1569 mask += ZOOM_SELECT_EXCEPT;
1571 ZOOM_connection_do_io(c, mask);
1573 else if (r == 0 && c->mask)
1575 ZOOM_Event event = ZOOM_Event_create(0);
1576 /* timeout and this connection was waiting */
1577 c->error = ZOOM_ERROR_TIMEOUT;
1579 ZOOM_connection_put_event(c, event);
1583 yaz_log (LOG_DEBUG, "select start");
1584 r = select (max_fd+1, &input, &output, &except, &tv);
1585 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
1586 for (i = 0; i<no; i++)
1588 ZOOM_connection c = cs[i];
1593 fd = z3950_connection_socket(c);
1597 /* no timeout and real socket */
1598 if (FD_ISSET(fd, &input))
1599 mask += ZOOM_SELECT_READ;
1600 if (FD_ISSET(fd, &output))
1601 mask += ZOOM_SELECT_WRITE;
1602 if (FD_ISSET(fd, &except))
1603 mask += ZOOM_SELECT_EXCEPT;
1605 ZOOM_connection_do_io(c, mask);
1607 if (r == 0 && c->mask)
1609 ZOOM_Event event = ZOOM_Event_create(0);
1610 /* timeout and this connection was waiting */
1611 c->error = ZOOM_ERROR_TIMEOUT;
1613 yaz_log (LOG_DEBUG, "timeout");
1614 ZOOM_connection_put_event(c, event);
1618 for (i = 0; i<no; i++)
1620 ZOOM_connection c = cs[i];
1622 if (c && (event = ZOOM_connection_get_event(c)))
1624 ZOOM_Event_destroy (event);