[asterisk-commits] russell: branch russell/smdi-1.4 r92734 - in /team/russell/smdi-1.4: configs/...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Dec 12 19:14:49 CST 2007


Author: russell
Date: Wed Dec 12 19:14:49 2007
New Revision: 92734

URL: http://svn.digium.com/view/asterisk?view=rev&rev=92734
Log:
Commit progress.  I'm almost done adding the polling thread that will provide
proper MWI monitoring over SMDI.

Modified:
    team/russell/smdi-1.4/configs/smdi.conf.sample
    team/russell/smdi-1.4/res/res_smdi.c

Modified: team/russell/smdi-1.4/configs/smdi.conf.sample
URL: http://svn.digium.com/view/asterisk/team/russell/smdi-1.4/configs/smdi.conf.sample?view=diff&rev=92734&r1=92733&r2=92734
==============================================================================
--- team/russell/smdi-1.4/configs/smdi.conf.sample (original)
+++ team/russell/smdi-1.4/configs/smdi.conf.sample Wed Dec 12 19:14:49 2007
@@ -41,3 +41,28 @@
 ;msgexpirytime = 30000
 
 ;smdiport => /dev/ttyS0
+
+
+[mailboxes]
+; This section configures parameters related to MWI handling for the SMDI link.
+
+; This option configures the polling interval used to check to see if the
+; mailboxes have any new messages.  This option is specified in seconds.
+; The default value is 10 seconds.
+;
+;pollinginterval=10
+
+; Every other entry in this section of the configuration file is interpreted as
+; a mapping between the mailbox ID on the SMDI link, and the local Asterisk
+; mailbox name.  In many cases, they are the same thing, but they still must be
+; listed here so that this module knows which mailboxes it needs to pay
+; attention to.
+;
+; Syntax:
+;   <SMDI mailbox ID>=<Asterisk Mailbox Name>[@Asterisk Voicemail Context]
+;
+; If no Asterisk voicemail context is specified, "default" will be assumed.
+;
+;2565551234=1234 at vmcontext1
+;2565555678=5678 at vmcontext2
+;2565559999=9999

Modified: team/russell/smdi-1.4/res/res_smdi.c
URL: http://svn.digium.com/view/asterisk/team/russell/smdi-1.4/res/res_smdi.c?view=diff&rev=92734&r1=92733&r2=92734
==============================================================================
--- team/russell/smdi-1.4/res/res_smdi.c (original)
+++ team/russell/smdi-1.4/res/res_smdi.c Wed Dec 12 19:14:49 2007
@@ -44,24 +44,50 @@
 #include "asterisk/logger.h"
 #include "asterisk/utils.h"
 #include "asterisk/options.h"
+#include "asterisk/stringfields.h"
+#include "asterisk/linkedlists.h"
 
 /* Message expiry time in milliseconds */
 #define SMDI_MSG_EXPIRY_TIME	30000 /* 30 seconds */
 
 static const char config_file[] = "smdi.conf";
-
-static void ast_smdi_md_message_push(struct ast_smdi_interface *iface, struct ast_smdi_md_message *msg);
-static void ast_smdi_mwi_message_push(struct ast_smdi_interface *iface, struct ast_smdi_mwi_message *msg);
-
-static void *smdi_read(void *iface_p);
-static int smdi_load(int reload);
-
-struct module_symbols *me;	/* initialized in load_module() */
 
 /*! \brief SMDI interface container. */
 struct ast_smdi_interface_container {
 	ASTOBJ_CONTAINER_COMPONENTS(struct ast_smdi_interface);
 } smdi_ifaces;
