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 */
13 #include "server.cmdline.h"
17 #include "afs_common.h"
19 #include "random_selector_command_list.h"
21 extern struct misc_meta_data *mmd;
23 static unsigned int num_audio_files, audio_file_count;
24 static char **audio_file_list;
26 static int count_audio_files(__a_unused const char *dir, __a_unused const char *name)
32 static int remember_file(const char *dir, const char *name)
34 if (audio_file_count < num_audio_files) {
35 audio_file_list[audio_file_count] =
36 make_message("%s/%s", dir, name);
42 int com_random_info(int fd, __a_unused int argc, __a_unused char * const * const argv)
44 return send_buffer(fd, "Don't use for huge directories as it is "
45 "very inefficient in this case.\n");
49 * Load a list of all audio files into memory and chose num of them randomly.
50 * Called by server to determine next audio file to be streamed.
52 static char **random_get_audio_file_list(unsigned int num)
56 char **ret_list = NULL; /* what we are going to return */
58 audio_file_list = NULL;
60 /* first run, just count all audio files. dopey */
61 ret = find_audio_files(conf.random_dir_arg, count_audio_files);
64 ret = -E_NOTHING_FOUND;
67 /* yeah, that doesn't scale, also dopey */
68 audio_file_list = para_malloc(num_audio_files * sizeof(char *));
70 /* second run (hot dentry cache, hopefully), fill audio_file_list */
71 ret = find_audio_files(conf.random_dir_arg, remember_file);
74 /* careful, files might got deleted underneath */
75 num_audio_files = audio_file_count; /* can only decrease */
76 len = PARA_MIN(num, num_audio_files);
77 ret = -E_NOTHING_FOUND;
78 if (!len) /* nothing found, return NULL */
80 /* success, return NULL-terminated list */
81 ret_list = para_calloc((len + 1) * sizeof(char *));
82 for (i = 0; i < len; i++) { /* choose randomly */
83 int r = (int) ((num_audio_files + 0.0) * (rand()
85 ret_list[i] = para_strdup(audio_file_list[r]);
89 if (audio_file_list) {
90 for (i = 0; i < num_audio_files; i++)
91 free(audio_file_list[i]);
92 free(audio_file_list);
96 sprintf(mmd->selector_info, "dbinfo1:%s\n", PARA_STRERROR(-ret));
100 static void random_update_audio_file(char *audio_file)
102 char *dn = para_dirname(audio_file);
103 snprintf(mmd->selector_info, MMD_INFO_SIZE - 1,
104 "dbinfo1:current dir: %s\n"
105 "dbinfo2:random_dir: %s\n"
106 "dbinfo3:%d files available\n",
107 dn, conf.random_dir_arg, num_audio_files);
109 mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';
111 static void random_shutdown(void)
116 * the init function for the random audio file selector
118 * \param s pointer ro the struct to iniitalize
120 * Init all function pointers of \a s, init the info text and seed the PRNG.
122 * \sa struct audio_file_selector, misc_meta_data::selector_info, mysql.c
124 int random_selector_init(struct audio_file_selector *s)
129 PARA_INFO_LOG("%s", "registering random handlers ;)\n");
130 gettimeofday(&now, NULL);
133 s->cmd_list = random_selector_cmds;
134 s->get_audio_file_list = random_get_audio_file_list;
135 s->shutdown = random_shutdown;
136 s->update_audio_file = random_update_audio_file;
137 snprintf(mmd->selector_info, MMD_INFO_SIZE - 1,
138 "dbinfo1: Welcome to the random selector\n"
139 "dbinfo2: random_dir: %s\n", conf.random_dir_arg);
140 mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';