[Asterisk-code-review] manager: Add AMI event Load/Unload (asterisk[master])

sungtae kim asteriskteam at digium.com
Tue Feb 6 14:27:15 CST 2018


sungtae kim has uploaded this change for review. ( https://gerrit.asterisk.org/8166


Change subject: manager: Add AMI event Load/Unload
......................................................................

manager: Add AMI event Load/Unload

Add an AMI events Load and Unload for notify when the
module has been loaded and unloaded.

ASTERISK-27661

Change-Id: Ib916c41eddd63651952998f2f49c57c42ef87a64
---
M include/asterisk/module.h
M main/loader.c
2 files changed, 100 insertions(+), 8 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/66/8166/1

diff --git a/include/asterisk/module.h b/include/asterisk/module.h
index ebd41c0..a1ce5b9 100644
--- a/include/asterisk/module.h
+++ b/include/asterisk/module.h
@@ -65,6 +65,10 @@
 				as it may cause crashes */
 };
 
+enum ast_module_unload_result {
+	AST_MODULE_UNLOAD_SUCCESS = 0,  /*!< Module was unloaded successfully */
+};
+
 enum ast_module_load_result {
 	AST_MODULE_LOAD_SUCCESS = 0,    /*!< Module loaded and configured */
 	AST_MODULE_LOAD_DECLINE = 1,    /*!< Module is not configured */
diff --git a/main/loader.c b/main/loader.c
index 6b29f0e..550298c 100644
--- a/main/loader.c
+++ b/main/loader.c
@@ -87,6 +87,44 @@
 			</syntax>
 		</managerEventInstance>
 	</managerEvent>
+	<managerEvent language="en_US" name="Load">
+		<managerEventInstance class="EVENT_FLAG_SYSTEM">
+			<synopsis>Raised when a module has been loaded in Asterisk.</synopsis>
+			<syntax>
+				<parameter name="Module">
+					<para>The name of the module that was loaded</para>
+				</parameter>
+				<parameter name="Status">
+					<para>The numeric status code denoting the success or failure
+					of the load request.</para>
+					<enumlist>
+						<enum name="-1"><para>Module could not be loaded properly</para></enum>
+						<enum name="0"><para>Success. Module loaded and configured</para></enum>
+						<enum name="1"><para>Module is not configured</para></enum>
+						<enum name="2"><para>Module was skipped for some reason</para></enum>
+						<enum name="3"><para>Module is not loaded yet, but is added to priority list</para></enum>
+					</enumlist>
+				</parameter>
+			</syntax>
+		</managerEventInstance>
+	</managerEvent>
+	<managerEvent language="en_US" name="Unload">
+		<managerEventInstance class="EVENT_FLAG_SYSTEM">
+			<synopsis>Raised when a module has been unloaded in Asterisk.</synopsis>
+			<syntax>
+				<parameter name="Module">
+					<para>The name of the module that was unloaded</para>
+				</parameter>
+				<parameter name="Status">
+					<para>The numeric status code denoting the success or failure
+					of the unload request.</para>
+					<enumlist>
+						<enum name="0"><para>Success. Module unloaded successfully</para></enum>
+					</enumlist>
+				</parameter>
+			</syntax>
+		</managerEventInstance>
+	</managerEvent>
  ***/
 
 #ifndef RTLD_NOW
@@ -160,6 +198,12 @@
 };
 
 static AST_DLLIST_HEAD_STATIC(module_list, ast_module);
+
+static void publish_load_message_type(const char* type, const char *name, int result);
+static void publish_reload_message(const char *name, enum ast_module_reload_result result);
+static void publish_load_message(const char *name, enum ast_module_load_result result);
+static void publish_unload_message(const char *name, enum ast_module_unload_result result);
+
 
 /*
  * module_list is cleared by its constructor possibly after
@@ -1007,6 +1051,7 @@
 		unload_dynamic_module(mod);
 		ast_test_suite_event_notify("MODULE_UNLOAD", "Message: %s", resource_name);
 		ast_update_use_count();
+		publish_unload_message(resource_name, AST_MODULE_UNLOAD_SUCCESS);
 	}
 
 	return res;
@@ -1196,9 +1241,9 @@
 /*!
  * \since 12
  * \internal
- * \brief Publish a \ref stasis message regarding the reload result
+ * \brief Publish a \ref stasis message regarding the type.
  */
-static void publish_reload_message(const char *name, enum ast_module_reload_result result)
+static void publish_load_message_type(const char* type, const char *name, int result)
 {
 	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
 	RAII_VAR(struct ast_json_payload *, payload, NULL, ao2_cleanup);
@@ -1206,19 +1251,22 @@
 	RAII_VAR(struct ast_json *, event_object, NULL, ast_json_unref);
 	char res_buffer[8];
 
+	if ((!type) || (!name)) {
+		return;
+	}
+
 	if (!ast_manager_get_generic_type()) {
 		return;
 	}
 
-	snprintf(res_buffer, sizeof(res_buffer), "%u", result);
-	event_object = ast_json_pack("{s: s, s: s}",
-			"Module", S_OR(name, "All"),
+	snprintf(res_buffer, sizeof(res_buffer), "%d", result);
+	event_object = ast_json_pack("{s:s, s:s}",
+			"Module", name,
 			"Status", res_buffer);
-	json_object = ast_json_pack("{s: s, s: i, s: o}",
-			"type", "Reload",
+	json_object = ast_json_pack("{s:s, s:i, s:o}",
+			"type", type,
 			"class_type", EVENT_FLAG_SYSTEM,
 			"event", ast_json_ref(event_object));
-
 	if (!json_object) {
 		return;
 	}
@@ -1234,6 +1282,44 @@
 	}
 
 	stasis_publish(ast_manager_get_topic(), message);
+}
+
+/*!
+ * \since 12
+ * \internal
+ * \brief Publish a \ref stasis message regarding the load result
+ */
+static void publish_load_message(const char *name, enum ast_module_load_result result)
+{
+	publish_load_message_type("Load", name, result);
+}
+
+/*!
+ * \since 12
+ * \internal
+ * \brief Publish a \ref stasis message regarding the unload result
+ */
+static void publish_unload_message(const char *name, enum ast_module_unload_result result)
+{
+	publish_load_message_type("Unload", name, result);
+}
+
+/*!
+ * \since 12
+ * \internal
+ * \brief Publish a \ref stasis message regarding the reload result
+ */
+static void publish_reload_message(const char *name, enum ast_module_reload_result result)
+{
+	const char* module_name;
+
+	module_name = name;
+	if (!module_name) {
+		module_name = "All";
+		return;
+	}
+
+	publish_load_message_type("Reload", module_name, result);
 }
 
 enum ast_module_reload_result ast_module_reload(const char *name)
@@ -1484,6 +1570,8 @@
 		res = start_resource(mod);
 	}
 
+	publish_load_message(resource_name, res);
+
 	return res;
 
 prestart_error:

-- 
To view, visit https://gerrit.asterisk.org/8166
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib916c41eddd63651952998f2f49c57c42ef87a64
Gerrit-Change-Number: 8166
Gerrit-PatchSet: 1
Gerrit-Owner: sungtae kim <pchero21 at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180206/87d5f0e7/attachment.html>


More information about the asterisk-code-review mailing list