Beginning to add support for record packing.
[yaz-moved-to-github.git] / server / seshigh.c
index 2a7a4c6..42a90d8 100644 (file)
@@ -4,7 +4,22 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: seshigh.c,v $
- * Revision 1.1  1995-03-15 16:02:10  quinn
+ * Revision 1.6  1995-03-21 12:30:09  quinn
+ * Beginning to add support for record packing.
+ *
+ * Revision 1.5  1995/03/17  10:44:13  quinn
+ * Added catch of null-string in makediagrec
+ *
+ * Revision 1.4  1995/03/17  10:18:08  quinn
+ * Added memory management.
+ *
+ * Revision 1.3  1995/03/16  17:42:39  quinn
+ * Little changes
+ *
+ * Revision 1.2  1995/03/16  13:29:01  quinn
+ * Partitioned server.
+ *
+ * Revision 1.1  1995/03/15  16:02:10  quinn
  * Modded session.c seshigh.c
  *
  * Revision 1.10  1995/03/15  15:18:51  quinn
@@ -85,6 +100,8 @@ void destroy_association(association *h)
     odr_destroy(h->decode);
     odr_destroy(h->encode);
     free(h->encode_buffer);
+    if (h->input_buffer)
+       free(h->input_buffer);
     free(h);
 }
 
@@ -112,7 +129,7 @@ void ir_session(IOCHAN h, int event)
                assoc->input_apdu_len = res;
                if (process_apdu(h) < 0)
                {
-                   fprintf(stderr, "Bad data from peer\n");
+                   fprintf(stderr, "Operation failed\n");
                    cs_close(conn);
                    destroy_association(assoc);
                    iochan_destroy(h);
@@ -170,6 +187,7 @@ static int process_apdu(IOCHAN chan)
            fprintf(stderr, "Bad APDU\n");
            return -1;
     }
+    odr_reset(assoc->decode);
     return res;
 }
 
@@ -214,14 +232,15 @@ static int process_initRequest(IOCHAN client, Z_InitRequest *req)
     resp.result = &result;
     resp.implementationId = "YAZ";
     resp.implementationName = "YAZ/Simple asynchronous test server";
-    resp.implementationVersion = "$Revision: 1.1 $";
+    resp.implementationVersion = "$Revision: 1.6 $";
     resp.userInformationField = 0;
     if (!z_APDU(assoc->encode, &apdup, 0))
     {
-       odr_perror(assoc->encode, "Encode init");
+       odr_perror(assoc->encode, "Encode initres");
        return -1;
     }
     odr_getbuf(assoc->encode, &assoc->encoded_len);
+    odr_reset(assoc->encode);
     iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);
     return 0;
 }
@@ -239,10 +258,32 @@ static Z_Records *diagrec(int error, char *addinfo)
     rec.u.nonSurrogateDiagnostic = &dr;
     dr.diagnosticSetId = bib1;
     dr.condition = &err;
-    dr.addinfo = addinfo;
+    dr.addinfo = addinfo ? addinfo : "";
     return &rec;
 }
 
+static Z_NamePlusRecord *surrogatediagrec(char *dbname, int error,
+                                           char *addinfo)
+{
+    static Z_NamePlusRecord rec;
+    static Z_DiagRec dr;
+    static Odr_oid bib1[] = { 1, 2, 3, 4, 5, -1 };
+    static int err;
+
+    fprintf(stderr, "SurrogateDiagnotic: %d -- %s\n", error, addinfo);
+    rec.databaseName = dbname;
+    rec.which = Z_NamePlusRecord_surrogateDiagnostic;
+    rec.u.surrogateDiagnostic = &dr;
+    dr.diagnosticSetId = bib1;
+    dr.condition = &err;
+    dr.addinfo = addinfo ? addinfo : "";
+    return &rec;
+}
+
+static Z_Records *pack_records(association *a, int num, Z_ElementSetNames *esn)
+{
+}
+
 static int process_searchRequest(IOCHAN client, Z_SearchRequest *req)
 {
     Z_APDU apdu, *apdup;
@@ -283,80 +324,15 @@ static int process_searchRequest(IOCHAN client, Z_SearchRequest *req)
 
     if (!z_APDU(assoc->encode, &apdup, 0))
     {
-       odr_perror(assoc->encode, "Encode init");
+       odr_perror(assoc->encode, "Encode searchres");
        return -1;
     }
     odr_getbuf(assoc->encode, &assoc->encoded_len);
+    odr_reset(assoc->encode);
     iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);
     return 0;
 }
 
