2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
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.
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.
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
21 * @brief Random track chooser
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.
37 #include <sys/types.h>
44 #include "configuration.h"
52 #include "trackdb-int.h"
55 static DB_TXN *global_tid;
57 static const struct option options[] = {
58 { "help", no_argument, 0, 'h' },
59 { "version", no_argument, 0, 'V' },
60 { "config", required_argument, 0, 'c' },
61 { "debug", no_argument, 0, 'd' },
62 { "no-debug", no_argument, 0, 'D' },
63 { "syslog", no_argument, 0, 's' },
64 { "no-syslog", no_argument, 0, 'S' },
68 /* display usage message and terminate */
69 static void help(void) {
71 " disorder-choose [OPTIONS]\n"
73 " --help, -h Display usage message\n"
74 " --version, -V Display version number\n"
75 " --config PATH, -c PATH Set configuration file\n"
76 " --debug, -d Turn on debugging\n"
77 " --[no-]syslog Enable/disable logging to syslog\n"
79 "Track choose for DisOrder. Not intended to be run\n"
85 /** @brief Weighted track record */
86 struct weighted_track {
87 /** @brief Next track in the list */
88 struct weighted_track *next;
89 /** @brief Track name */
91 /** @brief Weight for this track (always positive) */
95 /** @brief List of tracks with nonzero weight */
96 static struct weighted_track *tracks;
98 /** @brief Sum of all weights */
99 static unsigned long long total_weight;
101 /** @brief Count of tracks */
104 /** @brief Compute the weight of a track
105 * @param track Track name (UTF-8)
106 * @param data Track data
107 * @param prefs Track preferences
108 * @return Track weight (non-negative)
110 * Tracks to be excluded entirely are given a weight of 0.
112 static unsigned long compute_weight(const char attribute((unused)) *track,
113 struct kvp attribute((unused)) *data,
117 /* Firstly, tracks with random play disabled always have weight 0 and that's
119 if((s = kvp_get(prefs, "pick_at_random"))
125 /** @brief Called for each track */
126 static int collect_tracks_callback(const char *track,
129 void attribute((unused)) *u,
130 DB_TXN attribute((unused)) *tid) {
131 const unsigned long weight = compute_weight(track, data, prefs);
134 struct weighted_track *const t = xmalloc(sizeof *t);
140 total_weight += weight;
146 /** @brief Pick a random integer uniformly from [0, limit) */
147 static unsigned long long pick_weight(unsigned long long limit) {
148 unsigned long long n;
153 if((fd = open("/dev/urandom", O_RDONLY)) < 0)
154 fatal(errno, "opening /dev/urandom");
156 if((r = read(fd, &n, sizeof n)) < 0)
157 fatal(errno, "reading /dev/urandom");
158 if((size_t)r < sizeof n)
159 fatal(0, "short read from /dev/urandom");
163 /** @brief Pick a track at random and write it to stdout */
164 static void pick_track(void) {
166 struct weighted_track *t;
168 w = pick_weight(total_weight);
170 while(t && w >= t->weight) {
175 fatal(0, "ran out of tracks but %lld weighting left", w);
176 xprintf("%s", t->track);
179 int main(int argc, char **argv) {
180 int n, logsyslog = !isatty(2);
184 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
185 while((n = getopt_long(argc, argv, "hVc:dDSs", options, 0)) >= 0) {
188 case 'V': version("disorder-choose");
189 case 'c': configfile = optarg; break;
190 case 'd': debugging = 1; break;
191 case 'D': debugging = 0; break;
192 case 'S': logsyslog = 0; break;
193 case 's': logsyslog = 1; break;
194 default: fatal(0, "invalid option");
198 openlog(progname, LOG_PID, LOG_DAEMON);
199 log_default = &log_syslog;
201 if(config_read(0)) fatal(0, "cannot read configuration");
202 /* Generate the candidate track list */
203 trackdb_init(TRACKDB_NO_RECOVER);
204 trackdb_open(TRACKDB_NO_UPGRADE|TRACKDB_READ_ONLY);
205 global_tid = trackdb_begin_transaction();
206 if(trackdb_scan(0, collect_tracks_callback, 0, global_tid))
208 trackdb_commit_transaction(global_tid);
211 //info("ntracks=%ld total_weight=%lld", ntracks, total_weight);
213 fatal(0, "no tracks match random choice criteria");