ZOOM Z39.50 handling: refactor
authorAdam Dickmeiss <adam@indexdata.dk>
Tue, 28 Jan 2014 11:40:32 +0000 (12:40 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Tue, 28 Jan 2014 11:40:42 +0000 (12:40 +0100)
src/zoom-c.c
src/zoom-p.h
src/zoom-z3950.c

index 4359806..b47a44a 100644 (file)
@@ -1515,7 +1515,7 @@ ZOOM_API(int)
             if (c->proto == PROTO_HTTP)
                 ret = ZOOM_connection_srw_send_search(c);
             else
-                ret = ZOOM_connection_Z3950_present(c);
+                ret = ZOOM_connection_Z3950_search(c);
             break;
         case ZOOM_TASK_CONNECT:
             ret = do_connect(c);
index 9fb8f6f..2a64c5f 100644 (file)
@@ -226,7 +226,7 @@ void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo);
 ZOOM_Event ZOOM_Event_create(int kind);
 void ZOOM_connection_put_event(ZOOM_connection c, ZOOM_Event event);
 
-zoom_ret ZOOM_connection_Z3950_present(ZOOM_connection c);
+zoom_ret ZOOM_connection_Z3950_search(ZOOM_connection c);
 zoom_ret ZOOM_connection_Z3950_send_scan(ZOOM_connection c);
 zoom_ret ZOOM_send_buf(ZOOM_connection c);
 zoom_ret send_Z3950_sort(ZOOM_connection c, ZOOM_resultset resultset);
index 6e06f14..5697865 100644 (file)
@@ -1449,80 +1449,24 @@ zoom_ret send_Z3950_sort(ZOOM_connection c, ZOOM_resultset resultset)
     return zoom_complete;
 }
 
-zoom_ret ZOOM_connection_Z3950_present(ZOOM_connection c)
+static zoom_ret Z3950_send_present(ZOOM_connection c)
 {
-    Z_APDU *apdu = 0;
-    Z_PresentRequest *req = 0;
-    int i = 0;
-    const char *syntax = 0;
-    const char *elementSetName = 0;
-    const char *schema = 0;
-    ZOOM_resultset resultset;
-    int *start, *count;
-
-    if (!c->tasks)
-        return zoom_complete;
-    assert(c->tasks->which == ZOOM_TASK_SEARCH);
-    resultset = c->tasks->u.search.resultset;
-    start = &c->tasks->u.search.start;
-    count = &c->tasks->u.search.count;
-    syntax = c->tasks->u.search.syntax;
-    elementSetName = c->tasks->u.search.elementSetName;
-    schema =  c->tasks->u.search.schema;
-
-    yaz_log(c->log_details, "%p send_present start=%d count=%d",
-            c, *start, *count);
+    Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
+    Z_PresentRequest *req = apdu->u.presentRequest;
+    ZOOM_resultset resultset = c->tasks->u.search.resultset;
+    const char *syntax = c->tasks->u.search.syntax;
+    const char *elementSetName = c->tasks->u.search.elementSetName;
+    const char *schema = c->tasks->u.search.schema;
 
-    if (*start < 0 || *count < 0)
-    {
-        ZOOM_set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
-                       "", 0);
-    }
-    if (resultset->live_set && *start + *count > resultset->size)
-    {
-        ZOOM_set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
-                       "", 0);
-    }
+    *req->resultSetStartPoint = c->tasks->u.search.start + 1;
 
-    if (c->error)                  /* don't continue on error */
-        return zoom_complete;
-    yaz_log(c->log_details, "send_present resultset=%p start=%d count=%d",
-            resultset, *start, *count);
-
-    for (i = 0; i < *count; i++)
-    {
-        ZOOM_record rec =
-            ZOOM_record_cache_lookup(resultset, i + *start,
-                                     syntax, elementSetName, schema);
-        if (!rec)
-            break;
-        else
-        {
-            ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
-            ZOOM_connection_put_event(c, event);
-        }
-    }
-    *start += i;
-    *count -= i;
-
-    if (*count == 0 && resultset->live_set)
-        return zoom_complete;
-
-    if (resultset->live_set != 2)
-        return Z3950_send_search(c);
-
-    apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
-    req = apdu->u.presentRequest;
-
-    *req->resultSetStartPoint = *start + 1;
-
-    if (resultset->step > 0 && resultset->step < *count)
+    if (resultset->step > 0 && resultset->step < c->tasks->u.search.count)
         *req->numberOfRecordsRequested = resultset->step;
     else
