mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-30 10:33:13 +00:00
Break res_stasis into smaller files.
When implementing playback for stasis-http, the monolithicedness of res_stasis really started to get in my way. This patch breaks the major components of res_stasis.c into individual files. * res/stasis/app.c - Stasis application tracking * res/stasis/control.c - Channel control objects * res/stasis/command.c - Channel command object This refactoring also allows res_stasis applications to be loaded as independent modules, such as the new res_stasis_answer module. The bulk of this patch is simply moving code from one file to another, adjusting names and adding accessors as necessary. Review: https://reviewboard.asterisk.org/r/2530/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@388729 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
@@ -35,7 +35,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|||||||
#include "asterisk/app.h"
|
#include "asterisk/app.h"
|
||||||
#include "asterisk/module.h"
|
#include "asterisk/module.h"
|
||||||
#include "asterisk/stasis.h"
|
#include "asterisk/stasis.h"
|
||||||
#include "asterisk/stasis_app.h"
|
#include "asterisk/stasis_app_impl.h"
|
||||||
|
|
||||||
/*** DOCUMENTATION
|
/*** DOCUMENTATION
|
||||||
<application name="Stasis" language="en_US">
|
<application name="Stasis" language="en_US">
|
||||||
|
@@ -478,6 +478,9 @@ int ast_register_application2(const char *app, int (*execute)(struct ast_channel
|
|||||||
*/
|
*/
|
||||||
int ast_unregister_application(const char *app);
|
int ast_unregister_application(const char *app);
|
||||||
|
|
||||||
|
/*! Macro to safely ref and unref the self module for the current scope */
|
||||||
|
#define SCOPED_MODULE_USE(module) \
|
||||||
|
RAII_VAR(struct ast_module *, __self__ ## __LINE__, ast_module_ref(module), ast_module_unref)
|
||||||
|
|
||||||
#if defined(__cplusplus) || defined(c_plusplus)
|
#if defined(__cplusplus) || defined(c_plusplus)
|
||||||
}
|
}
|
||||||
|
@@ -53,27 +53,6 @@
|
|||||||
#include "asterisk/channel.h"
|
#include "asterisk/channel.h"
|
||||||
#include "asterisk/json.h"
|
#include "asterisk/json.h"
|
||||||
|
|
||||||
struct ast_channel_snapshot;
|
|
||||||
|
|
||||||
/*! @{ */
|
|
||||||
|
|
||||||
/*!
|
|
||||||
* \brief Control a channel using \c stasis_app.
|
|
||||||
*
|
|
||||||
* This function blocks until the channel hangs up, or
|
|
||||||
* stasis_app_control_continue() is called on the channel's \ref
|
|
||||||
* stasis_app_control struct.
|
|
||||||
*
|
|
||||||
* \param chan Channel to control with Stasis.
|
|
||||||
* \param app_name Application controlling the channel.
|
|
||||||
* \param argc Number of arguments for the application.
|
|
||||||
* \param argv Arguments for the application.
|
|
||||||
*/
|
|
||||||
int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
|
|
||||||
char *argv[]);
|
|
||||||
|
|
||||||
/*! @} */
|
|
||||||
|
|
||||||
/*! @{ */
|
/*! @{ */
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -87,7 +66,7 @@ int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
|
|||||||
* \param message Message to handle. (borrowed copy)
|
* \param message Message to handle. (borrowed copy)
|
||||||
*/
|
*/
|
||||||
typedef void (*stasis_app_cb)(void *data, const char *app_name,
|
typedef void (*stasis_app_cb)(void *data, const char *app_name,
|
||||||
struct ast_json *message);
|
struct ast_json *message);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Register a new Stasis application.
|
* \brief Register a new Stasis application.
|
||||||
@@ -147,6 +126,16 @@ struct stasis_app_control *stasis_app_control_find_by_channel(
|
|||||||
struct stasis_app_control *stasis_app_control_find_by_channel_id(
|
struct stasis_app_control *stasis_app_control_find_by_channel_id(
|
||||||
const char *channel_id);
|
const char *channel_id);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Returns the uniqueid of the channel associated with this control
|
||||||
|
*
|
||||||
|
* \param control Control object.
|
||||||
|
* \return Uniqueid of the associate channel.
|
||||||
|
* \return \c NULL if \a control is \c NULL.
|
||||||
|
*/
|
||||||
|
const char *stasis_app_control_get_channel_id(
|
||||||
|
const struct stasis_app_control *control);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Exit \c res_stasis and continue execution in the dialplan.
|
* \brief Exit \c res_stasis and continue execution in the dialplan.
|
||||||
*
|
*
|
||||||
@@ -160,20 +149,30 @@ void stasis_app_control_continue(struct stasis_app_control *control);
|
|||||||
* \brief Answer the channel associated with this control.
|
* \brief Answer the channel associated with this control.
|
||||||
* \param control Control for \c res_stasis.
|
* \param control Control for \c res_stasis.
|
||||||
* \return 0 for success.
|
* \return 0 for success.
|
||||||
* \return -1 for error.
|
* \return Non-zero for error.
|
||||||
*/
|
*/
|
||||||
int stasis_app_control_answer(struct stasis_app_control *control);
|
int stasis_app_control_answer(struct stasis_app_control *control);
|
||||||
|
|
||||||
/*! @} */
|
/*!
|
||||||
|
* \brief Returns the most recent snapshot for the associated channel.
|
||||||
/*! @{ */
|
*
|
||||||
|
* The returned pointer is AO2 managed, so ao2_cleanup() when you're done.
|
||||||
|
*
|
||||||
|
* \param control Control for \c res_stasis.
|
||||||
|
* \return Most recent snapshot. ao2_cleanup() when done.
|
||||||
|
* \return \c NULL if channel isn't in cache.
|
||||||
|
*/
|
||||||
|
struct ast_channel_snapshot *stasis_app_control_get_snapshot(
|
||||||
|
const struct stasis_app_control *control);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Build a JSON object from a \ref ast_channel_snapshot.
|
* \brief Publish a message to the \a control's channel's topic.
|
||||||
* \return JSON object representing channel snapshot.
|
*
|
||||||
* \return \c NULL on error
|
* \param control Control to publish to
|
||||||
|
* \param message Message to publish
|
||||||
*/
|
*/
|
||||||
struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot);
|
void stasis_app_control_publish(
|
||||||
|
struct stasis_app_control *control, struct stasis_message *message);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Increment the res_stasis reference count.
|
* \brief Increment the res_stasis reference count.
|
||||||
|
88
include/asterisk/stasis_app_impl.h
Normal file
88
include/asterisk/stasis_app_impl.h
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* Asterisk -- An open source telephony toolkit.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013, Digium, Inc.
|
||||||
|
*
|
||||||
|
* David M. Lee, II <dlee@digium.com>
|
||||||
|
*
|
||||||
|
* See http://www.asterisk.org for more information about
|
||||||
|
* the Asterisk project. Please do not directly contact
|
||||||
|
* any of the maintainers of this project for assistance;
|
||||||
|
* the project provides a web site, mailing lists and IRC
|
||||||
|
* channels for your use.
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License Version 2. See the LICENSE file
|
||||||
|
* at the top of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _ASTERISK_RES_STASIS_H
|
||||||
|
#define _ASTERISK_RES_STASIS_H
|
||||||
|
|
||||||
|
/*! \file
|
||||||
|
*
|
||||||
|
* \brief Backend API for implementing components of res_stasis.
|
||||||
|
*
|
||||||
|
* \author David M. Lee, II <dlee@digium.com>
|
||||||
|
* \since 12
|
||||||
|
*
|
||||||
|
* This file defines functions useful for defining new commands to execute
|
||||||
|
* on channels while they are in Stasis.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "asterisk/stasis_app.h"
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \since 12
|
||||||
|
* \brief Control a channel using \c stasis_app.
|
||||||
|
*
|
||||||
|
* This function blocks until the channel hangs up, or
|
||||||
|
* stasis_app_control_continue() is called on the channel's \ref
|
||||||
|
* stasis_app_control struct.
|
||||||
|
*
|
||||||
|
* \param chan Channel to control with Stasis.
|
||||||
|
* \param app_name Application controlling the channel.
|
||||||
|
* \param argc Number of arguments for the application.
|
||||||
|
* \param argv Arguments for the application.
|
||||||
|
*/
|
||||||
|
int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
|
||||||
|
char *argv[]);
|
||||||
|
|
||||||
|
/*! Callback type for stasis app commands */
|
||||||
|
typedef void *(*stasis_app_command_cb)(struct stasis_app_control *control,
|
||||||
|
struct ast_channel *chan, void *data);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \since 12
|
||||||
|
* \brief Invokes a \a command on a \a control's channel.
|
||||||
|
*
|
||||||
|
* This function dispatches the command to be executed in the context of
|
||||||
|
* stasis_app_exec(), so this command will block waiting for the results of
|
||||||
|
* the command.
|
||||||
|
*
|
||||||
|
* \param control Control object for the channel to send the command to.
|
||||||
|
* \param command Command function to execute.
|
||||||
|
* \param data Optional data to pass along with the control function.
|
||||||
|
* \return Return value from \a command.
|
||||||
|
* \return \c NULL on error.
|
||||||
|
*/
|
||||||
|
void *stasis_app_send_command(struct stasis_app_control *control,
|
||||||
|
stasis_app_command_cb command, void *data);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \since 12
|
||||||
|
* \brief Asynchronous version of stasis_app_send().
|
||||||
|
*
|
||||||
|
* This function enqueues a command for execution, but returns immediately
|
||||||
|
* without waiting for the response.
|
||||||
|
*
|
||||||
|
* \param control Control object for the channel to send the command to.
|
||||||
|
* \param command Command function to execute.
|
||||||
|
* \param data Optional data to pass along with the control function.
|
||||||
|
* \return 0 on success.
|
||||||
|
* \return Non-zero on error.
|
||||||
|
*/
|
||||||
|
int stasis_app_send_command_async(struct stasis_app_control *control,
|
||||||
|
stasis_app_command_cb command, void *data);
|
||||||
|
|
||||||
|
#endif /* _ASTERISK_RES_STASIS_H */
|
@@ -46,6 +46,9 @@ ael/ael_lex.o ael/ael.tab.o ael/pval.o: _ASTCFLAGS+=$(call MOD_ASTCFLAGS,res_ael
|
|||||||
$(if $(filter res_sip,$(EMBEDDED_MODS)),modules.link,res_sip.so): $(subst .c,.o,$(wildcard res_sip/*.c))
|
$(if $(filter res_sip,$(EMBEDDED_MODS)),modules.link,res_sip.so): $(subst .c,.o,$(wildcard res_sip/*.c))
|
||||||
$(subst .c,.o,$(wildcard res_sip/*.c)): _ASTCFLAGS+=$(call MOD_ASTCFLAGS,res_sip)
|
$(subst .c,.o,$(wildcard res_sip/*.c)): _ASTCFLAGS+=$(call MOD_ASTCFLAGS,res_sip)
|
||||||
|
|
||||||
|
$(if $(filter res_stasis,$(EMBEDDED_MODS)),modules.link,res_stasis.so): $(subst .c,.o,$(wildcard stasis/*.c))
|
||||||
|
$(subst .c,.o,$(wildcard stasis/*.c)): _ASTCFLAGS+=$(call MOD_ASTCFLAGS,res_stasis)
|
||||||
|
|
||||||
ifneq ($(findstring REBUILD_PARSERS,$(MENUSELECT_CFLAGS)),)
|
ifneq ($(findstring REBUILD_PARSERS,$(MENUSELECT_CFLAGS)),)
|
||||||
ael/ael_lex.c: ael/ael.flex
|
ael/ael_lex.c: ael/ael.flex
|
||||||
else
|
else
|
||||||
@@ -70,7 +73,8 @@ endif
|
|||||||
ael/pval.o: ael/pval.c
|
ael/pval.o: ael/pval.c
|
||||||
|
|
||||||
clean::
|
clean::
|
||||||
rm -f snmp/*.[oi] ael/*.[oi] ais/*.[oi] stasis_http/*.[oi] res_sip/*.[oi]
|
rm -f snmp/*.[oi] ael/*.[oi] ais/*.[oi] stasis_http/*.[oi]
|
||||||
|
rm -f res_sip/*.[oi] stasis/*.[oi]
|
||||||
|
|
||||||
# Dependencies for res_stasis_http_*.so are generated, so they're in this file
|
# Dependencies for res_stasis_http_*.so are generated, so they're in this file
|
||||||
include stasis_http.make
|
include stasis_http.make
|
||||||
|
570
res/res_stasis.c
570
res/res_stasis.c
@@ -21,6 +21,30 @@
|
|||||||
* \brief Stasis application support.
|
* \brief Stasis application support.
|
||||||
*
|
*
|
||||||
* \author David M. Lee, II <dlee@digium.com>
|
* \author David M. Lee, II <dlee@digium.com>
|
||||||
|
*
|
||||||
|
* <code>res_stasis.so</code> brings together the various components of the
|
||||||
|
* Stasis application infrastructure.
|
||||||
|
*
|
||||||
|
* First, there's the Stasis application handler, stasis_app_exec(). This is
|
||||||
|
* called by <code>app_stasis.so</code> to give control of a channel to the
|
||||||
|
* Stasis application code from the dialplan.
|
||||||
|
*
|
||||||
|
* While a channel is in stasis_app_exec(), it has a \ref stasis_app_control
|
||||||
|
* object, which may be used to control the channel.
|
||||||
|
*
|
||||||
|
* To control the channel, commands may be sent to channel using
|
||||||
|
* stasis_app_send_command() and stasis_app_send_async_command().
|
||||||
|
*
|
||||||
|
* Alongside this, applications may be registered/unregistered using
|
||||||
|
* stasis_app_register()/stasis_app_unregister(). While a channel is in Stasis,
|
||||||
|
* events received on the channel's topic are converted to JSON and forwarded to
|
||||||
|
* the \ref stasis_app_cb. The application may also subscribe to the channel to
|
||||||
|
* continue to receive messages even after the channel has left Stasis, but it
|
||||||
|
* will not be able to control it.
|
||||||
|
*
|
||||||
|
* Given all the stuff that comes together in this module, it's been broken up
|
||||||
|
* into several pieces that are in <code>res/stasis/</code> and compiled into
|
||||||
|
* <code>res_stasis.so</code>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*** MODULEINFO
|
/*** MODULEINFO
|
||||||
@@ -33,15 +57,14 @@
|
|||||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||||
|
|
||||||
#include "asterisk/astobj2.h"
|
#include "asterisk/astobj2.h"
|
||||||
#include "asterisk/channel.h"
|
|
||||||
#include "asterisk/lock.h"
|
|
||||||
#include "asterisk/module.h"
|
|
||||||
#include "asterisk/stasis.h"
|
|
||||||
#include "asterisk/stasis_app.h"
|
|
||||||
#include "asterisk/stasis_channels.h"
|
|
||||||
#include "asterisk/strings.h"
|
|
||||||
#include "asterisk/stasis_message_router.h"
|
|
||||||
#include "asterisk/callerid.h"
|
#include "asterisk/callerid.h"
|
||||||
|
#include "asterisk/module.h"
|
||||||
|
#include "asterisk/stasis_app_impl.h"
|
||||||
|
#include "asterisk/stasis_channels.h"
|
||||||
|
#include "asterisk/stasis_message_router.h"
|
||||||
|
#include "asterisk/strings.h"
|
||||||
|
#include "stasis/app.h"
|
||||||
|
#include "stasis/control.h"
|
||||||
#include "stasis_json/resource_events.h"
|
#include "stasis_json/resource_events.h"
|
||||||
|
|
||||||
/*! Time to wait for a frame in the application */
|
/*! Time to wait for a frame in the application */
|
||||||
@@ -59,12 +82,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|||||||
*/
|
*/
|
||||||
#define CONTROLS_NUM_BUCKETS 127
|
#define CONTROLS_NUM_BUCKETS 127
|
||||||
|
|
||||||
/*!
|
|
||||||
* \brief Number of buckets for the channels container for app instances. Remember
|
|
||||||
* to keep it a prime number!
|
|
||||||
*/
|
|
||||||
#define APP_CHANNELS_BUCKETS 7
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Number of buckets for the blob_handlers container. Remember to keep
|
* \brief Number of buckets for the blob_handlers container. Remember to keep
|
||||||
* it a prime number!
|
* it a prime number!
|
||||||
@@ -72,85 +89,20 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|||||||
#define BLOB_HANDLER_BUCKETS 7
|
#define BLOB_HANDLER_BUCKETS 7
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Stasis application container. Please call apps_registry() instead of
|
* \brief Stasis application container.
|
||||||
* directly accessing.
|
|
||||||
*/
|
*/
|
||||||
struct ao2_container *__apps_registry;
|
struct ao2_container *apps_registry;
|
||||||
|
|
||||||
struct ao2_container *__app_controls;
|
struct ao2_container *app_controls;
|
||||||
|
|
||||||
/*! \brief Message router for the channel caching topic */
|
/*! \brief Message router for the channel caching topic */
|
||||||
struct stasis_message_router *channel_router;
|
struct stasis_message_router *channel_router;
|
||||||
|
|
||||||
/*! Ref-counting accessor for the stasis applications container */
|
|
||||||
static struct ao2_container *apps_registry(void)
|
|
||||||
{
|
|
||||||
ao2_ref(__apps_registry, +1);
|
|
||||||
return __apps_registry;
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct ao2_container *app_controls(void)
|
|
||||||
{
|
|
||||||
ao2_ref(__app_controls, +1);
|
|
||||||
return __app_controls;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct app {
|
|
||||||
/*! Callback function for this application. */
|
|
||||||
stasis_app_cb handler;
|
|
||||||
/*! Opaque data to hand to callback function. */
|
|
||||||
void *data;
|
|
||||||
/*! List of channel identifiers this app instance is interested in */
|
|
||||||
struct ao2_container *channels;
|
|
||||||
/*! Name of the Stasis application */
|
|
||||||
char name[];
|
|
||||||
};
|
|
||||||
|
|
||||||
static void app_dtor(void *obj)
|
|
||||||
{
|
|
||||||
struct app *app = obj;
|
|
||||||
|
|
||||||
ao2_cleanup(app->data);
|
|
||||||
app->data = NULL;
|
|
||||||
ao2_cleanup(app->channels);
|
|
||||||
app->channels = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*! Constructor for \ref app. */
|
|
||||||
static struct app *app_create(const char *name, stasis_app_cb handler, void *data)
|
|
||||||
{
|
|
||||||
RAII_VAR(struct app *, app, NULL, ao2_cleanup);
|
|
||||||
size_t size;
|
|
||||||
|
|
||||||
ast_assert(name != NULL);
|
|
||||||
ast_assert(handler != NULL);
|
|
||||||
|
|
||||||
size = sizeof(*app) + strlen(name) + 1;
|
|
||||||
app = ao2_alloc_options(size, app_dtor, AO2_ALLOC_OPT_LOCK_MUTEX);
|
|
||||||
|
|
||||||
if (!app) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(app->name, name, size - sizeof(*app));
|
|
||||||
app->handler = handler;
|
|
||||||
ao2_ref(data, +1);
|
|
||||||
app->data = data;
|
|
||||||
|
|
||||||
app->channels = ast_str_container_alloc(APP_CHANNELS_BUCKETS);
|
|
||||||
if (!app->channels) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ao2_ref(app, +1);
|
|
||||||
return app;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*! AO2 hash function for \ref app */
|
/*! AO2 hash function for \ref app */
|
||||||
static int app_hash(const void *obj, const int flags)
|
static int app_hash(const void *obj, const int flags)
|
||||||
{
|
{
|
||||||
const struct app *app = obj;
|
const struct app *app = obj;
|
||||||
const char *name = flags & OBJ_KEY ? obj : app->name;
|
const char *name = flags & OBJ_KEY ? obj : app_name(app);
|
||||||
|
|
||||||
return ast_str_hash(name);
|
return ast_str_hash(name);
|
||||||
}
|
}
|
||||||
@@ -160,149 +112,22 @@ static int app_compare(void *lhs, void *rhs, int flags)
|
|||||||
{
|
{
|
||||||
const struct app *lhs_app = lhs;
|
const struct app *lhs_app = lhs;
|
||||||
const struct app *rhs_app = rhs;
|
const struct app *rhs_app = rhs;
|
||||||
const char *rhs_name = flags & OBJ_KEY ? rhs : rhs_app->name;
|
const char *lhs_name = app_name(lhs_app);
|
||||||
|
const char *rhs_name = flags & OBJ_KEY ? rhs : app_name(rhs_app);
|
||||||
|
|
||||||
if (strcmp(lhs_app->name, rhs_name) == 0) {
|
if (strcmp(lhs_name, rhs_name) == 0) {
|
||||||
return CMP_MATCH | CMP_STOP;
|
return CMP_MATCH | CMP_STOP;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int app_add_channel(struct app* app, const struct ast_channel *chan)
|
|
||||||
{
|
|
||||||
const char *uniqueid;
|
|
||||||
ast_assert(chan != NULL);
|
|
||||||
ast_assert(app != NULL);
|
|
||||||
|
|
||||||
uniqueid = ast_channel_uniqueid(chan);
|
|
||||||
if (!ast_str_container_add(app->channels, uniqueid)) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void app_remove_channel(struct app* app, const struct ast_channel *chan)
|
|
||||||
{
|
|
||||||
ast_assert(chan != NULL);
|
|
||||||
ast_assert(app != NULL);
|
|
||||||
|
|
||||||
ao2_find(app->channels, ast_channel_uniqueid(chan), OBJ_KEY | OBJ_NODATA | OBJ_UNLINK);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
* \brief Send a message to the given application.
|
|
||||||
* \param app App to send the message to.
|
|
||||||
* \param message Message to send.
|
|
||||||
*/
|
|
||||||
static void app_send(struct app *app, struct ast_json *message)
|
|
||||||
{
|
|
||||||
app->handler(app->data, app->name, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef void* (*stasis_app_command_cb)(struct stasis_app_control *control,
|
|
||||||
struct ast_channel *chan,
|
|
||||||
void *data);
|
|
||||||
|
|
||||||
struct stasis_app_command {
|
|
||||||
ast_mutex_t lock;
|
|
||||||
ast_cond_t condition;
|
|
||||||
stasis_app_command_cb callback;
|
|
||||||
void *data;
|
|
||||||
void *retval;
|
|
||||||
int is_done:1;
|
|
||||||
};
|
|
||||||
|
|
||||||
static void command_dtor(void *obj)
|
|
||||||
{
|
|
||||||
struct stasis_app_command *command = obj;
|
|
||||||
ast_mutex_destroy(&command->lock);
|
|
||||||
ast_cond_destroy(&command->condition);
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct stasis_app_command *command_create(stasis_app_command_cb callback,
|
|
||||||
void *data)
|
|
||||||
{
|
|
||||||
RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
|
|
||||||
|
|
||||||
command = ao2_alloc(sizeof(*command), command_dtor);
|
|
||||||
if (!command) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ast_mutex_init(&command->lock);
|
|
||||||
ast_cond_init(&command->condition, 0);
|
|
||||||
command->callback = callback;
|
|
||||||
command->data = data;
|
|
||||||
|
|
||||||
ao2_ref(command, +1);
|
|
||||||
return command;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void command_complete(struct stasis_app_command *command, void *retval)
|
|
||||||
{
|
|
||||||
SCOPED_MUTEX(lock, &command->lock);
|
|
||||||
|
|
||||||
command->is_done = 1;
|
|
||||||
command->retval = retval;
|
|
||||||
ast_cond_signal(&command->condition);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *wait_for_command(struct stasis_app_command *command)
|
|
||||||
{
|
|
||||||
SCOPED_MUTEX(lock, &command->lock);
|
|
||||||
while (!command->is_done) {
|
|
||||||
ast_cond_wait(&command->condition, &command->lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
return command->retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct stasis_app_control {
|
|
||||||
/*! Queue of commands to dispatch on the channel */
|
|
||||||
struct ao2_container *command_queue;
|
|
||||||
/*!
|
|
||||||
* When set, /c app_stasis should exit and continue in the dialplan.
|
|
||||||
*/
|
|
||||||
int continue_to_dialplan:1;
|
|
||||||
/*! Uniqueid of the associated channel */
|
|
||||||
char channel_id[];
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct stasis_app_control *control_create(const char *uniqueid)
|
|
||||||
{
|
|
||||||
struct stasis_app_control *control;
|
|
||||||
size_t size;
|
|
||||||
|
|
||||||
size = sizeof(*control) + strlen(uniqueid) + 1;
|
|
||||||
control = ao2_alloc(size, NULL);
|
|
||||||
if (!control) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
control->command_queue = ao2_container_alloc_list(0, 0, NULL, NULL);
|
|
||||||
|
|
||||||
strncpy(control->channel_id, uniqueid, size - sizeof(*control));
|
|
||||||
|
|
||||||
return control;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *exec_command(struct stasis_app_control *control,
|
|
||||||
struct stasis_app_command *command)
|
|
||||||
{
|
|
||||||
ao2_lock(control);
|
|
||||||
ao2_ref(command, +1);
|
|
||||||
ao2_link(control->command_queue, command);
|
|
||||||
ao2_unlock(control);
|
|
||||||
|
|
||||||
return wait_for_command(command);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*! AO2 hash function for \ref stasis_app_control */
|
/*! AO2 hash function for \ref stasis_app_control */
|
||||||
static int control_hash(const void *obj, const int flags)
|
static int control_hash(const void *obj, const int flags)
|
||||||
{
|
{
|
||||||
const struct stasis_app_control *control = obj;
|
const struct stasis_app_control *control = obj;
|
||||||
const char *id = flags & OBJ_KEY ? obj : control->channel_id;
|
const char *id = flags & OBJ_KEY ?
|
||||||
|
obj : stasis_app_control_get_channel_id(control);
|
||||||
|
|
||||||
return ast_str_hash(id);
|
return ast_str_hash(id);
|
||||||
}
|
}
|
||||||
@@ -312,10 +137,11 @@ static int control_compare(void *lhs, void *rhs, int flags)
|
|||||||
{
|
{
|
||||||
const struct stasis_app_control *lhs_control = lhs;
|
const struct stasis_app_control *lhs_control = lhs;
|
||||||
const struct stasis_app_control *rhs_control = rhs;
|
const struct stasis_app_control *rhs_control = rhs;
|
||||||
const char *rhs_name =
|
const char *lhs_id = stasis_app_control_get_channel_id(lhs_control);
|
||||||
flags & OBJ_KEY ? rhs : rhs_control->channel_id;
|
const char *rhs_id = flags & OBJ_KEY ?
|
||||||
|
rhs : stasis_app_control_get_channel_id(rhs_control);
|
||||||
|
|
||||||
if (strcmp(lhs_control->channel_id, rhs_name) == 0) {
|
if (strcmp(lhs_id, rhs_id) == 0) {
|
||||||
return CMP_MATCH | CMP_STOP;
|
return CMP_MATCH | CMP_STOP;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -336,154 +162,30 @@ struct stasis_app_control *stasis_app_control_find_by_channel(
|
|||||||
struct stasis_app_control *stasis_app_control_find_by_channel_id(
|
struct stasis_app_control *stasis_app_control_find_by_channel_id(
|
||||||
const char *channel_id)
|
const char *channel_id)
|
||||||
{
|
{
|
||||||
RAII_VAR(struct ao2_container *, controls, NULL, ao2_cleanup);
|
return ao2_find(app_controls, channel_id, OBJ_KEY);
|
||||||
controls = app_controls();
|
|
||||||
return ao2_find(controls, channel_id, OBJ_KEY);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
* \brief Test the \c continue_to_dialplan bit for the given \a app.
|
|
||||||
*
|
|
||||||
* The bit is also reset for the next call.
|
|
||||||
*
|
|
||||||
* \param app Application to check the \c continue_to_dialplan bit.
|
|
||||||
* \return Zero to remain in \c Stasis
|
|
||||||
* \return Non-zero to continue in the dialplan
|
|
||||||
*/
|
|
||||||
static int control_continue_test_and_reset(struct stasis_app_control *control)
|
|
||||||
{
|
|
||||||
int r;
|
|
||||||
SCOPED_AO2LOCK(lock, control);
|
|
||||||
|
|
||||||
r = control->continue_to_dialplan;
|
|
||||||
control->continue_to_dialplan = 0;
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
void stasis_app_control_continue(struct stasis_app_control *control)
|
|
||||||
{
|
|
||||||
SCOPED_AO2LOCK(lock, control);
|
|
||||||
control->continue_to_dialplan = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! \brief Typedef for blob handler callbacks */
|
/*! \brief Typedef for blob handler callbacks */
|
||||||
typedef struct ast_json *(*channel_blob_handler_cb)(struct ast_channel_blob *);
|
typedef struct ast_json *(*channel_blob_handler_cb)(struct ast_channel_blob *);
|
||||||
|
|
||||||
static int OK = 0;
|
|
||||||
static int FAIL = -1;
|
|
||||||
|
|
||||||
static void *__app_control_answer(struct stasis_app_control *control,
|
|
||||||
struct ast_channel *chan, void *data)
|
|
||||||
{
|
|
||||||
ast_debug(3, "%s: Answering", control->channel_id);
|
|
||||||
return __ast_answer(chan, 0, 1) == 0 ? &OK : &FAIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int stasis_app_control_answer(struct stasis_app_control *control)
|
|
||||||
{
|
|
||||||
RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
|
|
||||||
int *retval;
|
|
||||||
|
|
||||||
ast_debug(3, "%s: Sending answer command\n", control->channel_id);
|
|
||||||
|
|
||||||
command = command_create(__app_control_answer, NULL);
|
|
||||||
retval = exec_command(control, command);
|
|
||||||
|
|
||||||
if (*retval != 0) {
|
|
||||||
ast_log(LOG_WARNING, "Failed to answer channel");
|
|
||||||
}
|
|
||||||
|
|
||||||
return *retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int send_start_msg(struct app *app, struct ast_channel *chan,
|
|
||||||
int argc, char *argv[])
|
|
||||||
{
|
|
||||||
RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
|
|
||||||
RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
|
|
||||||
RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
|
|
||||||
|
|
||||||
struct ast_json *json_args;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
ast_assert(chan != NULL);
|
|
||||||
|
|
||||||
/* Set channel info */
|
|
||||||
snapshot = ast_channel_snapshot_create(chan);
|
|
||||||
if (!snapshot) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
blob = ast_json_pack("{s: []}", "args");
|
|
||||||
if (!blob) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Append arguments to args array */
|
|
||||||
json_args = ast_json_object_get(blob, "args");
|
|
||||||
ast_assert(json_args != NULL);
|
|
||||||
for (i = 0; i < argc; ++i) {
|
|
||||||
int r = ast_json_array_append(json_args,
|
|
||||||
ast_json_string_create(argv[i]));
|
|
||||||
if (r != 0) {
|
|
||||||
ast_log(LOG_ERROR, "Error appending start message\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
msg = stasis_json_event_stasis_start_create(snapshot, blob);
|
|
||||||
if (!msg) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
app_send(app, msg);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int send_end_msg(struct app *app, struct ast_channel *chan)
|
|
||||||
{
|
|
||||||
RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
|
|
||||||
RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
|
|
||||||
|
|
||||||
ast_assert(chan != NULL);
|
|
||||||
|
|
||||||
/* Set channel info */
|
|
||||||
snapshot = ast_channel_snapshot_create(chan);
|
|
||||||
if (snapshot == NULL) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
msg = stasis_json_event_stasis_end_create(snapshot);
|
|
||||||
if (!msg) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
app_send(app, msg);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int app_watching_channel_cb(void *obj, void *arg, int flags)
|
static int app_watching_channel_cb(void *obj, void *arg, int flags)
|
||||||
{
|
{
|
||||||
RAII_VAR(char *, uniqueid, NULL, ao2_cleanup);
|
|
||||||
struct app *app = obj;
|
struct app *app = obj;
|
||||||
char *chan_uniqueid = arg;
|
char *uniqueid = arg;
|
||||||
|
|
||||||
uniqueid = ao2_find(app->channels, chan_uniqueid, OBJ_KEY);
|
return app_is_watching_channel(app, uniqueid) ? CMP_MATCH : 0;
|
||||||
return uniqueid ? CMP_MATCH : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct ao2_container *get_watching_apps(const char *uniqueid)
|
static struct ao2_container *get_watching_apps(const char *uniqueid)
|
||||||
{
|
{
|
||||||
RAII_VAR(struct ao2_container *, apps, apps_registry(), ao2_cleanup);
|
|
||||||
struct ao2_container *watching_apps;
|
struct ao2_container *watching_apps;
|
||||||
char *uniqueid_dup;
|
char *uniqueid_dup;
|
||||||
RAII_VAR(struct ao2_iterator *,watching_apps_iter, NULL, ao2_iterator_destroy);
|
RAII_VAR(struct ao2_iterator *,watching_apps_iter, NULL, ao2_iterator_destroy);
|
||||||
ast_assert(uniqueid != NULL);
|
ast_assert(uniqueid != NULL);
|
||||||
ast_assert(apps != NULL);
|
|
||||||
|
|
||||||
uniqueid_dup = ast_strdupa(uniqueid);
|
uniqueid_dup = ast_strdupa(uniqueid);
|
||||||
|
|
||||||
watching_apps_iter = ao2_callback(apps, OBJ_MULTIPLE, app_watching_channel_cb, uniqueid_dup);
|
watching_apps_iter = ao2_callback(apps_registry, OBJ_MULTIPLE, app_watching_channel_cb, uniqueid_dup);
|
||||||
watching_apps = watching_apps_iter->c;
|
watching_apps = watching_apps_iter->c;
|
||||||
|
|
||||||
if (!ao2_container_count(watching_apps)) {
|
if (!ao2_container_count(watching_apps)) {
|
||||||
@@ -665,72 +367,111 @@ static void generic_blob_handler(struct ast_channel_blob *obj, channel_blob_hand
|
|||||||
*/
|
*/
|
||||||
static void control_unlink(struct stasis_app_control *control)
|
static void control_unlink(struct stasis_app_control *control)
|
||||||
{
|
{
|
||||||
RAII_VAR(struct ao2_container *, controls, NULL, ao2_cleanup);
|
|
||||||
|
|
||||||
if (!control) {
|
if (!control) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
controls = app_controls();
|
ao2_unlink_flags(app_controls, control,
|
||||||
ao2_unlink_flags(controls, control,
|
OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
|
||||||
OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
|
|
||||||
ao2_cleanup(control);
|
ao2_cleanup(control);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dispatch_commands(struct stasis_app_control *control,
|
int app_send_start_msg(struct app *app, struct ast_channel *chan,
|
||||||
struct ast_channel *chan)
|
int argc, char *argv[])
|
||||||
{
|
{
|
||||||
struct ao2_iterator i;
|
RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
|
||||||
void *obj;
|
RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
|
||||||
|
RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
|
||||||
|
|
||||||
SCOPED_AO2LOCK(lock, control);
|
struct ast_json *json_args;
|
||||||
|
int i;
|
||||||
|
|
||||||
i = ao2_iterator_init(control->command_queue, AO2_ITERATOR_UNLINK);
|
ast_assert(chan != NULL);
|
||||||
|
|
||||||
while ((obj = ao2_iterator_next(&i))) {
|
/* Set channel info */
|
||||||
RAII_VAR(struct stasis_app_command *, command, obj, ao2_cleanup);
|
snapshot = ast_channel_snapshot_create(chan);
|
||||||
void *retval = command->callback(control, chan, command->data);
|
if (!snapshot) {
|
||||||
command_complete(command, retval);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ao2_iterator_destroy(&i);
|
blob = ast_json_pack("{s: []}", "args");
|
||||||
|
if (!blob) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Append arguments to args array */
|
||||||
|
json_args = ast_json_object_get(blob, "args");
|
||||||
|
ast_assert(json_args != NULL);
|
||||||
|
for (i = 0; i < argc; ++i) {
|
||||||
|
int r = ast_json_array_append(json_args,
|
||||||
|
ast_json_string_create(argv[i]));
|
||||||
|
if (r != 0) {
|
||||||
|
ast_log(LOG_ERROR, "Error appending start message\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
msg = stasis_json_event_stasis_start_create(snapshot, blob);
|
||||||
|
if (!msg) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
app_send(app, msg);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int app_send_end_msg(struct app *app, struct ast_channel *chan)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
|
||||||
|
RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
|
||||||
|
|
||||||
|
ast_assert(chan != NULL);
|
||||||
|
|
||||||
|
/* Set channel info */
|
||||||
|
snapshot = ast_channel_snapshot_create(chan);
|
||||||
|
if (snapshot == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
msg = stasis_json_event_stasis_end_create(snapshot);
|
||||||
|
if (!msg) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
app_send(app, msg);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/*! /brief Stasis dialplan application callback */
|
/*! /brief Stasis dialplan application callback */
|
||||||
int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
|
int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
|
||||||
char *argv[])
|
char *argv[])
|
||||||
{
|
{
|
||||||
RAII_VAR(struct ao2_container *, apps, apps_registry(), ao2_cleanup);
|
SCOPED_MODULE_USE(ast_module_info->self);
|
||||||
|
|
||||||
RAII_VAR(struct app *, app, NULL, ao2_cleanup);
|
RAII_VAR(struct app *, app, NULL, ao2_cleanup);
|
||||||
RAII_VAR(struct stasis_app_control *, control, NULL, control_unlink);
|
RAII_VAR(struct stasis_app_control *, control, NULL, control_unlink);
|
||||||
int res = 0;
|
int res = 0;
|
||||||
int hungup = 0;
|
|
||||||
|
|
||||||
ast_assert(chan != NULL);
|
ast_assert(chan != NULL);
|
||||||
|
|
||||||
app = ao2_find(apps, app_name, OBJ_KEY);
|
app = ao2_find(apps_registry, app_name, OBJ_KEY);
|
||||||
if (!app) {
|
if (!app) {
|
||||||
ast_log(LOG_ERROR,
|
ast_log(LOG_ERROR,
|
||||||
"Stasis app '%s' not registered\n", app_name);
|
"Stasis app '%s' not registered\n", app_name);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
control = control_create(chan);
|
||||||
RAII_VAR(struct ao2_container *, controls, NULL, ao2_cleanup);
|
if (!control) {
|
||||||
|
ast_log(LOG_ERROR, "Allocated failed\n");
|
||||||
controls = app_controls();
|
return -1;
|
||||||
control = control_create(ast_channel_uniqueid(chan));
|
|
||||||
if (!control) {
|
|
||||||
ast_log(LOG_ERROR, "Allocated failed\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
ao2_link(controls, control);
|
|
||||||
}
|
}
|
||||||
|
ao2_link(app_controls, control);
|
||||||
|
|
||||||
res = send_start_msg(app, chan, argc, argv);
|
res = app_send_start_msg(app, chan, argc, argv);
|
||||||
if (res != 0) {
|
if (res != 0) {
|
||||||
ast_log(LOG_ERROR, "Error sending start message to %s\n", app_name);
|
ast_log(LOG_ERROR,
|
||||||
|
"Error sending start message to %s\n", app_name);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -739,21 +480,10 @@ int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
while (!control_is_done(control)) {
|
||||||
RAII_VAR(struct ast_frame *, f, NULL, ast_frame_dtor);
|
RAII_VAR(struct ast_frame *, f, NULL, ast_frame_dtor);
|
||||||
int r;
|
int r;
|
||||||
|
int command_count;
|
||||||
if (hungup) {
|
|
||||||
ast_debug(3, "%s: Hangup\n",
|
|
||||||
ast_channel_uniqueid(chan));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (control_continue_test_and_reset(control)) {
|
|
||||||
ast_debug(3, "%s: Continue\n",
|
|
||||||
ast_channel_uniqueid(chan));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
r = ast_waitfor(chan, MAX_WAIT_MS);
|
r = ast_waitfor(chan, MAX_WAIT_MS);
|
||||||
|
|
||||||
@@ -763,7 +493,12 @@ int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch_commands(control, chan);
|
command_count = control_dispatch_all(control, chan);
|
||||||
|
|
||||||
|
if (command_count > 0 && ast_channel_fdno(chan) == -1) {
|
||||||
|
/* Command drained the channel; wait for next frame */
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (r == 0) {
|
if (r == 0) {
|
||||||
/* Timeout */
|
/* Timeout */
|
||||||
@@ -772,14 +507,19 @@ int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
|
|||||||
|
|
||||||
f = ast_read(chan);
|
f = ast_read(chan);
|
||||||
if (!f) {
|
if (!f) {
|
||||||
ast_debug(3, "%s: No more frames. Must be done, I guess.\n", ast_channel_uniqueid(chan));
|
ast_debug(3,
|
||||||
|
"%s: No more frames. Must be done, I guess.\n",
|
||||||
|
ast_channel_uniqueid(chan));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (f->frametype) {
|
switch (f->frametype) {
|
||||||
case AST_FRAME_CONTROL:
|
case AST_FRAME_CONTROL:
|
||||||
if (f->subclass.integer == AST_CONTROL_HANGUP) {
|
if (f->subclass.integer == AST_CONTROL_HANGUP) {
|
||||||
hungup = 1;
|
/* Continue on in the dialplan */
|
||||||
|
ast_debug(3, "%s: Hangup\n",
|
||||||
|
ast_channel_uniqueid(chan));
|
||||||
|
control_continue(control);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -789,7 +529,7 @@ int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
|
|||||||
}
|
}
|
||||||
|
|
||||||
app_remove_channel(app, chan);
|
app_remove_channel(app, chan);
|
||||||
res = send_end_msg(app, chan);
|
res = app_send_end_msg(app, chan);
|
||||||
if (res != 0) {
|
if (res != 0) {
|
||||||
ast_log(LOG_ERROR,
|
ast_log(LOG_ERROR,
|
||||||
"Error sending end message to %s\n", app_name);
|
"Error sending end message to %s\n", app_name);
|
||||||
@@ -801,10 +541,9 @@ int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
|
|||||||
|
|
||||||
int stasis_app_send(const char *app_name, struct ast_json *message)
|
int stasis_app_send(const char *app_name, struct ast_json *message)
|
||||||
{
|
{
|
||||||
RAII_VAR(struct ao2_container *, apps, apps_registry(), ao2_cleanup);
|
|
||||||
RAII_VAR(struct app *, app, NULL, ao2_cleanup);
|
RAII_VAR(struct app *, app, NULL, ao2_cleanup);
|
||||||
|
|
||||||
app = ao2_find(apps, app_name, OBJ_KEY);
|
app = ao2_find(apps_registry, app_name, OBJ_KEY);
|
||||||
|
|
||||||
if (!app) {
|
if (!app) {
|
||||||
/* XXX We can do a better job handling late binding, queueing up
|
/* XXX We can do a better job handling late binding, queueing up
|
||||||
@@ -821,34 +560,29 @@ int stasis_app_send(const char *app_name, struct ast_json *message)
|
|||||||
|
|
||||||
int stasis_app_register(const char *app_name, stasis_app_cb handler, void *data)
|
int stasis_app_register(const char *app_name, stasis_app_cb handler, void *data)
|
||||||
{
|
{
|
||||||
RAII_VAR(struct ao2_container *, apps, apps_registry(), ao2_cleanup);
|
|
||||||
RAII_VAR(struct app *, app, NULL, ao2_cleanup);
|
RAII_VAR(struct app *, app, NULL, ao2_cleanup);
|
||||||
|
|
||||||
SCOPED_LOCK(apps_lock, apps, ao2_lock, ao2_unlock);
|
SCOPED_LOCK(apps_lock, apps_registry, ao2_lock, ao2_unlock);
|
||||||
|
|
||||||
app = ao2_find(apps, app_name, OBJ_KEY | OBJ_NOLOCK);
|
app = ao2_find(apps_registry, app_name, OBJ_KEY | OBJ_NOLOCK);
|
||||||
|
|
||||||
if (app) {
|
if (app) {
|
||||||
RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
|
|
||||||
RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
|
RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
|
||||||
SCOPED_LOCK(app_lock, app, ao2_lock, ao2_unlock);
|
RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
|
||||||
|
|
||||||
blob = ast_json_pack("{s: s}", "application", app_name);
|
blob = ast_json_pack("{s: s}", "application", app_name);
|
||||||
if (blob) {
|
if (blob) {
|
||||||
msg = stasis_json_event_application_replaced_create(blob);
|
msg = stasis_json_event_application_replaced_create(blob);
|
||||||
if (msg) {
|
if (msg) {
|
||||||
app->handler(app->data, app_name, msg);
|
app_send(app, msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
app->handler = handler;
|
app_update(app, handler, data);
|
||||||
ao2_cleanup(app->data);
|
|
||||||
ao2_ref(data, +1);
|
|
||||||
app->data = data;
|
|
||||||
} else {
|
} else {
|
||||||
app = app_create(app_name, handler, data);
|
app = app_create(app_name, handler, data);
|
||||||
if (app) {
|
if (app) {
|
||||||
ao2_link_flags(apps, app, OBJ_NOLOCK);
|
ao2_link_flags(apps_registry, app, OBJ_NOLOCK);
|
||||||
} else {
|
} else {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -859,11 +593,9 @@ int stasis_app_register(const char *app_name, stasis_app_cb handler, void *data)
|
|||||||
|
|
||||||
void stasis_app_unregister(const char *app_name)
|
void stasis_app_unregister(const char *app_name)
|
||||||
{
|
{
|
||||||
RAII_VAR(struct ao2_container *, apps, NULL, ao2_cleanup);
|
|
||||||
|
|
||||||
if (app_name) {
|
if (app_name) {
|
||||||
apps = apps_registry();
|
ao2_cleanup(ao2_find(
|
||||||
ao2_cleanup(ao2_find(apps, app_name, OBJ_KEY | OBJ_UNLINK));
|
apps_registry, app_name, OBJ_KEY | OBJ_UNLINK));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -957,15 +689,15 @@ static int load_module(void)
|
|||||||
{
|
{
|
||||||
int r = 0;
|
int r = 0;
|
||||||
|
|
||||||
__apps_registry =
|
apps_registry =
|
||||||
ao2_container_alloc(APPS_NUM_BUCKETS, app_hash, app_compare);
|
ao2_container_alloc(APPS_NUM_BUCKETS, app_hash, app_compare);
|
||||||
if (__apps_registry == NULL) {
|
if (apps_registry == NULL) {
|
||||||
return AST_MODULE_LOAD_FAILURE;
|
return AST_MODULE_LOAD_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
__app_controls = ao2_container_alloc(CONTROLS_NUM_BUCKETS,
|
app_controls = ao2_container_alloc(CONTROLS_NUM_BUCKETS,
|
||||||
control_hash, control_compare);
|
control_hash, control_compare);
|
||||||
if (__app_controls == NULL) {
|
if (app_controls == NULL) {
|
||||||
return AST_MODULE_LOAD_FAILURE;
|
return AST_MODULE_LOAD_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -993,11 +725,11 @@ static int unload_module(void)
|
|||||||
stasis_message_router_unsubscribe(channel_router);
|
stasis_message_router_unsubscribe(channel_router);
|
||||||
channel_router = NULL;
|
channel_router = NULL;
|
||||||
|
|
||||||
ao2_cleanup(__apps_registry);
|
ao2_cleanup(apps_registry);
|
||||||
__apps_registry = NULL;
|
apps_registry = NULL;
|
||||||
|
|
||||||
ao2_cleanup(__app_controls);
|
ao2_cleanup(app_controls);
|
||||||
__app_controls = NULL;
|
app_controls = NULL;
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
83
res/res_stasis_answer.c
Normal file
83
res/res_stasis_answer.c
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* Asterisk -- An open source telephony toolkit.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013, Digium, Inc.
|
||||||
|
*
|
||||||
|
* David M. Lee, II <dlee@digium.com>
|
||||||
|
*
|
||||||
|
* See http://www.asterisk.org for more information about
|
||||||
|
* the Asterisk project. Please do not directly contact
|
||||||
|
* any of the maintainers of this project for assistance;
|
||||||
|
* the project provides a web site, mailing lists and IRC
|
||||||
|
* channels for your use.
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License Version 2. See the LICENSE file
|
||||||
|
* at the top of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*! \file
|
||||||
|
*
|
||||||
|
* \brief Stasis application control support.
|
||||||
|
*
|
||||||
|
* \author David M. Lee, II <dlee@digium.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*** MODULEINFO
|
||||||
|
<depend type="module">res_stasis</depend>
|
||||||
|
<support_level>core</support_level>
|
||||||
|
***/
|
||||||
|
|
||||||
|
#include "asterisk.h"
|
||||||
|
|
||||||
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||||
|
|
||||||
|
#include "asterisk/module.h"
|
||||||
|
#include "asterisk/stasis_app_impl.h"
|
||||||
|
|
||||||
|
static int OK = 0;
|
||||||
|
static int FAIL = -1;
|
||||||
|
|
||||||
|
static void *app_control_answer(struct stasis_app_control *control,
|
||||||
|
struct ast_channel *chan, void *data)
|
||||||
|
{
|
||||||
|
const int delay = 0;
|
||||||
|
const int cdr_answer = 1;
|
||||||
|
ast_debug(3, "%s: Answering",
|
||||||
|
stasis_app_control_get_channel_id(control));
|
||||||
|
return __ast_answer(chan, delay, cdr_answer) == 0 ? &OK : &FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int stasis_app_control_answer(struct stasis_app_control *control)
|
||||||
|
{
|
||||||
|
int *retval;
|
||||||
|
|
||||||
|
ast_debug(3, "%s: Sending answer command\n",
|
||||||
|
stasis_app_control_get_channel_id(control));
|
||||||
|
|
||||||
|
retval = stasis_app_send_command(control, app_control_answer, NULL);
|
||||||
|
|
||||||
|
if (retval == NULL || *retval != 0) {
|
||||||
|
ast_log(LOG_WARNING, "%s: Failed to answer channel",
|
||||||
|
stasis_app_control_get_channel_id(control));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int load_module(void)
|
||||||
|
{
|
||||||
|
return AST_MODULE_LOAD_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int unload_module(void)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS,
|
||||||
|
"Stasis application answer support",
|
||||||
|
.load = load_module,
|
||||||
|
.unload = unload_module,
|
||||||
|
.nonoptreq = "res_stasis");
|
142
res/stasis/app.c
Normal file
142
res/stasis/app.c
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
/*
|
||||||
|
* Asterisk -- An open source telephony toolkit.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013, Digium, Inc.
|
||||||
|
*
|
||||||
|
* David M. Lee, II <dlee@digium.com>
|
||||||
|
*
|
||||||
|
* See http://www.asterisk.org for more information about
|
||||||
|
* the Asterisk project. Please do not directly contact
|
||||||
|
* any of the maintainers of this project for assistance;
|
||||||
|
* the project provides a web site, mailing lists and IRC
|
||||||
|
* channels for your use.
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License Version 2. See the LICENSE file
|
||||||
|
* at the top of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*! \file
|
||||||
|
*
|
||||||
|
* \brief Stasis application support.
|
||||||
|
*
|
||||||
|
* \author David M. Lee, II <dlee@digium.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "asterisk.h"
|
||||||
|
|
||||||
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||||
|
|
||||||
|
#include "app.h"
|
||||||
|
|
||||||
|
#include "asterisk/stasis_app.h"
|
||||||
|
#include "asterisk/stasis_channels.h"
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Number of buckets for the channels container for app instances. Remember
|
||||||
|
* to keep it a prime number!
|
||||||
|
*/
|
||||||
|
#define APP_CHANNELS_BUCKETS 7
|
||||||
|
|
||||||
|
struct app {
|
||||||
|
/*! Callback function for this application. */
|
||||||
|
stasis_app_cb handler;
|
||||||
|
/*! Opaque data to hand to callback function. */
|
||||||
|
void *data;
|
||||||
|
/*! List of channel identifiers this app instance is interested in */
|
||||||
|
struct ao2_container *channels;
|
||||||
|
/*! Name of the Stasis application */
|
||||||
|
char name[];
|
||||||
|
};
|
||||||
|
|
||||||
|
static void app_dtor(void *obj)
|
||||||
|
{
|
||||||
|
struct app *app = obj;
|
||||||
|
|
||||||
|
ao2_cleanup(app->data);
|
||||||
|
app->data = NULL;
|
||||||
|
ao2_cleanup(app->channels);
|
||||||
|
app->channels = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct app *app_create(const char *name, stasis_app_cb handler, void *data)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct app *, app, NULL, ao2_cleanup);
|
||||||
|
size_t size;
|
||||||
|
|
||||||
|
ast_assert(name != NULL);
|
||||||
|
ast_assert(handler != NULL);
|
||||||
|
|
||||||
|
size = sizeof(*app) + strlen(name) + 1;
|
||||||
|
app = ao2_alloc_options(size, app_dtor, AO2_ALLOC_OPT_LOCK_MUTEX);
|
||||||
|
|
||||||
|
if (!app) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
strncpy(app->name, name, size - sizeof(*app));
|
||||||
|
app->handler = handler;
|
||||||
|
ao2_ref(data, +1);
|
||||||
|
app->data = data;
|
||||||
|
|
||||||
|
app->channels = ast_str_container_alloc(APP_CHANNELS_BUCKETS);
|
||||||
|
if (!app->channels) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ao2_ref(app, +1);
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
int app_add_channel(struct app *app, const struct ast_channel *chan)
|
||||||
|
{
|
||||||
|
const char *uniqueid;
|
||||||
|
ast_assert(chan != NULL);
|
||||||
|
ast_assert(app != NULL);
|
||||||
|
|
||||||
|
uniqueid = ast_channel_uniqueid(chan);
|
||||||
|
if (!ast_str_container_add(app->channels, uniqueid)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void app_remove_channel(struct app* app, const struct ast_channel *chan)
|
||||||
|
{
|
||||||
|
ast_assert(chan != NULL);
|
||||||
|
ast_assert(app != NULL);
|
||||||
|
|
||||||
|
ao2_find(app->channels, ast_channel_uniqueid(chan), OBJ_KEY | OBJ_NODATA | OBJ_UNLINK);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Send a message to the given application.
|
||||||
|
* \param app App to send the message to.
|
||||||
|
* \param message Message to send.
|
||||||
|
*/
|
||||||
|
void app_send(struct app *app, struct ast_json *message)
|
||||||
|
{
|
||||||
|
app->handler(app->data, app->name, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
void app_update(struct app *app, stasis_app_cb handler, void *data)
|
||||||
|
{
|
||||||
|
SCOPED_AO2LOCK(lock, app);
|
||||||
|
|
||||||
|
app->handler = handler;
|
||||||
|
ao2_cleanup(app->data);
|
||||||
|
ao2_ref(data, +1);
|
||||||
|
app->data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *app_name(const struct app *app)
|
||||||
|
{
|
||||||
|
return app->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
int app_is_watching_channel(struct app *app, const char *uniqueid)
|
||||||
|
{
|
||||||
|
RAII_VAR(char *, found, NULL, ao2_cleanup);
|
||||||
|
found = ao2_find(app->channels, uniqueid, OBJ_KEY);
|
||||||
|
return found != NULL;
|
||||||
|
}
|
77
res/stasis/app.h
Normal file
77
res/stasis/app.h
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* Asterisk -- An open source telephony toolkit.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013, Digium, Inc.
|
||||||
|
*
|
||||||
|
* David M. Lee, II <dlee@digium.com>
|
||||||
|
*
|
||||||
|
* See http://www.asterisk.org for more information about
|
||||||
|
* the Asterisk project. Please do not directly contact
|
||||||
|
* any of the maintainers of this project for assistance;
|
||||||
|
* the project provides a web site, mailing lists and IRC
|
||||||
|
* channels for your use.
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License Version 2. See the LICENSE file
|
||||||
|
* at the top of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _ASTERISK_RES_STASIS_APP_H
|
||||||
|
#define _ASTERISK_RES_STASIS_APP_H
|
||||||
|
|
||||||
|
/*! \file
|
||||||
|
*
|
||||||
|
* \brief Internal API for the Stasis application controller.
|
||||||
|
*
|
||||||
|
* \author David M. Lee, II <dlee@digium.com>
|
||||||
|
* \since 12
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "asterisk/channel.h"
|
||||||
|
#include "asterisk/stasis.h"
|
||||||
|
#include "asterisk/stasis_app.h"
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Opaque pointer to \c res_stasis app structure.
|
||||||
|
*/
|
||||||
|
struct app;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Create a res_stasis application.
|
||||||
|
*
|
||||||
|
* \param name Name of the application.
|
||||||
|
* \param handler Callback for messages sent to the application.
|
||||||
|
* \param data Data pointer provided to the callback.
|
||||||
|
* \return New \c res_stasis application.
|
||||||
|
* \return \c NULL on error.
|
||||||
|
*/
|
||||||
|
struct app *app_create(const char *name, stasis_app_cb handler, void *data);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Update the handler and data for a \c res_stasis application.
|
||||||
|
*
|
||||||
|
* \param app Application to update.
|
||||||
|
* \param handler New application callback.
|
||||||
|
* \param data New data pointer for the callback.
|
||||||
|
*/
|
||||||
|
void app_update(struct app *app, stasis_app_cb handler, void *data);
|
||||||
|
|
||||||
|
const char *app_name(const struct app *app);
|
||||||
|
|
||||||
|
struct stasis_subscription *app_subscribe(struct app *app,
|
||||||
|
struct stasis_topic *topic);
|
||||||
|
|
||||||
|
void app_send(struct app *app, struct ast_json *message);
|
||||||
|
|
||||||
|
int app_send_start_msg(struct app *app, struct ast_channel *chan, int argc,
|
||||||
|
char *argv[]);
|
||||||
|
|
||||||
|
int app_send_end_msg(struct app *app, struct ast_channel *chan);
|
||||||
|
|
||||||
|
int app_is_watching_channel(struct app *app, const char *uniqueid);
|
||||||
|
|
||||||
|
int app_add_channel(struct app* app, const struct ast_channel *chan);
|
||||||
|
|
||||||
|
void app_remove_channel(struct app *app, const struct ast_channel *chan);
|
||||||
|
|
||||||
|
#endif /* _ASTERISK_RES_STASIS_APP_H */
|
95
res/stasis/command.c
Normal file
95
res/stasis/command.c
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
* Asterisk -- An open source telephony toolkit.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013, Digium, Inc.
|
||||||
|
*
|
||||||
|
* David M. Lee, II <dlee@digium.com>
|
||||||
|
*
|
||||||
|
* See http://www.asterisk.org for more information about
|
||||||
|
* the Asterisk project. Please do not directly contact
|
||||||
|
* any of the maintainers of this project for assistance;
|
||||||
|
* the project provides a web site, mailing lists and IRC
|
||||||
|
* channels for your use.
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License Version 2. See the LICENSE file
|
||||||
|
* at the top of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*! \file
|
||||||
|
*
|
||||||
|
* \brief Stasis application command support.
|
||||||
|
*
|
||||||
|
* \author David M. Lee, II <dlee@digium.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "asterisk.h"
|
||||||
|
|
||||||
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||||
|
|
||||||
|
#include "command.h"
|
||||||
|
|
||||||
|
#include "asterisk/lock.h"
|
||||||
|
#include "asterisk/stasis_app_impl.h"
|
||||||
|
|
||||||
|
struct stasis_app_command {
|
||||||
|
ast_mutex_t lock;
|
||||||
|
ast_cond_t condition;
|
||||||
|
stasis_app_command_cb callback;
|
||||||
|
void *data;
|
||||||
|
void *retval;
|
||||||
|
int is_done:1;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void command_dtor(void *obj)
|
||||||
|
{
|
||||||
|
struct stasis_app_command *command = obj;
|
||||||
|
ast_mutex_destroy(&command->lock);
|
||||||
|
ast_cond_destroy(&command->condition);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct stasis_app_command *command_create(
|
||||||
|
stasis_app_command_cb callback, void *data)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
|
||||||
|
|
||||||
|
command = ao2_alloc(sizeof(*command), command_dtor);
|
||||||
|
if (!command) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ast_mutex_init(&command->lock);
|
||||||
|
ast_cond_init(&command->condition, 0);
|
||||||
|
command->callback = callback;
|
||||||
|
command->data = data;
|
||||||
|
|
||||||
|
ao2_ref(command, +1);
|
||||||
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void command_complete(struct stasis_app_command *command, void *retval)
|
||||||
|
{
|
||||||
|
SCOPED_MUTEX(lock, &command->lock);
|
||||||
|
|
||||||
|
command->is_done = 1;
|
||||||
|
command->retval = retval;
|
||||||
|
ast_cond_signal(&command->condition);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *command_join(struct stasis_app_command *command)
|
||||||
|
{
|
||||||
|
SCOPED_MUTEX(lock, &command->lock);
|
||||||
|
while (!command->is_done) {
|
||||||
|
ast_cond_wait(&command->condition, &command->lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
return command->retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
void command_invoke(struct stasis_app_command *command,
|
||||||
|
struct stasis_app_control *control, struct ast_channel *chan)
|
||||||
|
{
|
||||||
|
void *retval = command->callback(control, chan, command->data);
|
||||||
|
command_complete(command, retval);
|
||||||
|
}
|
||||||
|
|
42
res/stasis/command.h
Normal file
42
res/stasis/command.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Asterisk -- An open source telephony toolkit.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013, Digium, Inc.
|
||||||
|
*
|
||||||
|
* David M. Lee, II <dlee@digium.com>
|
||||||
|
*
|
||||||
|
* See http://www.asterisk.org for more information about
|
||||||
|
* the Asterisk project. Please do not directly contact
|
||||||
|
* any of the maintainers of this project for assistance;
|
||||||
|
* the project provides a web site, mailing lists and IRC
|
||||||
|
* channels for your use.
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License Version 2. See the LICENSE file
|
||||||
|
* at the top of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _ASTERISK_RES_STASIS_COMMAND_H
|
||||||
|
#define _ASTERISK_RES_STASIS_COMMAND_H
|
||||||
|
|
||||||
|
/*! \file
|
||||||
|
*
|
||||||
|
* \brief Internal API for the Stasis application commands.
|
||||||
|
*
|
||||||
|
* \author David M. Lee, II <dlee@digium.com>
|
||||||
|
* \since 12
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "asterisk/stasis_app_impl.h"
|
||||||
|
|
||||||
|
struct stasis_app_command;
|
||||||
|
|
||||||
|
struct stasis_app_command *command_create(
|
||||||
|
stasis_app_command_cb callback, void *data);
|
||||||
|
|
||||||
|
void command_invoke(struct stasis_app_command *command,
|
||||||
|
struct stasis_app_control *control, struct ast_channel *chan);
|
||||||
|
|
||||||
|
void *command_join(struct stasis_app_command *command);
|
||||||
|
|
||||||
|
#endif /* _ASTERISK_RES_STASIS_CONTROL_H */
|
199
res/stasis/control.c
Normal file
199
res/stasis/control.c
Normal file
@@ -0,0 +1,199 @@
|
|||||||
|
/*
|
||||||
|
* Asterisk -- An open source telephony toolkit.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013, Digium, Inc.
|
||||||
|
*
|
||||||
|
* David M. Lee, II <dlee@digium.com>
|
||||||
|
*
|
||||||
|
* See http://www.asterisk.org for more information about
|
||||||
|
* the Asterisk project. Please do not directly contact
|
||||||
|
* any of the maintainers of this project for assistance;
|
||||||
|
* the project provides a web site, mailing lists and IRC
|
||||||
|
* channels for your use.
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License Version 2. See the LICENSE file
|
||||||
|
* at the top of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*! \file
|
||||||
|
*
|
||||||
|
* \brief Stasis application control support.
|
||||||
|
*
|
||||||
|
* \author David M. Lee, II <dlee@digium.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "asterisk.h"
|
||||||
|
|
||||||
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||||
|
|
||||||
|
#include "asterisk/stasis_channels.h"
|
||||||
|
|
||||||
|
#include "command.h"
|
||||||
|
#include "control.h"
|
||||||
|
|
||||||
|
struct stasis_app_control {
|
||||||
|
/*! Queue of commands to dispatch on the channel */
|
||||||
|
struct ao2_container *command_queue;
|
||||||
|
/*!
|
||||||
|
* When set, /c app_stasis should exit and continue in the dialplan.
|
||||||
|
*/
|
||||||
|
int is_done:1;
|
||||||
|
/*!
|
||||||
|
* The associated channel.
|
||||||
|
* Be very careful with the threading associated w/ manipulating
|
||||||
|
* the channel.
|
||||||
|
*/
|
||||||
|
struct ast_channel *channel;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct stasis_app_control *control_create(struct ast_channel *channel)
|
||||||
|
{
|
||||||
|
struct stasis_app_control *control;
|
||||||
|
|
||||||
|
control = ao2_alloc(sizeof(*control), NULL);
|
||||||
|
if (!control) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
control->command_queue = ao2_container_alloc_list(0, 0, NULL, NULL);
|
||||||
|
|
||||||
|
control->channel = channel;
|
||||||
|
|
||||||
|
return control;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct stasis_app_command *exec_command(
|
||||||
|
struct stasis_app_control *control, stasis_app_command_cb command_fn,
|
||||||
|
void *data)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
|
||||||
|
|
||||||
|
command = command_create(command_fn, data);
|
||||||
|
|
||||||
|
if (!command) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ao2_lock(control);
|
||||||
|
ao2_ref(command, +1);
|
||||||
|
ao2_link(control->command_queue, command);
|
||||||
|
ao2_unlock(control);
|
||||||
|
|
||||||
|
ao2_ref(command, +1);
|
||||||
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
int control_is_done(struct stasis_app_control *control)
|
||||||
|
{
|
||||||
|
/* Called from stasis_app_exec thread; no lock needed */
|
||||||
|
return control->is_done;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *app_control_continue(struct stasis_app_control *control,
|
||||||
|
struct ast_channel *chan, void *data)
|
||||||
|
{
|
||||||
|
/* Called from stasis_app_exec thread; no lock needed */
|
||||||
|
control->is_done = 1;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void stasis_app_control_continue(struct stasis_app_control *control)
|
||||||
|
{
|
||||||
|
stasis_app_send_command_async(control, app_control_continue, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ast_channel_snapshot *stasis_app_control_get_snapshot(
|
||||||
|
const struct stasis_app_control *control)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
|
||||||
|
struct stasis_caching_topic *caching_topic;
|
||||||
|
struct ast_channel_snapshot *snapshot;
|
||||||
|
|
||||||
|
caching_topic = ast_channel_topic_all_cached();
|
||||||
|
ast_assert(caching_topic != NULL);
|
||||||
|
|
||||||
|
msg = stasis_cache_get(caching_topic, ast_channel_snapshot_type(),
|
||||||
|
stasis_app_control_get_channel_id(control));
|
||||||
|
if (!msg) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
snapshot = stasis_message_data(msg);
|
||||||
|
ast_assert(snapshot != NULL);
|
||||||
|
|
||||||
|
ao2_ref(snapshot, +1);
|
||||||
|
return snapshot;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *stasis_app_send_command(struct stasis_app_control *control,
|
||||||
|
stasis_app_command_cb command_fn, void *data)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
|
||||||
|
|
||||||
|
if (control == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
command = exec_command(control, command_fn, data);
|
||||||
|
if (!command) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return command_join(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
int stasis_app_send_command_async(struct stasis_app_control *control,
|
||||||
|
stasis_app_command_cb command_fn, void *data)
|
||||||
|
{
|
||||||
|
RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
|
||||||
|
|
||||||
|
if (control == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
command = exec_command(control, command_fn, data);
|
||||||
|
if (!command) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *stasis_app_control_get_channel_id(
|
||||||
|
const struct stasis_app_control *control)
|
||||||
|
{
|
||||||
|
return ast_channel_uniqueid(control->channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
void stasis_app_control_publish(
|
||||||
|
struct stasis_app_control *control, struct stasis_message *message)
|
||||||
|
{
|
||||||
|
if (!control || !control->channel || !message) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
stasis_publish(ast_channel_topic(control->channel), message);
|
||||||
|
}
|
||||||
|
|
||||||
|
int control_dispatch_all(struct stasis_app_control *control,
|
||||||
|
struct ast_channel *chan)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
struct ao2_iterator i;
|
||||||
|
void *obj;
|
||||||
|
|
||||||
|
SCOPED_AO2LOCK(lock, control);
|
||||||
|
|
||||||
|
ast_assert(control->channel == chan);
|
||||||
|
|
||||||
|
i = ao2_iterator_init(control->command_queue, AO2_ITERATOR_UNLINK);
|
||||||
|
|
||||||
|
while ((obj = ao2_iterator_next(&i))) {
|
||||||
|
RAII_VAR(struct stasis_app_command *, command, obj, ao2_cleanup);
|
||||||
|
command_invoke(command, control, chan);
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
|
||||||
|
ao2_iterator_destroy(&i);
|
||||||
|
return count;
|
||||||
|
}
|
55
res/stasis/control.h
Normal file
55
res/stasis/control.h
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Asterisk -- An open source telephony toolkit.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013, Digium, Inc.
|
||||||
|
*
|
||||||
|
* David M. Lee, II <dlee@digium.com>
|
||||||
|
*
|
||||||
|
* See http://www.asterisk.org for more information about
|
||||||
|
* the Asterisk project. Please do not directly contact
|
||||||
|
* any of the maintainers of this project for assistance;
|
||||||
|
* the project provides a web site, mailing lists and IRC
|
||||||
|
* channels for your use.
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License Version 2. See the LICENSE file
|
||||||
|
* at the top of the source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _ASTERISK_RES_STASIS_CONTROL_H
|
||||||
|
#define _ASTERISK_RES_STASIS_CONTROL_H
|
||||||
|
|
||||||
|
/*! \file
|
||||||
|
*
|
||||||
|
* \brief Internal API for the Stasis application controller.
|
||||||
|
*
|
||||||
|
* \author David M. Lee, II <dlee@digium.com>
|
||||||
|
* \since 12
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "asterisk/stasis_app.h"
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Create a control object.
|
||||||
|
*
|
||||||
|
* \param channel Channel to control.
|
||||||
|
* \return New control object.
|
||||||
|
* \return \c NULL on error.
|
||||||
|
*/
|
||||||
|
struct stasis_app_control *control_create(struct ast_channel *channel);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Dispatch all commands enqueued to this control.
|
||||||
|
*
|
||||||
|
* \param control Control object to dispatch.
|
||||||
|
* \param chan Associated channel.
|
||||||
|
* \return Number of commands executed
|
||||||
|
*/
|
||||||
|
int control_dispatch_all(struct stasis_app_control *control,
|
||||||
|
struct ast_channel *chan);
|
||||||
|
|
||||||
|
int control_is_done(struct stasis_app_control *control);
|
||||||
|
|
||||||
|
void control_continue(struct stasis_app_control *control);
|
||||||
|
|
||||||
|
#endif /* _ASTERISK_RES_STASIS_CONTROL_H */
|
Reference in New Issue
Block a user