First kick at present handling
authorSebastian Hammer <quinn@indexdata.com>
Fri, 17 Feb 1995 13:58:01 +0000 (13:58 +0000)
committerSebastian Hammer <quinn@indexdata.com>
Fri, 17 Feb 1995 13:58:01 +0000 (13:58 +0000)
zlayer/zaccess.c

index d0eead4..b078ccb 100644 (file)
@@ -2,7 +2,10 @@
  * Europagate, 1995
  *
  * $Log: zaccess.c,v $
- * Revision 1.4  1995/02/16 15:33:45  quinn
+ * Revision 1.5  1995/02/17 13:58:01  quinn
+ * First kick at present handling
+ *
+ * Revision 1.4  1995/02/16  15:33:45  quinn
  * Fixed bug in KWAQS generator
  *
  * Revision 1.3  1995/02/16  15:20:45  quinn
@@ -252,10 +255,183 @@ const struct zass_searchent *zass_search(ZASS a, struct ccl_rpn_node *query,
 }
 
 /*
+ * Triple indirection - that's kinda heavy. We'll fix it later.
+ * There are worse things around, though. Like ZDist.
+ */
+void get_diagrec(zass_record ***p, DATA_DIR *rec)
+{
+    DATA_DIR *ad;
+
+    **p = malloc(sizeof(***p));
+    (**p)->next = 0;
+    (**p)->errcode = zutil_GetTaggedInt(rec, ASN1_INTEGER);
+    if ((ad = zutil_GetTaggedObject(rec, ASN1_VISIBLESTRING)))
+    {
+       char *s;
+
+       if ((s = OctetString_GetASCIIString(ad)))
+       {
+           strcpy((**p)->errstring, s);
+           FREE(s);
+       }
+    }
+    (**p)->which = ZASS_REC_DIAG;
+    *p = &(**p)->next;
+}
+
+void get_responserecords(zass_record ***p, DATA_DIR *rec)
+{
+    int num, recsyntaxlen, i;
+    DATA_DIR *record, *retrec, *align;
+    PEXTERNAL ext;
+    POBJECTIDENTIFIER oid;
+    char recsyntax[256];
+
+    num = ResponseRecords_GetCount(rec);
+    for (i = 1; i <= num; i++)
+    {
+       record = ResponseRecords_GetRecord(rec, i);
+       if (!record)
+       {
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "Failed to get record.");
+           return;
+       }
+       retrec = NamePlusRecord_GetRetrievalRecord(record);
+       if (!retrec)
+       {
+           /* check if it's a diagrec */
+           if (record->ptr.child->fldid == 2)
+               get_diagrec(p, record->ptr.child);
+           else
+           {
+               gw_log(GW_LOG_WARN, ZASS_TYPE, "Illegal record.");
+               return;
+           }
+       }
+       ext = RetrievalRecord_GetExternal(retrec);
+       if (!ext)
+       {
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "No external in record");
+           return;
+       }
+       oid = External_GetDirectReference(ext);
+       if (!oid)
+       {
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "Unknown record type.");
+           return;
+       }
+       recsyntaxlen = DirectReference_GetLength(oid);
+       memcpy(recsyntax, DirectReference_GetData(oid), recsyntaxlen);
+       recsyntax[recsyntaxlen] = '\0';
+       **p = malloc(sizeof(***p));
+       (**p)->next = 0;
+       if (!strcmp(recsyntax, USMARC_OID))
+           (**p)->which = ZASS_REC_USMARC;
+       else
+       {
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "ZLAYER only knows USMARC at this point.");
+           return;
+       }
+       align = External_GetEncodingAligned(ext);
+       if (!align)
+       {
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "AAAARRRGH!! Enough of these log-messages!!!");
+           return;
+       }
+       if (!((**p)->record = malloc(align->count + 1)))
+       {
+           gw_log(GW_LOG_FATAL, ZASS_TYPE, "malloc");
+           return;
+       }
+       memcpy((**p)->record, align->ptr.data, align->count);
+       (**p)->record[align->count] = '\0';
+       gw_log(ZASS_DEBUG, ZASS_TYPE, "Got a record of %d bytes",
+           align->count);
+
+       (*p) = &(**p)->next;
+    }
+}
+
+static void zass_records_free(zass_record *p)
+{
+}
+
+/*
  * Note that 1== first record.
  */
 const struct zass_presentent *zass_present(ZASS a, char *resname, int start,
     int num)
 {
-    return 0;
+    static struct zass_presentent r = {0, 0, 0, 0};
+    zass_record **rec = &r.records;
+    DATA_DIR *pdu;
+    int len;
+
+    r.num = 0;
+    if (r.records)
+    {
+       zass_records_free(r.records);
+       r.records = 0;
+    }
+    do
+    {
+       gw_log(ZASS_DEBUG, ZASS_TYPE, "Fetching %d records from # %d", num,
+           start);
+       pdu = PresentRequest_CreateInitAllASCII(0, resname, start, num, 0, 0);
+       if (!pdu)
+       {
+           gw_log(GW_LOG_FATAL, "ZASS_TYPE", "failed to create presentrequest");
+           return 0;
+       }
+       zutil_GetBEREncodedBuffer(pdu, (unsigned char*)a->buf, &len,
+           a->maxrecordsize);
+       if (len <= 0)
+       {
+           gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to encode presentrequest");
+           return 0;
+       }
+       PresentRequest_Destroy(pdu);
+       if (netbox_SendBuffer(a->ass, a->buf, len) != len)
+       {
+           gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to send presentrequest");
+           return 0;
+       }
+       gw_log(ZASS_DEBUG, ZASS_TYPE, "Sent presentrequest.");
+       if ((len = zutil_GetBERFromNet(a->ass, (unsigned char*)a->buf,
+           a->maxrecordsize)) <= 0)
+       {
+           gw_log(GW_LOG_FATAL, ZASS_TYPE, "Failed to receive presentresponse");
+           return 0;
+       }
+       pdu = zutil_CreateFromData((unsigned char*)a->buf, len);
+       if (zutil_GetTag(pdu) != PRESENTRESPONSE_TAG)
+       {
+           gw_log(GW_LOG_FATAL, ZASS_TYPE, "Expected presentresponse from target");
+           return 0;
+       }
+       gw_log(ZASS_DEBUG, ZASS_TYPE, "Got presentresponse");
+       r.num = PresentResponse_GetNumberOfRecordsReturned(pdu);
+       if (r.num == 0)
+       {
+           gw_log(GW_LOG_WARN, ZASS_TYPE, "Got 0 records from target.");
+           return 0;
+       }
+       r.nextpos = PresentResponse_GetNextResultSetPosition(pdu);
+       start += r.nextpos;
+       num -= r.num;
+       switch(PresentResponse_GetRecordType(pdu))
+       {
+           case RESPONSERECORDS_TAG:
+               get_responserecords(&rec, PresentResponse_GetResponseRecords(pdu));
+           case NONSURROGATEDIAGNOSTIC_TAG:
+               get_diagrec(&rec, PresentResponse_GetNonSurrogateDiagnostic(pdu));
+           default:
+               gw_log(GW_LOG_WARN, ZASS_TYPE, "Bad tag in response rec.");
+       }
+       *rec = 0;
+    }
+    while (num);
+    PresentResponse_Destroy(pdu);
+       
+    return &r;
 }