compute stats in a subprocess to avoid wedging the server if it takes ages
[disorder] / server / server.c
index 4ba43a6..a112f78 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004, 2005, 2006 Richard Kettlewell
+ * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -42,6 +42,7 @@
 #include "server.h"
 #include "syscalls.h"
 #include "queue.h"
+#include "server-queue.h"
 #include "play.h"
 #include "log.h"
 #include "mem.h"
@@ -139,6 +140,8 @@ static int reader_error(ev_source attribute((unused)) *ev,
   D(("server reader_error %d %d", fd, errno_value));
   error(errno, "S%x read error on socket", c->tag);
   ev_writer_cancel(c->w);
+  ev_report(ev);
+  info("closing fd %d", fd);
   xclose(fd);
   return 0;
 }
@@ -662,21 +665,19 @@ static int c_random_enabled(struct conn *c,
   return 1;                    /* completed */
 }
 
+static void got_stats(char *stats, void *u) {
+  struct conn *const c = u;
+
+  sink_printf(ev_writer_sink(c->w), "253 stats\n%s\n.\n", stats);
+  /* Now we can start processing commands again */
+  ev_reader_enable(c->r);
+}
+
 static int c_stats(struct conn *c,
                   char attribute((unused)) **vec,
                   int attribute((unused)) nvec) {
-  char **v;
-  int nv, n;
-
-  v = trackdb_stats(&nv);
-  sink_printf(ev_writer_sink(c->w), "253 stats\n");
-  for(n = 0; n < nv; ++n) {
-    if(v[n][0] == '.')
-      sink_writes(ev_writer_sink(c->w), ".");
-    sink_printf(ev_writer_sink(c->w), "%s\n", v[n]);
-  }
-  sink_writes(ev_writer_sink(c->w), ".\n");
-  return 1;
+  trackdb_stats_subprocess(c->ev, got_stats, c);
+  return 0;                            /* not yet complete */
 }
 
 static int c_volume(struct conn *c,
@@ -777,8 +778,8 @@ static int c_log(struct conn *c,
 static void post_move_cleanup(void) {
   struct queue_entry *q;
 
-  /* If we have caused the random track to not be at the end then we make it no
-   * longer be random. */
+  /* If we have caused any random tracks to not be at the end then we make them
+   * no longer be random. */
   for(q = qhead.next; q != &qhead; q = q->next)
     if(q->state == playing_random && q->next != &qhead)
       q->state = playing_unplayed;
@@ -1109,8 +1110,10 @@ static int listen_callback(ev_source *ev,
   cloexec(fd);
   c->tag = tags++;
   c->ev = ev;
-  c->w = ev_writer_new(ev, fd, writer_error, c);
-  c->r = ev_reader_new(ev, fd, redirect_reader_callback, reader_error, c);
+  c->w = ev_writer_new(ev, fd, writer_error, c,
+                      "client writer");
+  c->r = ev_reader_new(ev, fd, redirect_reader_callback, reader_error, c,
+                      "client reader");
   c->fd = fd;
   c->reader = reader_callback;
   c->l = l;
@@ -1146,7 +1149,8 @@ int server_start(ev_source *ev, int pf,
   cloexec(fd);
   l->name = name;
   l->pf = pf;
-  if(ev_listen(ev, fd, listen_callback, l)) exit(EXIT_FAILURE);
+  if(ev_listen(ev, fd, listen_callback, l, "server listener"))
+    exit(EXIT_FAILURE);
   return fd;
 }