[asterisk-commits] rmudgett: branch mjordan/cdrs-of-doom r388750 - in /team/mjordan/cdrs-of-doom...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue May 14 18:01:51 CDT 2013
Author: rmudgett
Date: Tue May 14 18:01:45 2013
New Revision: 388750
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=388750
Log:
Fix automerge conflicts and compiler warnings/errors.
Multiple revisions 388703,388732
........
r388703 | root | 2013-05-14 14:17:20 -0500 (Tue, 14 May 2013) | 17 lines
Make ao2 global objects not always use the debug version of the ao2_ref() calls.
The debug versions of ao2_ref() should only be used if REF_DEBUG is
enabled so nothing is written to /tmp/refs unexpectedly.
(closes issue ASTERISK_21785)
Reported by: abelbeck
Patches:
jira_asterisk_21785_v11.patch (license #5621) patch uploaded by rmudgett
Tested by: abelbeck
........
Merged revisions 388700 from http://svn.asterisk.org/svn/asterisk/branches/11
........
Merged revisions 388701 from file:///srv/subversion/repos/asterisk/trunk
........
r388732 | root | 2013-05-14 17:17:20 -0500 (Tue, 14 May 2013) | 23 lines
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/
........
Merged revisions 388729 from file:///srv/subversion/repos/asterisk/trunk
........
Merged revisions 388703,388732 from http://svn.asterisk.org/svn/asterisk/team/group/bridge_construction
Added:
team/mjordan/cdrs-of-doom/include/asterisk/stasis_app_impl.h
- copied unchanged from r388732, team/group/bridge_construction/include/asterisk/stasis_app_impl.h
team/mjordan/cdrs-of-doom/res/res_stasis_answer.c
- copied, changed from r388732, team/group/bridge_construction/res/res_stasis_answer.c
team/mjordan/cdrs-of-doom/res/stasis/ (props changed)
- copied from r388732, team/group/bridge_construction/res/stasis/
team/mjordan/cdrs-of-doom/res/stasis/app.c
- copied unchanged from r388732, team/group/bridge_construction/res/stasis/app.c
team/mjordan/cdrs-of-doom/res/stasis/app.h
- copied unchanged from r388732, team/group/bridge_construction/res/stasis/app.h
team/mjordan/cdrs-of-doom/res/stasis/command.c
- copied unchanged from r388732, team/group/bridge_construction/res/stasis/command.c
team/mjordan/cdrs-of-doom/res/stasis/command.h
- copied unchanged from r388732, team/group/bridge_construction/res/stasis/command.h
team/mjordan/cdrs-of-doom/res/stasis/control.c
- copied unchanged from r388732, team/group/bridge_construction/res/stasis/control.c
team/mjordan/cdrs-of-doom/res/stasis/control.h
- copied unchanged from r388732, team/group/bridge_construction/res/stasis/control.h
Modified:
team/mjordan/cdrs-of-doom/ (props changed)
team/mjordan/cdrs-of-doom/apps/app_stasis.c
team/mjordan/cdrs-of-doom/channels/chan_misdn.c
team/mjordan/cdrs-of-doom/include/asterisk/astobj2.h
team/mjordan/cdrs-of-doom/include/asterisk/module.h
team/mjordan/cdrs-of-doom/include/asterisk/stasis_app.h
team/mjordan/cdrs-of-doom/main/astobj2.c
team/mjordan/cdrs-of-doom/main/cdr.c
team/mjordan/cdrs-of-doom/res/Makefile
team/mjordan/cdrs-of-doom/res/res_stasis.c
Propchange: team/mjordan/cdrs-of-doom/
------------------------------------------------------------------------------
Binary property 'branch-11-merged' - no diff available.
Propchange: team/mjordan/cdrs-of-doom/
------------------------------------------------------------------------------
--- bridge_construction-integrated (original)
+++ bridge_construction-integrated Tue May 14 18:01:45 2013
@@ -1,1 +1,1 @@
-/trunk:1-388669
+/trunk:1-388731
Propchange: team/mjordan/cdrs-of-doom/
------------------------------------------------------------------------------
--- cdrs-of-doom-integrated (original)
+++ cdrs-of-doom-integrated Tue May 14 18:01:45 2013
@@ -1,1 +1,1 @@
-/team/group/bridge_construction:1-388694
+/team/group/bridge_construction:1-388749
Modified: team/mjordan/cdrs-of-doom/apps/app_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/mjordan/cdrs-of-doom/apps/app_stasis.c?view=diff&rev=388750&r1=388749&r2=388750
==============================================================================
--- team/mjordan/cdrs-of-doom/apps/app_stasis.c (original)
+++ team/mjordan/cdrs-of-doom/apps/app_stasis.c Tue May 14 18:01:45 2013
@@ -35,7 +35,7 @@
#include "asterisk/app.h"
#include "asterisk/module.h"
#include "asterisk/stasis.h"
-#include "asterisk/stasis_app.h"
+#include "asterisk/stasis_app_impl.h"
/*** DOCUMENTATION
<application name="Stasis" language="en_US">
Modified: team/mjordan/cdrs-of-doom/channels/chan_misdn.c
URL: http://svnview.digium.com/svn/asterisk/team/mjordan/cdrs-of-doom/channels/chan_misdn.c?view=diff&rev=388750&r1=388749&r2=388750
==============================================================================
--- team/mjordan/cdrs-of-doom/channels/chan_misdn.c (original)
+++ team/mjordan/cdrs-of-doom/channels/chan_misdn.c Tue May 14 18:01:45 2013
@@ -10154,7 +10154,6 @@
if (digits) {
strncat(bc->dialed.number, bc->info_dad, sizeof(bc->dialed.number) - strlen(bc->dialed.number) - 1);
ast_channel_exten_set(ch->ast, bc->dialed.number);
- ast_cdr_update(ch->ast);
}
ast_queue_frame(ch->ast, &fr);
Modified: team/mjordan/cdrs-of-doom/include/asterisk/astobj2.h
URL: http://svnview.digium.com/svn/asterisk/team/mjordan/cdrs-of-doom/include/asterisk/astobj2.h?view=diff&rev=388750&r1=388749&r2=388750
==============================================================================
--- team/mjordan/cdrs-of-doom/include/asterisk/astobj2.h (original)
+++ team/mjordan/cdrs-of-doom/include/asterisk/astobj2.h Tue May 14 18:01:45 2013
@@ -635,11 +635,20 @@
*
* \return Nothing
*/
+#ifdef REF_DEBUG
#define ao2_t_global_obj_release(holder, tag) \
__ao2_global_obj_release(&holder, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
#define ao2_global_obj_release(holder) \
__ao2_global_obj_release(&holder, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#else
+
+#define ao2_t_global_obj_release(holder, tag) \
+ __ao2_global_obj_release(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#define ao2_global_obj_release(holder) \
+ __ao2_global_obj_release(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#endif
+
void __ao2_global_obj_release(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name);
/*!
@@ -657,11 +666,20 @@
* \retval Reference to previous global ao2 object stored.
* \retval NULL if no object available.
*/
+#ifdef REF_DEBUG
#define ao2_t_global_obj_replace(holder, obj, tag) \
__ao2_global_obj_replace(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
#define ao2_global_obj_replace(holder, obj) \
__ao2_global_obj_replace(&holder, (obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#else
+
+#define ao2_t_global_obj_replace(holder, obj, tag) \
+ __ao2_global_obj_replace(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#define ao2_global_obj_replace(holder, obj) \
+ __ao2_global_obj_replace(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#endif
+
void *__ao2_global_obj_replace(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name);
/*!
@@ -680,10 +698,20 @@
* \retval 0 The global object was previously empty
* \retval 1 The global object was not previously empty
*/
+#ifdef REF_DEBUG
#define ao2_t_global_obj_replace_unref(holder, obj, tag) \
__ao2_global_obj_replace_unref(&holder, (obj), (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
#define ao2_global_obj_replace_unref(holder, obj) \
__ao2_global_obj_replace_unref(&holder, (obj), "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+
+#else
+
+#define ao2_t_global_obj_replace_unref(holder, obj, tag) \
+ __ao2_global_obj_replace_unref(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#define ao2_global_obj_replace_unref(holder, obj) \
+ __ao2_global_obj_replace_unref(&holder, (obj), NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#endif
+
int __ao2_global_obj_replace_unref(struct ao2_global_obj *holder, void *obj, const char *tag, const char *file, int line, const char *func, const char *name);
/*!
@@ -696,10 +724,19 @@
* \retval Reference to current ao2 object stored in the holder.
* \retval NULL if no object available.
*/
+#ifdef REF_DEBUG
#define ao2_t_global_obj_ref(holder, tag) \
__ao2_global_obj_ref(&holder, (tag), __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
#define ao2_global_obj_ref(holder) \
__ao2_global_obj_ref(&holder, "", __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+
+#else
+
+#define ao2_t_global_obj_ref(holder, tag) \
+ __ao2_global_obj_ref(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#define ao2_global_obj_ref(holder) \
+ __ao2_global_obj_ref(&holder, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__, #holder)
+#endif
void *__ao2_global_obj_ref(struct ao2_global_obj *holder, const char *tag, const char *file, int line, const char *func, const char *name);
Modified: team/mjordan/cdrs-of-doom/include/asterisk/module.h
URL: http://svnview.digium.com/svn/asterisk/team/mjordan/cdrs-of-doom/include/asterisk/module.h?view=diff&rev=388750&r1=388749&r2=388750
==============================================================================
--- team/mjordan/cdrs-of-doom/include/asterisk/module.h (original)
+++ team/mjordan/cdrs-of-doom/include/asterisk/module.h Tue May 14 18:01:45 2013
@@ -478,6 +478,9 @@
*/
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)
}
Modified: team/mjordan/cdrs-of-doom/include/asterisk/stasis_app.h
URL: http://svnview.digium.com/svn/asterisk/team/mjordan/cdrs-of-doom/include/asterisk/stasis_app.h?view=diff&rev=388750&r1=388749&r2=388750
==============================================================================
--- team/mjordan/cdrs-of-doom/include/asterisk/stasis_app.h (original)
+++ team/mjordan/cdrs-of-doom/include/asterisk/stasis_app.h Tue May 14 18:01:45 2013
@@ -53,27 +53,6 @@
#include "asterisk/channel.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 @@
* \param message Message to handle. (borrowed copy)
*/
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.
@@ -148,6 +127,16 @@
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.
*
* If the channel is no longer in \c res_stasis, this function does nothing.
@@ -160,20 +149,30 @@
* \brief Answer the channel associated with this control.
* \param control Control for \c res_stasis.
* \return 0 for success.
- * \return -1 for error.
+ * \return Non-zero for error.
*/
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.
- * \return JSON object representing channel snapshot.
- * \return \c NULL on error
+ * \brief Publish a message to the \a control's channel's topic.
+ *
+ * \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.
Modified: team/mjordan/cdrs-of-doom/main/astobj2.c
URL: http://svnview.digium.com/svn/asterisk/team/mjordan/cdrs-of-doom/main/astobj2.c?view=diff&rev=388750&r1=388749&r2=388750
==============================================================================
--- team/mjordan/cdrs-of-doom/main/astobj2.c (original)
+++ team/mjordan/cdrs-of-doom/main/astobj2.c Tue May 14 18:01:45 2013
@@ -684,7 +684,11 @@
/* Release the held ao2 object. */
if (holder->obj) {
- __ao2_ref_debug(holder->obj, -1, tag, file, line, func);
+ if (tag) {
+ __ao2_ref_debug(holder->obj, -1, tag, file, line, func);
+ } else {
+ __ao2_ref(holder->obj, -1);
+ }
holder->obj = NULL;
}
@@ -708,7 +712,11 @@
}
if (obj) {
- __ao2_ref_debug(obj, +1, tag, file, line, func);
+ if (tag) {
+ __ao2_ref_debug(obj, +1, tag, file, line, func);
+ } else {
+ __ao2_ref(obj, +1);
+ }
}
obj_old = holder->obj;
holder->obj = obj;
@@ -724,7 +732,11 @@
obj_old = __ao2_global_obj_replace(holder, obj, tag, file, line, func, name);
if (obj_old) {
- __ao2_ref_debug(obj_old, -1, tag, file, line, func);
+ if (tag) {
+ __ao2_ref_debug(obj_old, -1, tag, file, line, func);
+ } else {
+ __ao2_ref(obj_old, -1);
+ }
return 1;
}
return 0;
@@ -749,7 +761,11 @@
obj = holder->obj;
if (obj) {
- __ao2_ref_debug(obj, +1, tag, file, line, func);
+ if (tag) {
+ __ao2_ref_debug(obj, +1, tag, file, line, func);
+ } else {
+ __ao2_ref(obj, +1);
+ }
}
__ast_rwlock_unlock(file, line, func, &holder->lock, name);
Modified: team/mjordan/cdrs-of-doom/main/cdr.c
URL: http://svnview.digium.com/svn/asterisk/team/mjordan/cdrs-of-doom/main/cdr.c?view=diff&rev=388750&r1=388749&r2=388750
==============================================================================
--- team/mjordan/cdrs-of-doom/main/cdr.c (original)
+++ team/mjordan/cdrs-of-doom/main/cdr.c Tue May 14 18:01:45 2013
@@ -928,7 +928,8 @@
*/
static struct ast_cdr *cdr_object_create_public_records(struct cdr_object *cdr)
{
- struct ast_cdr *pub_cdr = NULL, *cdr_prev;
+ struct ast_cdr *pub_cdr = NULL;
+ struct ast_cdr *cdr_prev = NULL;
struct ast_var_t *it_var, *it_copy_var;
struct ast_channel_snapshot *party_a;
struct ast_channel_snapshot *party_b;
Modified: team/mjordan/cdrs-of-doom/res/Makefile
URL: http://svnview.digium.com/svn/asterisk/team/mjordan/cdrs-of-doom/res/Makefile?view=diff&rev=388750&r1=388749&r2=388750
==============================================================================
--- team/mjordan/cdrs-of-doom/res/Makefile (original)
+++ team/mjordan/cdrs-of-doom/res/Makefile Tue May 14 18:01:45 2013
@@ -46,6 +46,9 @@
$(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)
+$(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)),)
ael/ael_lex.c: ael/ael.flex
else
@@ -70,7 +73,8 @@
ael/pval.o: ael/pval.c
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]
rm -f parking/*.o parking/*.i
$(if $(filter res_parking,$(EMBEDDED_MODS)),modules.link,res_parking.so): $(subst .c,.o,$(wildcard parking/*.c))
Modified: team/mjordan/cdrs-of-doom/res/res_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/mjordan/cdrs-of-doom/res/res_stasis.c?view=diff&rev=388750&r1=388749&r2=388750
==============================================================================
--- team/mjordan/cdrs-of-doom/res/res_stasis.c (original)
+++ team/mjordan/cdrs-of-doom/res/res_stasis.c Tue May 14 18:01:45 2013
@@ -21,6 +21,30 @@
* \brief Stasis application support.
*
* \author David M. Lee, II <dlee at 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
@@ -33,15 +57,14 @@
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/astobj2.h"
-#include "asterisk/channel.h"
-#include "asterisk/lock.h"
+#include "asterisk/callerid.h"
#include "asterisk/module.h"
-#include "asterisk/stasis.h"
-#include "asterisk/stasis_app.h"
+#include "asterisk/stasis_app_impl.h"
#include "asterisk/stasis_channels.h"
+#include "asterisk/stasis_message_router.h"
#include "asterisk/strings.h"
-#include "asterisk/stasis_message_router.h"
-#include "asterisk/callerid.h"
+#include "stasis/app.h"
+#include "stasis/control.h"
#include "stasis_json/resource_events.h"
/*! Time to wait for a frame in the application */
@@ -60,97 +83,26 @@
#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
* it a prime number!
*/
#define BLOB_HANDLER_BUCKETS 7
/*!
- * \brief Stasis application container. Please call apps_registry() instead of
- * directly accessing.
+ * \brief Stasis application container.
*/
-struct ao2_container *__apps_registry;
-
-struct ao2_container *__app_controls;
+struct ao2_container *apps_registry;
+
+struct ao2_container *app_controls;
/*! \brief Message router for the channel caching topic */
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 */
static int app_hash(const void *obj, const int flags)
{
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);
}
@@ -160,149 +112,22 @@
{
const struct app *lhs_app = lhs;
const struct app *rhs_app = rhs;
- const char *rhs_name = flags & OBJ_KEY ? rhs : rhs_app->name;
-
- if (strcmp(lhs_app->name, rhs_name) == 0) {
+ const char *lhs_name = app_name(lhs_app);
+ const char *rhs_name = flags & OBJ_KEY ? rhs : app_name(rhs_app);
+
+ if (strcmp(lhs_name, rhs_name) == 0) {
return CMP_MATCH | CMP_STOP;
} else {
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 */
static int control_hash(const void *obj, const int flags)
{
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);
}
@@ -312,10 +137,11 @@
{
const struct stasis_app_control *lhs_control = lhs;
const struct stasis_app_control *rhs_control = rhs;
- const char *rhs_name =
- flags & OBJ_KEY ? rhs : rhs_control->channel_id;
-
- if (strcmp(lhs_control->channel_id, rhs_name) == 0) {
+ const char *lhs_id = stasis_app_control_get_channel_id(lhs_control);
+ const char *rhs_id = flags & OBJ_KEY ?
+ rhs : stasis_app_control_get_channel_id(rhs_control);
+
+ if (strcmp(lhs_id, rhs_id) == 0) {
return CMP_MATCH | CMP_STOP;
} else {
return 0;
@@ -336,68 +162,222 @@
struct stasis_app_control *stasis_app_control_find_by_channel_id(
const char *channel_id)
{
- RAII_VAR(struct ao2_container *, controls, NULL, ao2_cleanup);
- 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;
+ return ao2_find(app_controls, channel_id, OBJ_KEY);
}
/*! \brief Typedef for blob handler callbacks */
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) == 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[])
+static int app_watching_channel_cb(void *obj, void *arg, int flags)
+{
+ struct app *app = obj;
+ char *uniqueid = arg;
+
+ return app_is_watching_channel(app, uniqueid) ? CMP_MATCH : 0;
+}
+
+static struct ao2_container *get_watching_apps(const char *uniqueid)
+{
+ struct ao2_container *watching_apps;
+ char *uniqueid_dup;
+ RAII_VAR(struct ao2_iterator *,watching_apps_iter, NULL, ao2_iterator_destroy);
+ ast_assert(uniqueid != NULL);
+
+ uniqueid_dup = ast_strdupa(uniqueid);
+
+ watching_apps_iter = ao2_callback(apps_registry, OBJ_MULTIPLE, app_watching_channel_cb, uniqueid_dup);
+ watching_apps = watching_apps_iter->c;
+
+ if (!ao2_container_count(watching_apps)) {
+ return NULL;
+ }
+
+ ao2_ref(watching_apps, +1);
+ return watching_apps_iter->c;
+}
+
+/*! \brief Typedef for callbacks that get called on channel snapshot updates */
+typedef struct ast_json *(*channel_snapshot_monitor)(
+ struct ast_channel_snapshot *old_snapshot,
+ struct ast_channel_snapshot *new_snapshot);
+
+/*! \brief Handle channel state changes */
+static struct ast_json *channel_state(
+ struct ast_channel_snapshot *old_snapshot,
+ struct ast_channel_snapshot *new_snapshot)
+{
+ RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+ struct ast_channel_snapshot *snapshot = new_snapshot ? new_snapshot : old_snapshot;
+
+ if (!old_snapshot) {
+ return stasis_json_event_channel_created_create(snapshot);
+ } else if (!new_snapshot) {
+ json = ast_json_pack("{s: i, s: s}",
+ "cause", snapshot->hangupcause,
+ "cause_txt", ast_cause2str(snapshot->hangupcause));
+ if (!json) {
+ return NULL;
+ }
+ return stasis_json_event_channel_destroyed_create(snapshot, json);
+ } else if (old_snapshot->state != new_snapshot->state) {
+ return stasis_json_event_channel_state_change_create(snapshot);
+ }
+
+ return NULL;
+}
+
+static struct ast_json *channel_dialplan(
+ struct ast_channel_snapshot *old_snapshot,
+ struct ast_channel_snapshot *new_snapshot)
+{
+ RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+
+ /* No Newexten event on cache clear */
+ if (!new_snapshot) {
+ return NULL;
+ }
+
+ /* Empty application is not valid for a Newexten event */
+ if (ast_strlen_zero(new_snapshot->appl)) {
+ return NULL;
+ }
+
+ if (old_snapshot && ast_channel_snapshot_cep_equal(old_snapshot, new_snapshot)) {
+ return NULL;
+ }
+
+ json = ast_json_pack("{s: s, s: s}",
+ "application", new_snapshot->appl,
+ "application_data", new_snapshot->data);
+ if (!json) {
+ return NULL;
+ }
+
+ return stasis_json_event_channel_dialplan_create(new_snapshot, json);
+}
+
+static struct ast_json *channel_callerid(
+ struct ast_channel_snapshot *old_snapshot,
+ struct ast_channel_snapshot *new_snapshot)
+{
+ RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+
+ /* No NewCallerid event on cache clear or first event */
+ if (!old_snapshot || !new_snapshot) {
+ return NULL;
+ }
+
+ if (ast_channel_snapshot_caller_id_equal(old_snapshot, new_snapshot)) {
+ return NULL;
+ }
+
+ json = ast_json_pack("{s: i, s: s}",
+ "caller_presentation", new_snapshot->caller_pres,
+ "caller_presentation_txt", ast_describe_caller_presentation(new_snapshot->caller_pres));
+ if (!json) {
+ return NULL;
+ }
+
+ return stasis_json_event_channel_caller_id_create(new_snapshot, json);
+}
+
+static struct ast_json *channel_snapshot(
+ struct ast_channel_snapshot *old_snapshot,
+ struct ast_channel_snapshot *new_snapshot)
+{
+ if (!new_snapshot) {
+ return NULL;
+ }
+
+ return stasis_json_event_channel_snapshot_create(new_snapshot);
+}
+
+channel_snapshot_monitor channel_monitors[] = {
+ channel_snapshot,
+ channel_state,
+ channel_dialplan,
+ channel_callerid
+};
+
+static int app_send_cb(void *obj, void *arg, int flags)
+{
+ struct app *app = obj;
+ struct ast_json *msg = arg;
+
+ app_send(app, msg);
+ return 0;
+}
+
+static void sub_snapshot_handler(void *data,
+ struct stasis_subscription *sub,
+ struct stasis_topic *topic,
+ struct stasis_message *message)
+{
+ RAII_VAR(struct ao2_container *, watching_apps, NULL, ao2_cleanup);
+ struct stasis_cache_update *update = stasis_message_data(message);
+ struct ast_channel_snapshot *new_snapshot = stasis_message_data(update->new_snapshot);
+ struct ast_channel_snapshot *old_snapshot = stasis_message_data(update->old_snapshot);
+ int i;
+
+ watching_apps = get_watching_apps(new_snapshot ? new_snapshot->uniqueid : old_snapshot->uniqueid);
+ if (!watching_apps) {
+ return;
+ }
+
+ for (i = 0; i < ARRAY_LEN(channel_monitors); ++i) {
+ RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+
+ msg = channel_monitors[i](old_snapshot, new_snapshot);
+ if (msg) {
+ ao2_callback(watching_apps, OBJ_NODATA, app_send_cb, msg);
+ }
+ }
+}
+
+static void distribute_message(struct ao2_container *apps, struct ast_json *msg)
+{
+ ao2_callback(apps, OBJ_NODATA, app_send_cb, msg);
+}
+
+static void generic_blob_handler(struct ast_channel_blob *obj, channel_blob_handler_cb handler_cb)
+{
+ RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+ RAII_VAR(struct ao2_container *, watching_apps, NULL, ao2_cleanup);
+
+ if (!obj->snapshot) {
+ return;
+ }
+
+ watching_apps = get_watching_apps(obj->snapshot->uniqueid);
+ if (!watching_apps) {
+ return;
+ }
+
+ msg = handler_cb(obj);
+ if (!msg) {
+ return;
+ }
+
+ distribute_message(watching_apps, msg);
+}
+
+/*!
+ * \brief In addition to running ao2_cleanup(), this function also removes the
+ * object from the app_controls() container.
+ */
+static void control_unlink(struct stasis_app_control *control)
+{
+ if (!control) {
+ return;
+ }
+
+ ao2_unlink_flags(app_controls, control,
+ OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
+ ao2_cleanup(control);
+}
+
+int app_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);
@@ -440,7 +420,7 @@
return 0;
}
-static int send_end_msg(struct app *app, struct ast_channel *chan)
+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);
@@ -462,275 +442,36 @@
return 0;
}
-static int app_watching_channel_cb(void *obj, void *arg, int flags)
-{
- RAII_VAR(char *, uniqueid, NULL, ao2_cleanup);
- struct app *app = obj;
- char *chan_uniqueid = arg;
-
- uniqueid = ao2_find(app->channels, chan_uniqueid, OBJ_KEY);
- return uniqueid ? CMP_MATCH : 0;
-}
-
-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;
- char *uniqueid_dup;
- RAII_VAR(struct ao2_iterator *,watching_apps_iter, NULL, ao2_iterator_destroy);
- ast_assert(uniqueid != NULL);
- ast_assert(apps != NULL);
-
- uniqueid_dup = ast_strdupa(uniqueid);
-
- watching_apps_iter = ao2_callback(apps, OBJ_MULTIPLE, app_watching_channel_cb, uniqueid_dup);
- watching_apps = watching_apps_iter->c;
-
- if (!ao2_container_count(watching_apps)) {
- return NULL;
- }
-
- ao2_ref(watching_apps, +1);
- return watching_apps_iter->c;
-}
-
-/*! \brief Typedef for callbacks that get called on channel snapshot updates */
-typedef struct ast_json *(*channel_snapshot_monitor)(
- struct ast_channel_snapshot *old_snapshot,
- struct ast_channel_snapshot *new_snapshot);
-
-/*! \brief Handle channel state changes */
-static struct ast_json *channel_state(
- struct ast_channel_snapshot *old_snapshot,
- struct ast_channel_snapshot *new_snapshot)
-{
- RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
- struct ast_channel_snapshot *snapshot = new_snapshot ? new_snapshot : old_snapshot;
-
- if (!old_snapshot) {
- return stasis_json_event_channel_created_create(snapshot);
- } else if (!new_snapshot) {
- json = ast_json_pack("{s: i, s: s}",
- "cause", snapshot->hangupcause,
- "cause_txt", ast_cause2str(snapshot->hangupcause));
- if (!json) {
- return NULL;
- }
- return stasis_json_event_channel_destroyed_create(snapshot, json);
- } else if (old_snapshot->state != new_snapshot->state) {
- return stasis_json_event_channel_state_change_create(snapshot);
- }
-
- return NULL;
-}
-
-static struct ast_json *channel_dialplan(
- struct ast_channel_snapshot *old_snapshot,
- struct ast_channel_snapshot *new_snapshot)
-{
- RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
-
- /* No Newexten event on cache clear */
- if (!new_snapshot) {
- return NULL;
- }
-
- /* Empty application is not valid for a Newexten event */
- if (ast_strlen_zero(new_snapshot->appl)) {
- return NULL;
- }
-
- if (old_snapshot && ast_channel_snapshot_cep_equal(old_snapshot, new_snapshot)) {
- return NULL;
- }
-
- json = ast_json_pack("{s: s, s: s}",
- "application", new_snapshot->appl,
- "application_data", new_snapshot->data);
- if (!json) {
- return NULL;
- }
-
- return stasis_json_event_channel_dialplan_create(new_snapshot, json);
-}
-
-static struct ast_json *channel_callerid(
- struct ast_channel_snapshot *old_snapshot,
- struct ast_channel_snapshot *new_snapshot)
-{
- RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
-
- /* No NewCallerid event on cache clear or first event */
- if (!old_snapshot || !new_snapshot) {
- return NULL;
- }
-
- if (ast_channel_snapshot_caller_id_equal(old_snapshot, new_snapshot)) {
- return NULL;
- }
-
- json = ast_json_pack("{s: i, s: s}",
- "caller_presentation", new_snapshot->caller_pres,
- "caller_presentation_txt", ast_describe_caller_presentation(new_snapshot->caller_pres));
- if (!json) {
- return NULL;
- }
-
- return stasis_json_event_channel_caller_id_create(new_snapshot, json);
-}
-
-static struct ast_json *channel_snapshot(
- struct ast_channel_snapshot *old_snapshot,
- struct ast_channel_snapshot *new_snapshot)
-{
- if (!new_snapshot) {
- return NULL;
- }
-
- return stasis_json_event_channel_snapshot_create(new_snapshot);
-}
-
-channel_snapshot_monitor channel_monitors[] = {
- channel_snapshot,
- channel_state,
- channel_dialplan,
- channel_callerid
-};
-
-static int app_send_cb(void *obj, void *arg, int flags)
-{
- struct app *app = obj;
- struct ast_json *msg = arg;
-
- app_send(app, msg);
- return 0;
-}
-
-static void sub_snapshot_handler(void *data,
- struct stasis_subscription *sub,
- struct stasis_topic *topic,
- struct stasis_message *message)
-{
- RAII_VAR(struct ao2_container *, watching_apps, NULL, ao2_cleanup);
- struct stasis_cache_update *update = stasis_message_data(message);
- struct ast_channel_snapshot *new_snapshot = stasis_message_data(update->new_snapshot);
- struct ast_channel_snapshot *old_snapshot = stasis_message_data(update->old_snapshot);
- int i;
-
- watching_apps = get_watching_apps(new_snapshot ? new_snapshot->uniqueid : old_snapshot->uniqueid);
- if (!watching_apps) {
- return;
- }
-
- for (i = 0; i < ARRAY_LEN(channel_monitors); ++i) {
- RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
-
- msg = channel_monitors[i](old_snapshot, new_snapshot);
- if (msg) {
- ao2_callback(watching_apps, OBJ_NODATA, app_send_cb, msg);
- }
- }
-}
-
-static void distribute_message(struct ao2_container *apps, struct ast_json *msg)
-{
- ao2_callback(apps, OBJ_NODATA, app_send_cb, msg);
-}
-
-static void generic_blob_handler(struct ast_channel_blob *obj, channel_blob_handler_cb handler_cb)
-{
- RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
[... 307 lines stripped ...]
More information about the asterisk-commits
mailing list