]> git.tuebingen.mpg.de Git - paraslash.git/blob - dopey.c
add dbtool hooks
[paraslash.git] / dopey.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 dopey.c Simple database tool implementation. Feel free to modify.  */
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_dopey(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(__unused const char *dir, __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 database tool */
52 static struct server_command cmds[] = {
53 {
54 .name = "dopey",
55 .handler = com_dopey,
56 .perms = 0,
57 .description = "about the dopey database tool",
58 .synopsis = "dopey",
59 .help =
60
61 "It's so dumb. It hurts. Don't use it; switch to the mysql database\n"
62 "tool instead. OTOH: You typed 'help dopey', so if you serious about\n"
63 "that and you really intend to help the dopey database tool, look at\n"
64 "my source code, dopey.c, and modify it to make it something useful.\n"
65
66 }, {
67 .name = NULL,
68 }
69 };
70
71 static int com_dopey(int fd, __unused int argc, __unused char *argv[])
72 {
73         return send_buffer(fd, "Please do not use me. I'm too sick to do "
74                 "anything for you. Switch me off. Now!\n");
75 }
76
77 /*
78  * Load a list of all audio files into memory and chose num of them randomly.
79  * Called by server to determine next audio file to be streamed.
80  */
81 static char **dopey_get_audio_file_list(unsigned int num)
82 {
83         int i, ret;
84         unsigned int len;
85         char **ret_list = NULL; /* what we are going to return */
86
87         audio_file_list = NULL;
88         num_audio_files = 0;
89         /* first run, just count all audio files. dopey */
90         ret = find_audio_files(conf.dopey_dir_arg, count_audio_files);
91         if (ret < 0)
92                 goto out;
93         ret = -E_NOTHING_FOUND;
94         if (!num_audio_files)
95                 goto out;
96         /* yeah, that doesn't scale, also dopey */
97         audio_file_list = para_malloc(num_audio_files * sizeof(char *));
98         audio_file_count = 0;
99         /* second run (hot dentry cache, hopefully), fill audio_file_list */
100         ret = find_audio_files(conf.dopey_dir_arg, remember_file);
101         if (ret < 0)
102                 goto out;
103         /* careful, files might got deleted underneath */
104         num_audio_files = audio_file_count; /* can only decrease */
105         len = MIN(num, num_audio_files);
106         ret = -E_NOTHING_FOUND;
107         if (!len) /* nothing found, return NULL */
108                 goto out;
109         /* success, return NULL-terminated list */
110         ret_list = para_calloc((len + 1) * sizeof(char *));
111         for (i = 0; i < len; i++) { /* choose randomly */
112                 int r = (int) ((num_audio_files + 0.0) * (rand()
113                         / (RAND_MAX + 1.0)));
114                 ret_list[i] = para_strdup(audio_file_list[r]);
115         }
116 out:
117         if (audio_file_list) {
118                 for (i = 0; i < num_audio_files; i++)
119                         free(audio_file_list[i]);
120                 free(audio_file_list);
121         }
122 //      if (ret < 0)
123 //              PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
124         return ret_list;
125 }
126
127 static void dopey_shutdown(void)
128 {
129         PARA_DEBUG_LOG("%s", "thanks for using another dbtool.\n");
130 }
131
132 /** dopey's (constant) database info text */
133 #define DBINFO "dbinfo1:database info? You're kidding. I'm dopey!\ndbinfo2:\ndbinfo3:\n"
134
135 /** the dopey init function
136  *
137  * Init all function pointers of \a db, init the dbinfo text and seed the
138  * PRNG.
139  *
140  * \sa struct dbtool, misc_meta_data::dbinfo, mysql.c
141  */
142 int dopey_dbtool_init(struct dbtool *db)
143 {
144         struct timeval now;
145
146         PARA_INFO_LOG("%s", "registering dopey handlers\n");
147         sprintf(mmd->dbinfo, DBINFO);
148         gettimeofday(&now, NULL);
149         srand(now.tv_usec);
150         db->cmd_list = cmds;
151         db->get_audio_file_list = dopey_get_audio_file_list;
152         db->shutdown = dopey_shutdown;
153         return 1;
154 }