Added support for multi-homed YAZ frontend server. A backend config
[yaz-moved-to-github.git] / ztest / ztest.c
index 7374c3f..8307833 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 1995-2002, Index Data.
+ * Copyright (C) 1995-2005, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: ztest.c,v 1.48 2002-01-17 23:22:40 adam Exp $
+ * $Id: ztest.c,v 1.72 2005-02-01 14:46:48 adam Exp $
  */
 
 /*
 #include <stdlib.h>
 #include <ctype.h>
 
+#include <yaz/yaz-util.h>
 #include <yaz/backend.h>
-#include <yaz/log.h>
-
-#if YAZ_MODULE_ill
 #include <yaz/ill.h>
-#endif
 
-Z_GenericRecord *read_grs1(FILE *f, ODR o);
+static int log_level=0;
+static int log_level_set=0;
+
+Z_GenericRecord *dummy_grs_record (int num, ODR o);
+char *dummy_marc_record (int num, ODR odr);
+char *dummy_xml_record (int num, ODR odr);
 
-int ztest_search (void *handle, bend_search_rr *rr);
-int ztest_sort (void *handle, bend_sort_rr *rr);
-int ztest_present (void *handle, bend_present_rr *rr);
-int ztest_esrequest (void *handle, bend_esrequest_rr *rr);
-int ztest_delete (void *handle, bend_delete_rr *rr);
+int ztest_search(void *handle, bend_search_rr *rr);
+int ztest_sort(void *handle, bend_sort_rr *rr);
+int ztest_present(void *handle, bend_present_rr *rr);
+int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
+int ztest_delete(void *handle, bend_delete_rr *rr);
 
-int ztest_search (void *handle, bend_search_rr *rr)
+int ztest_search(void *handle, bend_search_rr *rr)
 {
     if (rr->num_bases != 1)
     {
         rr->errcode = 23;
         return 0;
     }
-    if (strcmp (rr->basenames[0], "Default"))
+#if NMEM_DEBUG
+    /* if database is stop, stop this process.. For debugging only. */
+    if (!yaz_matchstr (rr->basenames[0], "stop"))
+    {
+       nmem_print_list_l(YLOG_LOG);
+        exit(0);
+    }
+#endif
+    /* Throw Database unavailable if other than Default */
+    if (yaz_matchstr (rr->basenames[0], "Default"))
     {
         rr->errcode = 109;
         rr->errstring = rr->basenames[0];
         return 0;
     }
-    rr->hits = rand() % 22;
+    rr->hits = rand() % 24;
     return 0;
 }
 
-int ztest_present (void *handle, bend_present_rr *rr)
-{
-    return 0;
-}
 
