Added second half of close-handshake
[yaz-moved-to-github.git] / client / client.c
index 3ada612..e06ad37 100644 (file)
@@ -4,7 +4,19 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: client.c,v $
- * Revision 1.16  1995-08-15 12:00:04  quinn
+ * Revision 1.20  1995-08-29 14:24:13  quinn
+ * Added second half of close-handshake
+ *
+ * Revision 1.19  1995/08/29  11:17:28  quinn
+ * Added code to receive close
+ *
+ * Revision 1.18  1995/08/28  12:21:27  quinn
+ * Client can now ask for simple element set names.
+ *
+ * Revision 1.17  1995/08/17  12:45:02  quinn
+ * Fixed minor problems with GRS-1. Added support in c&s.
+ *
+ * Revision 1.16  1995/08/15  12:00:04  quinn
  * Updated External
  *
  * Revision 1.15  1995/06/22  09:28:03  quinn
@@ -96,9 +108,11 @@ static int setnumber = 0;               /* current result set number */
 static int smallSetUpperBound = 0;
 static int largeSetLowerBound = 1;
 static int mediumSetPresentNumber = 0;
+static Z_ElementSetNames *elementSetNames = 0; 
 static int setno = 1;                   /* current set offset */
 static int protocol = PROTO_Z3950;      /* current app protocol */
 static int recordsyntax = VAL_USMARC;
+static int sent_close = 0;
 static ODR_MEM session_mem;                /* memory handle for init-response */
 static Z_InitResponse *session = 0;        /* session parameters */
 static char last_scan[512] = "0";
@@ -143,6 +157,7 @@ static void send_initRequest()
 
     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
+    ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
 
     req->idAuthentication = auth;
 
@@ -175,7 +190,7 @@ static int process_initResponse(Z_InitResponse *res)
            odr_perror(print, "Printing userinfo\n");
            odr_reset(print);
        }
-       if (res->userInformationField->which == ODR_EXTERNAL_octet)
+       if (res->userInformationField->which == Z_External_octet)
        {
            printf("Guessing visiblestring:\n");
            printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
@@ -267,9 +282,42 @@ int cmd_authentication(char *arg)
 
 /* SEARCH SERVICE ------------------------------ */
 
+void display_grs1(Z_GenericRecord *r, int level)
+{
+    int i;
+
+    if (!r)
+       return;
+    for (i = 0; i < r->num_elements; i++)
+    {
+       Z_TaggedElement *t;
+
+       printf("%*s", level * 4, "");
+       t = r->elements[i];
+       printf("(");
+       if (t->tagType)
+           printf("%d,", *t->tagType);
+       else
+           printf("?,");
+       if (t->tagValue->which == Z_StringOrNumeric_numeric)
+           printf("%d) ", *t->tagValue->u.numeric);
+       else
+           printf("%s) ", t->tagValue->u.string);
+       if (t->content->which == Z_ElementData_subtree)
+       {
+           printf("\n");
+           display_grs1(t->content->u.subtree, level+1);
+       }
+       else if (t->content->which == Z_ElementData_string)
+           printf("%s\n", t->content->u.string);
+       else
+           printf("??????\n");
+    }
+}
+
 void display_record(Z_DatabaseRecord *p)
 {
-    Z_External *r = (Odr_external*) p;
+    Z_External *r = (Z_External*) p;
     oident *ent = oid_getentbyoid(r->direct_reference);
 
     if (r->direct_reference)
@@ -283,17 +331,26 @@ void display_record(Z_DatabaseRecord *p)
            odr_reset(print);
        }
     }
-    if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
+    if (r->which == Z_External_octet && p->u.octet_aligned->len)
        marc_display ((char*)p->u.octet_aligned->buf, stdout);
     else if (ent->value == VAL_SUTRS)
     {
-       if (r->which != Z_External_SUTRS)
+       if (r->which != Z_External_sutrs)
        {
            printf("Expecting single SUTRS type for SUTRS.\n");
            return;
        }
        printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
     }
