chiark / gitweb /
disorder-choose now uses an arcfour keystream as its RNG instead of
[disorder] / server / choose.c
index 4e1b3266cff077894d5d6ac0228745b1406924c8..927d6136db6dfc8766f846dbe97c580999cb93bb 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * This file is part of DisOrder 
  * Copyright (C) 2008 Richard Kettlewell
+ * Copyright (C) 2008 Mark Wooding
  *
  * 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
@@ -55,6 +56,9 @@
 #include "trackname.h"
 #include "queue.h"
 #include "server-queue.h"
+#include "random.h"
+
+#define BASE_WEIGHT 90000
 
 static DB_TXN *global_tid;
 
@@ -120,7 +124,7 @@ static unsigned long compute_weight(const char *track,
                                     struct kvp *prefs) {
   const char *s;
   char **track_tags;
-  time_t last, now;
+  time_t last, now = time(0);
 
   /* Reject tracks not in any collection (race between edit config and
    * rescan) */
@@ -141,7 +145,6 @@ static unsigned long compute_weight(const char *track,
   /* Reject tracks played within the last 8 hours */
   if((s = kvp_get(prefs, "played_time"))) {
     last = atoll(s);
-    now = time(0);
     if(now < last + config->replay_min)
       return 0;
   }
@@ -171,23 +174,20 @@ static unsigned long compute_weight(const char *track,
     if((errno == 0 || errno == ERANGE) && n >= 0)
       return n;
   }
-  
-  return 90000;
-}
 
-/** @brief Pick a random integer uniformly from [0, limit) */
-static void random_bytes(unsigned char *buf, size_t n) {
-  static int fd = -1;
-  int r;
+  /* Bias up tracks that were recently added */
+  if((s = kvp_get(data, "_noticed"))) {
+    const time_t noticed = atoll(s);
 
-  if(fd < 0) {
-    if((fd = open("/dev/urandom", O_RDONLY)) < 0)
-      fatal(errno, "opening /dev/urandom");
+    if(noticed + config->new_bias_age < now)
+      /* Currently we just step up the weight of tracks that are in range.  A
+       * more sophisticated approach would be to linearly decay from new_bias
+       * down to BASE_WEIGHT over the course of the new_bias_age interval
+       * starting when the track is added. */
+      return config->new_bias;
   }
-  if((r = read(fd, buf, n)) < 0)
-    fatal(errno, "reading /dev/urandom");
-  if((size_t)r < n)
-    fatal(0, "short read from /dev/urandom");
+  
+  return BASE_WEIGHT;
 }
 
 /** @brief Pick a random integer uniformly from [0, limit) */
@@ -234,7 +234,7 @@ static unsigned long long pick_weight(unsigned long long limit) {
 
   do {
     /* Actually get some random data. */
-    random_bytes(buf, nby);
+    random_get(buf, nby);
 
     /* Clobber the top byte.  */
     buf[0] &= m;