chiark / gitweb /
Use new random_id() for queue IDs
[disorder] / server / server-queue.c
index 2b0e89e8f51fbae93c3f219094207de2f4a5a6a5..b71d67773571298491b30f1c40a2210d02d6eaa4 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
+ * Copyright (C) 2004-2008 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
 #include "server-queue.h"
 #include "eventlog.h"
 #include "plugin.h"
-#include "basen.h"
+#include "random.h"
 #include "configuration.h"
 #include "inputline.h"
 #include "disorder.h"
 
 /* the head of the queue is played next, so normally we add to the tail */
-struct queue_entry qhead = { &qhead, &qhead, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+struct queue_entry qhead = { &qhead, &qhead, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
 /* the head of the recent list is the oldest thing, the tail the most recently
  * played */
-struct queue_entry phead = { &phead, &phead, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+struct queue_entry phead = { &phead, &phead, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
 static long pcount;
 
@@ -155,16 +155,22 @@ void recent_write(void) {
   queue_do_write(&phead, config_get_file("recent"));
 }
 
+static int id_in_use(const char *id) {
+  struct queue_entry *q;
+
+  for(q = qhead.next; q != &qhead; q = q->next)
+    if(!strcmp(id, q->id))
+      return 1;
+  return 0;
+}
+
 static void queue_id(struct queue_entry *q) {
-  static unsigned long serial;
-  unsigned long a[3];
-  char buffer[128];
-
-  a[0] = serial++ & 0xFFFFFFFFUL;
-  a[1] = time(0) & 0xFFFFFFFFUL;
-  a[2] = getpid() & 0xFFFFFFFFUL;
-  basen(a, 3, buffer, sizeof buffer, 62);
-  q->id = xstrdup(buffer);
+  const char *id;
+
+  id = random_id();
+  while(id_in_use(id))
+    id = random_id();
+  q->id = id;
 }
 
 struct queue_entry *queue_add(const char *track, const char *submitter,