-        *req->numberOfRecordsRequested = *count;
+        *req->numberOfRecordsRequested = c->tasks->u.search.count;
 
-    if (*req->numberOfRecordsRequested + *start > resultset->size)
-        *req->numberOfRecordsRequested = resultset->size - *start;
+    if (*req->numberOfRecordsRequested + c->tasks->u.search.start > resultset->size)
+        *req->numberOfRecordsRequested = resultset->size - c->tasks->u.search.start;
     assert(*req->numberOfRecordsRequested > 0);
 
     if (syntax && *syntax)
@@ -1589,6 +1533,69 @@ zoom_ret ZOOM_connection_Z3950_present(ZOOM_connection c)
     return send_APDU(c, apdu);
 }
 
+zoom_ret ZOOM_connection_Z3950_search(ZOOM_connection c)
+{
+    int i = 0;
+    const char *syntax = 0;
+    const char *elementSetName = 0;
+    const char *schema = 0;
+    ZOOM_resultset resultset;
+    int *start, *count;
+
+    if (!c->tasks)
+        return zoom_complete;
+    assert(c->tasks->which == ZOOM_TASK_SEARCH);
+    resultset = c->tasks->u.search.resultset;
+    start = &c->tasks->u.search.start;
+    count = &c->tasks->u.search.count;
+    syntax = c->tasks->u.search.syntax;
+    elementSetName = c->tasks->u.search.elementSetName;
+    schema =  c->tasks->u.search.schema;
+
+    yaz_log(c->log_details, "%p send_present start=%d count=%d",
+            c, *start, *count);
+
+    if (*start < 0 || *count < 0)
+    {
+        ZOOM_set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
+                       "", 0);
+    }
+    if (resultset->live_set && *start + *count > resultset->size)
+    {
+        ZOOM_set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
+                       "", 0);
+    }
+
+    if (c->error)                  /* don't continue on error */
+        return zoom_complete;
+    yaz_log(c->log_details, "send_present resultset=%p start=%d count=%d",
+            resultset, *start, *count);
+
+    for (i = 0; i < *count; i++)
+    {
+        ZOOM_record rec =
+            ZOOM_record_cache_lookup(resultset, i + *start,
+                                     syntax, elementSetName, schema);
+        if (!rec)
+            break;
+        else
+        {
+            ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
+            ZOOM_connection_put_event(c, event);
+        }
+    }
+    *start += i;
+    *count -= i;
+
+    if (*count == 0 && resultset->live_set)
+        return zoom_complete;
+
+    if (resultset->live_set == 2)
+        return Z3950_send_present(c);
+    else
+        return Z3950_send_search(c);
+}
+
 static zoom_ret send_Z3950_sort_present(ZOOM_connection c)
 {
     zoom_ret r = zoom_complete;
@@ -1596,7 +1603,7 @@ static zoom_ret send_Z3950_sort_present(ZOOM_connection c)
     if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
         r = send_Z3950_sort(c, c->tasks->u.search.resultset);
     if (r == zoom_complete)
-        r = ZOOM_connection_Z3950_present(c);
+        r = ZOOM_connection_Z3950_search(c);
     return r;
 }
 
@@ -1701,7 +1708,7 @@ void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu)
     case Z_APDU_presentResponse:
         yaz_log(c->log_api, "%p handle_Z3950_apdu Present response", c);
         handle_Z3950_present_response(c, apdu->u.presentResponse);
-        if (ZOOM_connection_Z3950_present(c) == zoom_complete)
+        if (ZOOM_connection_Z3950_search(c) == zoom_complete)
             ZOOM_connection_remove_task(c);
         break;
     case Z_APDU_sortResponse: