]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - plm_dbtool.c
the new plm database tool
[paraslash.git] / plm_dbtool.c
diff --git a/plm_dbtool.c b/plm_dbtool.c
new file mode 100644 (file)
index 0000000..fd18f25
--- /dev/null
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
+ *
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     This program is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU General Public License for more details.
+ *
+ *     You should have received a copy of the GNU General Public License
+ *     along with this program; if not, write to the Free Software
+ *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ */
+
+/** \file plm_dbtool.c Simple playlist manager for paraslash  */
+
+#include <sys/time.h> /* gettimeofday */
+#include "server.cmdline.h"
+#include "server.h"
+#include "db.h"
+#include "error.h"
+#include "net.h"
+#include "string.h"
+
+#define MAX_PLAYLIST_LEN 10000
+#define MAX_PLAYLIST_BYTES (1024 * 1024)
+
+static unsigned playlist_len, playlist_size, current_playlist_entry;
+static char **playlist;
+
+static int com_ppl(int, int, char **);
+static int com_lpl(int, int, char **);
+extern struct misc_meta_data *mmd;
+
+/* array of commands that are supported by this database tool */
+static struct server_command cmds[] = {
+{
+.name = "ppl",
+.handler = com_ppl,
+.perms = DB_READ,
+.description = "print playlist",
+.synopsis = "ppl",
+.help =
+"Print out the current playlist"
+}, {
+.name = "lpl",
+.handler = com_lpl,
+.perms = DB_WRITE,
+.description = "load playlist",
+.synopsis = "lpl",
+.help =
+"Read a new playlist from stdin"
+
+}, {
+.name = NULL,
+}
+};
+
+static void playlist_add(char *path)
+{
+       if (playlist_len >= playlist_size) {
+               if (playlist_size >= MAX_PLAYLIST_LEN)
+                       return;
+               playlist_size *= 2;
+               playlist = para_realloc(playlist, playlist_size * sizeof(char *));
+       }
+       PARA_DEBUG_LOG("adding #%d: %s\n", playlist_len, path);
+       playlist[playlist_len] = para_strdup(path);
+       playlist_len++;
+}
+
+static int com_lpl(int fd, __unused int argc, __unused char *argv[])
+{
+       unsigned i, loaded = 0;
+       char buf[_POSIX_PATH_MAX];
+       ssize_t ret;
+
+       PARA_DEBUG_LOG("freeing playlist (%d entries)\n", playlist_len);
+       for (i = 0; i < playlist_len; i++)
+               free(playlist[i]);
+       current_playlist_entry = 0;
+       playlist_len = 0;
+       ret = send_buffer(fd, AWAITING_DATA_MSG);
+       if (ret < 0)
+               return ret;
+again:
+       ret = recv_bin_buffer(fd, buf + loaded, sizeof(buf) - loaded);
+       if (ret < 0)
+               goto err_out;
+       if (!ret) {
+               PARA_DEBUG_LOG("loaded playlist (%d entries)\n", playlist_len);
+               return playlist_len;
+       }
+       loaded += ret;
+       loaded = for_each_line(buf, loaded, &playlist_add, 0);
+       if (loaded >= sizeof(buf))
+               goto err_out;
+       goto again;
+err_out:
+       return -E_LOAD_PLAYLIST;
+}
+
+static int com_ppl(int fd, __unused int argc, __unused char *argv[])
+{
+       unsigned i;
+
+       PARA_DEBUG_LOG("sending playlist (%d entries)\n", playlist_len);
+       for (i = 0; i < playlist_len; i++) {
+               int ret = send_buffer(fd, playlist[i]);
+               if (ret < 0)
+                       return ret;
+       }
+       return 1;
+}
+
+static char **plm_get_audio_file_list(unsigned int num)
+{
+       char **file_list;
+       unsigned i;
+
+       return NULL;
+       num = MIN(num, playlist_len);
+       if (!num)
+               return NULL;
+       file_list = para_malloc((num + 1) * sizeof(char *));
+       for (i = 0; i < num; i++) {
+               unsigned j = (current_playlist_entry + i) % playlist_len;
+               file_list[i] = para_strdup(playlist[j]);
+       }
+       file_list[i] = NULL;
+       return file_list;
+}
+
+static void plm_shutdown(void)
+{
+       /* free the playlist */
+}
+
+/**
+ *  the init function for the plm database tool
+ *
+ * Init all function pointers of \a db
+ *
+ * \sa struct dbtool, misc_meta_data::dbinfo, mysql.c random_dbtool.c
+ */
+int plm_dbtool_init(struct dbtool *db)
+{
+       playlist = para_calloc(100 * sizeof(char *)); /* guess 100 is enough */
+       playlist_size = 100;
+       sprintf(mmd->dbinfo, "plm initialized");
+       db->cmd_list = cmds;
+       db->get_audio_file_list = plm_get_audio_file_list;
+       db->shutdown = plm_shutdown;
+       return 1;
+}