2 * $Id: zoom-c.c,v 1.1 2001-10-23 21:00:20 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>
15 static Z3950_record record_cache_lookup (Z3950_resultset r,
17 const char *elementSetName);
19 static void clear_error (Z3950_connection c)
21 c->error = Z3950_ERROR_NONE;
26 Z3950_connection Z3950_connection_create (Z3950_options options)
28 Z3950_connection c = xmalloc (sizeof(*c));
33 c->state = STATE_IDLE;
34 c->error = Z3950_ERROR_NONE;
42 c->options = Z3950_options_create_with_parent(options);
51 c->odr_in = odr_createmem (ODR_DECODE);
52 c->odr_out = odr_createmem (ODR_ENCODE);
58 /* set database names. Take local databases (if set); otherwise
59 take databases given in ZURL (if set); otherwise use Default */
60 static char **set_DatabaseNames (Z3950_connection con, int *num)
65 const char *cp = Z3950_options_get (con->options, "databaseName");
69 cp = strchr (con->host_port, '/');
76 while ((c = strchr(c, '+')))
84 databaseNames = odr_malloc (con->odr_out, no * sizeof(*databaseNames));
96 /* cp ptr to first char of db name, c is char
98 databaseNames[no] = odr_malloc (con->odr_out, 1+c-cp);
99 memcpy (databaseNames[no], cp, c-cp);
100 databaseNames[no++][c-cp] = '\0';
105 databaseNames[no] = NULL;
107 return databaseNames;
110 Z3950_connection Z3950_connection_new (const char *host, int portnum)
112 Z3950_connection c = Z3950_connection_create (0);
114 Z3950_connection_connect (c, host, portnum);
118 void Z3950_connection_connect(Z3950_connection c,
119 const char *host, int portnum)
123 val = Z3950_options_get (c->options, "proxy");
125 c->proxy = xstrdup (val);
132 sprintf (hostn, "%.80s:%d", host, portnum);
133 c->host_port = xstrdup(hostn);
136 c->host_port = xstrdup(host);
138 c->async = Z3950_options_get_bool (c->options, "async", 0);
142 while (Z3950_event (1, &c))
147 Z3950_search Z3950_search_create(void)
149 Z3950_search s = xmalloc (sizeof(*s));
154 s->odr = odr_createmem (ODR_ENCODE);
159 const char *Z3950_connection_host (Z3950_connection c)
164 void Z3950_search_destroy(Z3950_search s)
170 yaz_log (LOG_DEBUG, "Z3950_search_destroy count=%d", s->refcount);
171 if (s->refcount == 0)
173 odr_destroy (s->odr);
178 int Z3950_search_prefix(Z3950_search s, const char *str)
180 s->query = odr_malloc (s->odr, sizeof(*s->query));
181 s->query->which = Z_Query_type_1;
182 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
183 if (!s->query->u.type_1)
188 int Z3950_search_sortby(Z3950_search s, const char *criteria)
190 s->sort_spec = yaz_sort_spec (s->odr, criteria);
196 static int do_write(Z3950_connection c);
199 Z3950_task Z3950_connection_add_task (Z3950_connection c, int which)
201 Z3950_task *taskp = &c->tasks;
203 taskp = &(*taskp)->next;
204 *taskp = xmalloc (sizeof(**taskp));
205 (*taskp)->running = 0;
206 (*taskp)->which = which;
207 (*taskp)->u.resultset = 0; /* one null pointer there at least */
213 void Z3950_connection_remove_task (Z3950_connection c)
215 Z3950_task task = c->tasks;
219 c->tasks = task->next;
222 case Z3950_TASK_SEARCH:
223 Z3950_resultset_destroy (task->u.resultset);
225 case Z3950_TASK_RETRIEVE:
226 Z3950_resultset_destroy (task->u.resultset);
235 void Z3950_connection_remove_tasks (Z3950_connection c)
238 Z3950_connection_remove_task(c);
241 void Z3950_connection_destroy(Z3950_connection c)
248 for (r = c->resultsets; r; r = r->next)
253 odr_destroy (c->odr_in);
254 odr_destroy (c->odr_out);
255 Z3950_options_destroy (c->options);
256 Z3950_connection_remove_tasks (c);
257 xfree (c->host_port);
261 void Z3950_resultset_addref (Z3950_resultset r)
266 Z3950_resultset Z3950_resultset_create ()
268 Z3950_resultset r = xmalloc (sizeof(*r));
272 r->odr = odr_createmem (ODR_ENCODE);
285 Z3950_resultset Z3950_connection_search_pqf(Z3950_connection c, const char *q)
288 Z3950_search s = Z3950_search_create();
290 Z3950_search_prefix (s, q);
292 r = Z3950_connection_search (c, s);
293 Z3950_search_destroy (s);
297 Z3950_resultset Z3950_connection_search(Z3950_connection c, Z3950_search q)
299 Z3950_resultset r = Z3950_resultset_create ();
302 r->r_sort_spec = q->sort_spec;
303 r->r_query = q->query;
306 r->options = Z3950_options_create_with_parent(c->options);
308 r->start = Z3950_options_get_int(r->options, "start", 0);
309 r->count = Z3950_options_get_int(r->options, "count", 0);
310 r->piggyback = Z3950_options_get_bool (r->options, "piggyback", 1);
313 r->next = c->resultsets;
316 task = Z3950_connection_add_task (c, Z3950_TASK_SEARCH);
317 task->u.resultset = r;
318 Z3950_resultset_addref (r);
324 while (Z3950_event (1, &c))
330 void Z3950_resultset_destroy(Z3950_resultset r)
335 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
336 if (r->refcount == 0)
340 /* remove ourselves from the resultsets in connection */
341 Z3950_resultset *rp = &r->connection->resultsets;
344 assert (*rp); /* we must be in this list!! */
346 { /* OK, we're here - take us out of it */
353 Z3950_search_destroy (r->search);
354 Z3950_options_destroy (r->options);
355 odr_destroy (r->odr);
360 int Z3950_resultset_size (Z3950_resultset r)
365 static void do_close (Z3950_connection c)
371 c->state = STATE_IDLE;
374 static void Z3950_resultset_retrieve (Z3950_resultset r,
375 int force_sync, int start, int count)
385 task = Z3950_connection_add_task (c, Z3950_TASK_RETRIEVE);
386 task->u.resultset = r;
387 Z3950_resultset_addref (r);
392 if (!r->connection->async || force_sync)
393 while (r->connection && Z3950_event (1, &r->connection))
397 void Z3950_resultset_records (Z3950_resultset r, Z3950_record *recs,
400 int force_present = 0;
405 start = Z3950_options_get_int (r->options, "start", 0);
406 count = Z3950_options_get_int (r->options, "count", 0);
409 Z3950_resultset_retrieve (r, force_present, start, count);
413 for (i = 0; i< *cnt; i++)
414 recs[i] = Z3950_resultset_record_immediate (r, i+start);
418 static void do_connect (Z3950_connection c)
421 const char *effective_host;
424 effective_host = c->proxy;
426 effective_host = c->host_port;
428 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
431 c->cs = cs_create_host (effective_host, 0, &add);
435 cs_connect (c->cs, add);
436 c->state = STATE_CONNECTING;
437 c->mask = Z3950_SELECT_READ | Z3950_SELECT_WRITE;
441 c->state = STATE_IDLE;
442 c->error = Z3950_ERROR_CONNECT;
446 int z3950_connection_socket(Z3950_connection c)
449 return cs_fileno(c->cs);
453 int z3950_connection_mask(Z3950_connection c)
460 static int encode_APDU(Z3950_connection c, Z_APDU *a, ODR out)
465 sprintf (str, "send_APDU t=%p type=%d", c, a->which);
468 Z_OtherInformation **oi;
470 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
472 if (!z_APDU(out, &a, 0, 0))
474 FILE *outf = fopen("/tmp/apdu.txt", "w");
477 ODR odr_pr = odr_createmem(ODR_PRINT);
478 fprintf (outf, "a=%p\n", a);
479 odr_setprint(odr_pr, outf);
480 z_APDU(odr_pr, &a, 0, 0);
484 c->error = Z3950_ERROR_ENCODE;
491 static int send_APDU (Z3950_connection c, Z_APDU *a)
494 if (encode_APDU(c, a, c->odr_out))
496 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
497 odr_reset(c->odr_out);
502 static int Z3950_connection_send_init (Z3950_connection c)
505 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
506 Z_InitRequest *ireq = apdu->u.initRequest;
507 Z_IdAuthentication *auth = odr_malloc(c->odr_out, sizeof(*auth));
508 const char *auth_groupId = Z3950_options_get (c->options, "group");
509 const char *auth_userId = Z3950_options_get (c->options, "user");
510 const char *auth_password = Z3950_options_get (c->options, "pass");
512 ODR_MASK_SET(ireq->options, Z_Options_search);
513 ODR_MASK_SET(ireq->options, Z_Options_present);
514 ODR_MASK_SET(ireq->options, Z_Options_scan);
515 ODR_MASK_SET(ireq->options, Z_Options_sort);
517 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
518 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
521 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
522 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
523 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
525 impname = Z3950_options_get (c->options, "implementationName");
526 ireq->implementationName =
527 odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
528 strcpy (ireq->implementationName, "");
531 strcat (ireq->implementationName, impname);
532 strcat (ireq->implementationName, "/");
534 strcat (ireq->implementationName, "ZOOM-C/YAZ");
536 *ireq->maximumRecordSize =
537 Z3950_options_get_int (c->options, "maximumRecordSize", 1024*1024);
538 *ireq->preferredMessageSize =
539 Z3950_options_get_int (c->options, "preferredMessageSize", 1024*1024);
541 if (auth_groupId || auth_password)
543 Z_IdPass *pass = odr_malloc(c->odr_out, sizeof(*pass));
546 if (auth_groupId && *auth_groupId)
548 pass->groupId = odr_malloc(c->odr_out, strlen(auth_groupId)+1);
549 strcpy(pass->groupId, auth_groupId);
553 if (auth_userId && *auth_userId)
555 pass->userId = odr_malloc(c->odr_out, strlen(auth_userId)+1);
556 strcpy(pass->userId, auth_userId);
560 if (auth_password && *auth_password)
562 pass->password = odr_malloc(c->odr_out, strlen(auth_password)+1);
563 strcpy(pass->password, auth_password);
568 auth->which = Z_IdAuthentication_idPass;
569 auth->u.idPass = pass;
570 ireq->idAuthentication = auth;
573 else if (auth_userId)
575 auth->which = Z_IdAuthentication_open;
576 auth->u.open = odr_malloc(c->odr_out, strlen(auth_userId)+1);
577 strcpy(auth->u.open, auth_userId);
578 ireq->idAuthentication = auth;
581 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
582 VAL_PROXY, 1, c->host_port);
589 static int Z3950_connection_send_search (Z3950_connection c)
592 int lslb, ssub, mspn;
594 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
595 Z_SearchRequest *search_req = apdu->u.searchRequest;
596 const char *elementSetName;
597 const char *smallSetElementSetName;
598 const char *mediumSetElementSetName;
601 assert (c->tasks->which == Z3950_TASK_SEARCH);
603 r = c->tasks->u.resultset;
606 Z3950_options_get (r->options, "elementSetName");
607 smallSetElementSetName =
608 Z3950_options_get (r->options, "smallSetElementSetName");
609 mediumSetElementSetName =
610 Z3950_options_get (r->options, "mediumSetElementSetName");
612 if (!smallSetElementSetName)
613 smallSetElementSetName = elementSetName;
615 if (!mediumSetElementSetName)
616 mediumSetElementSetName = elementSetName;
621 /* prepare query for the search request */
622 search_req->query = r->r_query;
624 search_req->databaseNames =
625 set_DatabaseNames (c, &search_req->num_databaseNames);
627 /* get syntax (no need to provide unless piggyback is in effect) */
628 syntax = Z3950_options_get (r->options, "preferredRecordSyntax");
630 lslb = Z3950_options_get_int (r->options, "largeSetLowerBound", -1);
631 ssub = Z3950_options_get_int (r->options, "smallSetUpperBound", -1);
632 mspn = Z3950_options_get_int (r->options, "mediumSetPresentNumber", -1);
633 if (lslb != -1 && ssub != -1 && mspn != -1)
635 /* So're a Z39.50 expert? Let's hope you don't do sort */
636 *search_req->largeSetLowerBound = lslb;
637 *search_req->smallSetUpperBound = ssub;
638 *search_req->mediumSetPresentNumber = mspn;
640 else if (r->start == 0 && r->count > 0
641 && r->piggyback && !r->r_sort_spec)
643 /* Regular piggyback - do it unless we're going to do sort */
644 *search_req->largeSetLowerBound = 2000000000;
645 *search_req->smallSetUpperBound = r->count;
646 *search_req->mediumSetPresentNumber = r->count;
647 smallSetElementSetName = 0; /* no need to provide this */
651 /* non-piggyback. Need not provide elementsets or syntaxes .. */
652 smallSetElementSetName = 0;
653 mediumSetElementSetName = 0;
656 if (smallSetElementSetName && *smallSetElementSetName)
658 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
660 esn->which = Z_ElementSetNames_generic;
661 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
662 search_req->smallSetElementSetNames = esn;
664 if (mediumSetElementSetName && *mediumSetElementSetName)
666 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
668 esn->which = Z_ElementSetNames_generic;
669 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
670 search_req->mediumSetElementSetNames = esn;
673 search_req->preferredRecordSyntax =
674 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
676 /* send search request */
682 static void response_diag (Z3950_connection c, Z_DiagRec *p)
684 Z_DefaultDiagFormat *r;
689 if (p->which != Z_DiagRec_defaultFormat)
691 c->error = Z3950_ERROR_DECODE;
694 r = p->u.defaultFormat;
697 case Z_DefaultDiagFormat_v2Addinfo:
698 addinfo = r->u.v2Addinfo;
700 case Z_DefaultDiagFormat_v3Addinfo:
701 addinfo = r->u.v3Addinfo;
705 c->addinfo = xstrdup (addinfo);
706 c->error = *r->condition;
709 Z3950_record Z3950_record_dup (Z3950_record srec)
716 odr_enc = odr_createmem(ODR_ENCODE);
717 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
719 buf = odr_getbuf (odr_enc, &size, 0);
721 nrec = xmalloc (sizeof(*nrec));
722 nrec->odr = odr_createmem(ODR_DECODE);
723 nrec->wrbuf_marc = 0;
724 odr_setbuf (nrec->odr, buf, size, 0);
725 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
727 odr_destroy (odr_enc);
731 Z3950_record Z3950_resultset_record_immediate (Z3950_resultset s, int pos)
733 Z3950_record rec = record_cache_lookup (s, pos, 0);
736 return Z3950_record_dup (rec);
739 Z3950_record Z3950_resultset_record (Z3950_resultset r, int pos)
741 Z3950_resultset_retrieve (r, 1, pos, 1);
742 return Z3950_resultset_record_immediate (r, pos);
745 void Z3950_record_destroy (Z3950_record rec)
750 wrbuf_free (rec->wrbuf_marc, 1);
751 odr_destroy (rec->odr);
755 void *Z3950_record_get (Z3950_record rec, const char *type, int *len)
757 Z_NamePlusRecord *npr;
763 if (!strcmp (type, "database"))
765 return npr->databaseName;
767 else if (!strcmp (type, "syntax"))
769 if (npr->which == Z_NamePlusRecord_databaseRecord)
771 Z_External *r = (Z_External *) npr->u.databaseRecord;
772 oident *ent = oid_getentbyoid(r->direct_reference);
778 else if (!strcmp (type, "render"))
780 if (npr->which == Z_NamePlusRecord_databaseRecord)
782 Z_External *r = (Z_External *) npr->u.databaseRecord;
783 oident *ent = oid_getentbyoid(r->direct_reference);
785 if (r->which == Z_External_sutrs)
787 *len = r->u.sutrs->len;
788 return r->u.sutrs->buf;
790 else if (r->which == Z_External_octet)
799 case VAL_APPLICATION_XML:
802 if (!rec->wrbuf_marc)
803 rec->wrbuf_marc = wrbuf_alloc();
804 if (marc_display_wrbuf (r->u.octet_aligned->buf,
806 r->u.octet_aligned->len) > 0)
808 *len = wrbuf_len(rec->wrbuf_marc);
809 return wrbuf_buf(rec->wrbuf_marc);
812 *len = r->u.octet_aligned->len;
813 return r->u.octet_aligned->buf;
815 else if (r->which == Z_External_grs1)
826 void *Z3950_resultset_get (Z3950_resultset s, int pos, const char *type,
829 Z3950_record rec = record_cache_lookup (s, pos, 0);
830 return Z3950_record_get (rec, type, len);
833 static void record_cache_add (Z3950_resultset r,
834 Z_NamePlusRecord *npr,
836 const char *elementSetName)
838 Z3950_record_cache rc;
840 for (rc = r->record_cache; rc; rc = rc->next)
844 if ((!elementSetName && !rc->elementSetName)
845 || (elementSetName && rc->elementSetName &&
846 !strcmp (elementSetName, rc->elementSetName)))
848 /* not destroying rc->npr (it's handled by nmem )*/
850 /* keeping wrbuf_marc too */
856 rc = odr_malloc (r->odr, sizeof(*rc));
859 rc->rec.wrbuf_marc = 0;
861 rc->elementSetName = odr_strdup (r->odr, elementSetName);
863 rc->elementSetName = 0;
865 rc->next = r->record_cache;
866 r->record_cache = rc;
869 static Z3950_record record_cache_lookup (Z3950_resultset r,
871 const char *elementSetName)
873 Z3950_record_cache rc;
875 for (rc = r->record_cache; rc; rc = rc->next)
879 if ((!elementSetName && !rc->elementSetName)
880 || (elementSetName && rc->elementSetName &&
881 !strcmp (elementSetName, rc->elementSetName)))
888 static void handle_records (Z3950_connection c, Z_Records *sr,
891 Z3950_resultset resultset;
895 if (c->tasks->which != Z3950_TASK_SEARCH &&
896 c->tasks->which != Z3950_TASK_RETRIEVE)
899 resultset = c->tasks->u.resultset;
901 if (sr && sr->which == Z_Records_NSD)
903 Z_DiagRec dr, *dr_p = &dr;
904 dr.which = Z_DiagRec_defaultFormat;
905 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
907 response_diag (c, dr_p);
909 else if (sr && sr->which == Z_Records_multipleNSD)
911 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
912 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
914 c->error = Z3950_ERROR_DECODE;
918 if (resultset->count + resultset->start > resultset->size)
919 resultset->count = resultset->size - resultset->start;
920 if (resultset->count < 0)
921 resultset->count = 0;
922 if (sr && sr->which == Z_Records_DBOSD)
925 NMEM nmem = odr_extract_mem (c->odr_in);
926 Z_NamePlusRecordList *p =
927 sr->u.databaseOrSurDiagnostics;
928 for (i = 0; i<p->num_records; i++)
930 record_cache_add (resultset, p->records[i],
931 i+ resultset->start, 0);
933 /* transfer our response to search_nmem .. we need it later */
934 nmem_transfer (resultset->odr->mem, nmem);
936 if (present_phase && p->num_records == 0)
938 /* present response and we didn't get any records! */
939 c->error = Z3950_ERROR_DECODE;
942 else if (present_phase)
944 /* present response and we didn't get any records! */
945 c->error = Z3950_ERROR_DECODE;
950 static void handle_present_response (Z3950_connection c, Z_PresentResponse *pr)
952 handle_records (c, pr->records, 1);
955 static void handle_search_response (Z3950_connection c, Z_SearchResponse *sr)
957 Z3950_resultset resultset;
959 yaz_log (LOG_DEBUG, "got search response");
961 if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH)
964 resultset = c->tasks->u.resultset;
966 resultset->size = *sr->resultCount;
967 handle_records (c, sr->records, 0);
970 static void sort_response (Z3950_connection c, Z_SortResponse *res)
972 if (res->diagnostics && res->num_diagnostics > 0)
973 response_diag (c, res->diagnostics[0]);
976 static int send_sort (Z3950_connection c)
978 Z3950_resultset resultset;
980 if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH)
983 resultset = c->tasks->u.resultset;
987 resultset->r_sort_spec = 0;
990 if (resultset->r_sort_spec)
992 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
993 Z_SortRequest *req = apdu->u.sortRequest;
995 req->num_inputResultSetNames = 1;
996 req->inputResultSetNames = (Z_InternationalString **)
997 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
998 req->inputResultSetNames[0] = odr_strdup (c->odr_out, "default");
999 req->sortedResultSetName = odr_strdup (c->odr_out, "default");
1000 req->sortSequence = resultset->r_sort_spec;
1001 resultset->r_sort_spec = 0;
1002 send_APDU (c, apdu);
1008 static int send_present (Z3950_connection c)
1010 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1011 Z_PresentRequest *req = apdu->u.presentRequest;
1013 const char *syntax =
1014 Z3950_options_get (c->options, "preferredRecordSyntax");
1015 const char *element =
1016 Z3950_options_get (c->options, "elementSetName");
1017 Z3950_resultset resultset;
1021 if (c->tasks->which != Z3950_TASK_SEARCH &&
1022 c->tasks->which != Z3950_TASK_RETRIEVE)
1025 resultset = c->tasks->u.resultset;
1027 if (c->error) /* don't continue on error */
1029 if (resultset->start < 0)
1031 for (i = 0; i<resultset->count; i++)
1034 record_cache_lookup (resultset, i + resultset->start, 0);
1038 if (i == resultset->count)
1041 resultset->start += i;
1042 resultset->count -= i;
1043 *req->resultSetStartPoint = resultset->start + 1;
1044 *req->numberOfRecordsRequested = resultset->count;
1045 assert (*req->numberOfRecordsRequested > 0);
1047 if (syntax && *syntax)
1048 req->preferredRecordSyntax =
1049 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1051 if (element && *element)
1053 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
1054 Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1056 esn->which = Z_ElementSetNames_generic;
1057 esn->u.generic = odr_strdup (c->odr_out, element);
1058 compo->which = Z_RecordComp_simple;
1059 compo->u.simple = esn;
1060 req->recordComposition = compo;
1062 send_APDU (c, apdu);
1066 static int Z3950_connection_exec_task (Z3950_connection c)
1068 Z3950_task task = c->tasks;
1070 yaz_log (LOG_DEBUG, "Z3950_connection_exec_task");
1073 if (c->error != Z3950_ERROR_NONE || !c->cs)
1075 Z3950_connection_remove_tasks (c);
1078 yaz_log (LOG_DEBUG, "Z3950_connection_exec_task type=%d", task->which);
1082 switch (task->which)
1084 case Z3950_TASK_SEARCH:
1085 /* see if search hasn't been sent yet. */
1086 if (Z3950_connection_send_search (c))
1089 case Z3950_TASK_RETRIEVE:
1090 if (send_present (c))
1094 Z3950_connection_remove_task (c);
1098 static int send_sort_present (Z3950_connection c)
1100 int r = send_sort (c);
1102 r = send_present (c);
1106 static void handle_apdu (Z3950_connection c, Z_APDU *apdu)
1108 Z_InitResponse *initrs;
1110 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1114 case Z_APDU_initResponse:
1115 initrs = apdu->u.initResponse;
1116 if (!*initrs->result)
1118 c->error = Z3950_ERROR_INIT;
1123 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1125 xfree (c->cookie_in);
1128 c->cookie_in = xstrdup(cookie);
1129 Z3950_connection_exec_task (c);
1132 case Z_APDU_searchResponse:
1133 handle_search_response (c, apdu->u.searchResponse);
1134 if (!send_sort_present (c))
1135 Z3950_connection_remove_task (c);
1137 case Z_APDU_presentResponse:
1138 handle_present_response (c, apdu->u.presentResponse);
1139 if (!send_present (c))
1140 Z3950_connection_remove_task (c);
1142 case Z_APDU_sortResponse:
1143 sort_response (c, apdu->u.sortResponse);
1144 if (!send_present (c))
1145 Z3950_connection_remove_task (c);
1149 static int do_read (Z3950_connection c)
1154 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1159 c->error= Z3950_ERROR_CONNECTION_LOST;
1164 odr_reset (c->odr_in);
1165 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1166 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1168 c->error = Z3950_ERROR_DECODE;
1173 handle_apdu (c, apdu);
1179 static int do_write_ex (Z3950_connection c, char *buf_out, int len_out)
1183 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1185 if (c->state == STATE_CONNECTING)
1186 c->error = Z3950_ERROR_CONNECT;
1188 c->error = Z3950_ERROR_CONNECTION_LOST;
1194 c->state = STATE_ESTABLISHED;
1195 c->mask = Z3950_SELECT_READ|Z3950_SELECT_WRITE;
1199 c->state = STATE_ESTABLISHED;
1200 c->mask = Z3950_SELECT_READ;
1205 static int do_write(Z3950_connection c)
1207 return do_write_ex (c, c->buf_out, c->len_out);
1210 const char *Z3950_connection_option (Z3950_connection c, const char *key,
1213 const char *old_val = Z3950_options_get (c->options, key);
1216 Z3950_options_set (c->options, key, val);
1221 const char *Z3950_resultset_option (Z3950_resultset r, const char *key,
1224 const char *old_val = Z3950_options_get (r->options, key);
1227 Z3950_options_set (r->options, key, val);
1233 int Z3950_connection_errcode (Z3950_connection c)
1235 return Z3950_connection_error (c, 0, 0);
1238 const char *Z3950_connection_errmsg (Z3950_connection c)
1241 Z3950_connection_error (c, &msg, 0);
1245 const char *Z3950_connection_addinfo (Z3950_connection c)
1247 const char *addinfo;
1248 Z3950_connection_error (c, 0, &addinfo);
1252 int Z3950_connection_error (Z3950_connection c, const char **cp,
1253 const char **addinfo)
1255 int error = c->error;
1260 case Z3950_ERROR_NONE:
1261 *cp = "No error"; break;
1262 case Z3950_ERROR_CONNECT:
1263 *cp = "Connect failed"; break;
1264 case Z3950_ERROR_MEMORY:
1265 *cp = "Out of memory"; break;
1266 case Z3950_ERROR_ENCODE:
1267 *cp = "Encoding failed"; break;
1268 case Z3950_ERROR_DECODE:
1269 *cp = "Decoding failed"; break;
1270 case Z3950_ERROR_CONNECTION_LOST:
1271 *cp = "Connection lost"; break;
1272 case Z3950_ERROR_INIT:
1273 *cp = "Init rejected"; break;
1274 case Z3950_ERROR_INTERNAL:
1275 *cp = "Internal failure"; break;
1276 case Z3950_ERROR_TIMEOUT:
1277 *cp = "Timeout"; break;
1279 *cp = diagbib1_str (error);
1285 *addinfo = c->addinfo;
1292 int Z3950_connection_do_io(Z3950_connection c, int mask)
1295 int r = cs_look(c->cs);
1296 yaz_log (LOG_LOG, "Z3950_connection_do_io c=%p mask=%d cs_look=%d",
1301 c->error = Z3950_ERROR_CONNECT;
1304 else if (r == CS_CONNECT)
1306 yaz_log (LOG_LOG, "calling rcvconnect");
1307 if (cs_rcvconnect (c->cs) < 0)
1309 c->error = Z3950_ERROR_CONNECT;
1313 Z3950_connection_send_init (c);
1317 if (mask & Z3950_SELECT_READ)
1319 if (c->cs && (mask & Z3950_SELECT_WRITE))
1323 yaz_log (LOG_DEBUG, "Z3950_connection_do_io c=%p mask=%d", c, mask);
1324 if (c->state == STATE_CONNECTING)
1326 if (mask & Z3950_SELECT_WRITE)
1327 Z3950_connection_send_init (c);
1330 c->error = Z3950_ERROR_CONNECT;
1334 else if (c->state == STATE_ESTABLISHED)
1336 if (mask & Z3950_SELECT_READ)
1338 if (c->cs && (mask & Z3950_SELECT_WRITE))
1343 c->error = Z3950_ERROR_INTERNAL;
1347 c->event_pending = 1;
1351 int Z3950_event (int no, Z3950_connection *cs)
1354 fd_set input, output;
1358 for (i = 0; i<no; i++)
1360 Z3950_connection c = cs[i];
1361 if (c && c->event_pending)
1363 c->event_pending = 0;
1374 for (i = 0; i<no; i++)
1376 Z3950_connection c = cs[i];
1381 fd = z3950_connection_socket(c);
1382 mask = z3950_connection_mask(c);
1388 if (mask & Z3950_SELECT_READ)
1390 FD_SET (fd, &input);
1393 if (mask & Z3950_SELECT_WRITE)
1395 FD_SET (fd, &output);
1401 for (i = 0; i<no; i++)
1403 Z3950_connection c = cs[i];
1406 if (!c->cs && c->host_port && c->error == Z3950_ERROR_NONE)
1413 if (Z3950_connection_exec_task (c))
1417 yaz_log (LOG_DEBUG, "no more events");
1420 r = select (max_fd+1, &input, &output, 0, &tv);
1422 for (i = 0; i<no; i++)
1424 Z3950_connection c = cs[i];
1429 fd = z3950_connection_socket(c);
1433 /* no timeout and real socket */
1434 if (FD_ISSET(fd, &input))
1435 mask += Z3950_SELECT_READ;
1436 if (FD_ISSET(fd, &output))
1437 mask += Z3950_SELECT_WRITE;
1439 Z3950_connection_do_io(c, mask);
1441 if (r == 0 && c->mask)
1443 /* timeout and this connection was waiting */
1444 c->error = Z3950_ERROR_TIMEOUT;
1445 c->event_pending = 1;
1448 for (i = 0; i<no; i++)
1450 Z3950_connection c = cs[i];
1453 if (c->event_pending)
1455 c->event_pending = 0;