+    else if (ent->value == VAL_GRS1)
+    {
+       if (r->which != Z_External_grs1)
+       {
+           printf("Expecting single GRS type for GRS.\n");
+           return;
+       }
+       display_grs1(r->u.grs1, 0);
+    }
     else 
     {
        printf("Unknown record representation.\n");
@@ -418,6 +475,8 @@ static int send_searchRequest(char *arg)
        prefsyn.class = CLASS_RECSYN;
        prefsyn.value = recordsyntax;
        req->preferredRecordSyntax = odr_oiddup(out, oid_getoidbyent(&prefsyn));
+       req->smallSetElementSetNames =
+           req->mediumSetElementSetNames = elementSetNames;
     }
     req->num_databaseNames = 1;
     req->databaseNames = &databaseNames;
@@ -549,6 +608,7 @@ static int send_presentRequest(char *arg)
 {
     Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
     Z_PresentRequest *req = apdu->u.presentRequest;
+    Z_RecordComposition compo;
     oident prefsyn;
     int nos = 1;
     char *p;
@@ -573,11 +633,49 @@ static int send_presentRequest(char *arg)
     prefsyn.class = CLASS_RECSYN;
     prefsyn.value = recordsyntax;
     req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
+    if (elementSetNames)
+    {
+       req->recordComposition = &compo;
+       compo.which = Z_RecordComp_simple;
+       compo.u.simple = elementSetNames;
+    }
     send_apdu(apdu);
     printf("Sent presentRequest (%d+%d).\n", setno, nos);
     return 2;
 }
 
+void process_close(Z_Close *req)
+{
+    Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
+    Z_Close *res = apdu->u.close;
+
+    static char *reasons[] =
+    {
+       "finished",
+       "shutdown",
+       "systemProblem",
+       "costLimit",
+       "resources",
+       "securityViolation",
+       "protocolError",
+       "lackOfActivity",
+       "peerAbort",
+       "unspecified"
+    };
+
+    printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
+       req->diagnosticInformation ? req->diagnosticInformation : "NULL");
+    if (sent_close)
+    {
+       printf("Goodbye.\n");
+       exit(0);
+    }
+    *res->closeReason = Z_Close_finished;
+    send_apdu(apdu);
+    printf("Sent response.\n");
+    sent_close = 1;
+}
+
 static int cmd_show(char *arg)
 {
     if (!send_presentRequest(arg))
@@ -724,13 +822,48 @@ int cmd_format(char *arg)
        recordsyntax = VAL_DANMARC;
        return 1;
     }
+    else if (!strcmp(arg, "grs1"))
+    {
+       printf("Preferred format is GRS1\n");
+       recordsyntax = VAL_GRS1;
+       return 1;
+    }
     else
     {
-       printf("Specify one of {sutrs,usmarc,danmarc}.\n");
+       printf("Specify one of {sutrs,usmarc,danmarc,grs1}.\n");
        return 0;
     }
 }
 
+int cmd_elements(char *arg)
+{
+    static Z_ElementSetNames esn;
+    static char what[100];
+
+    if (!arg || !*arg)
+    {
+       printf("Usage: elements <esn>\n");
+       return 0;
+    }
+    strcpy(what, arg);
+    esn.which = Z_ElementSetNames_generic;
+    esn.u.generic = what;
+    elementSetNames = &esn;
+    return 1;
+}
+
+int cmd_close(char *arg)
+{
+    Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
+    Z_Close *req = apdu->u.close;
+
+    *req->closeReason = Z_Close_finished;
+    send_apdu(apdu);
+    printf("Sent close request.\n");
+    sent_close = 1;
+    return 2;
+}
+
 static void initialize(void)
 {
 #ifdef RPN_QUERY
@@ -782,6 +915,8 @@ static int client(void)
        {"setnames", cmd_setnames, ""},
        {"cancel", cmd_cancel, ""},
        {"format", cmd_format, "<recordsyntax>"},
+       {"elements", cmd_elements, "<elementSetName>"},
+       {"close", cmd_close, ""},
        {0,0}
     };
     char *netbuffer= 0;
@@ -888,6 +1023,10 @@ static int client(void)
                        else
                            printf("No records.\n");
                        break;
+                   case Z_APDU_close:
+                       printf("Target has closed the association.\n");
+                       process_close(apdu->u.close);
+                       break;
                    default:
                        printf("Received unknown APDU type (%d).\n", 
                            apdu->which);