+
+/*! \brief A mapping between an SMDI mailbox ID and an Asterisk mailbox */
+struct mailbox_mapping {
+	AST_DECLARE_STRING_FIELDS(
+		/*! The Name of the mailbox for the SMDI link. */
+		AST_STRING_FIELD(smdi);
+		/*! The name of the mailbox on the Asterisk side */
+		AST_STRING_FIELD(mailbox);
+		/*! The name of the voicemail context in use */
+		AST_STRING_FIELD(context);
+	);
+	AST_LIST_ENTRY(mailbox_mapping) entry;
+};
+
+/*! 10 seconds */
+#define DEFAULT_POLLING_INTERVAL 10
+
+/*! \brief Data that gets used by the SMDI MWI monitoring thread */
+static struct {
+	/*! The thread ID */
+	pthread_t thread;
+	ast_mutex_t lock;
+	ast_cond_t cond;
+	/*! A list of mailboxes that need to be monitored */
+	AST_LIST_HEAD_NOLOCK(, mailbox_mapping) mailbox_mappings;
+	/*! Polling Interval for checking mailbox status */
+	unsigned int polling_interval;
+	/*! Set to 1 to tell the polling thread to stop */
+	unsigned int stop:1;
+} mwi_monitor = {
+	.thread = AST_PTHREADT_NULL,
+};
 
 /*! 
  * \internal
@@ -502,6 +528,73 @@
 	free(iface);
 
 	ast_module_unref(ast_module_info->self);
+}
+
+static void destroy_mailbox_mapping(struct mailbox_mapping *mm)
+{
+	ast_string_field_free_memory(mm);
+	free(mm);
+}
+
+static void destroy_all_mailbox_mappings(void)
+{
+	struct mailbox_mapping *mm;
+
+	ast_mutex_lock(&mwi_monitor.lock);
+	while ((mm = AST_LIST_REMOVE_HEAD(&mwi_monitor.mailbox_mappings, entry)))
+		destroy_mailbox_mapping(mm);
+	ast_mutex_unlock(&mwi_monitor.lock);
+}
+
+static void append_mailbox_mapping(struct ast_variable *var)
+{
+	struct mailbox_mapping *mm;
+	char *mailbox, *context;
+
+	if (!(mm = ast_calloc(1, sizeof(*mm))))
+		return;
+	
+	if (ast_string_field_init(mm, 32)) {
+		free(mm);
+		return;
+	}
+
+	ast_string_field_set(mm, smdi, var->name);
+
+	context = ast_strdupa(var->value);
+	mailbox = strsep(&context, "@");
+	if (ast_strlen_zero(context))
+		context = "default";
+
+	ast_string_field_set(mm, mailbox, mailbox);
+	ast_string_field_set(mm, context, context);
+
+	ast_mutex_lock(&mwi_monitor.lock);
+	AST_LIST_INSERT_TAIL(&mwi_monitor.mailbox_mappings, mm, entry);
+	ast_mutex_unlock(&mwi_monitor.lock);
+}
+
+static void *mwi_monitor_handler(void *data)
+{
+	while (!mwi_monitor.stop) {
+		struct timespec ts = { 0, };
+		struct timeval tv;
+
+		ast_mutex_lock(&mwi_monitor.lock);
+
+		/* XXX Do polling */
+
+		/* Sleep up to the configured polling interval.  Allow unload_module()
+		 * to signal us to wake up and exit. */
+		tv = ast_tvadd(ast_tvnow(), ast_tv(mwi_monitor.polling_interval, 0));
+		ts.tv_sec = tv.tv_sec;
+		ts.tv_nsec = tv.tv_usec * 1000;
+		ast_cond_timedwait(&mwi_monitor.cond, &mwi_monitor.lock, &ts);
+
+		ast_mutex_unlock(&mwi_monitor.lock);
+	}
+
+	return NULL;
 }
 
 /*!
@@ -675,7 +768,7 @@
 			/* set the message expiry time */
 			iface->msg_expiry = msg_expiry;
 
-                        /* start the listner thread */
+			/* start the listener thread */
 			if (option_verbose > 2)
 				ast_verbose(VERBOSE_PREFIX_3 "Starting SMDI monitor thread for %s\n", iface->name);
 			if (ast_pthread_create_background(&iface->thread, NULL, smdi_read, iface)) {
@@ -691,7 +784,27 @@
 			ast_log(LOG_NOTICE, "Ignoring unknown option %s in %s\n", v->name, config_file);
 		}
 	}
+
+	destroy_all_mailbox_mappings();
+	mwi_monitor.polling_interval = DEFAULT_POLLING_INTERVAL;
+
+	for (v = ast_variable_browse(conf, "mailboxes"); v; v = v->next) {
+		if (!strcasecmp(v->name, "pollinginterval")) {
+			if (sscanf(v->value, "%u", &mwi_monitor.polling_interval) != 1) {
+				ast_log(LOG_ERROR, "Invalid value for pollinginterval: %s\n", v->value);
+				mwi_monitor.polling_interval = DEFAULT_POLLING_INTERVAL;
+			}
+		} else
+			append_mailbox_mapping(v);
+	}
+
 	ast_config_destroy(conf);
+
+	if (!AST_LIST_EMPTY(&mwi_monitor.mailbox_mappings) && mwi_monitor.thread == AST_PTHREADT_NULL
+		&& ast_pthread_create_background(&mwi_monitor.thread, NULL, mwi_monitor_handler, NULL)) {
+		ast_log(LOG_ERROR, "Failed to start MWI monitoring thread.  This module will not operate.\n");
+		return AST_MODULE_LOAD_FAILURE;
+	}
 
 	/* Prune any interfaces we should no longer monitor. */
 	if (reload)
@@ -714,6 +827,9 @@
 	memset(&smdi_ifaces, 0, sizeof(smdi_ifaces));
 	ASTOBJ_CONTAINER_INIT(&smdi_ifaces);
 
+	ast_mutex_init(&mwi_monitor.lock);
+	ast_cond_init(&mwi_monitor.cond, NULL);
+
 	/* load the config and start the listener threads*/
 	res = smdi_load(0);
 	if (res < 0) {
@@ -721,8 +837,9 @@
 	} else if (res == 1) {
 		ast_log(LOG_WARNING, "No SMDI interfaces are available to listen on, not starting SMDI listener.\n");
 		return AST_MODULE_LOAD_DECLINE;
-	} else
-		return 0;
+	}
+
+	return 0;
 }
 
 static int unload_module(void)
@@ -730,6 +847,15 @@
 	/* this destructor stops any running smdi_read threads */
 	ASTOBJ_CONTAINER_DESTROYALL(&smdi_ifaces, ast_smdi_interface_destroy);
 	ASTOBJ_CONTAINER_DESTROY(&smdi_ifaces);
+
+	destroy_all_mailbox_mappings();
+
+	ast_mutex_lock(&mwi_monitor.lock);
+	mwi_monitor.stop = 1;
+	ast_cond_signal(&mwi_monitor.cond);
+	ast_mutex_unlock(&mwi_monitor.lock);
+
+	pthread_join(mwi_monitor.thread, NULL);
 
 	return 0;
 }




More information about the asterisk-commits mailing list