Add vtable and methods for to_json and to_ami for Stasis messages

When a Stasis message type is defined in a loadable module, handling
those messages for AMI and res_stasis events can be cumbersome.

This patch adds a vtable to stasis_message_type, with to_ami and
to_json virtual functions. These allow messages to be handled
abstractly without putting module-specific code in core.

As an example, the VarSet AMI event was refactored to use the to_ami
virtual function.

(closes issue ASTERISK-21817)
Review: https://reviewboard.asterisk.org/r/2579/


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@391403 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
David M. Lee
2013-06-11 15:46:35 +00:00
parent 2053fc3159
commit dbdb2b1b3a
8 changed files with 430 additions and 119 deletions

View File

@@ -1313,6 +1313,23 @@ struct ast_str *ast_manager_str_from_json_object(struct ast_json *blob, key_excl
return output_str;
}
static void manager_default_msg_cb(void *data, struct stasis_subscription *sub,
struct stasis_topic *topic,
struct stasis_message *message)
{
RAII_VAR(struct ast_manager_event_blob *, ev, NULL, ao2_cleanup);
ev = stasis_message_to_ami(message);
if (ev == NULL) {
/* Not and AMI message; disregard */
return;
}
manager_event(ev->event_flags, ev->manager_event, "%s",
ev->extra_fields);
}
static void manager_generic_msg_cb(void *data, struct stasis_subscription *sub,
struct stasis_topic *topic,
struct stasis_message *message)
@@ -7686,7 +7703,12 @@ static void manager_shutdown(void)
*/
static int manager_subscriptions_init(void)
{
STASIS_MESSAGE_TYPE_INIT(ast_manager_get_generic_type);
int res;
res = STASIS_MESSAGE_TYPE_INIT(ast_manager_get_generic_type);
if (res != 0) {
return -1;
}
manager_topic = stasis_topic_create("manager_topic");
if (!manager_topic) {
return -1;
@@ -7696,10 +7718,13 @@ static int manager_subscriptions_init(void)
return -1;
}
if (stasis_message_router_add(stasis_router,
ast_manager_get_generic_type(),
manager_generic_msg_cb,
NULL)) {
res |= stasis_message_router_set_default(stasis_router,
manager_default_msg_cb, NULL);
res |= stasis_message_router_add(stasis_router,
ast_manager_get_generic_type(), manager_generic_msg_cb, NULL);
if (res != 0) {
return -1;
}
return 0;