2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * David M. Lee, II <dlee@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Stasis application support.
23 * \author David M. Lee, II <dlee@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/stasis_app.h"
33 #include "asterisk/stasis_channels.h"
36 * \brief Number of buckets for the channels container for app instances. Remember
37 * to keep it a prime number!
39 #define APP_CHANNELS_BUCKETS 7
42 /*! Callback function for this application. */
43 stasis_app_cb handler;
44 /*! Opaque data to hand to callback function. */
46 /*! List of channel identifiers this app instance is interested in */
47 struct ao2_container *channels;
48 /*! Name of the Stasis application */
52 static void app_dtor(void *obj)
54 struct app *app = obj;
56 ao2_cleanup(app->data);
58 ao2_cleanup(app->channels);
62 struct app *app_create(const char *name, stasis_app_cb handler, void *data)
64 RAII_VAR(struct app *, app, NULL, ao2_cleanup);
67 ast_assert(name != NULL);
68 ast_assert(handler != NULL);
70 size = sizeof(*app) + strlen(name) + 1;
71 app = ao2_alloc_options(size, app_dtor, AO2_ALLOC_OPT_LOCK_MUTEX);
77 strncpy(app->name, name, size - sizeof(*app));
78 app->handler = handler;
82 app->channels = ast_str_container_alloc(APP_CHANNELS_BUCKETS);
91 int app_add_channel(struct app *app, const struct ast_channel *chan)
94 ast_assert(chan != NULL);
95 ast_assert(app != NULL);
97 uniqueid = ast_channel_uniqueid(chan);
98 return ast_str_container_add(app->channels, uniqueid) ? -1 : 0;
101 void app_remove_channel(struct app* app, const struct ast_channel *chan)
103 ast_assert(chan != NULL);
104 ast_assert(app != NULL);
106 ao2_find(app->channels, ast_channel_uniqueid(chan), OBJ_KEY | OBJ_NODATA | OBJ_UNLINK);
110 * \brief Send a message to the given application.
111 * \param app App to send the message to.
112 * \param message Message to send.
114 void app_send(struct app *app, struct ast_json *message)
116 app->handler(app->data, app->name, message);
119 void app_update(struct app *app, stasis_app_cb handler, void *data)
121 SCOPED_AO2LOCK(lock, app);
123 app->handler = handler;
124 ao2_cleanup(app->data);
129 const char *app_name(const struct app *app)
134 int app_is_watching_channel(struct app *app, const char *uniqueid)
136 RAII_VAR(char *, found, NULL, ao2_cleanup);
137 found = ao2_find(app->channels, uniqueid, OBJ_KEY);
138 return found != NULL;