struct task task;
};
+extern struct misc_meta_data *mmd;
+
static int server_socket;
static struct command_task command_task_struct;
static struct signal_task signal_task_struct;
+static enum play_mode current_play_mode;
+static char *current_mop; /* mode or playlist specifier. NULL means dummy mooe */
+
/**
* A random number used to "authenticate" the connection.
* \param result Callback result will be stored here.
*
* This function creates a shared memory area, copies the buffer pointed to by
- * \a buf to that area and notifies the afs process that \a f should be
+ * query to that area and notifies the afs process that \a f should be
* called ASAP.
*
* \return Negative, on errors, the return value of the callback function
return ret;
}
-int pass_afd(int fd, char *buf, size_t size)
+static int pass_afd(int fd, char *buf, size_t size)
{
struct msghdr msg = {.msg_iov = NULL};
struct cmsghdr *cmsg;
/**
* Open the audio file with highest score.
*
- * \param afd Audio file data is returned here.
- *
- * This stores all information for streaming the "best" audio file
- * in the \a afd structure.
+ * This stores all information for streaming the "best" audio file in a shared
+ * memory area. The id of that area and an open file descriptor for the next
+ * audio file are passed to the server process.
*
- * \return Positive on success, negative on errors.
+ * \return Standard.
*
- * \sa close_audio_file(), open_and_update_audio_file().
+ * \sa open_and_update_audio_file().
*/
int open_next_audio_file(void)
{
*(uint32_t *)buf = NEXT_AUDIO_FILE;
*(uint32_t *)(buf + 4) = (uint32_t)shmid;
ret = pass_afd(afd.fd, buf, 8);
+ close(afd.fd);
if (ret >= 0)
return ret;
PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
return ret;
}
-static enum play_mode init_admissible_files(void)
+/* Never fails if arg == NULL */
+static int activate_mood_or_playlist(char *arg, int *num_admissible)
{
- int ret = 0;
- char *arg = conf.afs_initial_mode_arg;
+ enum play_mode mode;
+ int ret;
- if (conf.afs_initial_mode_given) {
+ if (!arg) {
+ ret = change_current_mood(NULL); /* always successful */
+ mode = PLAY_MODE_MOOD;
+ } else {
if (!strncmp(arg, "p:", 2)) {
ret = playlist_open(arg + 2);
- if (ret >= 0)
- return PLAY_MODE_PLAYLIST;
- goto dummy;
- }
- if (!strncmp(arg, "m:", 2)) {
+ mode = PLAY_MODE_PLAYLIST;
+ } else if (!strncmp(arg, "m:", 2)) {
ret = change_current_mood(arg + 2);
- if (ret >= 0)
- return PLAY_MODE_MOOD;
- goto dummy;
+ mode = PLAY_MODE_MOOD;
+ } else
+ ret = -E_AFS_SYNTAX;
+ if (ret < 0)
+ return ret;
+ }
+ if (num_admissible)
+ *num_admissible = ret;
+ current_play_mode = mode;
+ if (arg != current_mop) {
+ free(current_mop);
+ if (arg) {
+ current_mop = para_strdup(arg);
+ mmd_lock();
+ strcpy(mmd->afs_mode_string, arg); /* FIXME: check length */
+ mmd_unlock();
+ } else {
+ mmd_lock();
+ strcpy(mmd->afs_mode_string, "dummy");
+ mmd_unlock();
+ current_mop = NULL;
}
- PARA_ERROR_LOG("bad afs initial mode arg: %s\n", arg);
}
-dummy:
+ return 1;
+}
+
+static int com_select_callback(const struct osl_object *query,
+ struct osl_object *result)
+{
+ struct para_buffer pb = {.buf = NULL};
+ char *arg = query->data;
+ int num_admissible, ret;
+
+ ret = clear_score_table();
if (ret < 0)
- PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
+ return ret;
+ if (current_play_mode == PLAY_MODE_MOOD)
+ close_current_mood();
+ else
+ playlist_close();
+ ret = activate_mood_or_playlist(arg, &num_admissible);
+ if (ret < 0) {
+ para_printf(&pb, "%s\n", PARA_STRERROR(-ret));
+ para_printf(&pb, "switching back to %s\n", current_mop?
+ current_mop : "dummy");
+ ret = activate_mood_or_playlist(current_mop, &num_admissible);
+ if (ret < 0) {
+ para_printf(&pb, "failed, switching to dummy\n");
+ activate_mood_or_playlist(NULL, &num_admissible);
+ }
+ }
+ para_printf(&pb, "activated %s (%d admissible files)\n", current_mop?
+ current_mop : "dummy mood", num_admissible);
+ result->data = pb.buf;
+ result->size = pb.size;
+ return 1;
+}
+
+int com_select(int fd, int argc, char * const * const argv)
+{
+ int ret;
+ struct osl_object query, result;
+
+ if (argc != 2)
+ return -E_AFS_SYNTAX;
+ query.data = argv[1];
+ query.size = strlen(argv[1]) + 1;
+ ret = send_callback_request(com_select_callback, &query,
+ &result);
+ if (ret > 0 && result.data && result.size) {
+ ret = send_va_buffer(fd, "%s", (char *)result.data);
+ free(result.data);
+ }
+ return ret;
+}
+
+static void init_admissible_files(void)
+{
+ int ret = 0;
+ char *arg = conf.afs_initial_mode_arg;
+ ret = activate_mood_or_playlist(arg, NULL);
+ if (ret >= 0)
+ return;
+ 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;
+ activate_mood_or_playlist(NULL, NULL); /* always successful */
}
static int setup_command_socket_or_die(void)
continue;
PARA_ERROR_LOG("%s init: %s\n", afs_tables[i].name,
PARA_STRERROR(-ret));
+ break;
}
if (ret >= 0)
return ret;
- do
- afs_tables[i].close();
- while (i--);
+ while (i)
+ afs_tables[--i].close();
return ret;
}
*/
__noreturn void afs_init(uint32_t cookie, int socket_fd)
{
- enum play_mode current_play_mode;
struct sched s;
int i, ret;
exit(EXIT_FAILURE);
PARA_INFO_LOG("server_socket: %d, afs_socket_cookie: %u\n",
server_socket, (unsigned) cookie);
- current_play_mode = init_admissible_files();
+ init_admissible_files();
register_tasks(cookie);
s.default_timeout.tv_sec = 0;
s.default_timeout.tv_usec = 99 * 1000;