[asterisk-commits] branch oej/amieventhook-561 r11712 - in /team/oej/amieventhook-561: ./ includ...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Fri Mar 3 04:28:01 MST 2006


Author: oej
Date: Fri Mar  3 05:27:56 2006
New Revision: 11712

URL: http://svn.digium.com/view/asterisk?rev=11712&view=rev
Log:
Adding anthm's patch from #5161 with some small documentation changes

Modified:
    team/oej/amieventhook-561/   (props changed)
    team/oej/amieventhook-561/include/asterisk/manager.h
    team/oej/amieventhook-561/manager.c

Propchange: team/oej/amieventhook-561/
------------------------------------------------------------------------------
    svnmerge-integrated = /trunk:1-11709

Modified: team/oej/amieventhook-561/include/asterisk/manager.h
URL: http://svn.digium.com/view/asterisk/team/oej/amieventhook-561/include/asterisk/manager.h?rev=11712&r1=11711&r2=11712&view=diff
==============================================================================
--- team/oej/amieventhook-561/include/asterisk/manager.h (original)
+++ team/oej/amieventhook-561/include/asterisk/manager.h Fri Mar  3 05:27:56 2006
@@ -54,6 +54,72 @@
 #define EVENT_FLAG_COMMAND		(1 << 4) /* Ability to read/set commands */
 #define EVENT_FLAG_AGENT		(1 << 5) /* Ability to read/set agent info */
 #define EVENT_FLAG_USER                 (1 << 6) /* Ability to read/set user info */
+
+/*! \page AMIhooks Manager event hooks 
+	\par This is a way to register your own function to be called whenever something
+	calls manager_event() making it possible to gateway events to other systems
+	without hacking the manager The backend takes the liberty of consolidating the
+	varargs printf like expression into 1 string.
+
+	\par Example Code
+	*	This is your custom function:
+	*
+	*	\code
+	*	static int verbose_manager_event(int category, char *event, char *body)
+	*	{
+    	*	ast_verbose("Manager EVENT\n%s\n", body);
+    	*	return 	0;
+	*	}
+	*	\endcode
+	*	
+	*	This is the container where the function is stored in manager's scope:
+	*	\code
+	*	static struct manager_custom_hook my_hook = {
+    	*		.file = "app_myapp",
+    	*		.helper = verbose_manager_event
+	*	};
+	*	\endcode
+	*	
+	*	This is how you register it:
+	*	\code
+	*	int load_module(void) 
+	*	{
+     	*		add_manager_hook(&my_hook);
+	*	}
+	*	\endcode
+	*	
+	*	This is how you un-register it:
+	*	\code
+	*	int unload_module(void) 
+	*	{
+     	*		del_manager_hook(&my_hook);
+	*	}
+	*	\endcode
+
+	\par Functions
+	- manager_custom_hook()
+	- add_manager_hook()
+	- del_manager_hook()
+*/
+
+/*! \brief Manager Helper Function for custom AMI hooks.  See AMIhooks */
+typedef int (*manager_hook_t)(int, char *, char *); 
+
+struct manager_custom_hook {
+	char *file;		/*!< Identifier */
+	manager_hook_t helper;	/*!< helper function */
+	struct manager_custom_hook *next;
+};
+
+/*! \brief Add a custom hook to be called when an event is fired 
+	\param hook struct manager_custom_hook object to add
+*/
+extern void add_manager_hook(struct manager_custom_hook *hook);
+
+/*! Delete a custom hook to be called when an event is fired 
+	\param hook struct manager_custom_hook object to delete
+*/
+extern void del_manager_hook(struct manager_custom_hook *hook);
 
 /* Export manager structures */
 #define AST_MAX_MANHEADERS 80

Modified: team/oej/amieventhook-561/manager.c
URL: http://svn.digium.com/view/asterisk/team/oej/amieventhook-561/manager.c?rev=11712&r1=11711&r2=11712&view=diff
==============================================================================
--- team/oej/amieventhook-561/manager.c (original)
+++ team/oej/amieventhook-561/manager.c Fri Mar  3 05:27:56 2006
@@ -109,6 +109,39 @@
 static struct mansession *sessions = NULL;
 static struct manager_action *first_action = NULL;
 AST_MUTEX_DEFINE_STATIC(actionlock);
+AST_MUTEX_DEFINE_STATIC(hooklock);
+
+static struct manager_custom_hook *manager_hooks = NULL;
+
+
+void add_manager_hook(struct manager_custom_hook *hook)
+{
+	ast_mutex_lock(&hooklock);
+	if (hook) {
+		hook->next = manager_hooks;
+		manager_hooks = hook;
+	}
+	ast_mutex_unlock(&hooklock);
+}
+
+void del_manager_hook(struct manager_custom_hook *hook)
+{
+	struct manager_custom_hook *hookp, *lasthook = NULL;
+
+	ast_mutex_lock(&hooklock);
+	for (hookp = manager_hooks; hookp ; hookp = hookp->next) {
+		if (hookp == hook) {
+			if (lasthook) {
+				lasthook->next = hookp->next;
+			} else {
+				manager_hooks = hookp->next;
+			}
+		}
+		lasthook = hookp;
+	}
+	ast_mutex_unlock(&hooklock);
+
+}
 
 /*! If you are calling ast_carefulwrite, it is assumed that you are calling
    it on a file descriptor that _DOES_ have NONBLOCK set.  This way,
@@ -1556,7 +1589,22 @@
 		ast_mutex_unlock(&s->__lock);
 	}
 	ast_mutex_unlock(&sessionlock);
-
+	if (manager_hooks) {
+		struct manager_custom_hook *hookp;
+		ast_mutex_lock(&hooklock);
+		char *p;
+		int len;
+		snprintf(tmp, sizeof(tmp), "event: %s\r\nprivilege: %s\r\n", event, authority_to_str(category, tmp, sizeof(tmp)));
+		len = strlen(tmp);
+		p = tmp + len;
+		va_start(ap, fmt);
+		vsnprintf(p, sizeof(tmp) - len, fmt, ap);
+		va_end(ap);
+		for (hookp = manager_hooks ; hookp; hookp = hookp->next) {
+				hookp->helper(category, event, tmp);
+			}
+			ast_mutex_unlock(&hooklock);
+		}
 	return 0;
 }
 



More information about the asterisk-commits mailing list