This is a much cleaner approach and it simplifies afs.c a bit.
#include <fnmatch.h>
#include "server.cmdline.h"
#include "para.h"
+#include "string.h"
#include "afh.h"
#include "server.h"
#include "error.h"
#include "net.h"
#include "afs.h"
#include "ipc.h"
-#include "string.h"
#include "list.h"
#include "sched.h"
#include "signal.h"
NUM_AFS_TABLES
};
-static struct table_info afs_tables[NUM_AFS_TABLES];
+static struct afs_table afs_tables[NUM_AFS_TABLES] = {
+ [TBLNUM_AUDIO_FILES] = {.init = aft_init},
+ [TBLNUM_ATTRIBUTES] = {.init = attribute_init},
+ [TBLNUM_SCORES] = {.init = score_init},
+ [TBLNUM_MOODS] = {.init = moods_init},
+ [TBLNUM_LYRICS] = {.init = lyrics_init},
+ [TBLNUM_IMAGES] = {.init = images_init},
+ [TBLNUM_PLAYLIST] = {.init = playlists_init},
+};
struct command_task {
/** The file descriptor for the local socket. */
static enum play_mode init_admissible_files(void)
{
- int ret;
-
- if (conf.mood_given) {
- ret = change_current_mood(conf.mood_arg);
- if (ret >= 0) {
- if (conf.playlist_given)
- PARA_WARNING_LOG("ignoring playlist %s\n",
- conf.playlist_arg);
- return PLAY_MODE_MOOD;
+ int ret = 0;
+ char *arg = conf.afs_initial_mode_arg;
+
+ if (conf.afs_initial_mode_given) {
+ if (!strncmp(arg, "p:", 2)) {
+ ret = playlist_open(arg + 2);
+ if (ret >= 0)
+ return PLAY_MODE_PLAYLIST;
+ goto dummy;
}
+ if (!strncmp(arg, "m:", 2)) {
+ ret = change_current_mood(arg + 2);
+ if (ret >= 0)
+ return PLAY_MODE_MOOD;
+ goto dummy;
+ }
+ PARA_ERROR_LOG("bad afs initial mode arg: %s\n", arg);
}
- if (conf.playlist_given) {
- ret = playlist_open(conf.playlist_arg);
- if (ret >= 0)
- return PLAY_MODE_PLAYLIST;
- }
- ret = change_current_mood(NULL); /* open first available mood */
- if (ret >= 0)
- return PLAY_MODE_MOOD;
- change_current_mood(""); /* open dummy mood, always successful */
+dummy:
+ if (ret < 0)
+ PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
+ PARA_NOTICE_LOG("defaulting to dummy mood\n");
+ change_current_mood(""); /* always successful */
return PLAY_MODE_MOOD;
}
return ret;
}
-static void close_afs_tables(enum osl_close_flags flags)
+static void close_afs_tables(void)
{
+ int i;
PARA_NOTICE_LOG("closing afs_tables\n");
- score_shutdown(flags);
- attribute_shutdown(flags);
- close_current_mood();
- playlist_close();
- moods_shutdown(flags);
- playlists_shutdown(flags);
- lyrics_shutdown(flags);
- images_shutdown(flags);
- aft_shutdown(flags);
+ for (i = 0; i < NUM_AFS_TABLES; i++)
+ afs_tables[i].close();
}
static char *database_dir;
-static int make_database_dir(void)
+static void get_database_dir(void)
{
- int ret;
-
if (!database_dir) {
if (conf.afs_database_dir_given)
database_dir = para_strdup(conf.afs_database_dir_arg);
}
}
PARA_INFO_LOG("afs_database dir %s\n", database_dir);
+}
+
+static int make_database_dir(void)
+{
+ int ret;
+
+ get_database_dir();
ret = para_mkdir(database_dir, 0777);
if (ret >= 0 || is_errno(-ret, EEXIST))
return 1;
- free(database_dir);
- database_dir = NULL;
return ret;
}
-
static int open_afs_tables(void)
{
- int ret = make_database_dir();
+ int i, ret;
- if (ret < 0)
- return ret;
- ret = attribute_init(&afs_tables[TBLNUM_ATTRIBUTES], database_dir);
- if (ret < 0)
+ get_database_dir();
+ for (i = 0; i < NUM_AFS_TABLES; i++) {
+ ret = afs_tables[i].open(database_dir);
+ if (ret >= 0)
+ continue;
+ PARA_ERROR_LOG("%s init: %s\n", afs_tables[i].name,
+ PARA_STRERROR(-ret));
+ }
+ if (ret >= 0)
return ret;
- ret = moods_init(&afs_tables[TBLNUM_MOODS], database_dir);
- if (ret < 0)
- goto moods_init_error;
- ret = playlists_init(&afs_tables[TBLNUM_PLAYLIST], database_dir);
- if (ret < 0)
- goto playlists_init_error;
- ret = lyrics_init(&afs_tables[TBLNUM_LYRICS], database_dir);
- if (ret < 0)
- goto lyrics_init_error;
- ret = images_init(&afs_tables[TBLNUM_IMAGES], database_dir);
- if (ret < 0)
- goto images_init_error;
- ret = score_init(&afs_tables[TBLNUM_SCORES], database_dir);
- if (ret < 0)
- goto score_init_error;
- ret = aft_init(&afs_tables[TBLNUM_AUDIO_FILES], database_dir);
- if (ret < 0)
- goto aft_init_error;
- return 1;
-
-aft_init_error:
- score_shutdown(OSL_MARK_CLEAN);
-score_init_error:
- images_shutdown(OSL_MARK_CLEAN);
-images_init_error:
- lyrics_shutdown(OSL_MARK_CLEAN);
-lyrics_init_error:
- playlists_shutdown(OSL_MARK_CLEAN);
-playlists_init_error:
- moods_shutdown(OSL_MARK_CLEAN);
-moods_init_error:
- attribute_shutdown(OSL_MARK_CLEAN);
+ do
+ afs_tables[i].close();
+ while (i--);
return ret;
}
if (st->signum == SIGUSR1)
return; /* ignore SIGUSR1 */
if (st->signum == SIGHUP) {
- close_afs_tables(OSL_MARK_CLEAN);
+ close_afs_tables();
t->ret = open_afs_tables();
return;
}
{
enum play_mode current_play_mode;
struct sched s;
- int ret;
+ int i, ret;
INIT_LIST_HEAD(&afs_client_list);
+ for (i = 0; i < NUM_AFS_TABLES; i++)
+ afs_tables[i].init(&afs_tables[i]);
ret = open_afs_tables();
if (ret < 0) {
ret = sched(&s);
if (ret < 0)
PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
- close_afs_tables(OSL_MARK_CLEAN);
+ close_afs_tables();
exit(EXIT_FAILURE);
}
uint32_t table_mask = *(uint32_t *)query->data;
int i, ret;
- close_afs_tables(OSL_MARK_CLEAN);
+ close_afs_tables();
for (i = 0; i < NUM_AFS_TABLES; i++) {
- struct table_info *ti = afs_tables + i;
+ struct afs_table *t = &afs_tables[i];
- if (ti->flags & TBLFLAG_SKIP_CREATE)
- continue;
if (!(table_mask & (1 << i)))
continue;
- ret = osl_create_table(ti->desc);
+ if (!t->create)
+ continue;
+ ret = t->create(database_dir);
if (ret < 0)
return ret;
}
struct osl_object query = {.data = &table_mask,
.size = sizeof(table_mask)};
+ ret = make_database_dir();
+ if (ret < 0)
+ return ret;
if (argc != 1) {
table_mask = 0;
for (i = 1; i < argc; i++) {
for (j = 0; j < NUM_AFS_TABLES; j++) {
- struct table_info *ti = afs_tables + j;
+ struct afs_table *t = &afs_tables[j];
- if (ti->flags & TBLFLAG_SKIP_CREATE)
- continue;
- if (strcmp(argv[i], ti->desc->name))
+ if (strcmp(argv[i], t->name))
continue;
table_mask |= (1 << j);
break;
uint8_t audio_format_id;
};
-enum afs_table_flags {TBLFLAG_SKIP_CREATE};
+enum afs_events {
+ AFSI_CHANGE,
+ AFHI_CHANGE,
+ AUDIO_FILE_RENAME,
+ AUDIO_FILE_ADD,
+ AUDIO_FILE_REMOVE,
+ AUDIO_FILE_STREAMED,
+ ATTRIBUTE_ADD,
+ ATTRIBUTE_RENAME,
+ ATTRIBUTE_DELETE,
+ LYRICS_DELETE,
+ IMAGE_DELETE,
+ LYRICS_RENAME,
+ IMAGE_RENAME,
-struct table_info {
- const struct osl_table_description *desc;
- enum afs_table_flags flags;
+};
+
+struct afs_table {
+ void (*init)(struct afs_table *t);
+ const char *name;
+ int (*open)(const char *base_dir);
+ void (*close)(void);
+ int (*create)(const char *);
+ int (*event_handler)(enum afs_events event, struct para_buffer *pb, void *data);
+ /* int *(check)() */
};
enum play_mode {PLAY_MODE_MOOD, PLAY_MODE_PLAYLIST};
int for_each_matching_row(struct pattern_match_data *pmd);
/* score */
-int score_init(struct table_info *ti, const char *db);
-void score_shutdown(enum osl_close_flags flags);
+void score_init(struct afs_table *t);
int admissible_file_loop(void *data, osl_rbtree_loop_func *func);
int admissible_file_loop_reverse(void *data, osl_rbtree_loop_func *func);
int score_get_best(struct osl_row **aft_row, long *score);
int row_belongs_to_score_table(const struct osl_row *aft_row);
/* attribute */
-int attribute_init(struct table_info *ti, const char *db);
-void attribute_shutdown(enum osl_close_flags flags);
+void attribute_init(struct afs_table *t);
void get_attribute_bitmap(const uint64_t *atts, char *buf); /* needed by com_ls() */
int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum);
int get_attribute_text(uint64_t *atts, const char *delim, char **text);
/* aft */
-int aft_init(struct table_info *ti, const char *db);
-void aft_shutdown(enum osl_close_flags flags);
+void aft_init(struct afs_table *t);
int aft_get_row_of_path(const char *path, struct osl_row **row);
int open_and_update_audio_file(struct osl_row *aft_row, struct audio_file_data *afd);
int load_afsi(struct afs_info *afsi, struct osl_object *obj);
/* mood */
int change_current_mood(char *mood_name);
-void close_current_mood(void);
int mood_update_audio_file(const struct osl_row *aft_row, struct afs_info *old_afsi);
int reload_current_mood(void);
int mood_delete_audio_file(const struct osl_row *aft_row);
/* playlist */
int playlist_open(char *name);
-void playlist_close(void);
int playlist_update_audio_file(struct osl_row *aft_row);
int playlist_check_callback(__a_unused const struct osl_object *query,
struct osl_object *result);
#define DECLARE_BLOB_SYMBOLS(table_name, cmd_prefix) \
- int table_name ## _init(struct table_info *ti, const char *db); \
- void table_name ## _shutdown(enum osl_close_flags flags); \
+ void table_name ## _init(struct afs_table *t); \
int cmd_prefix ## _get_name_by_id(uint32_t id, char **name); \
int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def); \
int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
#include "para.h"
#include "error.h"
+#include "string.h"
#include <sys/mman.h>
#include <fnmatch.h>
#include "afh.h"
#include "afs.h"
#include "net.h"
-#include "string.h"
#include "vss.h"
static struct osl_table *audio_file_table;
*
* \sa osl_close_table().
*/
-void aft_shutdown(enum osl_close_flags flags)
+static void aft_close(void)
{
- osl_close_table(audio_file_table, flags);
+ osl_close_table(audio_file_table, OSL_MARK_CLEAN);
audio_file_table = NULL;
}
/**
* Open the audio file table.
*
- * \param ti Gets initialized by this function.
- * \param db The database directory.
+ * \param dir The database directory.
*
- * \return Positive on success, negative on errors.
+ * \return Standard.
*
* \sa osl_open_table().
*/
-int aft_init(struct table_info *ti, const char *db)
+static int aft_open(const char *dir)
{
int ret;
- audio_file_table_desc.dir = db;
- ti->desc = &audio_file_table_desc;
- ret = osl_open_table(ti->desc, &audio_file_table);
+ audio_file_table_desc.dir = dir;
+ ret = osl_open_table(&audio_file_table_desc, &audio_file_table);
if (ret >= 0) {
unsigned num;
osl_get_num_rows(audio_file_table, &num);
return 1;
return ret;
}
+
+static int aft_create(const char *dir)
+{
+ audio_file_table_desc.dir = dir;
+ return osl_create_table(&audio_file_table_desc);
+}
+
+void aft_init(struct afs_table *t)
+{
+ t->name = audio_file_table_desc.name;
+ t->open = aft_open;
+ t->close = aft_close;
+ t->create = aft_create;
+}
/** \file attribute.c Attribute handling functions. */
#include "para.h"
#include "error.h"
+#include "string.h"
#include "afh.h"
#include "afs.h"
-#include "string.h"
#include "net.h"
static struct osl_table *attribute_table;
*
* \sa osl_close_table().
*/
-void attribute_shutdown(enum osl_close_flags flags)
+void attribute_close(void)
{
- osl_close_table(attribute_table, flags);
+ osl_close_table(attribute_table, OSL_MARK_CLEAN);
attribute_table = NULL;
}
*
* \sa osl_open_table().
*/
-int attribute_init(struct table_info *ti, const char *db)
+static int attribute_open(const char *dir)
{
int ret;
- attribute_table_desc.dir = db;
- ti->desc = &attribute_table_desc;
- ret = osl_open_table(ti->desc, &attribute_table);
+ attribute_table_desc.dir = dir;
+ ret = osl_open_table(&attribute_table_desc, &attribute_table);
greatest_att_bitnum = -1; /* no atts available */
if (ret >= 0) {
find_greatest_att_bitnum();
return 1;
return ret;
}
+
+static int attribute_create(const char *dir)
+{
+ attribute_table_desc.dir = dir;
+ return osl_create_table(&attribute_table_desc);
+}
+
+
+void attribute_init(struct afs_table *t)
+{
+ t->name = attribute_table_desc.name;
+ t->open = attribute_open;
+ t->close = attribute_close;
+ t->create = attribute_create;
+}
#include <fnmatch.h>
#include "para.h"
#include "error.h"
+#include "string.h"
#include "afh.h"
#include "afs.h"
-#include "string.h"
#include "net.h"
static struct osl_column_description blob_cols[] = {
row, name, def); \
}
-/** Define the \p shutdown function for this blob type. */
-#define DEFINE_BLOB_SHUTDOWN(table_name) \
- void table_name ## _shutdown(enum osl_close_flags flags) \
+/** Define the \p close function for this blob type. */
+#define DEFINE_BLOB_CLOSE(table_name) \
+ void table_name ## _close(void) \
{ \
- osl_close_table(table_name ## _table, flags); \
+ osl_close_table(table_name ## _table, OSL_MARK_CLEAN); \
table_name ## _table = NULL; \
}
-static int blob_init(struct osl_table **table,
+/** Define the \p create function for this blob type. */
+#define DEFINE_BLOB_CREATE(table_name) \
+ int table_name ## _create(const char *dir) \
+ { \
+ table_name ## _table_desc.dir = dir; \
+ return osl_create_table(&table_name ## _table_desc); \
+ }
+
+static int blob_open(struct osl_table **table,
struct osl_table_description *desc,
- struct table_info *ti, const char *db)
+ const char *dir)
{
int ret;
- desc->dir = db;
- ti->desc = desc;
- ret = osl_open_table(ti->desc, table);
+ desc->dir = dir;
+ ret = osl_open_table(desc, table);
if (ret >= 0)
return ret;
*table = NULL;
- if (ret >= 0 || is_errno(-ret, -ENOENT))
+ if (ret >= 0 || is_errno(-ret, ENOENT))
return 1;
return ret;
}
+#define DEFINE_BLOB_OPEN(table_name) \
+ int table_name ## _open(const char *dir) \
+ { \
+ return blob_open(&table_name ## _table, \
+ &table_name ## _table_desc, dir); \
+ }
+
+
/** Define the \p init function for this blob type. */
#define DEFINE_BLOB_INIT(table_name) \
- int table_name ## _init(struct table_info *ti, const char *db) \
+ void table_name ## _init(struct afs_table *t) \
{ \
- return blob_init(&table_name ## _table, \
- &table_name ## _table_desc, ti, db); \
+ t->name = table_name ## _table_desc.name; \
+ t->open = table_name ## _open; \
+ t->close = table_name ## _close; \
+ t->create = table_name ## _create;\
}
/** Define all functions for this blob type. */
#define DEFINE_BLOB_FUNCTIONS(table_name, cmd_prefix) \
+ DEFINE_BLOB_OPEN(table_name) \
+ DEFINE_BLOB_CLOSE(table_name) \
+ DEFINE_BLOB_CREATE(table_name) \
+ DEFINE_BLOB_INIT(table_name) \
DEFINE_BLOB_COMMAND(ls, table_name, cmd_prefix) \
DEFINE_BLOB_COMMAND(cat, table_name, cmd_prefix) \
DEFINE_BLOB_COMMAND(add, table_name, cmd_prefix) \
DEFINE_GET_NAME_BY_ID(table_name, cmd_prefix); \
DEFINE_GET_DEF_BY_ID(table_name, cmd_prefix); \
DEFINE_GET_NAME_AND_DEF_BY_ROW(table_name, cmd_prefix); \
- DEFINE_BLOB_SHUTDOWN(table_name); \
- DEFINE_BLOB_INIT(table_name);
/** \cond doxygen isn't smart enough to recognize these */
DEFINE_BLOB_FUNCTIONS(lyrics, lyr);
#include <fnmatch.h>
#include "para.h"
#include "error.h"
+#include "string.h"
#include "afh.h"
#include "afs.h"
#include "list.h"
-#include "string.h"
/**
* Contains statistical data of the currently admissible audio files.
* Free all resources of the current mood which were allocated during
* mood_open().
*/
-void close_current_mood(void)
+static void close_current_mood(void)
{
destroy_mood(current_mood);
current_mood = NULL;
if (!current_mood)
return 1;
- score_shutdown(0);
+// score_close(0);
mood_name = para_strdup(current_mood->name);
close_current_mood();
ret = change_current_mood(mood_name);
#include "para.h"
#include "error.h"
+#include "string.h"
#include "afh.h"
#include "afs.h"
-#include "string.h"
/** \file playlist.c Functions for loading and saving playlists. */
/** \file score.c Scoring functions to determine the audio file streaming order. */
#include "para.h"
#include "error.h"
+#include "string.h"
#include "afh.h"
#include "afs.h"
#include "list.h"
-#include "string.h"
static struct osl_table *score_table;
return ret;
}
-/**
- * Close the score table.
- *
- * \param flags Options passed to osl_close_table().
- *
- * \sa score_init().
- */
-void score_shutdown(enum osl_close_flags flags)
+/* Close the score table. */
+static void score_close(void)
{
- osl_close_table(score_table, flags | OSL_FREE_VOLATILE);
+ osl_close_table(score_table, OSL_FREE_VOLATILE);
score_table = NULL;
}
/**
- * Init the scoring subsystem.
+ * Open the score table.
*
- * \param ti Gets filled in by the function.
- * \param db The database directory.
+ * \param dir The database directory.
*
* \return The return value of the underlying call to osl_open_table().
*
* \sa score_shutdown().
*/
-int score_init(struct table_info *ti, const char *db)
+static int score_open(const char *dir)
+{
+ score_table_desc.dir = dir;
+ return osl_open_table(&score_table_desc, &score_table);
+}
+
+/**
+ * Initialize the scoring subsystem.
+ *
+ * \param t Members Gets filled in by the function.
+ */
+void score_init(struct afs_table *t)
{
- score_table_desc.dir = db;
- ti->desc = &score_table_desc;
- ti->flags = TBLFLAG_SKIP_CREATE;
- return osl_open_table(ti->desc, &score_table);
+ t->name = score_table_desc.name;
+ t->open = score_open;
+ t->close = score_close;
}
default="/var/paraslash/afs_command_socket"
optional
-option "mood" m
-#~~~~~~~~~~~~~~
-
-"Mood to load on startup."
-
- string typestr="mood_name"
- optional
-
-option "playlist" P
-#~~~~~~~~~~~~~~
+option "afs_initial_mode" i
+#~~~~~~~~~~~~~~~~~~~~~~~~~~
-"Playlist to load on startup. Ignored if
---mood is given as well."
+"Mood or playlist to load on startup. Must be
+prefixed with either 'p:' or 'm:' to indicate
+whether a playlist or a mood should be
+loaded. Example:
+ --afs_initial_mode p:foo
+loads the playlist named 'foo'."
- string typestr="playlist_name"
+ string typestr="name"
optional
section "mysql selector"