Hit pazpar2 directly (no apache)
[pazpar2-moved-to-github.git] / src / client.c
index e70a08e..08c006c 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2009 Index Data
+   Copyright (C) 2006-2010 Index Data
 
 Pazpar2 is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -58,7 +58,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/timing.h>
 #endif
 
-#include "pazpar2.h"
+#include "session.h"
 #include "parameters.h"
 #include "client.h"
 #include "connection.h"
@@ -72,7 +72,7 @@ struct client {
     struct session *session;
     char *pquery; // Current search
     char *cqlquery; // used for SRU targets only
-    int hits;
+    Odr_int hits;
     int record_offset;
     int maxrecs;
     int startrecs;
@@ -104,8 +104,6 @@ static const char *client_states[] = {
     "Client_Disconnected"
 };
 
-static struct client *client_freelist = 0; /* thread pr */
-
 const char *client_get_state_str(struct client *cl)
 {
     return client_states[cl->state];
@@ -119,7 +117,9 @@ enum client_state client_get_state(struct client *cl)
 void client_set_state(struct client *cl, enum client_state st)
 {
     cl->state = st;
-    if (cl->session)
+    /* no need to check for all client being non-active if this one
+       already is. Note that session_active_clients also LOCKS session */
+    if (!client_is_active(cl) && cl->session)
     {
         int no_active = session_active_clients(cl->session);
         if (no_active == 0)
@@ -394,7 +394,7 @@ void client_search_response(struct client *cl)
     struct session *se = cl->session;
     ZOOM_connection link = connection_get_link(co);
     ZOOM_resultset resultset = cl->resultset;
-    const char *error, *addinfo;
+    const char *error, *addinfo = 0;
 
     if (ZOOM_connection_error(link, &error, &addinfo))
     {
@@ -457,22 +457,27 @@ void client_record_response(struct client *cl)
                 else
                 {
                     struct session_database *sdb = client_get_database(cl);
+                    NMEM nmem = nmem_create();
                     const char *xmlrec;
                     char type[80];
                     if (nativesyntax_to_type(sdb, type, rec))
                         yaz_log(YLOG_WARN, "Failed to determine record type");
-                    if ((xmlrec = ZOOM_record_get(rec, type, NULL)))
+                    xmlrec = ZOOM_record_get(rec, type, NULL);
+                    if (!xmlrec)
+                        yaz_log(YLOG_WARN, "ZOOM_record_get failed from %s",
+                                client_get_url(cl));
+                    else
                     {
-                        if (ingest_record(cl, xmlrec, cl->record_offset))
+                        if (ingest_record(cl, xmlrec, cl->record_offset, nmem))
+                            yaz_log(YLOG_WARN, "Failed to ingest from %s",
+                                    client_get_url(cl));
+                        else
                         {
                             session_alert_watch(cl->session, SESSION_WATCH_SHOW);
                             session_alert_watch(cl->session, SESSION_WATCH_RECORD);
                         }
-                        else
-                            yaz_log(YLOG_WARN, "Failed to ingest");
                     }
-                    else
-                        yaz_log(YLOG_WARN, "Failed to extract ZOOM record");
+                    nmem_destroy(nmem);
                 }
 
             }
@@ -562,14 +567,7 @@ void client_start_search(struct client *cl)
 
 struct client *client_create(void)
 {
-    struct client *r;
-    if (client_freelist)
-    {
-        r = client_freelist;
-        client_freelist = client_freelist->next;
-    }
-    else
-        r = xmalloc(sizeof(struct client));
+    struct client *r = xmalloc(sizeof(*r));
     r->maxrecs = 100;
     r->startrecs = 0;
     r->pquery = 0;
@@ -607,9 +605,7 @@ void client_destroy(struct client *c)
         connection_release(c->connection);
 
     ZOOM_resultset_destroy(c->resultset);
-    c->resultset = 0;
-    c->next = client_freelist;
-    client_freelist = c;
+    xfree(c);
 }
 
 void client_set_connection(struct client *cl, struct connection *con)
@@ -785,7 +781,7 @@ struct client *client_next_in_session(struct client *cl)
 
 }
 
-int client_get_hits(struct client *cl)
+Odr_int client_get_hits(struct client *cl)
 {
     return cl->hits;
 }