2 * Copyright (C) 2004-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file random_selector.c An audio file selector which chooses files by random */
9 #include <sys/time.h> /* gettimeofday */
11 #include "server.cmdline.h"
14 #include "afs_common.h"
18 #include "random_selector_command_list.h"
20 extern struct misc_meta_data *mmd;
22 static unsigned int num_audio_files, audio_file_count;
23 static char **audio_file_list;
25 static int count_audio_files(__a_unused const char *dir, __a_unused const char *name)
31 static int remember_file(const char *dir, const char *name)
33 if (audio_file_count < num_audio_files) {
34 audio_file_list[audio_file_count] =
35 make_message("%s/%s", dir, name);
41 int com_random_info(int fd, __a_unused int argc, __a_unused char * const * const argv)
43 return send_buffer(fd, "Don't use for huge directories as it is "
44 "very inefficient in this case.\n");
48 * Load a list of all audio files into memory and chose num of them randomly.
49 * Called by server to determine next audio file to be streamed.
51 static char **random_get_audio_file_list(unsigned int num)
55 char **ret_list = NULL; /* what we are going to return */
57 audio_file_list = NULL;
59 /* first run, just count all audio files. dopey */
60 ret = find_audio_files(conf.random_dir_arg, count_audio_files);
63 ret = -E_NOTHING_FOUND;
66 /* yeah, that doesn't scale, also dopey */
67 audio_file_list = para_malloc(num_audio_files * sizeof(char *));
69 /* second run (hot dentry cache, hopefully), fill audio_file_list */
70 ret = find_audio_files(conf.random_dir_arg, remember_file);
73 /* careful, files might got deleted underneath */
74 num_audio_files = audio_file_count; /* can only decrease */
75 len = PARA_MIN(num, num_audio_files);
76 ret = -E_NOTHING_FOUND;
77 if (!len) /* nothing found, return NULL */
79 /* success, return NULL-terminated list */
80 ret_list = para_calloc((len + 1) * sizeof(char *));
81 for (i = 0; i < len; i++) { /* choose randomly */
82 int r = (int) ((num_audio_files + 0.0) * (rand()
84 ret_list[i] = para_strdup(audio_file_list[r]);
88 if (audio_file_list) {
89 for (i = 0; i < num_audio_files; i++)
90 free(audio_file_list[i]);
91 free(audio_file_list);
95 sprintf(mmd->selector_info, "dbinfo1:%s\n", PARA_STRERROR(-ret));
99 static void random_update_audio_file(char *audio_file)
101 char *dn = para_dirname(audio_file);
102 snprintf(mmd->selector_info, MMD_INFO_SIZE - 1,
103 "dbinfo1:current dir: %s\n"
104 "dbinfo2:random_dir: %s\n"
105 "dbinfo3:%d files available\n",
106 dn, conf.random_dir_arg, num_audio_files);
108 mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';
110 static void random_shutdown(void)
115 * the init function for the random audio file selector
117 * \param s pointer ro the struct to iniitalize
119 * Init all function pointers of \a s, init the info text and seed the PRNG.
121 * \sa struct audio_file_selector, misc_meta_data::selector_info, mysql.c
123 int random_selector_init(struct audio_file_selector *s)
128 PARA_INFO_LOG("%s", "registering random handlers ;)\n");
129 gettimeofday(&now, NULL);
132 s->cmd_list = random_selector_cmds;
133 s->get_audio_file_list = random_get_audio_file_list;
134 s->shutdown = random_shutdown;
135 s->update_audio_file = random_update_audio_file;
136 snprintf(mmd->selector_info, MMD_INFO_SIZE - 1,
137 "dbinfo1: Welcome to the random selector\n"
138 "dbinfo2: random_dir: %s\n", conf.random_dir_arg);
139 mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';