/*
- * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2007-2013 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.
+ * Licensed under the GPL v2. For licencing details see COPYING.
*/
-/** \file afs.h data structures common to all audio file selectors */
+/** \file afs.h Exported symbols of the audio file selector. */
-#include <sys/select.h>
+#include <regex.h>
-
-int find_audio_files(const char *dirname, int (*f)(const char *, const char *));
+/** Audio file selector data stored in the audio file table. */
+struct afs_info {
+ /** Seconds since the epoch. */
+ uint64_t last_played;
+ /** Bit field of set attributes. */
+ uint64_t attributes;
+ /** Counts how many times the file was selected. */
+ uint32_t num_played;
+ /** Image blob associated with this file (foreign key). */
+ uint32_t image_id;
+ /** Lyrics blob associated with this file (foreign key). */
+ uint32_t lyrics_id;
+ /** Mp3, ogg, aac, wma, spx. */
+ uint8_t audio_format_id;
+ /** Amplification value. */
+ uint8_t amp;
+};
/**
- * describes one supported audio file selector
- *
- * There is one such struct for each supported selector. During the startup
- * part of para_server the \a init() function of the activated selector gets
- * called which fills in all other function pointers.
+ * Events caused by changes to an afs table.
*
+ * Whenever an afs table changes, an event is generated which causes afs to
+ * call the event handlers of all other tables. For example, if an audio file
+ * is added, the event handler of the mood table checks the new file for
+ * admissibility.
*/
-struct audio_file_selector {
+enum afs_events {
+ /** An attribute was added. */
+ ATTRIBUTE_ADD,
+ /** An attribute was renamed. */
+ ATTRIBUTE_RENAME,
+ /** An attribute was removed. */
+ ATTRIBUTE_REMOVE,
+ /** The afs info struct of an audio file changed. */
+ AFSI_CHANGE,
+ /** The afh info struct of an audio file changed. */
+ AFHI_CHANGE,
+ /** An audio file was renamed. */
+ AUDIO_FILE_RENAME,
+ /** An audio file was added. */
+ AUDIO_FILE_ADD,
+ /** An audio file is about to be removed. */
+ AUDIO_FILE_REMOVE,
+ /** A new blob was added. */
+ BLOB_ADD,
+ /** A blob was renamed. */
+ BLOB_RENAME,
+ /** A blob is about to be removed. */
+ BLOB_REMOVE,
+};
+
/**
- * name name of this selector
+ * Used as data for \ref afs_event() for events of type \p ATTRIBUTE_ADD.
*/
-const char *name;
+struct rmatt_event_data {
+ /** The name of the attribute being added. */
+ const char *name;
+ /** Its bit number. */
+ unsigned char bitnum;
+};
+
/**
- * the init routine of the selector
- *
- * It should check its command line options and do all necessary initialization
- * like connecting to a database server.
- *
- * A negative return value indicates an initialization error and means that
- * this selector should be ignored for now (it may later be activated again via
- * the chs command).
- *
- * If \a init() returns success (non-negative return value), it must have
- * initialized in all non-optional function pointers of the given selector
- * struct. Moreover, \a cmd_list must point to a NULL-terminated array which
- * holds the list of all commands that are supported by this selector.
+ * Used as data for \ref afs_event() for events of type \p ATTRIBUTE_AFSI_CHANGE.
*/
-int (*init)(struct audio_file_selector *self);
+struct afsi_change_event_data {
+ /** Pointer to the row that has changed. */
+ struct osl_row *aft_row;
+ /** Afs info before the change. */
+ struct afs_info *old_afsi;
+};
+
+/** Function pointers for table handling. */
+struct afs_table {
+ /** Initializes the other pointers in this struct. */
+ void (*init)(struct afs_table *t);
+ /** The name of this table. */
+ const char *name;
+ /** Gets called on startup and on \p SIGHUP. */
+ int (*open)(const char *base_dir);
+ /** Gets called on shutdown and on \p SIGHUP. */
+ void (*close)(void);
+ /** Called by the \a init afs command. */
+ int (*create)(const char *);
+ /** Handles afs events. */
+ int (*event_handler)(enum afs_events event, struct para_buffer *pb,
+ void *data);
+};
+
+/** How audio files are selected by afs. */
+enum play_mode {
+ /** Admissible files are determined by a mood definition. */
+ PLAY_MODE_MOOD,
+ /** All listed files are admissible. */
+ PLAY_MODE_PLAYLIST,
+};
+
/**
- * list of commands supported by this selector
+ * Data about one audio file.
+ *
+ * Needed to produce ls and stat output.
*/
-struct server_command *cmd_list;
+struct ls_data {
+ /** Usual audio format handler information. */
+ struct afh_info afhi;
+ /** Audio file selector information. */
+ struct afs_info afsi;
+ /** The full path of the audio file. */
+ char *path;
+ /** The score value (if -a was given). */
+ long score;
+ /** The hash value of audio file data. */
+ unsigned char *hash;
+};
+
+/** Data about the current audio file, passed from afs to server. */
+struct audio_file_data {
+ /** The open file descriptor to the current audio file. */
+ int fd;
+ /** Vss needs this for streaming. */
+ struct afh_info afhi;
+ /** Size of the largest chunk. */
+ uint32_t max_chunk_size;
+ /** Needed to get the audio file header. */
+ uint8_t audio_format_id;
+};
+
/**
- * pointer to function returning list of at most \a num audio files to be
- * streamed next
- *
- * \a get_audio_file_list() must return a pointer to a array of at most \a num
- * char* pointers (terminated by a NULL pointer), or NULL on errors. Both the
- * array and its contents must be dynamically allocated and are freed by the
- * caller.
+ * Codes used for communication between the server and the afs process.
*
-*/
-char **(*get_audio_file_list)(unsigned int num);
+ * Before forking the afs child, para_server creates a bidirectional pipe
+ * through which both processes communicate. Usually para_server requests a new
+ * audio in order to start streaming or when the end of the current audio file
+ * has been reached. The afs process responds to such a request by sending
+ * back an eight byte buffer. The first four bytes is the uint32_t
+ * representation of the code, usually \p NEXT_AUDIO_FILE if an admissible
+ * audio file was found, successfully opened and verified. The other four bytes
+ * represent the shared memory id of the shared memory area that contains
+ * details about the audio file to be streamed next. The open file descriptor
+ * of that file is also passed from afs to para_server through the same pipe.
+ */
+enum afs_server_code {
+ /** An audio file was successfully opened. */
+ NEXT_AUDIO_FILE,
+ /** No admissible audio file was found. */
+ NO_ADMISSIBLE_FILES,
+};
+
+/** Flags passed to for_each_matching_row(). */
+enum pattern_match_flags {
+ /** Loop in reverse order. */
+ PM_REVERSE_LOOP = 1,
+ /** If no pattern is given, loop over all rows. */
+ PM_NO_PATTERN_MATCHES_EVERYTHING = 2,
+ /** If the data in match_column is the empty string, skip this row. */
+ PM_SKIP_EMPTY_NAME = 4,
+};
+
+/** Structure passed to for_each_matching_row(). */
+struct pattern_match_data {
+ /** Loop over all rows in this table. */
+ struct osl_table *table;
+ /** Determines the loop order. Must be an rbtree column. */
+ unsigned loop_col_num;
+ /** Data from this column is matched against the given patterns. */
+ unsigned match_col_num;
+ /** \see pattern_match_flags. */
+ unsigned pm_flags;
+ /** This value is passed verbatim to fnmatch(). */
+ int fnmatch_flags;
+ /** Null-terminated array of patterns. */
+ struct osl_object patterns;
+ /** Data pointer passed to the action function. */
+ void *data;
+ /** Gets increased by one for each match. */
+ unsigned num_matches;
+ /** For each matching row, this function will be called. */
+ int (*action)(struct osl_table *table, struct osl_row *row, const char *name, void *data);
+};
+
+
/**
+ * Afs command handlers run as a process which is not related to the afs
+ * process, i.e. they can not change the address space of afs directly.
+ * Therefore afs commands typically consist of two functions: The command
+ * handler and the corresponding callback function that runs in afs context.
*
- * the update hook
- *
- * The \a update_audio_file pointer is optional and need not be supplied. In this
- * case it is not neccessary to init this pointer from within init(). If
- * \a update_audio_file is non-NULL, the function it points to gets called
- * whenever a new audio file was successfully loaded and is going to be
- * streamed by any of paraslash's senders. The full path of the audio file is
- * passed \a update_audio_file().
- *
+ * \sa send_callback_request().
*/
-void (*update_audio_file)(char *audio_file);
+typedef void callback_function(int fd, const struct osl_object *);
+
/**
+ * Callbacks send chunks to data back to the command handler. Pointers to
+ * this type of function are used by \ref send_callback_request and friends
+ * to deal with the data in the command handler process.
*
- * shutdown this selector and free all resources
- *
- * This gets called whenever the audio file selector changes. The reason for
- * this change might be that some user sent the chs command, that para_server
- * receives the HUP signal, or that para_server shuts down. It is assumed to
- * succeed.
+ * \sa \ref send_callback_request().
*/
-void (*shutdown)(void);
+typedef int callback_result_handler(struct osl_object *result, uint8_t band, void *private);
+int afs_cb_result_handler(struct osl_object *result, uint8_t band, void *private);
+int pass_buffer_as_shm(int fd, uint8_t band, char *buf, size_t size);
+
+/** Structure passed to the AFS max_size handler. */
+struct afs_max_size_handler_data {
+ /** Local socket connecting the command handler and the AFS process. */
+ int fd;
+ /** The sideband designator for this data packet. */
+ uint8_t band;
+};
+
/**
+ * Standard max_size handler for AFS commands.
*
- * add file descriptors to fd_sets
- *
- * The pre_select function of the activated selector gets called just before
- * para_server enters its main select loop. The selector may add its own file
- * descriptors to the \a rfds or the \a wfds set.
+ * \param buf Contains (part of) the AFS command output.
+ * \param size The number of bytes in \a buf.
+ * \param private Pointer to a \ref afs_max_size_handler_data structure.
*
- * \return The highest-numbered file descriptor which was added to either of
- * the two fd sets (or -1 if no file descriptors were added).
+ * Whenever the output of an AFS command exceeds the maximal size of a shared
+ * memory area, the max size handler of the para_buffer which holds the command
+ * output is called with \a private being a pointer to a structure of type
+ * afs_max_size_handler_data.
*
- * \sa select(2)
+ * \return The return value of the underlying call to \ref
+ * pass_buffer_as_shm().
*/
-int (*pre_select)(fd_set *rfds, fd_set *wfds);
-/**
- * handle the file descriptors which are ready for I/O
- *
- * If the pre_select hook added one ore more file descriptors to the read or write
- * set, this is the hook to check the result and do any I/O on those descriptors
- * which are ready for reading/writing.
- */
-void (*post_select)(fd_set *rfds, fd_set *wfds);
-/**
- * each selector has its private data pointer */
-void *private_data;
-};
+_static_inline_ int afs_max_size_handler(char *buf, size_t size, void *private)
+{
+ struct afs_max_size_handler_data *amshd = private;
+ return pass_buffer_as_shm(amshd->fd, amshd->band, buf, size);
+}
+
+__noreturn void afs_init(uint32_t cookie, int socket_fd);
+void afs_event(enum afs_events event, struct para_buffer *pb,
+ void *data);
+int send_callback_request(callback_function *f, struct osl_object *query,
+ callback_result_handler *result_handler,
+ void *private_result_data);
+int send_option_arg_callback_request(struct osl_object *options,
+ int argc, char * const * const argv, callback_function *f,
+ callback_result_handler *result_handler,
+ void *private_result_data);
+int send_standard_callback_request(int argc, char * const * const argv,
+ callback_function *f, callback_result_handler *result_handler,
+ void *private_result_data);
+int string_compare(const struct osl_object *obj1, const struct osl_object *obj2);
+int for_each_matching_row(struct pattern_match_data *pmd);
+
+/* score */
+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 get_score_and_aft_row(struct osl_row *score_row, long *score, struct osl_row **aft_row);
+int score_add(const struct osl_row *row, long score);
+int score_update(const struct osl_row *aft_row, long new_score);
+int get_num_admissible_files(unsigned *num);
+int score_delete(const struct osl_row *aft_row);
+int clear_score_table(void);
+int row_belongs_to_score_table(const struct osl_row *aft_row, unsigned *rank);
+
+/* attribute */
+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);
-int mysql_selector_init(struct audio_file_selector*);
-int playlist_selector_init(struct audio_file_selector*);
-int random_selector_init(struct audio_file_selector*);
+/* aft */
+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, long score,
+ struct audio_file_data *afd);
+int load_afd(int shmid, struct audio_file_data *afd);
+int load_afsi(struct afs_info *afsi, struct osl_object *obj);
+void save_afsi(struct afs_info *afsi, struct osl_object *obj);
+int get_afsi_of_row(const struct osl_row *row, struct afs_info *afsi);
+int get_afhi_of_row(const struct osl_row *row, struct afh_info *afhi);
+int get_afsi_of_path(const char *path, struct afs_info *afsi);
+int get_audio_file_path_of_row(const struct osl_row *row, char **path);
+int get_afsi_object_of_row(const struct osl_row *row, struct osl_object *obj);
+int audio_file_loop(void *private_data, osl_rbtree_loop_func *func);
+void aft_check_callback(int fd, __a_unused const struct osl_object *query);
+/* playlist */
+int playlist_open(char *name);
+void playlist_close(void);
+void playlist_check_callback(int fd, __a_unused const struct osl_object *query);
+
+/** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
+#define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
+
+
+/** Define exported functions and a table pointer for an osl blob table. */
+#define DECLARE_BLOB_SYMBOLS(table_name, cmd_prefix) \
+ 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_def_by_name(char *name, struct osl_object *def); \
+ int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
+ char **name, struct osl_object *def); \
+ int table_name ##_event_handler(enum afs_events event, \
+ struct para_buffer *pb, void *data); \
+ extern struct osl_table *table_name ## _table;
+
+DECLARE_BLOB_SYMBOLS(lyrics, lyr);
+DECLARE_BLOB_SYMBOLS(images, img);
+DECLARE_BLOB_SYMBOLS(moods, mood);
+DECLARE_BLOB_SYMBOLS(playlists, pl);
+
+/** The columns of an abstract blob table. */
+enum blob_table_columns {
+ /** The identifier, a positive integer that never repeats. */
+ BLOBCOL_ID,
+ /** The unique name of the blob. */
+ BLOBCOL_NAME,
+ /** The actual blob contents. */
+ BLOBCOL_DEF,
+ /** A blob table has that many columns. */
+ NUM_BLOB_COLUMNS
+};