chiark / gitweb /
Merge more 3.0 branch changes
[disorder] / server / choose.c
1 /*
2  * This file is part of DisOrder 
3  * Copyright (C) 2008 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file choose.c
21  * @brief Random track chooser
22  *
23  * Picks a track at random and writes it to standard output.  If for
24  * any reason no track can be picked - even a trivial reason like a
25  * deadlock - it just exits and expects the server to try again.
26  */
27
28 #include <config.h>
29 #include "types.h"
30
31 #include <getopt.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <db.h>
35 #include <locale.h>
36 #include <errno.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <pcre.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <syslog.h>
43 #include <time.h>
44
45 #include "configuration.h"
46 #include "log.h"
47 #include "defs.h"
48 #include "mem.h"
49 #include "kvp.h"
50 #include "syscalls.h"
51 #include "printf.h"
52 #include "trackdb.h"
53 #include "trackdb-int.h"
54 #include "version.h"
55 #include "trackname.h"
56 #include "queue.h"
57 #include "server-queue.h"
58
59 static DB_TXN *global_tid;
60
61 static const struct option options[] = {
62   { "help", no_argument, 0, 'h' },
63   { "version", no_argument, 0, 'V' },
64   { "config", required_argument, 0, 'c' },
65   { "debug", no_argument, 0, 'd' },
66   { "no-debug", no_argument, 0, 'D' },
67   { "syslog", no_argument, 0, 's' },
68   { "no-syslog", no_argument, 0, 'S' },
69   { 0, 0, 0, 0 }
70 };
71
72 /* display usage message and terminate */
73 static void help(void) {
74   xprintf("Usage:\n"
75           "  disorder-choose [OPTIONS]\n"
76           "Options:\n"
77           "  --help, -h              Display usage message\n"
78           "  --version, -V           Display version number\n"
79           "  --config PATH, -c PATH  Set configuration file\n"
80           "  --debug, -d             Turn on debugging\n"
81           "  --[no-]syslog           Enable/disable logging to syslog\n"
82           "\n"
83           "Track choose for DisOrder.  Not intended to be run\n"
84           "directly.\n");
85   xfclose(stdout);
86   exit(0);
87 }
88
89 /** @brief Weighted track record */
90 struct weighted_track {
91   /** @brief Next track in the list */
92   struct weighted_track *next;
93   /** @brief Track name */
94   const char *track;
95   /** @brief Weight for this track (always positive) */
96   unsigned long weight;
97 };
98
99 /** @brief List of tracks with nonzero weight */
100 static struct weighted_track *tracks;
101
102 /** @brief Sum of all weights */
103 static unsigned long long total_weight;
104
105 /** @brief Count of tracks */
106 static long ntracks;
107
108 static char **required_tags;
109 static char **prohibited_tags;
110
111 static int queue_contains(const struct queue_entry *head,
112                           const char *track) {
113   const struct queue_entry *q;
114
115   for(q = head->next; q != head; q = q->next)
116     if(!strcmp(q->track, track))
117       return 1;
118   return 0;
119 }
120
121 /** @brief Compute the weight of a track
122  * @param track Track name (UTF-8)
123  * @param data Track data
124  * @param prefs Track preferences
125  * @return Track weight (non-negative)
126  *
127  * Tracks to be excluded entirely are given a weight of 0.
128  */
129 static unsigned long compute_weight(const char *track,
130                                     struct kvp *data,
131                                     struct kvp *prefs) {
132   const char *s;
133   char **track_tags;
134   time_t last, now;
135
136   /* Reject tracks not in any collection (race between edit config and
137    * rescan) */
138   if(!find_track_root(track)) {
139     info("found track not in any collection: %s", track);
140     return 0;
141   }
142
143   /* Reject aliases to avoid giving aliased tracks extra weight */
144   if(kvp_get(data, "_alias_for"))
145     return 0;
146   
147   /* Reject tracks with random play disabled */
148   if((s = kvp_get(prefs, "pick_at_random"))
149      && !strcmp(s, "0"))
150     return 0;
151
152   /* Reject tracks played within the last 8 hours */
153   if((s = kvp_get(prefs, "played_time"))) {
154     last = atoll(s);
155     now = time(0);
156     if(now < last + config->replay_min)
157       return 0;
158   }
159
160   /* Reject tracks currently in the queue or in the recent list */
161   if(queue_contains(&qhead, track)
162      || queue_contains(&phead, track))
163     return 0;
164
165   /* We'll need tags for a number of things */
166   track_tags = parsetags(kvp_get(prefs, "tags"));
167
168   /* Reject tracks with prohibited tags */
169   if(prohibited_tags && tag_intersection(track_tags, prohibited_tags))
170     return 0;
171
172   /* Reject tracks that lack required tags */
173   if(*required_tags && !tag_intersection(track_tags, required_tags))
174     return 0;
175
176   /* Use the configured weight if available */
177   if((s = kvp_get(prefs, "weight"))) {
178     long n;
179     errno = 0;
180
181     n = strtol(s, 0, 10);
182     if((errno == 0 || errno == ERANGE) && n >= 0)
183       return n;
184   }
185   
186   return 90000;
187 }
188
189 /** @brief Called for each track */
190 static int collect_tracks_callback(const char *track,
191                                    struct kvp *data,
192                                    struct kvp *prefs,
193                                    void attribute((unused)) *u,
194                                    DB_TXN attribute((unused)) *tid) {
195   unsigned long weight = compute_weight(track, data, prefs);
196
197   if(weight) {
198     struct weighted_track *const t = xmalloc(sizeof *t);
199
200     /* Clamp weight so that we can fit in billions of tracks when we do
201      * arithmetic in long long */
202     if(weight > 0x7fffffff)
203       weight = 0x7fffffff;
204     t->next = tracks;
205     t->track = track;
206     t->weight = weight;
207     tracks = t;
208     total_weight += weight;
209     ++ntracks;
210   }
211   return 0;
212 }
213
214 /** @brief Pick a random integer uniformly from [0, limit) */
215 static unsigned long long pick_weight(unsigned long long limit) {
216   unsigned long long n;
217   static int fd = -1;
218   int r;
219
220   if(fd < 0) {
221     if((fd = open("/dev/urandom", O_RDONLY)) < 0)
222       fatal(errno, "opening /dev/urandom");
223   }
224   if((r = read(fd, &n, sizeof n)) < 0)
225     fatal(errno, "reading /dev/urandom");
226   if((size_t)r < sizeof n)
227     fatal(0, "short read from /dev/urandom");
228   return n % limit;
229 }
230
231 /** @brief Pick a track at random and write it to stdout */
232 static void pick_track(void) {
233   long long w;
234   struct weighted_track *t;
235
236   w = pick_weight(total_weight);
237   t = tracks;
238   while(t && w >= t->weight) {
239     w -= t->weight;
240     t = t->next;
241   }
242   if(!t)
243     fatal(0, "ran out of tracks but %lld weighting left", w);
244   xprintf("%s", t->track);
245 }
246
247 int main(int argc, char **argv) {
248   int n, logsyslog = !isatty(2), err;
249   const char *tags;
250   
251   set_progname(argv);
252   mem_init();
253   if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
254   while((n = getopt_long(argc, argv, "hVc:dDSs", options, 0)) >= 0) {
255     switch(n) {
256     case 'h': help();
257     case 'V': version("disorder-choose");
258     case 'c': configfile = optarg; break;
259     case 'd': debugging = 1; break;
260     case 'D': debugging = 0; break;
261     case 'S': logsyslog = 0; break;
262     case 's': logsyslog = 1; break;
263     default: fatal(0, "invalid option");
264     }
265   }
266   if(logsyslog) {
267     openlog(progname, LOG_PID, LOG_DAEMON);
268     log_default = &log_syslog;
269   }
270   if(config_read(0)) fatal(0, "cannot read configuration");
271   /* Find out current queue/recent list */
272   queue_read();
273   recent_read();
274   /* Generate the candidate track list */
275   trackdb_init(TRACKDB_NO_RECOVER);
276   trackdb_open(TRACKDB_NO_UPGRADE|TRACKDB_READ_ONLY);
277   global_tid = trackdb_begin_transaction();
278   if((err = trackdb_get_global_tid("required-tags", global_tid, &tags)))
279     fatal(0, "error getting required-tags: %s", db_strerror(err));
280   required_tags = parsetags(tags);
281   if((err = trackdb_get_global_tid("prohibited-tags", global_tid, &tags)))
282     fatal(0, "error getting prohibited-tags: %s", db_strerror(err));
283   prohibited_tags = parsetags(tags);
284   if(trackdb_scan(0, collect_tracks_callback, 0, global_tid))
285     exit(1);
286   trackdb_commit_transaction(global_tid);
287   trackdb_close();
288   trackdb_deinit();
289   //info("ntracks=%ld total_weight=%lld", ntracks, total_weight);
290   if(!total_weight)
291     fatal(0, "no tracks match random choice criteria");
292   /* Pick a track */
293   pick_track();
294   xfclose(stdout);
295   return 0;
296 }
297
298 /*
299 Local Variables:
300 c-basic-offset:2
301 comment-column:40
302 fill-column:79
303 indent-tabs-mode:nil
304 End:
305 */