configure.ac: Add check for alarm().
[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 "server.cmdline.h"
11 #include "server.h"
12 #include "afs.h"
13 #include "error.h"
14 #include "net.h"
15 #include "string.h"
16 #include "random_selector_command_list.h"
17
18 extern struct misc_meta_data *mmd;
19
20 static unsigned int num_audio_files, audio_file_count;
21 static char **audio_file_list;
22
23 static int count_audio_files(__a_unused const char *dir, __a_unused const char *name)
24 {
25         num_audio_files++;
26         return 1;
27 }
28
29 static int remember_file(const char *dir, const char *name)
30 {
31         if (audio_file_count < num_audio_files) {
32                 audio_file_list[audio_file_count] =
33                         make_message("%s/%s", dir, name);
34                 audio_file_count++;
35         }
36         return 1;
37 }
38
39 int com_random_info(int fd, __a_unused int argc, __a_unused char *argv[])
40 {
41         return send_buffer(fd, "Don't use for huge directories as it is "
42                 "very inefficient in this case.\n");
43 }
44
45 /*
46  * Load a list of all audio files into memory and chose num of them randomly.
47  * Called by server to determine next audio file to be streamed.
48  */
49 static char **random_get_audio_file_list(unsigned int num)
50 {
51         int i, ret;
52         unsigned int len;
53         char **ret_list = NULL; /* what we are going to return */
54
55         audio_file_list = NULL;
56         num_audio_files = 0;
57         /* first run, just count all audio files. dopey */
58         ret = find_audio_files(conf.random_dir_arg, count_audio_files);
59         if (ret < 0)
60                 goto out;
61         ret = -E_NOTHING_FOUND;
62         if (!num_audio_files)
63                 goto out;
64         /* yeah, that doesn't scale, also dopey */
65         audio_file_list = para_malloc(num_audio_files * sizeof(char *));
66         audio_file_count = 0;
67         /* second run (hot dentry cache, hopefully), fill audio_file_list */
68         ret = find_audio_files(conf.random_dir_arg, remember_file);
69         if (ret < 0)
70                 goto out;
71         /* careful, files might got deleted underneath */
72         num_audio_files = audio_file_count; /* can only decrease */
73         len = PARA_MIN(num, num_audio_files);
74         ret = -E_NOTHING_FOUND;
75         if (!len) /* nothing found, return NULL */
76                 goto out;
77         /* success, return NULL-terminated list */
78         ret_list = para_calloc((len + 1) * sizeof(char *));
79         for (i = 0; i < len; i++) { /* choose randomly */
80                 int r = (int) ((num_audio_files + 0.0) * (rand()
81                         / (RAND_MAX + 1.0)));
82                 ret_list[i] = para_strdup(audio_file_list[r]);
83         }
84         ret = 1;
85 out:
86         if (audio_file_list) {
87                 for (i = 0; i < num_audio_files; i++)
88                         free(audio_file_list[i]);
89                 free(audio_file_list);
90         }
91         if (ret > 0) {
92         } else
93                 sprintf(mmd->selector_info, "dbinfo1:%s\n", PARA_STRERROR(-ret));
94         return ret_list;
95 }
96
97 static void random_update_audio_file(char *audio_file)
98 {
99         char *dn = para_dirname(audio_file);
100         snprintf(mmd->selector_info, MMD_INFO_SIZE - 1,
101                 "dbinfo1:current dir: %s\n"
102                 "dbinfo2:random_dir: %s\n"
103                 "dbinfo3:%d files available\n",
104                 dn, conf.random_dir_arg, num_audio_files);
105         free(dn);
106         mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';
107 }
108 static void random_shutdown(void)
109 {
110 }
111
112 /**
113  * the init function for the random audio file selector
114  *
115  * \param s pointer ro the struct to iniitalize
116  *
117  * Init all function pointers of \a s, init the info text and seed the PRNG.
118  *
119  * \sa struct audio_file_selector, misc_meta_data::selector_info, mysql.c
120  */
121 int random_selector_init(struct audio_file_selector *s)
122 {
123         struct timeval now;
124         unsigned int seed;
125
126         PARA_INFO_LOG("%s", "registering random handlers ;)\n");
127         gettimeofday(&now, NULL);
128         seed = now.tv_usec;
129         srand(seed);
130         s->cmd_list = random_selector_cmds;
131         s->get_audio_file_list = random_get_audio_file_list;
132         s->shutdown = random_shutdown;
133         s->update_audio_file = random_update_audio_file;
134         snprintf(mmd->selector_info, MMD_INFO_SIZE - 1,
135                 "dbinfo1: Welcome to the random selector\n"
136                 "dbinfo2: random_dir: %s\n", conf.random_dir_arg);
137         mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';
138         return 1;
139 }