-static Z_Records *dummy_database_records (int start, int no_requested, 
-                                         int *no_returned, 
-                                          int preferredMessageSize)
-{
-    static Z_Records rec;
-    static int no_records = 0;
-    static Z_NamePlusRecordList z_nprl;
-    int i, size;
-    static Z_NamePlusRecord **records;
-    
-    rec.which = Z_Records_DBOSD;
-    rec.u.databaseOrSurDiagnostics = &z_nprl;
-    if (!no_records)
-    {
-        FILE *inf;
-        char *buf;
-        Odr_external *oep;
-        
-        inf = fopen ("dummy-records", "r");
-        if (!inf)
-            return NULL;
-        records = malloc (sizeof(*records) * 50);
-        assert (records);
-        for (i=0; i<50; i++)
-        {
-            buf = iso2709_read (inf);
-            if (!buf)
-                break;
-            records[i] = malloc (sizeof(**records));
-            assert (records[i]);
-            records[i]->databaseName = NULL;
-            records[i]->which = Z_NamePlusRecord_databaseRecord;
-            oep = records[i]->u.databaseRecord = 
-                malloc (sizeof(*oep));
-            assert (oep);
-            oep->direct_reference = NULL;
-            oep->indirect_reference = NULL;
-            oep->descriptor = NULL;
-            oep->which = ODR_EXTERNAL_octet;
-            oep->u.octet_aligned = malloc (sizeof(*oep->u.octet_aligned));
-            assert (oep->u.octet_aligned);
-            oep->u.octet_aligned->size = oep->u.octet_aligned->len
-                = strlen(buf);
-            oep->u.octet_aligned->buf = buf;
-        }
-        no_records = i;
-        fclose (inf);
-    }
-    if (no_records < start + no_requested)
-        return NULL;
-    
-    z_nprl.records = records + start -1;
-    for (size = 0, i = 0; i<no_requested; i++)
-    {
-        if (z_nprl.records[i]->which == Z_NamePlusRecord_databaseRecord)
-            size += z_nprl.records[i]->u.databaseRecord->u.octet_aligned->size;
-        printf ("size=%d\n", size);
-        if (size > preferredMessageSize)
-            break;
-    }
-    if (i == 0)
-        return NULL;
-    z_nprl.num_records = *no_returned = i;
-    return &rec;
-}
-
 static int process_presentRequest(IOCHAN client, Z_PresentRequest *req)
 {
     Z_APDU apdu, *apdup;
@@ -374,21 +350,15 @@ static int process_presentRequest(IOCHAN client, Z_PresentRequest *req)
     resp.nextResultSetPosition = &nrr;
     resp.presentStatus = &nrr;
 
-#if 0
-    resp.records = diagrec(1, "Waiting for records from Adam.  :)");
-#else
-    if (!(resp.records = dummy_database_records (*req->resultSetStartPoint,
-        *req->numberOfRecordsRequested, &nrr, assoc->preferredMessageSize)))
-        resp.records = diagrec (1, "No records from Adam. Sorry");
-    printf ("nrr=%d\n", nrr);
-#endif
+    resp.records = diagrec(1, "No records yet.");
 
     if (!z_APDU(assoc->encode, &apdup, 0))
     {
-       odr_perror(assoc->encode, "Encode init");
+       odr_perror(assoc->encode, "Encode presentres");
        return -1;
     }
     odr_getbuf(assoc->encode, &assoc->encoded_len);
+    odr_reset(assoc->encode);
     iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);
     return 0;
 }