2 * $Id: zoom-c.c,v 1.8 2001-11-15 21:59:40 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 Z3950_Event Z3950_Event_create (int kind)
21 Z3950_Event event = xmalloc (sizeof(*event));
28 static void Z3950_Event_destroy (Z3950_Event event)
33 static void Z3950_connection_put_event (Z3950_connection c, Z3950_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 Z3950_Event Z3950_connection_get_event(Z3950_connection c)
53 // get from front of queue
54 Z3950_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 (Z3950_connection c)
71 c->error = Z3950_ERROR_NONE;
76 Z3950_task Z3950_connection_add_task (Z3950_connection c, int which)
78 Z3950_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 Z3950_connection_remove_task (Z3950_connection c)
92 Z3950_task task = c->tasks;
96 c->tasks = task->next;
99 case Z3950_TASK_SEARCH:
100 Z3950_resultset_destroy (task->u.resultset);
102 case Z3950_TASK_RETRIEVE:
103 Z3950_resultset_destroy (task->u.resultset);
105 case Z3950_TASK_CONNECT:
114 void Z3950_connection_remove_tasks (Z3950_connection c)
117 Z3950_connection_remove_task(c);
120 static Z3950_record record_cache_lookup (Z3950_resultset r,
122 const char *elementSetName);
124 Z3950_connection Z3950_connection_create (Z3950_options options)
126 Z3950_connection c = xmalloc (sizeof(*c));
130 c->state = STATE_IDLE;
131 c->error = Z3950_ERROR_NONE;
139 c->options = Z3950_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 (Z3950_connection con, int *num)
162 char **databaseNames;
165 const char *cp = Z3950_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 Z3950_connection Z3950_connection_new (const char *host, int portnum)
212 Z3950_connection c = Z3950_connection_create (0);
214 Z3950_connection_connect (c, host, portnum);
218 void Z3950_connection_connect(Z3950_connection c,
219 const char *host, int portnum)
224 val = Z3950_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 c->async = Z3950_options_get_bool (c->options, "async", 0);
241 task = Z3950_connection_add_task (c, Z3950_TASK_CONNECT);
245 while (Z3950_event (1, &c))
250 Z3950_query Z3950_query_create(void)
252 Z3950_query s = xmalloc (sizeof(*s));
257 s->odr = odr_createmem (ODR_ENCODE);
262 const char *Z3950_connection_host (Z3950_connection c)
267 void Z3950_query_destroy(Z3950_query s)
273 yaz_log (LOG_DEBUG, "Z3950_query_destroy count=%d", s->refcount);
274 if (s->refcount == 0)
276 odr_destroy (s->odr);
281 int Z3950_query_prefix(Z3950_query s, const char *str)
283 s->query = odr_malloc (s->odr, sizeof(*s->query));
284 s->query->which = Z_Query_type_1;
285 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
286 if (!s->query->u.type_1)
291 int Z3950_query_sortby(Z3950_query s, const char *criteria)
293 s->sort_spec = yaz_sort_spec (s->odr, criteria);
299 static int do_write(Z3950_connection c);
301 void Z3950_connection_destroy(Z3950_connection c)
308 for (r = c->resultsets; r; r = r->next)
313 odr_destroy (c->odr_in);
314 odr_destroy (c->odr_out);
315 Z3950_options_destroy (c->options);
316 Z3950_connection_remove_tasks (c);
317 xfree (c->host_port);
321 void Z3950_resultset_addref (Z3950_resultset r)
326 Z3950_resultset Z3950_resultset_create ()
328 Z3950_resultset r = xmalloc (sizeof(*r));
332 r->odr = odr_createmem (ODR_ENCODE);
345 Z3950_resultset Z3950_connection_search_pqf(Z3950_connection c, const char *q)
348 Z3950_query s = Z3950_query_create();
350 Z3950_query_prefix (s, q);
352 r = Z3950_connection_search (c, s);
353 Z3950_query_destroy (s);
357 Z3950_resultset Z3950_connection_search(Z3950_connection c, Z3950_query q)
359 Z3950_resultset r = Z3950_resultset_create ();
362 r->r_sort_spec = q->sort_spec;
363 r->r_query = q->query;
366 r->options = Z3950_options_create_with_parent(c->options);
368 r->start = Z3950_options_get_int(r->options, "start", 0);
369 r->count = Z3950_options_get_int(r->options, "count", 0);
370 r->piggyback = Z3950_options_get_bool (r->options, "piggyback", 1);
373 r->next = c->resultsets;
376 task = Z3950_connection_add_task (c, Z3950_TASK_SEARCH);
377 task->u.resultset = r;
378 Z3950_resultset_addref (r);
384 while (Z3950_event (1, &c))
390 void Z3950_resultset_destroy(Z3950_resultset r)
395 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
396 if (r->refcount == 0)
400 /* remove ourselves from the resultsets in connection */
401 Z3950_resultset *rp = &r->connection->resultsets;
404 assert (*rp); /* we must be in this list!! */
406 { /* OK, we're here - take us out of it */
413 Z3950_query_destroy (r->search);
414 Z3950_options_destroy (r->options);
415 odr_destroy (r->odr);
420 size_t Z3950_resultset_size (Z3950_resultset r)
425 static void do_close (Z3950_connection c)
431 c->state = STATE_IDLE;
434 static void Z3950_resultset_retrieve (Z3950_resultset r,
435 int force_sync, int start, int count)
445 if (start >= r->size)
448 if (start + count > r->size)
449 count = r->size - start;
451 task = Z3950_connection_add_task (c, Z3950_TASK_RETRIEVE);
452 task->u.resultset = r;
453 Z3950_resultset_addref (r);
458 if (!r->connection->async || force_sync)
459 while (r->connection && Z3950_event (1, &r->connection))
463 void Z3950_resultset_records (Z3950_resultset r, Z3950_record *recs,
464 size_t start, size_t count)
466 int force_present = 0;
472 Z3950_resultset_retrieve (r, force_present, start, count);
476 for (i = 0; i< count; i++)
477 recs[i] = Z3950_resultset_record_immediate (r, i+start);
481 static int do_connect (Z3950_connection c)
484 const char *effective_host;
487 effective_host = c->proxy;
489 effective_host = c->host_port;
491 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
494 c->cs = cs_create_host (effective_host, 0, &add);
498 int ret = cs_connect (c->cs, add);
499 yaz_log (LOG_DEBUG, "cs_connect returned %d", ret);
502 c->state = STATE_CONNECTING;
503 c->mask = Z3950_SELECT_READ | Z3950_SELECT_WRITE |
508 c->state = STATE_IDLE;
509 c->error = Z3950_ERROR_CONNECT;
513 int z3950_connection_socket(Z3950_connection c)
516 return cs_fileno(c->cs);
520 int z3950_connection_mask(Z3950_connection c)
527 static int encode_APDU(Z3950_connection c, Z_APDU *a, ODR out)
532 sprintf (str, "send_APDU t=%p type=%d", c, a->which);
535 Z_OtherInformation **oi;
537 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
539 if (!z_APDU(out, &a, 0, 0))
541 FILE *outf = fopen("/tmp/apdu.txt", "w");
544 ODR odr_pr = odr_createmem(ODR_PRINT);
545 fprintf (outf, "a=%p\n", a);
546 odr_setprint(odr_pr, outf);
547 z_APDU(odr_pr, &a, 0, 0);
551 c->error = Z3950_ERROR_ENCODE;
558 static int send_APDU (Z3950_connection c, Z_APDU *a)
561 if (encode_APDU(c, a, c->odr_out))
563 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
564 odr_reset(c->odr_out);
569 static int Z3950_connection_send_init (Z3950_connection c)
572 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
573 Z_InitRequest *ireq = apdu->u.initRequest;
574 Z_IdAuthentication *auth = odr_malloc(c->odr_out, sizeof(*auth));
575 const char *auth_groupId = Z3950_options_get (c->options, "group");
576 const char *auth_userId = Z3950_options_get (c->options, "user");
577 const char *auth_password = Z3950_options_get (c->options, "pass");
579 ODR_MASK_SET(ireq->options, Z_Options_search);
580 ODR_MASK_SET(ireq->options, Z_Options_present);
581 ODR_MASK_SET(ireq->options, Z_Options_scan);
582 ODR_MASK_SET(ireq->options, Z_Options_sort);
584 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
585 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
588 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
589 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
590 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
592 impname = Z3950_options_get (c->options, "implementationName");
593 ireq->implementationName =
594 odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
595 strcpy (ireq->implementationName, "");
598 strcat (ireq->implementationName, impname);
599 strcat (ireq->implementationName, "/");
601 strcat (ireq->implementationName, "ZOOM-C/YAZ");
603 *ireq->maximumRecordSize =
604 Z3950_options_get_int (c->options, "maximumRecordSize", 1024*1024);
605 *ireq->preferredMessageSize =
606 Z3950_options_get_int (c->options, "preferredMessageSize", 1024*1024);
608 if (auth_groupId || auth_password)
610 Z_IdPass *pass = odr_malloc(c->odr_out, sizeof(*pass));
613 if (auth_groupId && *auth_groupId)
615 pass->groupId = odr_malloc(c->odr_out, strlen(auth_groupId)+1);
616 strcpy(pass->groupId, auth_groupId);
620 if (auth_userId && *auth_userId)
622 pass->userId = odr_malloc(c->odr_out, strlen(auth_userId)+1);
623 strcpy(pass->userId, auth_userId);
627 if (auth_password && *auth_password)
629 pass->password = odr_malloc(c->odr_out, strlen(auth_password)+1);
630 strcpy(pass->password, auth_password);
635 auth->which = Z_IdAuthentication_idPass;
636 auth->u.idPass = pass;
637 ireq->idAuthentication = auth;
640 else if (auth_userId)
642 auth->which = Z_IdAuthentication_open;
643 auth->u.open = odr_malloc(c->odr_out, strlen(auth_userId)+1);
644 strcpy(auth->u.open, auth_userId);
645 ireq->idAuthentication = auth;
648 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
649 VAL_PROXY, 1, c->host_port);
656 static int Z3950_connection_send_search (Z3950_connection c)
659 int lslb, ssub, mspn;
661 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
662 Z_SearchRequest *search_req = apdu->u.searchRequest;
663 const char *elementSetName;
664 const char *smallSetElementSetName;
665 const char *mediumSetElementSetName;
668 assert (c->tasks->which == Z3950_TASK_SEARCH);
670 r = c->tasks->u.resultset;
673 Z3950_options_get (r->options, "elementSetName");
674 smallSetElementSetName =
675 Z3950_options_get (r->options, "smallSetElementSetName");
676 mediumSetElementSetName =
677 Z3950_options_get (r->options, "mediumSetElementSetName");
679 if (!smallSetElementSetName)
680 smallSetElementSetName = elementSetName;
682 if (!mediumSetElementSetName)
683 mediumSetElementSetName = elementSetName;
688 /* prepare query for the search request */
689 search_req->query = r->r_query;
691 search_req->databaseNames =
692 set_DatabaseNames (c, &search_req->num_databaseNames);
694 /* get syntax (no need to provide unless piggyback is in effect) */
695 syntax = Z3950_options_get (r->options, "preferredRecordSyntax");
697 lslb = Z3950_options_get_int (r->options, "largeSetLowerBound", -1);
698 ssub = Z3950_options_get_int (r->options, "smallSetUpperBound", -1);
699 mspn = Z3950_options_get_int (r->options, "mediumSetPresentNumber", -1);
700 if (lslb != -1 && ssub != -1 && mspn != -1)
702 /* So're a Z39.50 expert? Let's hope you don't do sort */
703 *search_req->largeSetLowerBound = lslb;
704 *search_req->smallSetUpperBound = ssub;
705 *search_req->mediumSetPresentNumber = mspn;
707 else if (r->start == 0 && r->count > 0
708 && r->piggyback && !r->r_sort_spec)
710 /* Regular piggyback - do it unless we're going to do sort */
711 *search_req->largeSetLowerBound = 2000000000;
712 *search_req->smallSetUpperBound = r->count;
713 *search_req->mediumSetPresentNumber = r->count;
714 smallSetElementSetName = 0; /* no need to provide this */
718 /* non-piggyback. Need not provide elementsets or syntaxes .. */
719 smallSetElementSetName = 0;
720 mediumSetElementSetName = 0;
723 if (smallSetElementSetName && *smallSetElementSetName)
725 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
727 esn->which = Z_ElementSetNames_generic;
728 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
729 search_req->smallSetElementSetNames = esn;
731 if (mediumSetElementSetName && *mediumSetElementSetName)
733 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
735 esn->which = Z_ElementSetNames_generic;
736 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
737 search_req->mediumSetElementSetNames = esn;
740 search_req->preferredRecordSyntax =
741 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
743 /* send search request */
749 static void response_diag (Z3950_connection c, Z_DiagRec *p)
751 Z_DefaultDiagFormat *r;
756 if (p->which != Z_DiagRec_defaultFormat)
758 c->error = Z3950_ERROR_DECODE;
761 r = p->u.defaultFormat;
764 case Z_DefaultDiagFormat_v2Addinfo:
765 addinfo = r->u.v2Addinfo;
767 case Z_DefaultDiagFormat_v3Addinfo:
768 addinfo = r->u.v3Addinfo;
772 c->addinfo = xstrdup (addinfo);
773 c->error = *r->condition;
776 Z3950_record Z3950_record_dup (const Z3950_record srec)
783 odr_enc = odr_createmem(ODR_ENCODE);
784 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
786 buf = odr_getbuf (odr_enc, &size, 0);
788 nrec = xmalloc (sizeof(*nrec));
789 nrec->odr = odr_createmem(ODR_DECODE);
790 nrec->wrbuf_marc = 0;
791 odr_setbuf (nrec->odr, buf, size, 0);
792 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
794 odr_destroy (odr_enc);
798 Z3950_record Z3950_resultset_record_immediate (Z3950_resultset s,size_t pos)
800 return record_cache_lookup (s, pos, 0);
803 Z3950_record Z3950_resultset_record (Z3950_resultset r, size_t pos)
805 Z3950_resultset_retrieve (r, 1, pos, 1);
806 return Z3950_resultset_record_immediate (r, pos);
809 void Z3950_record_destroy (Z3950_record rec)
814 wrbuf_free (rec->wrbuf_marc, 1);
815 odr_destroy (rec->odr);
819 void *Z3950_record_get (Z3950_record rec, const char *type, size_t *len)
821 Z_NamePlusRecord *npr;
827 if (!strcmp (type, "database"))
829 return npr->databaseName;
831 else if (!strcmp (type, "syntax"))
833 if (npr->which == Z_NamePlusRecord_databaseRecord)
835 Z_External *r = (Z_External *) npr->u.databaseRecord;
836 oident *ent = oid_getentbyoid(r->direct_reference);
842 else if (!strcmp (type, "render"))
844 if (npr->which == Z_NamePlusRecord_databaseRecord)
846 Z_External *r = (Z_External *) npr->u.databaseRecord;
847 oident *ent = oid_getentbyoid(r->direct_reference);
849 if (r->which == Z_External_sutrs)
851 *len = r->u.sutrs->len;
852 return r->u.sutrs->buf;
854 else if (r->which == Z_External_octet)
863 case VAL_APPLICATION_XML:
866 if (!rec->wrbuf_marc)
867 rec->wrbuf_marc = wrbuf_alloc();
868 if (marc_display_wrbuf (r->u.octet_aligned->buf,
870 r->u.octet_aligned->len) > 0)
872 *len = wrbuf_len(rec->wrbuf_marc);
873 return wrbuf_buf(rec->wrbuf_marc);
876 *len = r->u.octet_aligned->len;
877 return r->u.octet_aligned->buf;
879 else if (r->which == Z_External_grs1)
887 else if (!strcmp (type, "raw"))
889 if (npr->which == Z_NamePlusRecord_databaseRecord)
892 return (Z_External *) npr->u.databaseRecord;
899 static void record_cache_add (Z3950_resultset r,
900 Z_NamePlusRecord *npr,
902 const char *elementSetName)
904 Z3950_record_cache rc;
906 for (rc = r->record_cache; rc; rc = rc->next)
910 if ((!elementSetName && !rc->elementSetName)
911 || (elementSetName && rc->elementSetName &&
912 !strcmp (elementSetName, rc->elementSetName)))
914 /* not destroying rc->npr (it's handled by nmem )*/
916 /* keeping wrbuf_marc too */
922 rc = odr_malloc (r->odr, sizeof(*rc));
925 rc->rec.wrbuf_marc = 0;
927 rc->elementSetName = odr_strdup (r->odr, elementSetName);
929 rc->elementSetName = 0;
931 rc->next = r->record_cache;
932 r->record_cache = rc;
935 static Z3950_record record_cache_lookup (Z3950_resultset r,
937 const char *elementSetName)
939 Z3950_record_cache rc;
941 for (rc = r->record_cache; rc; rc = rc->next)
945 if ((!elementSetName && !rc->elementSetName)
946 || (elementSetName && rc->elementSetName &&
947 !strcmp (elementSetName, rc->elementSetName)))
954 static void handle_records (Z3950_connection c, Z_Records *sr,
957 Z3950_resultset resultset;
961 if (c->tasks->which != Z3950_TASK_SEARCH &&
962 c->tasks->which != Z3950_TASK_RETRIEVE)
965 resultset = c->tasks->u.resultset;
967 if (sr && sr->which == Z_Records_NSD)
969 Z_DiagRec dr, *dr_p = &dr;
970 dr.which = Z_DiagRec_defaultFormat;
971 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
973 response_diag (c, dr_p);
975 else if (sr && sr->which == Z_Records_multipleNSD)
977 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
978 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
980 c->error = Z3950_ERROR_DECODE;
984 if (resultset->count + resultset->start > resultset->size)
985 resultset->count = resultset->size - resultset->start;
986 if (resultset->count < 0)
987 resultset->count = 0;
988 if (sr && sr->which == Z_Records_DBOSD)
991 NMEM nmem = odr_extract_mem (c->odr_in);
992 Z_NamePlusRecordList *p =
993 sr->u.databaseOrSurDiagnostics;
994 for (i = 0; i<p->num_records; i++)
996 record_cache_add (resultset, p->records[i],
997 i+ resultset->start, 0);
999 /* transfer our response to search_nmem .. we need it later */
1000 nmem_transfer (resultset->odr->mem, nmem);
1001 nmem_destroy (nmem);
1002 if (present_phase && p->num_records == 0)
1004 /* present response and we didn't get any records! */
1005 c->error = Z3950_ERROR_DECODE;
1008 else if (present_phase)
1010 /* present response and we didn't get any records! */
1011 c->error = Z3950_ERROR_DECODE;
1016 static void handle_present_response (Z3950_connection c, Z_PresentResponse *pr)
1018 handle_records (c, pr->records, 1);
1021 static void handle_search_response (Z3950_connection c, Z_SearchResponse *sr)
1023 Z3950_resultset resultset;
1025 yaz_log (LOG_DEBUG, "got search response");
1027 if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH)
1030 resultset = c->tasks->u.resultset;
1032 resultset->size = *sr->resultCount;
1033 handle_records (c, sr->records, 0);
1036 static void sort_response (Z3950_connection c, Z_SortResponse *res)
1038 if (res->diagnostics && res->num_diagnostics > 0)
1039 response_diag (c, res->diagnostics[0]);
1042 static int send_sort (Z3950_connection c)
1044 Z3950_resultset resultset;
1046 if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH)
1049 resultset = c->tasks->u.resultset;
1053 resultset->r_sort_spec = 0;
1056 if (resultset->r_sort_spec)
1058 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1059 Z_SortRequest *req = apdu->u.sortRequest;
1061 req->num_inputResultSetNames = 1;
1062 req->inputResultSetNames = (Z_InternationalString **)
1063 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1064 req->inputResultSetNames[0] = odr_strdup (c->odr_out, "default");
1065 req->sortedResultSetName = odr_strdup (c->odr_out, "default");
1066 req->sortSequence = resultset->r_sort_spec;
1067 resultset->r_sort_spec = 0;
1068 send_APDU (c, apdu);
1074 static int send_present (Z3950_connection c)
1076 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1077 Z_PresentRequest *req = apdu->u.presentRequest;
1079 const char *syntax =
1080 Z3950_options_get (c->options, "preferredRecordSyntax");
1081 const char *element =
1082 Z3950_options_get (c->options, "elementSetName");
1083 Z3950_resultset resultset;
1087 if (c->tasks->which != Z3950_TASK_SEARCH &&
1088 c->tasks->which != Z3950_TASK_RETRIEVE)
1091 resultset = c->tasks->u.resultset;
1093 if (c->error) /* don't continue on error */
1095 if (resultset->start < 0)
1097 for (i = 0; i<resultset->count; i++)
1100 record_cache_lookup (resultset, i + resultset->start, 0);
1104 if (i == resultset->count)
1107 resultset->start += i;
1108 resultset->count -= i;
1109 *req->resultSetStartPoint = resultset->start + 1;
1110 *req->numberOfRecordsRequested = resultset->count;
1111 assert (*req->numberOfRecordsRequested > 0);
1113 if (syntax && *syntax)
1114 req->preferredRecordSyntax =
1115 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1117 if (element && *element)
1119 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
1120 Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1122 esn->which = Z_ElementSetNames_generic;
1123 esn->u.generic = odr_strdup (c->odr_out, element);
1124 compo->which = Z_RecordComp_simple;
1125 compo->u.simple = esn;
1126 req->recordComposition = compo;
1128 send_APDU (c, apdu);
1132 static int Z3950_connection_exec_task (Z3950_connection c)
1134 Z3950_task task = c->tasks;
1136 yaz_log (LOG_LOG, "Z3950_connection_exec_task");
1139 if (c->error != Z3950_ERROR_NONE ||
1140 (!c->cs && task->which != Z3950_TASK_CONNECT))
1142 Z3950_connection_remove_tasks (c);
1145 yaz_log (LOG_DEBUG, "Z3950_connection_exec_task type=%d", task->which);
1149 switch (task->which)
1151 case Z3950_TASK_SEARCH:
1152 /* see if search hasn't been sent yet. */
1153 if (Z3950_connection_send_search (c))
1156 case Z3950_TASK_RETRIEVE:
1157 if (send_present (c))
1160 case Z3950_TASK_CONNECT:
1164 Z3950_connection_remove_task (c);
1168 static int send_sort_present (Z3950_connection c)
1170 int r = send_sort (c);
1172 r = send_present (c);
1176 static void handle_apdu (Z3950_connection c, Z_APDU *apdu)
1178 Z_InitResponse *initrs;
1180 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1184 case Z_APDU_initResponse:
1185 initrs = apdu->u.initResponse;
1186 if (!*initrs->result)
1188 c->error = Z3950_ERROR_INIT;
1193 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1195 xfree (c->cookie_in);
1198 c->cookie_in = xstrdup(cookie);
1201 assert (c->tasks->which == Z3950_TASK_CONNECT);
1202 Z3950_connection_remove_task (c);
1204 Z3950_connection_exec_task (c);
1207 case Z_APDU_searchResponse:
1208 handle_search_response (c, apdu->u.searchResponse);
1209 if (!send_sort_present (c))
1210 Z3950_connection_remove_task (c);
1212 case Z_APDU_presentResponse:
1213 handle_present_response (c, apdu->u.presentResponse);
1214 if (!send_present (c))
1215 Z3950_connection_remove_task (c);
1217 case Z_APDU_sortResponse:
1218 sort_response (c, apdu->u.sortResponse);
1219 if (!send_present (c))
1220 Z3950_connection_remove_task (c);
1224 static int do_read (Z3950_connection c)
1229 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1234 c->error= Z3950_ERROR_CONNECTION_LOST;
1239 odr_reset (c->odr_in);
1240 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1241 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1243 c->error = Z3950_ERROR_DECODE;
1248 handle_apdu (c, apdu);
1254 static int do_write_ex (Z3950_connection c, char *buf_out, int len_out)
1258 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1260 if (c->state == STATE_CONNECTING)
1261 c->error = Z3950_ERROR_CONNECT;
1263 c->error = Z3950_ERROR_CONNECTION_LOST;
1269 c->state = STATE_ESTABLISHED;
1270 c->mask = Z3950_SELECT_READ|Z3950_SELECT_WRITE|Z3950_SELECT_EXCEPT;
1274 c->state = STATE_ESTABLISHED;
1275 c->mask = Z3950_SELECT_READ|Z3950_SELECT_EXCEPT;
1280 static int do_write(Z3950_connection c)
1282 return do_write_ex (c, c->buf_out, c->len_out);
1286 const char *Z3950_connection_option_get (Z3950_connection c, const char *key)
1288 return Z3950_options_get (c->options, key);
1291 void Z3950_connection_option_set (Z3950_connection c, const char *key,
1294 Z3950_options_set (c->options, key, val);
1297 const char *Z3950_resultset_option_get (Z3950_resultset r, const char *key)
1299 return Z3950_options_get (r->options, key);
1302 void Z3950_resultset_option_set (Z3950_resultset r, const char *key,
1305 Z3950_options_set (r->options, key, val);
1309 int Z3950_connection_errcode (Z3950_connection c)
1311 return Z3950_connection_error (c, 0, 0);
1314 const char *Z3950_connection_errmsg (Z3950_connection c)
1317 Z3950_connection_error (c, &msg, 0);
1321 const char *Z3950_connection_addinfo (Z3950_connection c)
1323 const char *addinfo;
1324 Z3950_connection_error (c, 0, &addinfo);
1328 int Z3950_connection_error (Z3950_connection c, const char **cp,
1329 const char **addinfo)
1331 int error = c->error;
1336 case Z3950_ERROR_NONE:
1337 *cp = "No error"; break;
1338 case Z3950_ERROR_CONNECT:
1339 *cp = "Connect failed"; break;
1340 case Z3950_ERROR_MEMORY:
1341 *cp = "Out of memory"; break;
1342 case Z3950_ERROR_ENCODE:
1343 *cp = "Encoding failed"; break;
1344 case Z3950_ERROR_DECODE:
1345 *cp = "Decoding failed"; break;
1346 case Z3950_ERROR_CONNECTION_LOST:
1347 *cp = "Connection lost"; break;
1348 case Z3950_ERROR_INIT:
1349 *cp = "Init rejected"; break;
1350 case Z3950_ERROR_INTERNAL:
1351 *cp = "Internal failure"; break;
1352 case Z3950_ERROR_TIMEOUT:
1353 *cp = "Timeout"; break;
1355 *cp = diagbib1_str (error);
1361 *addinfo = c->addinfo;
1368 int Z3950_connection_do_io(Z3950_connection c, int mask)
1372 int r = cs_look(c->cs);
1373 yaz_log (LOG_LOG, "Z3950_connection_do_io c=%p mask=%d cs_look=%d",
1378 c->error = Z3950_ERROR_CONNECT;
1381 else if (r == CS_CONNECT)
1383 yaz_log (LOG_LOG, "calling rcvconnect");
1384 if (cs_rcvconnect (c->cs) < 0)
1386 c->error = Z3950_ERROR_CONNECT;
1390 Z3950_connection_send_init (c);
1394 if (mask & Z3950_SELECT_READ)
1396 if (c->cs && (mask & Z3950_SELECT_WRITE))
1400 yaz_log (LOG_DEBUG, "Z3950_connection_do_io c=%p mask=%d", c, mask);
1401 if (c->state == STATE_CONNECTING)
1403 if (mask & Z3950_SELECT_WRITE)
1404 Z3950_connection_send_init (c);
1407 c->error = Z3950_ERROR_CONNECT;
1411 else if (c->state == STATE_ESTABLISHED)
1413 if (mask & Z3950_SELECT_READ)
1415 if (c->cs && (mask & Z3950_SELECT_WRITE))
1420 c->error = Z3950_ERROR_INTERNAL;
1424 event = Z3950_Event_create (1);
1425 Z3950_connection_put_event (c, event);
1430 int Z3950_event (int no, Z3950_connection *cs)
1433 struct pollfd pollfds[1024];
1434 Z3950_connection poll_cs[1024];
1437 fd_set input, output, except;
1442 for (i = 0; i<no; i++)
1444 Z3950_connection c = cs[i];
1446 if (c && (event = Z3950_connection_get_event(c)))
1448 Z3950_Event_destroy (event);
1452 for (i = 0; i<no; i++)
1454 Z3950_connection c = cs[i];
1455 if (c && Z3950_connection_exec_task (c))
1469 for (i = 0; i<no; i++)
1471 Z3950_connection c = cs[i];
1476 fd = z3950_connection_socket(c);
1477 mask = z3950_connection_mask(c);
1487 short poll_events = 0;
1489 if (mask & Z3950_SELECT_READ)
1490 poll_events += POLLIN;
1491 if (mask & Z3950_SELECT_WRITE)
1492 poll_events += POLLOUT;
1493 if (mask & Z3950_SELECT_EXCEPT)
1494 poll_events += POLLERR;
1495 pollfds[nfds].fd = fd;
1496 pollfds[nfds].events = poll_events;
1497 pollfds[nfds].revents = 0;
1502 if (mask & Z3950_SELECT_READ)
1504 FD_SET (fd, &input);
1507 if (mask & Z3950_SELECT_WRITE)
1509 FD_SET (fd, &output);
1512 if (mask & Z3950_SELECT_EXCEPT)
1514 FD_SET (fd, &except);
1522 yaz_log (LOG_LOG, "poll start");
1523 r = poll (pollfds, nfds, 15000);
1524 yaz_log (LOG_LOG, "poll stop, returned r=%d", r);
1525 for (i = 0; i<nfds; i++)
1527 Z3950_connection c = poll_cs[i];
1531 if (pollfds[i].revents & POLLIN)
1532 mask += Z3950_SELECT_READ;
1533 if (pollfds[i].revents & POLLOUT)
1534 mask += Z3950_SELECT_WRITE;
1535 if (pollfds[i].revents & POLLERR)
1536 mask += Z3950_SELECT_EXCEPT;
1538 Z3950_connection_do_io(c, mask);
1540 else if (r == 0 && c->mask)
1542 Z3950_Event event = Z3950_Event_create(0);
1543 /* timeout and this connection was waiting */
1544 c->error = Z3950_ERROR_TIMEOUT;
1546 Z3950_connection_put_event(c, event);
1550 yaz_log (LOG_DEBUG, "select start");
1551 r = select (max_fd+1, &input, &output, &except, &tv);
1552 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
1553 for (i = 0; i<no; i++)
1555 Z3950_connection c = cs[i];
1560 fd = z3950_connection_socket(c);
1564 /* no timeout and real socket */
1565 if (FD_ISSET(fd, &input))
1566 mask += Z3950_SELECT_READ;
1567 if (FD_ISSET(fd, &output))
1568 mask += Z3950_SELECT_WRITE;
1569 if (FD_ISSET(fd, &except))
1570 mask += Z3950_SELECT_EXCEPT;
1572 Z3950_connection_do_io(c, mask);
1574 if (r == 0 && c->mask)
1576 Z3950_Event event = Z3950_Event_create(0);
1577 /* timeout and this connection was waiting */
1578 c->error = Z3950_ERROR_TIMEOUT;
1580 yaz_log (LOG_LOG, "timeout");
1581 Z3950_connection_put_event(c, event);
1585 for (i = 0; i<no; i++)
1587 Z3950_connection c = cs[i];
1589 if (c && (event = Z3950_connection_get_event(c)))
1591 Z3950_Event_destroy (event);