[asterisk-commits] jrose: trunk r387594 - in /trunk: channels/ include/ main/ res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri May 3 13:03:37 CDT 2013


Author: jrose
Date: Fri May  3 13:03:26 2013
New Revision: 387594

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=387594
Log:
Stasis: Convert network change events into network change stasis messages

(issue ASTERISK-21103)
Review: https://reviewboard.asterisk.org/r/2490/

Modified:
    trunk/channels/chan_iax2.c
    trunk/channels/chan_sip.c
    trunk/include/asterisk.h
    trunk/main/asterisk.c
    trunk/main/event.c
    trunk/res/res_stun_monitor.c

Modified: trunk/channels/chan_iax2.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/chan_iax2.c?view=diff&rev=387594&r1=387593&r2=387594
==============================================================================
--- trunk/channels/chan_iax2.c (original)
+++ trunk/channels/chan_iax2.c Fri May  3 13:03:26 2013
@@ -283,9 +283,9 @@
 static char language[MAX_LANGUAGE] = "";
 static char regcontext[AST_MAX_CONTEXT] = "";
 
-static struct ast_event_sub *network_change_event_subscription; /*!< subscription id for network change events */
+static struct stasis_subscription *network_change_sub; /*!< subscription id for network change events */
 static struct stasis_subscription *acl_change_sub; /*!< subscription id for ACL change events */
-static int network_change_event_sched_id = -1;
+static int network_change_sched_id = -1;
 
 static int maxauthreq = 3;
 static int max_retries = 4;
@@ -1254,7 +1254,7 @@
 static int get_unused_callno(enum callno_type type, int validated, callno_entry *entry);
 static int replace_callno(const void *obj);
 static void sched_delay_remove(struct sockaddr_in *sin, callno_entry entry);
-static void network_change_event_cb(const struct ast_event *, void *);
+static void network_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_topic *topic, struct stasis_message *message);
 static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_topic *topic, struct stasis_message *message);
 
 static struct ast_channel_tech iax2_tech = {
@@ -1323,18 +1323,18 @@
 	 * is time to send MWI, since it is only sent with a REGACK. */
 }
 
-static void network_change_event_subscribe(void)
-{
-	if (!network_change_event_subscription) {
-		network_change_event_subscription = ast_event_subscribe(AST_EVENT_NETWORK_CHANGE,
-			network_change_event_cb, "IAX2 Network Change", NULL, AST_EVENT_IE_END);
-	}
-}
-
-static void network_change_event_unsubscribe(void)
-{
-	if (network_change_event_subscription) {
-		network_change_event_subscription = ast_event_unsubscribe(network_change_event_subscription);
+static void network_change_stasis_subscribe(void)
+{
+	if (!network_change_sub) {
+		network_change_sub = stasis_subscribe(ast_system_topic(),
+			network_change_stasis_cb, NULL);
+	}
+}
+
+static void network_change_stasis_unsubscribe(void)
+{
+	if (network_change_sub) {
+		network_change_sub = stasis_unsubscribe(network_change_sub);
 	}
 }
 
@@ -1353,10 +1353,10 @@
 	}
 }
 
