summaryrefslogtreecommitdiff
path: root/res/stasis/app.c
blob: 6f80ed64ab6c7250662ce532aaa259085fe1582a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * 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

/*!
 * \brief Number of buckets for the bridges container for app instances.  Remember
 * to keep it a prime number!
 */
#define APP_BRIDGES_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;
	/*! List of bridge identifiers this app instance owns */
	struct ao2_container *bridges;
	/*! 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;
	ao2_cleanup(app->bridges);
	app->bridges = 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);

	ast_verb(1, "Creating Stasis app '%s'\n", name);

	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;
	}

	app->bridges = ast_str_container_alloc(APP_BRIDGES_BUCKETS);
	if (!app->bridges) {
		return NULL;
	}

	ao2_ref(app, +1);
	return app;
}

int app_add_channel(struct app *app, const struct ast_channel *chan)
{
	SCOPED_AO2LOCK(lock, app);
	const char *uniqueid;

	ast_assert(app != NULL);
	ast_assert(chan != NULL);

	/* Don't accept new channels in an inactive application */
	if (!app->handler) {
		return -1;
	}

	uniqueid = ast_channel_uniqueid(chan);
	return ast_str_container_add(app->channels, uniqueid) ? -1 : 0;
}

void app_remove_channel(struct app* app, const struct ast_channel *chan)
{
	SCOPED_AO2LOCK(lock, app);

	ast_assert(app != NULL);
	ast_assert(chan != NULL);

	ao2_find(app->channels, ast_channel_uniqueid(chan), OBJ_KEY | OBJ_NODATA | OBJ_UNLINK);
}

int app_add_bridge(struct app *app, const char *uniqueid)
{
	SCOPED_AO2LOCK(lock, app);

	ast_assert(app != NULL);
	ast_assert(uniqueid != NULL);

	/* Don't accept new bridges in an inactive application */
	if (!app->handler) {
		return -1;
	}

	return ast_str_container_add(app->bridges, uniqueid) ? -1 : 0;
}

void app_remove_bridge(struct app* app, const char *uniqueid)
{
	SCOPED_AO2LOCK(lock, app);

	ast_assert(app != NULL);
	ast_assert(uniqueid != NULL);

	ao2_find(app->bridges, uniqueid, OBJ_KEY | OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE);
}

/*!
 * \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)
{
	stasis_app_cb handler;
	RAII_VAR(void *, data, NULL, ao2_cleanup);

	/* Copy off mutable state with lock held */
	{
		SCOPED_AO2LOCK(lock, app);
		handler = app->handler;
		if (app->data) {
			ao2_ref(app->data, +1);
			data = app->data;
		}
		/* Name is immutable; no need to copy */
	}

	if (!handler) {
		ast_verb(3,
			"Inactive Stasis app '%s' missed message\n", app->name);
		return;
	}

	handler(data, app->name, message);
}

void app_deactivate(struct app *app)
{
	SCOPED_AO2LOCK(lock, app);
	ast_verb(1, "Deactivating Stasis app '%s'\n", app->name);
	app->handler = NULL;
	ao2_cleanup(app->data);
	app->data = NULL;
}

int app_is_active(struct app *app)
{
	SCOPED_AO2LOCK(lock, app);
	return app->handler != NULL;
}

int app_is_finished(struct app *app)
{
	SCOPED_AO2LOCK(lock, app);

	return app->handler == NULL &&
		ao2_container_count(app->channels) == 0;
}

void app_update(struct app *app, stasis_app_cb handler, void *data)
{
	SCOPED_AO2LOCK(lock, app);

	if (app->handler) {
		RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);

		ast_verb(1, "Replacing Stasis app '%s'\n", app->name);

		msg = ast_json_pack("{s: s, s: s}",
			"type", "ApplicationReplaced",
			"application", app->name);
		if (msg) {
			app_send(app, msg);
		}
	} else {
		ast_verb(1, "Activating Stasis app '%s'\n", app->name);
	}


	app->handler = handler;
	ao2_cleanup(app->data);
	if (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;
}

int app_is_watching_bridge(struct app *app, const char *uniqueid)
{
	RAII_VAR(char *, found, NULL, ao2_cleanup);
	found = ao2_find(app->bridges, uniqueid, OBJ_KEY);
	return found != NULL;
}