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