-static int network_change_event_sched_cb(const void *data)
+static int network_change_sched_cb(const void *data)
 {
 	struct iax2_registry *reg;
-	network_change_event_sched_id = -1;
+	network_change_sched_id = -1;
 	AST_LIST_LOCK(&registrations);
 	AST_LIST_TRAVERSE(&registrations, reg, entry) {
 		iax2_do_register(reg);
@@ -1366,13 +1366,18 @@
 	return 0;
 }
 
-static void network_change_event_cb(const struct ast_event *event, void *userdata)
-{
-	ast_debug(1, "IAX, got a network change event, renewing all IAX registrations.\n");
-	if (network_change_event_sched_id == -1) {
-		network_change_event_sched_id = iax2_sched_add(sched, 1000, network_change_event_sched_cb, NULL);
-	}
-
+static void network_change_stasis_cb(void *data, struct stasis_subscription *sub,
+	struct stasis_topic *topic, struct stasis_message *message)
+{
+	/* This callback is only concerned with network change messages from the system topic. */
+	if (stasis_message_type(message) != ast_network_change_type()) {
+		return;
+	}
+
+	ast_verb(1, "IAX, got a network change message, renewing all IAX registrations.\n");
+	if (network_change_sched_id == -1) {
+		network_change_sched_id = iax2_sched_add(sched, 1000, network_change_sched_cb, NULL);
+	}
 }
 
 static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub,
@@ -13442,9 +13447,9 @@
 	}
 
 	if (subscribe_network_change) {
-		network_change_event_subscribe();
+		network_change_stasis_subscribe();
 	} else {
-		network_change_event_unsubscribe();
+		network_change_stasis_unsubscribe();
 	}
 
 	if (defaultsockfd < 0) {
@@ -14287,7 +14292,7 @@
 	struct ast_context *con;
 	int x;
 
-	network_change_event_unsubscribe();
+	network_change_stasis_unsubscribe();
 	acl_change_stasis_unsubscribe();
 
 	ast_manager_unregister("IAXpeers");
@@ -14789,7 +14794,7 @@
 
 	ast_realtime_require_field("iaxpeers", "name", RQ_CHAR, 10, "ipaddr", RQ_CHAR, 15, "port", RQ_UINTEGER2, 5, "regseconds", RQ_UINTEGER2, 6, SENTINEL);
 
-	network_change_event_subscribe();
+	network_change_stasis_subscribe();
 
 	return AST_MODULE_LOAD_SUCCESS;
 }

Modified: trunk/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/chan_sip.c?view=diff&rev=387594&r1=387593&r2=387594
==============================================================================
--- trunk/channels/chan_sip.c (original)
+++ trunk/channels/chan_sip.c Fri May  3 13:03:26 2013
@@ -843,9 +843,9 @@
 static struct ast_flags global_flags[3] = {{0}};  /*!< global SIP_ flags */
 static int global_t38_maxdatagram;                /*!< global T.38 FaxMaxDatagram override */
 
-static struct ast_event_sub *network_change_event_subscription; /*!< subscription id for network change events */
+static struct stasis_subscription *network_change_sub; /*!< subscription id for network change events */
 static struct stasis_subscription *acl_change_sub; /*!< subscription id for named ACL system change events */
-static int network_change_event_sched_id = -1;
+static int network_change_sched_id = -1;
 
 static char used_context[AST_MAX_CONTEXT];        /*!< name of automatically created context for unloading */
 
@@ -1286,7 +1286,7 @@
 static void sip_poke_all_peers(void);
 static void sip_peer_hold(struct sip_pvt *p, int hold);
 static void mwi_event_cb(void *, struct stasis_subscription *, struct stasis_topic *, struct stasis_message *);
-static void network_change_event_cb(const struct ast_event *, void *);
+static void network_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_topic *topic, struct stasis_message *message);
 static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_topic *topic, struct stasis_message *message);
 static void sip_keepalive_all_peers(void);
 
@@ -16707,18 +16707,18 @@
 	}
 }
 
