Piggyback present
[pazpar2-moved-to-github.git] / command.c
index 4ea72e1..1b9fd6d 100644 (file)
--- a/command.c
+++ b/command.c
@@ -1,4 +1,4 @@
-/* $Id: command.c,v 1.1 2006-11-14 20:44:37 quinn Exp $ */
+/* $Id: command.c,v 1.5 2006-12-03 06:43:24 quinn Exp $ */
 
 #include <stdio.h>
 #include <sys/socket.h>
@@ -44,6 +44,7 @@ static int cmd_quit(struct command_session *s, char **argv, int argc)
     return 0;
 }
 
+#ifdef GAGA
 static int cmd_load(struct command_session *s, char **argv, int argc)
 {
     if (argc != 2) {
@@ -53,6 +54,7 @@ static int cmd_load(struct command_session *s, char **argv, int argc)
         command_puts(s, "Failed to open file\n");
     return 1;
 }
+#endif
 
 static int cmd_search(struct command_session *s, char **argv, int argc)
 {
@@ -75,20 +77,52 @@ static int cmd_hitsbytarget(struct command_session *s, char **argv, int argc)
     {
         char buf[1024];
 
-        sprintf(buf, "%s: %d (%d records, diag=%d, state=%s)\n", ht[i].id, ht[i].hits,
-            ht[i].records, ht[i].diagnostic, ht[i].state);
+        sprintf(buf, "%s: %d (%d records, diag=%d, state=%s conn=%d)\n", ht[i].id, ht[i].hits,
+            ht[i].records, ht[i].diagnostic, ht[i].state, ht[i].connected);
         command_puts(s, buf);
     }
     return 1;
 }
 
+static int cmd_show(struct command_session *s, char **argv, int argc)
+{
+    struct record **recs;
+    int num = 10;
+    int merged, total;
+    int i;
+
+    if (argc == 2)
+        num = atoi(argv[1]);
+
+    recs = show(s->psession, 0, &num, &merged, &total);
+
+    for (i = 0; i < num; i++)
+    {
+        int rc;
+        struct record *cnode;
+        struct record *r = recs[i];
+
+        command_puts(s, r->merge_key);
+        for (rc = 1, cnode = r->next_cluster; cnode; cnode = cnode->next_cluster, rc++)
+            ;
+        if (rc > 1)
+        {
+            char buf[256];
+            sprintf(buf, " (%d records)", rc);
+            command_puts(s, buf);
+        }
+        command_puts(s, "\n");
+    }
+    return 1;
+}
+
 static int cmd_stat(struct command_session *s, char **argv, int argc)
 {
     char buf[1024];
     struct statistics stat;
 
     statistics(s->psession, &stat);
-    sprintf(buf, "Number of connections: %d\n", stat.num_connections);
+    sprintf(buf, "Number of connections: %d\n", stat.num_clients);
     command_puts(s, buf);
     if (stat.num_no_connection)
     {
@@ -138,10 +172,13 @@ static struct {
     int (*fun)(struct command_session *s, char *argv[], int argc);
 } cmd_array[] = {
     {"quit", cmd_quit},
+#ifdef GAGA
     {"load", cmd_load},
-    {"search", cmd_search},
+#endif
+    {"find", cmd_search},
     {"ht", cmd_hitsbytarget},
     {"stat", cmd_stat},
+    {"show", cmd_show},
     {0,0}
 };
 
@@ -319,6 +356,7 @@ void command_init(int port)
     int l;
     struct protoent *p;
     struct sockaddr_in myaddr;
+    int one = 1;
 
     yaz_log(YLOG_LOG, "Command port is %d", port);
     if (!(p = getprotobyname("tcp"))) {
@@ -326,13 +364,17 @@ void command_init(int port)
     }
     if ((l = socket(PF_INET, SOCK_STREAM, p->p_proto)) < 0)
         yaz_log(YLOG_FATAL|YLOG_ERRNO, "socket");
+    if (setsockopt(l, SOL_SOCKET, SO_REUSEADDR, (char*)
+                    &one, sizeof(one)) < 0)
+        abort();
+
     bzero(&myaddr, sizeof myaddr);
     myaddr.sin_family = AF_INET;
     myaddr.sin_addr.s_addr = INADDR_ANY;
-    myaddr.sin_port = port;
+    myaddr.sin_port = htons(port);
     if (bind(l, (struct sockaddr *) &myaddr, sizeof myaddr) < 0) 
         yaz_log(YLOG_FATAL|YLOG_ERRNO, "bind");
-    if (listen(l, 5) < 0) 
+    if (listen(l, SOMAXCONN) < 0) 
         yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen");
 
     c = iochan_create(l, command_accept, EVENT_INPUT | EVENT_EXCEPT);