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 */
10 #include "server.cmdline.h"
16 #include "random_selector_command_list.h"
18 extern struct misc_meta_data
*mmd
;
20 static unsigned int num_audio_files
, audio_file_count
;
21 static char **audio_file_list
;
23 static int count_audio_files(__a_unused
const char *dir
, __a_unused
const char *name
)
29 static int remember_file(const char *dir
, const char *name
)
31 if (audio_file_count
< num_audio_files
) {
32 audio_file_list
[audio_file_count
] =
33 make_message("%s/%s", dir
, name
);
39 int com_random_info(int fd
, __a_unused
int argc
, __a_unused
char *argv
[])
41 return send_buffer(fd
, "Don't use for huge directories as it is "
42 "very inefficient in this case.\n");
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.
49 static char **random_get_audio_file_list(unsigned int num
)
53 char **ret_list
= NULL
; /* what we are going to return */
55 audio_file_list
= NULL
;
57 /* first run, just count all audio files. dopey */
58 ret
= find_audio_files(conf
.random_dir_arg
, count_audio_files
);
61 ret
= -E_NOTHING_FOUND
;
64 /* yeah, that doesn't scale, also dopey */
65 audio_file_list
= para_malloc(num_audio_files
* sizeof(char *));
67 /* second run (hot dentry cache, hopefully), fill audio_file_list */
68 ret
= find_audio_files(conf
.random_dir_arg
, remember_file
);
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 */
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()
82 ret_list
[i
] = para_strdup(audio_file_list
[r
]);
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
);
93 sprintf(mmd
->selector_info
, "dbinfo1:%s\n", PARA_STRERROR(-ret
));
97 static void random_update_audio_file(char *audio_file
)
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
);
106 mmd
->selector_info
[MMD_INFO_SIZE
- 1] = '\0';
108 static void random_shutdown(void)
113 * the init function for the random audio file selector
115 * \param s pointer ro the struct to iniitalize
117 * Init all function pointers of \a s, init the info text and seed the PRNG.
119 * \sa struct audio_file_selector, misc_meta_data::selector_info, mysql.c
121 int random_selector_init(struct audio_file_selector
*s
)
126 PARA_INFO_LOG("%s", "registering random handlers ;)\n");
127 gettimeofday(&now
, NULL
);
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';