2 * $Id: zoom-c.c,v 1.11 2001-11-18 21:14:23 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);
153 c->m_queue_front = 0;
158 /* set database names. Take local databases (if set); otherwise
159 take databases given in ZURL (if set); otherwise use Default */
160 static char **set_DatabaseNames (ZOOM_connection con, int *num)
162 char **databaseNames;
165 const char *cp = ZOOM_options_get (con->options, "databaseName");
169 cp = strchr (con->host_port, '/');
176 while ((c = strchr(c, '+')))
184 databaseNames = odr_malloc (con->odr_out, no * sizeof(*databaseNames));
188 c = strchr (cp, '+');
196 /* cp ptr to first char of db name, c is char
198 databaseNames[no] = odr_malloc (con->odr_out, 1+c-cp);
199 memcpy (databaseNames[no], cp, c-cp);
200 databaseNames[no++][c-cp] = '\0';
205 databaseNames[no] = NULL;
207 return databaseNames;
210 ZOOM_connection ZOOM_connection_new (const char *host, int portnum)
212 ZOOM_connection c = ZOOM_connection_create (0);
214 ZOOM_connection_connect (c, host, portnum);
218 void ZOOM_connection_connect(ZOOM_connection c,
219 const char *host, int portnum)
224 val = ZOOM_options_get (c->options, "proxy");
226 c->proxy = xstrdup (val);
233 sprintf (hostn, "%.80s:%d", host, portnum);
234 c->host_port = xstrdup(hostn);
237 c->host_port = xstrdup(host);
239 ZOOM_options_set(c->options, "host", c->host_port);
241 c->async = ZOOM_options_get_bool (c->options, "async", 0);
243 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
247 while (ZOOM_event (1, &c))
252 ZOOM_query ZOOM_query_create(void)
254 ZOOM_query s = xmalloc (sizeof(*s));
259 s->odr = odr_createmem (ODR_ENCODE);
264 void ZOOM_query_destroy(ZOOM_query s)
270 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
271 if (s->refcount == 0)
273 odr_destroy (s->odr);
278 int ZOOM_query_prefix(ZOOM_query s, const char *str)
280 s->query = odr_malloc (s->odr, sizeof(*s->query));
281 s->query->which = Z_Query_type_1;
282 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
283 if (!s->query->u.type_1)
288 int ZOOM_query_sortby(ZOOM_query s, const char *criteria)
290 s->sort_spec = yaz_sort_spec (s->odr, criteria);
296 static int do_write(ZOOM_connection c);
298 void ZOOM_connection_destroy(ZOOM_connection c)
305 for (r = c->resultsets; r; r = r->next)
310 odr_destroy (c->odr_in);
311 odr_destroy (c->odr_out);
312 ZOOM_options_destroy (c->options);
313 ZOOM_connection_remove_tasks (c);
314 xfree (c->host_port);
318 void ZOOM_resultset_addref (ZOOM_resultset r)
323 ZOOM_resultset ZOOM_resultset_create ()
325 ZOOM_resultset r = xmalloc (sizeof(*r));
329 r->odr = odr_createmem (ODR_ENCODE);
342 ZOOM_resultset ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
345 ZOOM_query s = ZOOM_query_create();
347 ZOOM_query_prefix (s, q);
349 r = ZOOM_connection_search (c, s);
350 ZOOM_query_destroy (s);
354 ZOOM_resultset ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
356 ZOOM_resultset r = ZOOM_resultset_create ();
359 r->r_sort_spec = q->sort_spec;
360 r->r_query = q->query;
363 r->options = ZOOM_options_create_with_parent(c->options);
365 r->start = ZOOM_options_get_int(r->options, "start", 0);
366 r->count = ZOOM_options_get_int(r->options, "count", 0);
367 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
370 r->next = c->resultsets;
373 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
374 task->u.resultset = r;
375 ZOOM_resultset_addref (r);
381 while (ZOOM_event (1, &c))
387 void ZOOM_resultset_destroy(ZOOM_resultset r)
392 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
393 if (r->refcount == 0)
397 /* remove ourselves from the resultsets in connection */
398 ZOOM_resultset *rp = &r->connection->resultsets;
401 assert (*rp); /* we must be in this list!! */
403 { /* OK, we're here - take us out of it */
410 ZOOM_query_destroy (r->search);
411 ZOOM_options_destroy (r->options);
412 odr_destroy (r->odr);
417 size_t ZOOM_resultset_size (ZOOM_resultset r)
422 static void do_close (ZOOM_connection c)
428 c->state = STATE_IDLE;
431 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
432 int force_sync, int start, int count)
442 if (start >= r->size)
445 if (start + count > r->size)
446 count = r->size - start;
448 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
449 task->u.resultset = r;
450 ZOOM_resultset_addref (r);
455 if (!r->connection->async || force_sync)
456 while (r->connection && ZOOM_event (1, &r->connection))
460 void ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
461 size_t start, size_t count)
463 int force_present = 0;
469 ZOOM_resultset_retrieve (r, force_present, start, count);
473 for (i = 0; i< count; i++)
474 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
478 static int do_connect (ZOOM_connection c)
481 const char *effective_host;
484 effective_host = c->proxy;
486 effective_host = c->host_port;
488 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
491 c->cs = cs_create_host (effective_host, 0, &add);
495 int ret = cs_connect (c->cs, add);
496 yaz_log (LOG_DEBUG, "cs_connect returned %d", ret);
499 c->state = STATE_CONNECTING;
500 c->mask = ZOOM_SELECT_READ | ZOOM_SELECT_WRITE |
505 c->state = STATE_IDLE;
506 c->error = ZOOM_ERROR_CONNECT;
510 int z3950_connection_socket(ZOOM_connection c)
513 return cs_fileno(c->cs);
517 int z3950_connection_mask(ZOOM_connection c)
524 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
529 sprintf (str, "send_APDU t=%p type=%d", c, a->which);
532 Z_OtherInformation **oi;
534 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
536 if (!z_APDU(out, &a, 0, 0))
538 FILE *outf = fopen("/tmp/apdu.txt", "w");
541 ODR odr_pr = odr_createmem(ODR_PRINT);
542 fprintf (outf, "a=%p\n", a);
543 odr_setprint(odr_pr, outf);
544 z_APDU(odr_pr, &a, 0, 0);
548 c->error = ZOOM_ERROR_ENCODE;
555 static int send_APDU (ZOOM_connection c, Z_APDU *a)
558 if (encode_APDU(c, a, c->odr_out))
560 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
561 odr_reset(c->odr_out);
566 static int ZOOM_connection_send_init (ZOOM_connection c)
569 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
570 Z_InitRequest *ireq = apdu->u.initRequest;
571 Z_IdAuthentication *auth = odr_malloc(c->odr_out, sizeof(*auth));
572 const char *auth_groupId = ZOOM_options_get (c->options, "group");
573 const char *auth_userId = ZOOM_options_get (c->options, "user");
574 const char *auth_password = ZOOM_options_get (c->options, "pass");
576 ODR_MASK_SET(ireq->options, Z_Options_search);
577 ODR_MASK_SET(ireq->options, Z_Options_present);
578 ODR_MASK_SET(ireq->options, Z_Options_scan);
579 ODR_MASK_SET(ireq->options, Z_Options_sort);
581 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
582 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
585 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
586 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
587 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
589 impname = ZOOM_options_get (c->options, "implementationName");
590 ireq->implementationName =
591 odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
592 strcpy (ireq->implementationName, "");
595 strcat (ireq->implementationName, impname);
596 strcat (ireq->implementationName, "/");
598 strcat (ireq->implementationName, "ZOOM-C/YAZ");
600 *ireq->maximumRecordSize =
601 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
602 *ireq->preferredMessageSize =
603 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
605 if (auth_groupId || auth_password)
607 Z_IdPass *pass = odr_malloc(c->odr_out, sizeof(*pass));
610 if (auth_groupId && *auth_groupId)
612 pass->groupId = odr_malloc(c->odr_out, strlen(auth_groupId)+1);
613 strcpy(pass->groupId, auth_groupId);
617 if (auth_userId && *auth_userId)
619 pass->userId = odr_malloc(c->odr_out, strlen(auth_userId)+1);
620 strcpy(pass->userId, auth_userId);
624 if (auth_password && *auth_password)
626 pass->password = odr_malloc(c->odr_out, strlen(auth_password)+1);
627 strcpy(pass->password, auth_password);
632 auth->which = Z_IdAuthentication_idPass;
633 auth->u.idPass = pass;
634 ireq->idAuthentication = auth;
637 else if (auth_userId)
639 auth->which = Z_IdAuthentication_open;
640 auth->u.open = odr_malloc(c->odr_out, strlen(auth_userId)+1);
641 strcpy(auth->u.open, auth_userId);
642 ireq->idAuthentication = auth;
645 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
646 VAL_PROXY, 1, c->host_port);
653 static int ZOOM_connection_send_search (ZOOM_connection c)
656 int lslb, ssub, mspn;
658 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
659 Z_SearchRequest *search_req = apdu->u.searchRequest;
660 const char *elementSetName;
661 const char *smallSetElementSetName;
662 const char *mediumSetElementSetName;
665 assert (c->tasks->which == ZOOM_TASK_SEARCH);
667 r = c->tasks->u.resultset;
670 ZOOM_options_get (r->options, "elementSetName");
671 smallSetElementSetName =
672 ZOOM_options_get (r->options, "smallSetElementSetName");
673 mediumSetElementSetName =
674 ZOOM_options_get (r->options, "mediumSetElementSetName");
676 if (!smallSetElementSetName)
677 smallSetElementSetName = elementSetName;
679 if (!mediumSetElementSetName)
680 mediumSetElementSetName = elementSetName;
685 /* prepare query for the search request */
686 search_req->query = r->r_query;
688 search_req->databaseNames =
689 set_DatabaseNames (c, &search_req->num_databaseNames);
691 /* get syntax (no need to provide unless piggyback is in effect) */
692 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
694 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
695 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
696 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
697 if (lslb != -1 && ssub != -1 && mspn != -1)
699 /* So're a Z39.50 expert? Let's hope you don't do sort */
700 *search_req->largeSetLowerBound = lslb;
701 *search_req->smallSetUpperBound = ssub;
702 *search_req->mediumSetPresentNumber = mspn;
704 else if (r->start == 0 && r->count > 0
705 && r->piggyback && !r->r_sort_spec)
707 /* Regular piggyback - do it unless we're going to do sort */
708 *search_req->largeSetLowerBound = 2000000000;
709 *search_req->smallSetUpperBound = r->count;
710 *search_req->mediumSetPresentNumber = r->count;
711 smallSetElementSetName = 0; /* no need to provide this */
715 /* non-piggyback. Need not provide elementsets or syntaxes .. */
716 smallSetElementSetName = 0;
717 mediumSetElementSetName = 0;
720 if (smallSetElementSetName && *smallSetElementSetName)
722 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
724 esn->which = Z_ElementSetNames_generic;
725 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
726 search_req->smallSetElementSetNames = esn;
728 if (mediumSetElementSetName && *mediumSetElementSetName)
730 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
732 esn->which = Z_ElementSetNames_generic;
733 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
734 search_req->mediumSetElementSetNames = esn;
737 search_req->preferredRecordSyntax =
738 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
740 /* send search request */
746 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
748 Z_DefaultDiagFormat *r;
753 if (p->which != Z_DiagRec_defaultFormat)
755 c->error = ZOOM_ERROR_DECODE;
758 r = p->u.defaultFormat;
761 case Z_DefaultDiagFormat_v2Addinfo:
762 addinfo = r->u.v2Addinfo;
764 case Z_DefaultDiagFormat_v3Addinfo:
765 addinfo = r->u.v3Addinfo;
769 c->addinfo = xstrdup (addinfo);
770 c->error = *r->condition;
773 ZOOM_record ZOOM_record_clone (ZOOM_record srec)
780 odr_enc = odr_createmem(ODR_ENCODE);
781 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
783 buf = odr_getbuf (odr_enc, &size, 0);
785 nrec = xmalloc (sizeof(*nrec));
786 nrec->odr = odr_createmem(ODR_DECODE);
787 nrec->wrbuf_marc = 0;
788 odr_setbuf (nrec->odr, buf, size, 0);
789 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
791 odr_destroy (odr_enc);
795 ZOOM_record ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
797 return record_cache_lookup (s, pos, 0);
800 ZOOM_record ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
802 ZOOM_resultset_retrieve (r, 1, pos, 1);
803 return ZOOM_resultset_record_immediate (r, pos);
806 void ZOOM_record_destroy (ZOOM_record rec)
811 wrbuf_free (rec->wrbuf_marc, 1);
812 odr_destroy (rec->odr);
816 void *ZOOM_record_get (ZOOM_record rec, const char *type, size_t *len)
818 Z_NamePlusRecord *npr;
824 if (!strcmp (type, "database"))
826 return npr->databaseName;
828 else if (!strcmp (type, "syntax"))
830 if (npr->which == Z_NamePlusRecord_databaseRecord)
832 Z_External *r = (Z_External *) npr->u.databaseRecord;
833 oident *ent = oid_getentbyoid(r->direct_reference);
839 else if (!strcmp (type, "render"))
841 if (npr->which == Z_NamePlusRecord_databaseRecord)
843 Z_External *r = (Z_External *) npr->u.databaseRecord;
844 oident *ent = oid_getentbyoid(r->direct_reference);
846 if (r->which == Z_External_sutrs)
848 *len = r->u.sutrs->len;
849 return r->u.sutrs->buf;
851 else if (r->which == Z_External_octet)
860 case VAL_APPLICATION_XML:
863 if (!rec->wrbuf_marc)
864 rec->wrbuf_marc = wrbuf_alloc();
865 if (marc_display_wrbuf (r->u.octet_aligned->buf,
867 r->u.octet_aligned->len) > 0)
869 *len = wrbuf_len(rec->wrbuf_marc);
870 return wrbuf_buf(rec->wrbuf_marc);
873 *len = r->u.octet_aligned->len;
874 return r->u.octet_aligned->buf;
876 else if (r->which == Z_External_grs1)
884 else if (!strcmp (type, "raw"))
886 if (npr->which == Z_NamePlusRecord_databaseRecord)
889 return (Z_External *) npr->u.databaseRecord;
896 static void record_cache_add (ZOOM_resultset r,
897 Z_NamePlusRecord *npr,
899 const char *elementSetName)
901 ZOOM_record_cache rc;
903 for (rc = r->record_cache; rc; rc = rc->next)
907 if ((!elementSetName && !rc->elementSetName)
908 || (elementSetName && rc->elementSetName &&
909 !strcmp (elementSetName, rc->elementSetName)))
911 /* not destroying rc->npr (it's handled by nmem )*/
913 /* keeping wrbuf_marc too */
919 rc = odr_malloc (r->odr, sizeof(*rc));
922 rc->rec.wrbuf_marc = 0;
924 rc->elementSetName = odr_strdup (r->odr, elementSetName);
926 rc->elementSetName = 0;
928 rc->next = r->record_cache;
929 r->record_cache = rc;
932 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
934 const char *elementSetName)
936 ZOOM_record_cache rc;
938 for (rc = r->record_cache; rc; rc = rc->next)
942 if ((!elementSetName && !rc->elementSetName)
943 || (elementSetName && rc->elementSetName &&
944 !strcmp (elementSetName, rc->elementSetName)))
951 static void handle_records (ZOOM_connection c, Z_Records *sr,
954 ZOOM_resultset resultset;
958 if (c->tasks->which != ZOOM_TASK_SEARCH &&
959 c->tasks->which != ZOOM_TASK_RETRIEVE)
962 resultset = c->tasks->u.resultset;
964 if (sr && sr->which == Z_Records_NSD)
966 Z_DiagRec dr, *dr_p = &dr;
967 dr.which = Z_DiagRec_defaultFormat;
968 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
970 response_diag (c, dr_p);
972 else if (sr && sr->which == Z_Records_multipleNSD)
974 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
975 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
977 c->error = ZOOM_ERROR_DECODE;
981 if (resultset->count + resultset->start > resultset->size)
982 resultset->count = resultset->size - resultset->start;
983 if (resultset->count < 0)
984 resultset->count = 0;
985 if (sr && sr->which == Z_Records_DBOSD)
988 NMEM nmem = odr_extract_mem (c->odr_in);
989 Z_NamePlusRecordList *p =
990 sr->u.databaseOrSurDiagnostics;
991 for (i = 0; i<p->num_records; i++)
993 record_cache_add (resultset, p->records[i],
994 i+ resultset->start, 0);
996 /* transfer our response to search_nmem .. we need it later */
997 nmem_transfer (resultset->odr->mem, nmem);
999 if (present_phase && p->num_records == 0)
1001 /* present response and we didn't get any records! */
1002 c->error = ZOOM_ERROR_DECODE;
1005 else if (present_phase)
1007 /* present response and we didn't get any records! */
1008 c->error = ZOOM_ERROR_DECODE;
1013 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1015 handle_records (c, pr->records, 1);
1018 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1020 ZOOM_resultset resultset;
1022 yaz_log (LOG_DEBUG, "got search response");
1024 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1027 resultset = c->tasks->u.resultset;
1029 resultset->size = *sr->resultCount;
1030 handle_records (c, sr->records, 0);
1033 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1035 if (res->diagnostics && res->num_diagnostics > 0)
1036 response_diag (c, res->diagnostics[0]);
1039 static int send_sort (ZOOM_connection c)
1041 ZOOM_resultset resultset;
1043 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1046 resultset = c->tasks->u.resultset;
1050 resultset->r_sort_spec = 0;
1053 if (resultset->r_sort_spec)
1055 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1056 Z_SortRequest *req = apdu->u.sortRequest;
1058 req->num_inputResultSetNames = 1;
1059 req->inputResultSetNames = (Z_InternationalString **)
1060 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1061 req->inputResultSetNames[0] = odr_strdup (c->odr_out, "default");
1062 req->sortedResultSetName = odr_strdup (c->odr_out, "default");
1063 req->sortSequence = resultset->r_sort_spec;
1064 resultset->r_sort_spec = 0;
1065 send_APDU (c, apdu);
1071 static int send_present (ZOOM_connection c)
1073 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1074 Z_PresentRequest *req = apdu->u.presentRequest;
1076 const char *syntax =
1077 ZOOM_options_get (c->options, "preferredRecordSyntax");
1078 const char *element =
1079 ZOOM_options_get (c->options, "elementSetName");
1080 ZOOM_resultset resultset;
1084 if (c->tasks->which != ZOOM_TASK_SEARCH &&
1085 c->tasks->which != ZOOM_TASK_RETRIEVE)
1088 resultset = c->tasks->u.resultset;
1090 if (c->error) /* don't continue on error */
1092 if (resultset->start < 0)
1094 for (i = 0; i<resultset->count; i++)
1097 record_cache_lookup (resultset, i + resultset->start, 0);
1101 if (i == resultset->count)
1104 resultset->start += i;
1105 resultset->count -= i;
1106 *req->resultSetStartPoint = resultset->start + 1;
1107 *req->numberOfRecordsRequested = resultset->count;
1108 assert (*req->numberOfRecordsRequested > 0);
1110 if (syntax && *syntax)
1111 req->preferredRecordSyntax =
1112 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1114 if (element && *element)
1116 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
1117 Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1119 esn->which = Z_ElementSetNames_generic;
1120 esn->u.generic = odr_strdup (c->odr_out, element);
1121 compo->which = Z_RecordComp_simple;
1122 compo->u.simple = esn;
1123 req->recordComposition = compo;
1125 send_APDU (c, apdu);
1129 static int ZOOM_connection_exec_task (ZOOM_connection c)
1131 ZOOM_task task = c->tasks;
1133 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1136 if (c->error != ZOOM_ERROR_NONE ||
1137 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1139 ZOOM_connection_remove_tasks (c);
1142 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d", task->which);
1146 switch (task->which)
1148 case ZOOM_TASK_SEARCH:
1149 /* see if search hasn't been sent yet. */
1150 if (ZOOM_connection_send_search (c))
1153 case ZOOM_TASK_RETRIEVE:
1154 if (send_present (c))
1157 case ZOOM_TASK_CONNECT:
1161 ZOOM_connection_remove_task (c);
1165 static int send_sort_present (ZOOM_connection c)
1167 int r = send_sort (c);
1169 r = send_present (c);
1173 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1175 Z_InitResponse *initrs;
1177 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1181 case Z_APDU_initResponse:
1182 initrs = apdu->u.initResponse;
1183 if (!*initrs->result)
1185 c->error = ZOOM_ERROR_INIT;
1190 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1192 xfree (c->cookie_in);
1195 c->cookie_in = xstrdup(cookie);
1198 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1199 ZOOM_connection_remove_task (c);
1201 ZOOM_connection_exec_task (c);
1204 case Z_APDU_searchResponse:
1205 handle_search_response (c, apdu->u.searchResponse);
1206 if (!send_sort_present (c))
1207 ZOOM_connection_remove_task (c);
1209 case Z_APDU_presentResponse:
1210 handle_present_response (c, apdu->u.presentResponse);
1211 if (!send_present (c))
1212 ZOOM_connection_remove_task (c);
1214 case Z_APDU_sortResponse:
1215 sort_response (c, apdu->u.sortResponse);
1216 if (!send_present (c))
1217 ZOOM_connection_remove_task (c);
1221 static int do_read (ZOOM_connection c)
1226 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1231 c->error= ZOOM_ERROR_CONNECTION_LOST;
1236 odr_reset (c->odr_in);
1237 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1238 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1240 c->error = ZOOM_ERROR_DECODE;
1245 handle_apdu (c, apdu);
1251 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
1255 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1257 if (c->state == STATE_CONNECTING)
1258 c->error = ZOOM_ERROR_CONNECT;
1260 c->error = ZOOM_ERROR_CONNECTION_LOST;
1266 c->state = STATE_ESTABLISHED;
1267 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_WRITE|ZOOM_SELECT_EXCEPT;
1271 c->state = STATE_ESTABLISHED;
1272 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
1277 static int do_write(ZOOM_connection c)
1279 return do_write_ex (c, c->buf_out, c->len_out);
1283 const char *ZOOM_connection_option_get (ZOOM_connection c, const char *key)
1285 return ZOOM_options_get (c->options, key);
1288 void ZOOM_connection_option_set (ZOOM_connection c, const char *key,
1291 ZOOM_options_set (c->options, key, val);
1294 const char *ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
1296 return ZOOM_options_get (r->options, key);
1299 void ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
1302 ZOOM_options_set (r->options, key, val);
1306 int ZOOM_connection_errcode (ZOOM_connection c)
1308 return ZOOM_connection_error (c, 0, 0);
1311 const char *ZOOM_connection_errmsg (ZOOM_connection c)
1314 ZOOM_connection_error (c, &msg, 0);
1318 const char *ZOOM_connection_addinfo (ZOOM_connection c)
1320 const char *addinfo;
1321 ZOOM_connection_error (c, 0, &addinfo);
1325 int ZOOM_connection_error (ZOOM_connection c, const char **cp,
1326 const char **addinfo)
1328 int error = c->error;
1333 case ZOOM_ERROR_NONE:
1334 *cp = "No error"; break;
1335 case ZOOM_ERROR_CONNECT:
1336 *cp = "Connect failed"; break;
1337 case ZOOM_ERROR_MEMORY:
1338 *cp = "Out of memory"; break;
1339 case ZOOM_ERROR_ENCODE:
1340 *cp = "Encoding failed"; break;
1341 case ZOOM_ERROR_DECODE:
1342 *cp = "Decoding failed"; break;
1343 case ZOOM_ERROR_CONNECTION_LOST:
1344 *cp = "Connection lost"; break;
1345 case ZOOM_ERROR_INIT:
1346 *cp = "Init rejected"; break;
1347 case ZOOM_ERROR_INTERNAL:
1348 *cp = "Internal failure"; break;
1349 case ZOOM_ERROR_TIMEOUT:
1350 *cp = "Timeout"; break;
1352 *cp = diagbib1_str (error);
1358 *addinfo = c->addinfo;
1365 int ZOOM_connection_do_io(ZOOM_connection c, int mask)
1369 int r = cs_look(c->cs);
1370 yaz_log (LOG_LOG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
1375 c->error = ZOOM_ERROR_CONNECT;
1378 else if (r == CS_CONNECT)
1380 yaz_log (LOG_LOG, "calling rcvconnect");
1381 if (cs_rcvconnect (c->cs) < 0)
1383 c->error = ZOOM_ERROR_CONNECT;
1387 ZOOM_connection_send_init (c);
1391 if (mask & ZOOM_SELECT_READ)
1393 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1397 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d", c, mask);
1398 if (c->state == STATE_CONNECTING)
1400 if (mask & ZOOM_SELECT_WRITE)
1401 ZOOM_connection_send_init (c);
1404 c->error = ZOOM_ERROR_CONNECT;
1408 else if (c->state == STATE_ESTABLISHED)
1410 if (mask & ZOOM_SELECT_READ)
1412 if (c->cs && (mask & ZOOM_SELECT_WRITE))
1417 c->error = ZOOM_ERROR_INTERNAL;
1421 event = ZOOM_Event_create (1);
1422 ZOOM_connection_put_event (c, event);
1427 int ZOOM_event (int no, ZOOM_connection *cs)
1430 struct pollfd pollfds[1024];
1431 ZOOM_connection poll_cs[1024];
1434 fd_set input, output, except;
1439 for (i = 0; i<no; i++)
1441 ZOOM_connection c = cs[i];
1443 if (c && (event = ZOOM_connection_get_event(c)))
1445 ZOOM_Event_destroy (event);
1449 for (i = 0; i<no; i++)
1451 ZOOM_connection c = cs[i];
1452 if (c && ZOOM_connection_exec_task (c))
1466 for (i = 0; i<no; i++)
1468 ZOOM_connection c = cs[i];
1473 fd = z3950_connection_socket(c);
1474 mask = z3950_connection_mask(c);
1484 short poll_events = 0;
1486 if (mask & ZOOM_SELECT_READ)
1487 poll_events += POLLIN;
1488 if (mask & ZOOM_SELECT_WRITE)
1489 poll_events += POLLOUT;
1490 if (mask & ZOOM_SELECT_EXCEPT)
1491 poll_events += POLLERR;
1492 pollfds[nfds].fd = fd;
1493 pollfds[nfds].events = poll_events;
1494 pollfds[nfds].revents = 0;
1499 if (mask & ZOOM_SELECT_READ)
1501 FD_SET (fd, &input);
1504 if (mask & ZOOM_SELECT_WRITE)
1506 FD_SET (fd, &output);
1509 if (mask & ZOOM_SELECT_EXCEPT)
1511 FD_SET (fd, &except);
1519 yaz_log (LOG_DEBUG, "poll start");
1520 r = poll (pollfds, nfds, 15000);
1521 yaz_log (LOG_DEBUG, "poll stop, returned r=%d", r);
1522 for (i = 0; i<nfds; i++)
1524 ZOOM_connection c = poll_cs[i];
1528 if (pollfds[i].revents & POLLIN)
1529 mask += ZOOM_SELECT_READ;
1530 if (pollfds[i].revents & POLLOUT)
1531 mask += ZOOM_SELECT_WRITE;
1532 if (pollfds[i].revents & POLLERR)
1533 mask += ZOOM_SELECT_EXCEPT;
1535 ZOOM_connection_do_io(c, mask);
1537 else if (r == 0 && c->mask)
1539 ZOOM_Event event = ZOOM_Event_create(0);
1540 /* timeout and this connection was waiting */
1541 c->error = ZOOM_ERROR_TIMEOUT;
1543 ZOOM_connection_put_event(c, event);
1547 yaz_log (LOG_DEBUG, "select start");
1548 r = select (max_fd+1, &input, &output, &except, &tv);
1549 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
1550 for (i = 0; i<no; i++)
1552 ZOOM_connection c = cs[i];
1557 fd = z3950_connection_socket(c);
1561 /* no timeout and real socket */
1562 if (FD_ISSET(fd, &input))
1563 mask += ZOOM_SELECT_READ;
1564 if (FD_ISSET(fd, &output))
1565 mask += ZOOM_SELECT_WRITE;
1566 if (FD_ISSET(fd, &except))
1567 mask += ZOOM_SELECT_EXCEPT;
1569 ZOOM_connection_do_io(c, mask);
1571 if (r == 0 && c->mask)
1573 ZOOM_Event event = ZOOM_Event_create(0);
1574 /* timeout and this connection was waiting */
1575 c->error = ZOOM_ERROR_TIMEOUT;
1577 yaz_log (LOG_DEBUG, "timeout");
1578 ZOOM_connection_put_event(c, event);
1582 for (i = 0; i<no; i++)
1584 ZOOM_connection c = cs[i];
1586 if (c && (event = ZOOM_connection_get_event(c)))
1588 ZOOM_Event_destroy (event);