vss.c: Remove some unused code.
[paraslash.git] / random_selector.c
1 /*
2  * Copyright (C) 2004-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file random_selector.c An audio file selector which chooses files by random */
8
9 #include <sys/time.h> /* gettimeofday */
10 #include "para.h"
11 #include "error.h"
12 #include "string.h"
13 #include "server.cmdline.h"
14 #include "afh.h"
15 #include "afs.h"
16 #include "server.h"
17 #include "afs_common.h"
18 #include "net.h"
19 #include "random_selector_command_list.h"
20
21 extern struct misc_meta_data *mmd;
22
23 static unsigned int num_audio_files, audio_file_count;
24 static char **audio_file_list;
25
26 static int count_audio_files(__a_unused const char *dir, __a_unused const char *name)
27 {
28         num_audio_files++;
29         return 1;
30 }
31
32 static int remember_file(const char *dir, const char *name)
33 {
34         if (audio_file_count < num_audio_files) {
35                 audio_file_list[audio_file_count] =
36                         make_message("%s/%s", dir, name);
37                 audio_file_count++;
38         }
39         return 1;
40 }
41
42 int com_random_info(int fd, __a_unused int argc, __a_unused char * const * const argv)
43 {
44         return send_buffer(fd, "Don't use for huge directories as it is "
45                 "very inefficient in this case.\n");
46 }
47
48 /*
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.
51  */
52 static char **random_get_audio_file_list(unsigned int num)
53 {
54         int i, ret;
55         unsigned int len;
56         char **ret_list = NULL; /* what we are going to return */
57
58         audio_file_list = NULL;
59         num_audio_files = 0;
60         /* first run, just count all audio files. dopey */
61         ret = find_audio_files(conf.random_dir_arg, count_audio_files);
62         if (ret < 0)
63                 goto out;
64         ret = -E_NOTHING_FOUND;
65         if (!num_audio_files)
66                 goto out;
67         /* yeah, that doesn't scale, also dopey */
68         audio_file_list = para_malloc(num_audio_files * sizeof(char *));
69         audio_file_count = 0;
70         /* second run (hot dentry cache, hopefully), fill audio_file_list */
71         ret = find_audio_files(conf.random_dir_arg, remember_file);
72         if (ret < 0)
73                 goto out;
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 */
79                 goto out;
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()
84                         / (RAND_MAX + 1.0)));
85                 ret_list[i] = para_strdup(audio_file_list[r]);
86         }
87         ret = 1;
88 out:
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);
93         }
94         if (ret > 0) {
95         } else
96                 sprintf(mmd->selector_info, "dbinfo1:%s\n", PARA_STRERROR(-ret));
97         return ret_list;
98 }
99
100 static void random_update_audio_file(char *audio_file)
101 {
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);
108         free(dn);
109         mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';
110 }
111 static void random_shutdown(void)
112 {
113 }
114
115 /**
116  * the init function for the random audio file selector
117  *
118  * \param s pointer ro the struct to iniitalize
119  *
120  * Init all function pointers of \a s, init the info text and seed the PRNG.
121  *
122  * \sa struct audio_file_selector, misc_meta_data::selector_info, mysql.c
123  */
124 int random_selector_init(struct audio_file_selector *s)
125 {
126         struct timeval now;
127         unsigned int seed;
128
129         PARA_INFO_LOG("%s", "registering random handlers ;)\n");
130         gettimeofday(&now, NULL);
131         seed = now.tv_usec;
132         srand(seed);
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';
141         return 1;
142 }