Happy new year
[pazpar2-moved-to-github.git] / src / pazpar2_play.c
index 298a3d3..4ecbbbe 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2011 Index Data
+   Copyright (C) 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
@@ -21,7 +21,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <config.h>
 #endif
 
+#include <time.h>
 #include <stdlib.h>
+#include <sys/select.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netdb.h>
@@ -39,11 +41,12 @@ struct con {
 };
 
 
-static int run(FILE *inf, struct addrinfo *res)
+static int run(int verbose, FILE *inf, struct addrinfo *res)
 {
-    long long tv_sec0;
-    long long tv_usec0;
+    long long tv_sec0 = 0;
+    long long tv_usec0 = 0;
     struct con *cons = 0;
+
     while (1)
     {
         long long tv_sec1;
@@ -51,6 +54,7 @@ static int run(FILE *inf, struct addrinfo *res)
         long long id;
         int sz, r, c;
         char req[100];
+        char request_type[100];
         size_t i;
         struct con **conp;
         c = fgetc(inf);
@@ -63,12 +67,32 @@ static int run(FILE *inf, struct addrinfo *res)
             c = fgetc(inf);
         }
         req[i] = 0;
-        r = sscanf(req, "%lld %lld %lld %d", &tv_sec1, &tv_usec1, &id, &sz);
-        if (r != 4)
+        r = sscanf(req, "%s %lld %lld %lld %d", request_type,
+                   &tv_sec1, &tv_usec1, &id, &sz);
+        if (r != 5)
         {
             fprintf(stderr, "bad line %s\n", req);
             return -1;
         }
+        if (verbose)
+            fprintf(stderr, "read line: %s\n", req);
+        if (tv_sec0)
+        {
+            struct timeval spec;
+
+            spec.tv_sec = tv_sec1 - tv_sec0;
+            if (tv_usec0 > tv_usec1)
+            {
+                spec.tv_usec = 1000000 + tv_usec1 - tv_usec0;
+                spec.tv_sec--;
+            }
+            else
+                spec.tv_usec = tv_usec1 - tv_usec0;
+
+            select(0, 0, 0, 0, &spec);
+        }
+        tv_sec0 = tv_sec1;
+        tv_usec0 = tv_usec1;
         for (conp = &cons; *conp; conp = &(*conp)->next)
             if ((*conp)->id == id)
                 break;
@@ -125,11 +149,20 @@ static int run(FILE *inf, struct addrinfo *res)
                             (long long) toread, (long long) r);
                     return -1;
                 }
-                w = write((*conp)->fd, buf, toread);
-                if (w != toread)
+                if (verbose)
                 {
-                    fprintf(stderr, "write truncated\n");
-                    return -1;
+                    fprintf(stderr, "read %ld bytes\n---\n", (long) r);
+                    fwrite(buf, 1, r, stderr);
+                    fprintf(stderr, "\n----\n");
+                }
+                if (*request_type == 'r')
+                {   /* Only deal with things that Pazpar2 received */
+                    w = write((*conp)->fd, buf, toread);
+                    if (w != toread)
+                    {
+                        fprintf(stderr, "write truncated\n");
+                        return -1;
+                    }
                 }
                 cnt += toread;
             }
@@ -150,6 +183,7 @@ int main(int argc, char **argv)
     int ret;
     char *arg;
     char *host = 0;
+    int verbose = 0;
     const char *file = 0;
     while ((ret = options("v:", argv, argc, &arg)) != -2)
     {
@@ -188,7 +222,7 @@ int main(int argc, char **argv)
         hints.ai_addr           = NULL;
         hints.ai_canonname      = NULL;
         hints.ai_next           = NULL;
-    
+
         cp = strchr(host, ':');
         if (*cp)
         {
@@ -211,7 +245,7 @@ int main(int argc, char **argv)
             fprintf(stderr, "cannot open %s\n", file);
             exit(1);
         }
-        run(inf, res);
+        run(verbose, inf, res);
         fclose(inf);
     }
     else