+/* this huge function handles extended services */
 int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
 {
     /* user-defined handle - created in bend_init */
     int *counter = (int*) handle;  
 
-    yaz_log(LOG_LOG, "ESRequest no %d", *counter);
+    yaz_log(log_level, "ESRequest no %d", *counter);
 
     (*counter)++;
 
     if (rr->esr->packageName)
-       yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
-    yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
+       yaz_log(log_level, "packagename: %s", rr->esr->packageName);
+    yaz_log(log_level, "Waitaction: %d", *rr->esr->waitAction);
 
 
-    yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
+    yaz_log(log_level, "function: %d", *rr->esr->function);
 
     if (!rr->esr->taskSpecificParameters)
     {
-        yaz_log (LOG_WARN, "No task specific parameters");
+        yaz_log (log_level, "No task specific parameters");
     }
     else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
     {
        Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
-       yaz_log (LOG_LOG, "Received ItemOrder");
+       yaz_log (log_level, "Received ItemOrder");
         if (it->which == Z_IOItemOrder_esRequest)
        {
            Z_IORequest *ir = it->u.esRequest;
@@ -83,24 +91,23 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
            if (k && k->contact)
            {
                if (k->contact->name)
-                   yaz_log(LOG_LOG, "contact name %s", k->contact->name);
+                   yaz_log(log_level, "contact name %s", k->contact->name);
                if (k->contact->phone)
-                   yaz_log(LOG_LOG, "contact phone %s", k->contact->phone);
+                   yaz_log(log_level, "contact phone %s", k->contact->phone);
                if (k->contact->email)
-                   yaz_log(LOG_LOG, "contact email %s", k->contact->email);
+                   yaz_log(log_level, "contact email %s", k->contact->email);
            }
            if (k->addlBilling)
            {
-               yaz_log(LOG_LOG, "Billing info (not shown)");
+               yaz_log(log_level, "Billing info (not shown)");
            }
            
            if (n->resultSetItem)
            {
-               yaz_log(LOG_LOG, "resultsetItem");
-               yaz_log(LOG_LOG, "setId: %s", n->resultSetItem->resultSetId);
-               yaz_log(LOG_LOG, "item: %d", *n->resultSetItem->item);
+               yaz_log(log_level, "resultsetItem");
+               yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
+               yaz_log(log_level, "item: %d", *n->resultSetItem->item);
            }
-#if YAZ_MODULE_ill
            if (n->itemRequest)
            {
                Z_External *r = (Z_External*) n->itemRequest;
@@ -110,17 +117,17 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
                {
                    oident *ent = oid_getentbyoid(r->direct_reference);
                    if (ent)
-                       yaz_log(LOG_LOG, "OID %s", ent->desc);
+                       yaz_log(log_level, "OID %s", ent->desc);
                     if (ent && ent->value == VAL_TEXT_XML)
                     {
-                       yaz_log (LOG_LOG, "ILL XML request");
+                       yaz_log (log_level, "ILL XML request");
                         if (r->which == Z_External_octet)
-                            yaz_log (LOG_LOG, "%.*s", r->u.octet_aligned->len,
+                            yaz_log (log_level, "%.*s", r->u.octet_aligned->len,
                                      r->u.octet_aligned->buf); 
                     }
                    if (ent && ent->value == VAL_ISO_ILL_1)
                    {
-                       yaz_log (LOG_LOG, "Decode ItemRequest begin");
+                       yaz_log (log_level, "Decode ItemRequest begin");
                        if (r->which == ODR_EXTERNAL_single)
                        {
                            odr_setbuf(rr->decode,
@@ -129,19 +136,13 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
                            
                            if (!ill_ItemRequest (rr->decode, &item_req, 0, 0))
                            {
-                               yaz_log (LOG_LOG,
+                               yaz_log (log_level,
                                     "Couldn't decode ItemRequest %s near %d",
                                        odr_errmsg(odr_geterror(rr->decode)),
                                        odr_offset(rr->decode));
-#if 0
-                                yaz_log(LOG_LOG, "PDU dump:");
-                                odr_dumpBER(yaz_log_file(),
-                                     r->u.single_ASN1_type->buf,
-                                     r->u.single_ASN1_type->len);
-#endif
                             }
                            else
-                               yaz_log(LOG_LOG, "Decode ItemRequest OK");
+                               yaz_log(log_level, "Decode ItemRequest OK");
                            if (rr->print)
                            {
                                ill_ItemRequest (rr->print, &item_req, 0,
@@ -151,24 +152,24 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
                        }
                        if (!item_req && r->which == ODR_EXTERNAL_single)
                        {
-                           yaz_log (LOG_LOG, "Decode ILL APDU begin");
+                           yaz_log (log_level, "Decode ILL APDU begin");
                            odr_setbuf(rr->decode,
                                       (char*) r->u.single_ASN1_type->buf,
                                       r->u.single_ASN1_type->len, 0);
                            
                            if (!ill_APDU (rr->decode, &ill_apdu, 0, 0))
                            {
-                               yaz_log (LOG_LOG,
+                               yaz_log (log_level,
                                     "Couldn't decode ILL APDU %s near %d",
                                        odr_errmsg(odr_geterror(rr->decode)),
                                        odr_offset(rr->decode));
-                                yaz_log(LOG_LOG, "PDU dump:");
+                                yaz_log(log_level, "PDU dump:");
                                 odr_dumpBER(yaz_log_file(),
                                      (char *) r->u.single_ASN1_type->buf,
                                      r->u.single_ASN1_type->len);
                             }
                            else
-                               yaz_log(LOG_LOG, "Decode ILL APDU OK");
+                               yaz_log(log_level, "Decode ILL APDU OK");
                            if (rr->print)
                             {
                                ill_APDU (rr->print, &ill_apdu, 0,
@@ -180,11 +181,10 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
                }
                if (item_req)
                {
-                   yaz_log (LOG_LOG, "ILL protocol version = %d",
+                   yaz_log (log_level, "ILL protocol version = %d",
                             *item_req->protocol_version_num);
                }
            }
-#endif
             if (k)
             {
 
@@ -238,40 +238,40 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
     else if (rr->esr->taskSpecificParameters->which == Z_External_update)
     {
        Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
-       yaz_log (LOG_LOG, "Received DB Update");
+       yaz_log (log_level, "Received DB Update");
        if (up->which == Z_IUUpdate_esRequest)
        {
            Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
            Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
            Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
            
-           yaz_log (LOG_LOG, "action");
+           yaz_log (log_level, "action");
            if (toKeep->action)
            {
                switch (*toKeep->action)
                {
                case Z_IUOriginPartToKeep_recordInsert:
-                   yaz_log (LOG_LOG, " recordInsert");
+                   yaz_log (log_level, " recordInsert");
                    break;
                case Z_IUOriginPartToKeep_recordReplace:
-                   yaz_log (LOG_LOG, " recordReplace");
+                   yaz_log (log_level, " recordReplace");
                    break;
                case Z_IUOriginPartToKeep_recordDelete:
-                   yaz_log (LOG_LOG, " recordDelete");
+                   yaz_log (log_level, " recordDelete");
                    break;
                case Z_IUOriginPartToKeep_elementUpdate:
-                   yaz_log (LOG_LOG, " elementUpdate");
+                   yaz_log (log_level, " elementUpdate");
                    break;
                case Z_IUOriginPartToKeep_specialUpdate:
-                   yaz_log (LOG_LOG, " specialUpdate");
+                   yaz_log (log_level, " specialUpdate");
                    break;
                default:
-                   yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
+                   yaz_log (log_level, " unknown (%d)", *toKeep->action);
                }
            }
            if (toKeep->databaseName)
            {
-               yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
+               yaz_log (log_level, "database: %s", toKeep->databaseName);
                if (!strcmp(toKeep->databaseName, "fault"))
                {
                    rr->errcode = 109;
@@ -334,10 +334,29 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
                targetPart->updateStatus = odr_intdup (rr->stream, 1);
                targetPart->num_globalDiagnostics = 0;
                targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
-               targetPart->num_taskPackageRecords = 0;
-               targetPart->taskPackageRecords =
-                    (Z_IUTaskPackageRecordStructure **) odr_nullval();
-           }
+               targetPart->num_taskPackageRecords = 1;
+               targetPart->taskPackageRecords = 
+                    (Z_IUTaskPackageRecordStructure **)
+                    odr_malloc (rr->stream,
+                                sizeof(Z_IUTaskPackageRecordStructure *));
+               targetPart->taskPackageRecords[0] =
+                    (Z_IUTaskPackageRecordStructure *)
+                    odr_malloc (rr->stream,
+                                sizeof(Z_IUTaskPackageRecordStructure));
+                
+               targetPart->taskPackageRecords[0]->which =
+                    Z_IUTaskPackageRecordStructure_record;
+               targetPart->taskPackageRecords[0]->u.record = 
+                    z_ext_record (rr->stream, VAL_SUTRS, "test", 4);
+               targetPart->taskPackageRecords[0]->correlationInfo = 0; 
+               targetPart->taskPackageRecords[0]->recordStatus =
+                    odr_intdup (rr->stream,
+                                Z_IUTaskPackageRecordStructure_success);  
+               targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
+                    = 0;
+
+               targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
+            }
            if (notToKeep)
            {
                int i;
@@ -350,28 +369,28 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
                        struct oident *oident;
                        oident = oid_getentbyoid(rec->direct_reference);
                        if (oident)
-                           yaz_log (LOG_LOG, "record %d type %s", i,
+                           yaz_log (log_level, "record %d type %s", i,
                                     oident->desc);
                    }
                    switch (rec->which)
                    {
                    case Z_External_sutrs:
                        if (rec->u.octet_aligned->len > 170)
-                           yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
+                           yaz_log (log_level, "%d bytes:\n%.168s ...",
                                     rec->u.sutrs->len,
                                     rec->u.sutrs->buf);
                        else
-                           yaz_log (LOG_LOG, "%d bytes:\n%s",
+                           yaz_log (log_level, "%d bytes:\n%s",
                                     rec->u.sutrs->len,
                                     rec->u.sutrs->buf);
                         break;
                    case Z_External_octet        :
                        if (rec->u.octet_aligned->len > 170)
-                           yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
+                           yaz_log (log_level, "%d bytes:\n%.168s ...",
                                     rec->u.octet_aligned->len,
                                     rec->u.octet_aligned->buf);
                        else
-                           yaz_log (LOG_LOG, "%d bytes\n%s",
+                           yaz_log (log_level, "%d bytes\n%s",
                                     rec->u.octet_aligned->len,
                                     rec->u.octet_aligned->buf);
                    }
@@ -379,15 +398,20 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
            }
        }
     }
+    else if (rr->esr->taskSpecificParameters->which == Z_External_update0)
+    {
+       yaz_log(log_level, "Received DB Update (version 0)");
+    }
     else
     {
-        yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
+        yaz_log (YLOG_WARN, "Unknown Extended Service(%d)",
                 rr->esr->taskSpecificParameters->which);
        
     }
     return 0;
 }
 
+/* result set delete */
 int ztest_delete (void *handle, bend_delete_rr *rr)
 {
     if (rr->num_setnames == 1 && !strcmp (rr->setnames[0], "1"))
@@ -401,115 +425,35 @@ int ztest_delete (void *handle, bend_delete_rr *rr)
 int ztest_sort (void *handle, bend_sort_rr *rr)
 {
     rr->errcode = 0;
-    rr->sort_status = Z_SortStatus_success;
+    rr->sort_status = Z_SortResponse_success;
     return 0;
 }
 
-static int atoin (const char *buf, int n)
-{
-    int val = 0;
-    while (--n >= 0)
-    {
-        if (isdigit(*buf))
-            val = val*10 + (*buf - '0');
-        buf++;
-    }
-    return val;
-}
-
-char *marc_read(FILE *inf, ODR odr)
-{
-    char length_str[5];
-    size_t size;
-    char *buf;
-
-    if (fread (length_str, 1, 5, inf) != 5)
-        return NULL;
-    size = atoin (length_str, 5);
-    if (size <= 6)
-        return NULL;
-    if (!(buf = (char*) odr_malloc (odr, size+1)))
-        return NULL;
-    if (fread (buf+5, 1, size-5, inf) != (size-5))
-    {
-        xfree (buf);
-        return NULL;
-    }
-    memcpy (buf, length_str, 5);
-    buf[size] = '\0';
-    return buf;
-}
-
-static char *dummy_database_record (int num, ODR odr)
-{
-    FILE *inf = fopen ("dummy-records", "r");
-    char *buf = 0;
-
-    if (!inf)
-       return NULL;
-    if (num == 98) 
-    {   /* this will generate a very bad MARC record (testing only) */
-        buf = (char*) odr_malloc(odr, 2101);
-        memset(buf, '7', 2100);
-        buf[2100] = '\0';
-    }
-    else
-    {
-        /* OK, try to get proper MARC records from the file */
-        while (--num >= 0)
-        {
-            buf = marc_read (inf, odr);
-            if (!buf)
-                break;
-        }
-    }
-    fclose(inf);
-    return buf;
-}
 
-static Z_GenericRecord *dummy_grs_record (int num, ODR o)
+/* present request handler */
+int ztest_present (void *handle, bend_present_rr *rr)
 {
-    FILE *f = fopen("dummy-grs", "r");
-    char line[512];
-    Z_GenericRecord *r = 0;
-    int n;
-
-    if (!f)
-       return 0;
-    while (fgets(line, 512, f))
-       if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
-       {
-           r = read_grs1(f, o);
-           break;
-       }
-    fclose(f);
-    return r;
+    return 0;
 }
 
+/* retrieval of a single record (present, and piggy back search) */
 int ztest_fetch(void *handle, bend_fetch_rr *r)
 {
     char *cp;
-    r->errstring = 0;
+
     r->last_in_set = 0;
-    r->basename = "DUMMY";
+    r->basename = "Default";
     r->output_format = r->request_format;  
     if (r->request_format == VAL_SUTRS)
     {
-#if 0
-/* this section returns a huge record (for testing non-blocking write, etc) */
-       r->len = 980000;
-       r->record = odr_malloc (r->stream, r->len);
-       memset (r->record, 'x', r->len);
-#else
-/* this section returns a small record */
+        /* this section returns a small record */
        char buf[100];
-
+        
        sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
 
        r->len = strlen(buf);
        r->record = (char *) odr_malloc (r->stream, r->len+1);
        strcpy(r->record, buf);
-#endif
     }
     else if (r->request_format == VAL_GRS1)
     {
@@ -521,7 +465,49 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
            return 0;
        }
     }
-    else if ((cp = dummy_database_record(r->number, r->stream)))
+    else if (r->request_format == VAL_POSTSCRIPT)
+    {
+        char fname[20];
+        FILE *f;
+        long size;
+
+        sprintf (fname, "part.%d.ps", r->number);
+        f = fopen(fname, "rb");
+        if (!f)
+       {
+            r->errcode = 13;
+            return 0;
+        }
+        fseek (f, 0L, SEEK_END);
+        size = ftell (f);
+        if (size <= 0 || size >= 5000000)
+        {
+            r->errcode = 14;
+            return 0;
+        }
+        fseek (f, 0L, SEEK_SET);
+        r->record = (char*) odr_malloc (r->stream, size);
+        r->len = size;
+        r->output_format = VAL_POSTSCRIPT;
+        fread (r->record, size, 1, f);
+        fclose (f);
+    }
+    else if (r->request_format == VAL_TEXT_XML)
+    {
+       if ((cp = dummy_xml_record (r->number, r->stream)))
+       {
+           r->len = strlen(cp);
+           r->record = cp;
+           r->output_format = VAL_TEXT_XML;
+       }
+       else 
+       {
+           r->errcode = 14;
+           r->surrogate_flag = 1;
+           return 0;
+       }
+    }
+    else if ((cp = dummy_marc_record(r->number, r->stream)))
     {
        r->len = strlen(cp);
        r->record = cp;
@@ -550,6 +536,13 @@ int ztest_scan(void *handle, bend_scan_rr *q)
     int term_position_req = q->term_position;
     int num_entries_req = q->num_entries;
 
+    if (yaz_matchstr (q->basenames[0], "Default"))
+    {
+        q->errcode = 109;
+        q->errstring = q->basenames[0];
+        return 0;
+    }
+
     q->errcode = 0;
     q->errstring = 0;
     q->entries = list;
@@ -559,30 +552,40 @@ int ztest_scan(void *handle, bend_scan_rr *q)
        perror("dummy-words");
        exit(1);
     }
-    if (q->term->term->which != Z_Term_general)
-    {
-       q->errcode = 229; /* unsupported term type */
-       return 0;
-    }
-    if (*q->step_size != 0)
+    if (q->num_entries > 200)
     {
-       q->errcode = 205; /*Only zero step size supported for Scan */
+       q->errcode = 31;
        return 0;
     }
-    if (q->term->term->u.general->len >= 80)
+    if (q->term)
     {
-       q->errcode = 11; /* term too long */
-       return 0;
+       int len;
+       if (q->term->term->which != Z_Term_general)
+       {
+           q->errcode = 229; /* unsupported term type */
+           return 0;
+       }
+       if (*q->step_size != 0)
+       {
+           q->errcode = 205; /*Only zero step size supported for Scan */
+           return 0;
+       }
+       len = q->term->term->u.general->len;
+       if (len >= sizeof(term))
+           len = sizeof(term)-1;
+       memcpy(term, q->term->term->u.general->buf, len);
+       term[len] = '\0';
     }
-    if (q->num_entries > 200)
+    else if (q->scanClause)
     {
-       q->errcode = 31;
-       return 0;
+       strncpy(term, q->scanClause, sizeof(term)-1);
+       term[sizeof(term)-1] = '\0';
     }
-    memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
-    term[q->term->term->u.general->len] = '\0';
+    else
+       strcpy(term, "0");
+
     for (p = term; *p; p++)
-       if (islower(*p))
+       if (islower(*(unsigned char *) p))
            *p = toupper(*p);
 
     fseek(f, 0, SEEK_SET);
@@ -634,11 +637,32 @@ int ztest_scan(void *handle, bend_scan_rr *q)
     return 0;
 }
 
+static int ztest_explain(void *handle, bend_explain_rr *rr)
+{
+    if (rr->database && !strcmp(rr->database, "Default"))
+    {
+       rr->explain_buf = "<explain>\n"
+           "\t<serverInfo>\n"
+           "\t\t<host>localhost</host>\n"
+           "\t\t<port>210</port>\n"
+           "\t</serverInfo>\n"
+           "</explain>\n";
+    }
+    return 0;
+}
+
 bend_initresult *bend_init(bend_initrequest *q)
 {
-    bend_initresult *r = (bend_initresult *) odr_malloc (q->stream, sizeof(*r));
+    bend_initresult *r = (bend_initresult *)
+        odr_malloc (q->stream, sizeof(*r));
     int *counter = (int *) xmalloc (sizeof(int));
 
+    if (!log_level_set)
+    {
+        log_level=yaz_log_module_level("ztest");
+        log_level_set=1;
+    }
+
     *counter = 0;
     r->errcode = 0;
     r->errstring = 0;
@@ -650,11 +674,20 @@ bend_initresult *bend_init(bend_initrequest *q)
     q->bend_delete = ztest_delete;
     q->bend_fetch = ztest_fetch;
     q->bend_scan = ztest_scan;
+    q->bend_explain = ztest_explain;
+    q->bend_srw_scan = ztest_scan;
+
+    yaz_log(YLOG_LOG, "ztest_init handle=%p control=%p",
+           counter, statserv_getcontrol());
+
     return r;
 }
 
 void bend_close(void *handle)
 {
+    yaz_log(YLOG_LOG, "ztest_close handle=%p control=%p",
+           handle, statserv_getcontrol());
+
     xfree (handle);              /* release our user-defined handle */
     return;
 }