some audiod fixes
[paraslash.git] / random_selector.c
1 /*
2  * Copyright (C) 2004-2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file random_selector.c An audio file selector which chooses files by random */
20
21 #include <sys/time.h> /* gettimeofday */
22 #include "server.cmdline.h"
23 #include "server.h"
24 #include "db.h"
25 #include "error.h"
26 #include "net.h"
27 #include "string.h"
28
29 static int com_random_info(int, int, char **);
30 extern struct gengetopt_args_info conf;
31 extern struct misc_meta_data *mmd;
32
33 static unsigned int num_audio_files, audio_file_count;
34 static char **audio_file_list;
35
36 static int count_audio_files(__a_unused const char *dir, __a_unused const char *name)
37 {
38         num_audio_files++;
39         return 1;
40 }
41
42 static int remember_file(const char *dir, const char *name)
43 {
44         if (audio_file_count >= num_audio_files)
45                 return -E_FILE_COUNT;
46         audio_file_list[audio_file_count] = make_message("%s/%s", dir, name);
47         audio_file_count++;
48         return 1;
49 }
50
51 /* array of commands that are supported by this selector */
52 static struct server_command cmds[] = {
53 {
54 .name = "random_info",
55 .handler = com_random_info,
56 .perms = 0,
57 .description = "about the random audio file selector",
58 .synopsis = "random_info",
59 .help =
60
61 "Select a random file under the given directory"
62 }, {
63 .name = NULL,
64 }
65 };
66
67 static int com_random_info(int fd, __a_unused int argc, __a_unused char *argv[])
68 {
69         return send_buffer(fd, "Don't use for huge directories as it is "
70                 "very inefficient in this case.\n");
71 }
72
73 /*
74  * Load a list of all audio files into memory and chose num of them randomly.
75  * Called by server to determine next audio file to be streamed.
76  */
77 static char **random_get_audio_file_list(unsigned int num)
78 {
79         int i, ret;
80         unsigned int len;
81         char **ret_list = NULL; /* what we are going to return */
82
83         audio_file_list = NULL;
84         num_audio_files = 0;
85         /* first run, just count all audio files. dopey */
86         ret = find_audio_files(conf.random_dir_arg, count_audio_files);
87         if (ret < 0)
88                 goto out;
89         ret = -E_NOTHING_FOUND;
90         if (!num_audio_files)
91                 goto out;
92         /* yeah, that doesn't scale, also dopey */
93         audio_file_list = para_malloc(num_audio_files * sizeof(char *));
94         audio_file_count = 0;
95         /* second run (hot dentry cache, hopefully), fill audio_file_list */
96         ret = find_audio_files(conf.random_dir_arg, remember_file);
97         if (ret < 0)
98                 goto out;
99         /* careful, files might got deleted underneath */
100         num_audio_files = audio_file_count; /* can only decrease */
101         len = PARA_MIN(num, num_audio_files);
102         ret = -E_NOTHING_FOUND;
103         if (!len) /* nothing found, return NULL */
104                 goto out;
105         /* success, return NULL-terminated list */
106         ret_list = para_calloc((len + 1) * sizeof(char *));
107         for (i = 0; i < len; i++) { /* choose randomly */
108                 int r = (int) ((num_audio_files + 0.0) * (rand()
109                         / (RAND_MAX + 1.0)));
110                 ret_list[i] = para_strdup(audio_file_list[r]);
111         }
112         ret = 1;
113 out:
114         if (audio_file_list) {
115                 for (i = 0; i < num_audio_files; i++)
116                         free(audio_file_list[i]);
117                 free(audio_file_list);
118         }
119         if (ret > 0) {
120         } else
121                 sprintf(mmd->selector_info, "dbinfo1:%s\n", PARA_STRERROR(-ret));
122         return ret_list;
123 }
124
125 void random_update_audio_file(char *audio_file)
126 {
127         char *dn = para_dirname(audio_file);
128         snprintf(mmd->selector_info, MMD_INFO_SIZE - 1,
129                 "dbinfo1:current dir: %s\n"
130                 "dbinfo2:random_dir: %s\n"
131                 "dbinfo3:%d files available\n",
132                 dn, conf.random_dir_arg, num_audio_files);
133         free(dn);
134         mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';
135 }
136 static void random_shutdown(void)
137 {
138 }
139
140 /**
141  *  the init function for the random audio file selector
142  *
143  * Init all function pointers of \a s, init the info text and seed the
144  * PRNG.
145  *
146  * \sa struct audio_file_selector, misc_meta_data::selector_info, mysql.c
147  */
148 int random_selector_init(struct audio_file_selector *s)
149 {
150         struct timeval now;
151
152         PARA_INFO_LOG("%s", "registering random handlers ;)\n");
153         gettimeofday(&now, NULL);
154         srand(now.tv_usec);
155         s->cmd_list = cmds;
156         s->get_audio_file_list = random_get_audio_file_list;
157         s->shutdown = random_shutdown;
158         s->update_audio_file = random_update_audio_file;
159         snprintf(mmd->selector_info, MMD_INFO_SIZE - 1,
160                 "dbinfo1: Welcome to the random selector\n"
161                 "dbinfo2: random_dir: %s\n", conf.random_dir_arg);
162         mmd->selector_info[MMD_INFO_SIZE - 1] = '\0';
163         return 1;
164 }