getting the paraz2 server host and port info from the HTTP request, and mirroring...
[pazpar2-moved-to-github.git] / src / database.c
index 3a987ea..5ee51bb 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: database.c,v 1.1 2007-03-15 16:55:34 quinn Exp $ */
+/* $Id: database.c,v 1.4 2007-03-23 03:26:22 quinn Exp $ */
 
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 #include "http.h"
 #include "zeerex.h"
 
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <netinet/in.h>
+
 static struct host *hosts = 0;  // The hosts we know about 
 static struct database *databases = 0; // The databases we know about
 static NMEM nmem = 0;
@@ -152,7 +157,7 @@ static struct database *load_database(const char *id)
     memset(db, 0, sizeof(*db));
     db->host = host;
     db->url = nmem_strdup(nmem, id);
-    db->name = dbname;
+    db->name = 0;
     db->databases = xmalloc(2 * sizeof(char *));
     db->databases[0] = nmem_strdup(nmem, dbname);
     db->databases[1] = 0;
@@ -180,17 +185,47 @@ struct database *find_database(const char *id, int new)
     return load_database(id);
 }
 
-// Needs to be extended with criteria
-// Cycles through databases, calling a handler function on each.
-int grep_databases(void *context, void (*fun)(void *context, struct database *db))
+// This will be generalized at some point
+static int match_criterion(struct database *db, struct database_criterion *c)
+{
+    if (!strcmp(c->name, "id"))
+    {
+        struct database_criterion_value *v;
+        for (v = c->values; v; v = v->next)
+            if (!strcmp(v->value, db->url))
+                return 1;
+        return 0;
+    }
+    else
+        return 0;
+}
+
+int database_match_criteria(struct database *db, struct database_criterion *cl)
+{
+    for (; cl; cl = cl->next)
+        if (!match_criterion(db, cl))
+            break;
+    if (cl) // one of the criteria failed to match -- skip this db
+        return 0;
+    else
+        return 1;
+}
+
+// Cycles through databases, calling a handler function on the ones for
+// which all criteria matched.
+int grep_databases(void *context, struct database_criterion *cl,
+        void (*fun)(void *context, struct database *db))
 {
     struct database *p;
     int i;
 
     for (p = databases; p; p = p->next)
     {
-        (*fun)(context, p);
-        i++;
+        if (database_match_criteria(p, cl))
+        {
+            (*fun)(context, p);
+            i++;
+        }
     }
     return i;
 }
@@ -212,6 +247,7 @@ void load_simpletargets(const char *fn)
     {
         char *url;
         char *name;
+        struct database *db;
 
         if (strncmp(line, "target ", 7))
             continue;
@@ -222,8 +258,10 @@ void load_simpletargets(const char *fn)
 
         url = line + 7;
 
-        if (!find_database(url, 0))
+        if (!(db = find_database(url, 0)))
             yaz_log(YLOG_WARN, "Unable to load database %s", url);
+        if (name && db)
+            db->name = nmem_strdup(nmem, name);
     }
     fclose(f);
 }