-static void network_change_event_subscribe(void)
-{
-	if (!network_change_event_subscription) {
-		network_change_event_subscription = ast_event_subscribe(AST_EVENT_NETWORK_CHANGE,
-			network_change_event_cb, "SIP Network Change", NULL, AST_EVENT_IE_END);
-	}
-}
-
-static void network_change_event_unsubscribe(void)
-{
-	if (network_change_event_subscription) {
-		network_change_event_subscription = ast_event_unsubscribe(network_change_event_subscription);
+static void network_change_stasis_subscribe(void)
+{
+	if (!network_change_sub) {
+		network_change_sub = stasis_subscribe(ast_system_topic(),
+			network_change_stasis_cb, NULL);
+	}
+}
+
+static void network_change_stasis_unsubscribe(void)
+{
+	if (network_change_sub) {
+		network_change_sub = stasis_unsubscribe(network_change_sub);
 	}
 }
 
@@ -16731,26 +16731,31 @@
 
 }
 
-static void acl_change_event_unsubscribe(void)
+static void acl_change_event_stasis_unsubscribe(void)
 {
 	if (acl_change_sub) {
 		acl_change_sub = stasis_unsubscribe(acl_change_sub);
 	}
 }
 
-static int network_change_event_sched_cb(const void *data)
-{
-	network_change_event_sched_id = -1;
+static int network_change_sched_cb(const void *data)
+{
+	network_change_sched_id = -1;
 	sip_send_all_registers();
 	sip_send_all_mwi_subscriptions();
 	return 0;
 }
 
-static void network_change_event_cb(const struct ast_event *event, void *userdata)
-{
-	ast_debug(1, "SIP, got a network change event, renewing all SIP registrations.\n");
-	if (network_change_event_sched_id == -1) {
-		network_change_event_sched_id = ast_sched_add(sched, 1000, network_change_event_sched_cb, NULL);
+static void network_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_topic *topic, struct stasis_message *message)
+{
+	/* This callback is only concerned with network change messages from the system topic. */
+	if (stasis_message_type(message) != ast_network_change_type()) {
+		return;
+	}
+
+	ast_verb(1, "SIP, got a network change message, renewing all SIP registrations.\n");
+	if (network_change_sched_id == -1) {
+		network_change_sched_id = ast_sched_add(sched, 1000, network_change_sched_cb, NULL);
 	}
 }
 
@@ -32329,9 +32334,9 @@
 	}
 
 	if (subscribe_network_change) {
-		network_change_event_subscribe();
+		network_change_stasis_subscribe();
 	} else {
-		network_change_event_unsubscribe();
+		network_change_stasis_unsubscribe();
 	}
 
 	if (global_t1 < global_t1min) {
@@ -34819,7 +34824,7 @@
 
 
 	sip_register_tests();
-	network_change_event_subscribe();
+	network_change_stasis_subscribe();
 
 	ast_websocket_add_protocol("sip", sip_websocket_callback);
 
@@ -34839,8 +34844,8 @@
 
 	ast_websocket_remove_protocol("sip", sip_websocket_callback);
 
-	network_change_event_unsubscribe();
-	acl_change_event_unsubscribe();
+	network_change_stasis_unsubscribe();
+	acl_change_event_stasis_unsubscribe();
 
 	ast_sched_dump(sched);
 

Modified: trunk/include/asterisk.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk.h?view=diff&rev=387594&r1=387593&r2=387594
==============================================================================
--- trunk/include/asterisk.h (original)
+++ trunk/include/asterisk.h Fri May  3 13:03:26 2013
@@ -127,6 +127,31 @@
 char *ast_complete_source_filename(const char *partial, int n);
 
 /*!
+ * \brief accessor for the system stasis topic
+ * \since 12
+ *
+ * \retval NULL if the stasis topic hasn't been created or has been
+ *         deliberately disabled. Unless it is ran prior to system
+ *         initialization, this should never return NULL.
+ * \retval a pointer to the System stasis topic
+ */
+struct stasis_topic *ast_system_topic(void);
+
+/*!
+ * \brief accessor for the network change stasis message type
+ * \since 12
+ *
+ * \retval NULL if the message type hasn't been created or has been
+ *         deliberately disabled. Unless it is ran prior to system
+ *         initialization, this should never return NULL.
+ * \retval a pointer to the network change stasis message type
+ *
+ * \note Messages of this type should always be issued on and expected from
+ *       the system stasis topic.
+ */
+struct stasis_message_type *ast_network_change_type(void);
+
+/*!
  * \brief Register/unregister a source code file with the core.
  * \param file the source file name
  * \param version the version string (typically a SVN revision keyword string)

Modified: trunk/main/asterisk.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/asterisk.c?view=diff&rev=387594&r1=387593&r2=387594
==============================================================================
--- trunk/main/asterisk.c (original)
+++ trunk/main/asterisk.c Fri May  3 13:03:26 2013
@@ -3560,6 +3560,26 @@
 	setenv("AST_VERSION", ast_get_version(), 1);
 }
 
+static struct stasis_topic *system_topic;
+
+static struct stasis_message_type *network_change_type;
+
+struct stasis_topic *ast_system_topic(void)
+{
+	return system_topic;
+}
+
+struct stasis_message_type *ast_network_change_type(void)
+{
+	return network_change_type;
+}
+
+static void stasis_system_topic_init(void)
+{
+	system_topic = stasis_topic_create("ast_system");
+	network_change_type = stasis_message_type_create("network_change");
+}
+
 static void print_intro_message(const char *runuser, const char *rungroup)
 {
 	if (ast_opt_console || option_verbose || (ast_opt_remote && !ast_opt_exec)) {
@@ -4127,6 +4147,7 @@
 		printf("Stasis initialization failed.\n%s", term_quit());
 		exit(1);
 	}
+	stasis_system_topic_init();
 
 	ast_makesocket();
 	sigemptyset(&sigs);

Modified: trunk/main/event.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/event.c?view=diff&rev=387594&r1=387593&r2=387594
==============================================================================
--- trunk/main/event.c (original)
+++ trunk/main/event.c Fri May  3 13:03:26 2013
@@ -142,7 +142,6 @@
 static int ast_event_hash_mwi(const void *obj, const int flags);
 static int ast_event_hash_devstate(const void *obj, const int flags);
 static int ast_event_hash_devstate_change(const void *obj, const int flags);
-static int ast_event_hash_presence_state_change(const void *obj, const int flags);
 
 #ifdef LOW_MEMORY
 #define NUM_CACHE_BUCKETS 17
@@ -188,10 +187,6 @@
 	[AST_EVENT_DEVICE_STATE_CHANGE] = {
 		.hash_fn = ast_event_hash_devstate_change,
 		.cache_args = { AST_EVENT_IE_DEVICE, AST_EVENT_IE_EID, },
-	},
-	[AST_EVENT_PRESENCE_STATE] = {
-		.hash_fn = ast_event_hash_presence_state_change,
-		.cache_args = { AST_EVENT_IE_PRESENCE_STATE, },
 	},
 };
 
@@ -1614,22 +1609,6 @@
 	return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
 }
 
-/*!
- * \internal
- * \brief Hash function for AST_EVENT_PRESENCE_STATE
- *
- * \param[in] obj an ast_event
- * \param[in] flags unused
- *
- * \return hash value
- */
-static int ast_event_hash_presence_state_change(const void *obj, const int flags)
-{
-	const struct ast_event *event = obj;
-
-	return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_PROVIDER));
-}
-
 static int ast_event_hash(const void *obj, const int flags)
 {
 	const struct ast_event_ref *event_ref;

Modified: trunk/res/res_stun_monitor.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_stun_monitor.c?view=diff&rev=387594&r1=387593&r2=387594
==============================================================================
--- trunk/res/res_stun_monitor.c (original)
+++ trunk/res/res_stun_monitor.c Fri May  3 13:03:26 2013
@@ -32,7 +32,6 @@
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/module.h"
-#include "asterisk/event.h"
 #include "asterisk/sched.h"
 #include "asterisk/config.h"
 #include "asterisk/stun.h"
@@ -40,6 +39,9 @@
 #include "asterisk/lock.h"
 #include "asterisk/acl.h"
 #include "asterisk/cli.h"
+#include "asterisk/stasis.h"
+#include "asterisk/json.h"
+#include "asterisk/astobj2.h"
 
 #include <fcntl.h>
 
@@ -152,18 +154,27 @@
 			args.external_addr = answer;
 
 			if (args.external_addr_known) {
-				struct ast_event *event;
-
-				/*
-				 * The external address was already known, and has changed...
-				 * generate event.
-				 */
-				event = ast_event_new(AST_EVENT_NETWORK_CHANGE, AST_EVENT_IE_END);
-				if (!event) {
-					ast_log(LOG_ERROR, "Could not create AST_EVENT_NETWORK_CHANGE event.\n");
-				} else if (ast_event_queue(event)) {
-					ast_event_destroy(event);
-					ast_log(LOG_ERROR, "Could not queue AST_EVENT_NETWORK_CHANGE event.\n");
+				RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+				RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
+				RAII_VAR(struct ast_json *, json_object, ast_json_object_create(), ast_json_unref);
+
+				/* This json_object doesn't actually contain anything yet. We have to reference something
+				 * for stasis, and this is useful for if we want to ever add data for any reason. */
+				if (!json_object) {
+					goto publish_failure;
+				}
+
+				if (!(json_payload = ast_json_payload_create(json_object))) {
+					goto publish_failure;
+				}
+
+				msg = stasis_message_create(ast_network_change_type(), json_payload);
+
+publish_failure:
+				if (msg) {
+					stasis_publish(ast_system_topic(), msg);
+				} else {
+					ast_log(LOG_ERROR, "Failed to issue network change message.\n");
 				}
 			} else {
 				/* this was the first external address we found, do not alert listeners




More information about the asterisk-commits mailing list