[asterisk-commits] russell: branch russell/smdi-1.4 r93295 - /team/russell/smdi-1.4/res/res_smdi.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Dec 17 14:31:48 CST 2007


Author: russell
Date: Mon Dec 17 14:31:48 2007
New Revision: 93295

URL: http://svn.digium.com/view/asterisk?view=rev&rev=93295
Log:
Implement the station option for smdi_message_wait so that threads can sleep
while waiting for an smdi message of a certain type and for a certain station

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

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=93295&r1=93294&r2=93295
==============================================================================
--- team/russell/smdi-1.4/res/res_smdi.c (original)
+++ team/russell/smdi-1.4/res/res_smdi.c Mon Dec 17 14:31:48 2007
@@ -311,12 +311,12 @@
 	}
 }
 
-static inline void *smdi_msg_pop(struct ast_smdi_interface *iface, enum smdi_message_type type)
+static void purge_old_messages(struct ast_smdi_interface *iface, enum smdi_message_type type)
 {
 	struct timeval now;
 	long elapsed = 0;
 	void *msg;
-
+	
 	lock_msg_q(iface, type);
 	msg = unlink_from_msg_q(iface, type);
 	unlock_msg_q(iface, type);
@@ -329,28 +329,64 @@
 		if (elapsed > iface->msg_expiry) {
 			/* found an expired message */
 			unref_msg(msg, type);
-			ast_log(LOG_NOTICE, "Purged expired message from %s SMDI MD message queue.  Message was %ld milliseconds too old.\n",
-				iface->name, elapsed - iface->msg_expiry);
+			ast_log(LOG_NOTICE, "Purged expired message from %s SMDI %s message queue.  "
+				"Message was %ld milliseconds too old.\n",
+				iface->name, (type == SMDI_MD) ? "MD" : "MWI", 
+				elapsed - iface->msg_expiry);
 
 			lock_msg_q(iface, type);
 			msg = unlink_from_msg_q(iface, type);
 			unlock_msg_q(iface, type);
-		}
-		else {
-			/* good message, return it */
+		} else {
+			/* good message, put it back and return */
+			switch (type) {
+			case SMDI_MD:
+				ast_smdi_md_message_push(iface, msg);
+				break;
+			case SMDI_MWI:
+				ast_smdi_mwi_message_push(iface, msg);
+				break;
+			}
+			unref_msg(msg, type);
 			break;
 		}
 	}
+}
+
+static void *smdi_msg_pop(struct ast_smdi_interface *iface, enum smdi_message_type type)
+{
+	void *msg;
+
+	purge_old_messages(iface, type);
+
+	lock_msg_q(iface, type);
+	msg = unlink_from_msg_q(iface, type);
+	unlock_msg_q(iface, type);
 
 	return msg;
 }
 
-/*!
- * \todo XXX Make use of the terminal argument to allow waiting for a message
- * for a specific terminal.
- */
+static void *smdi_msg_find(struct ast_smdi_interface *iface,
+	enum smdi_message_type type, const char *station)
+{
+	void *msg = NULL;
+
+	purge_old_messages(iface, type);
+
+	switch (type) {
+	case SMDI_MD:
+		msg = ASTOBJ_CONTAINER_FIND(&iface->md_q, station);
+		break;
+	case SMDI_MWI:
+		msg = ASTOBJ_CONTAINER_FIND(&iface->mwi_q, station);
+		break;
+	}
+
+	return msg;
+}
+
 static void *smdi_message_wait(struct ast_smdi_interface *iface, int timeout, 
-	enum smdi_message_type type, const char *terminal)
+	enum smdi_message_type type, const char *station)
 {
 	struct timeval start;
 	long diff = 0;
@@ -363,7 +399,7 @@
 
 		lock_msg_q(iface, type);
 
-		if ((msg = smdi_msg_pop(iface, type))) {
+		if ((msg = smdi_msg_find(iface, type, station))) {
 			unlock_msg_q(iface, type);
 			return msg;
 		}
@@ -377,7 +413,7 @@
 
 		ast_cond_wait(&iface->md_q_cond, &iface->md_q_lock);
 
-		if ((msg = smdi_msg_pop(iface, type))) {
+		if ((msg = smdi_msg_find(iface, type, station))) {
 			unlock_msg_q(iface, type);
 			return msg;
 		}
@@ -538,7 +574,11 @@
 			/* make sure the value is null terminated, even if this truncates it */
 			md_msg->fwd_st[sizeof(md_msg->fwd_st) - 1] = '\0';
 			cp = NULL;
-			
+		
+			/* Put the fwd_st in the name field so that we can use ASTOBJ_FIND to look
+			 * up a message on this field */
+			ast_copy_string(md_msg->name, md_msg->fwd_st, sizeof(md_msg->name));
+
 			/* read the calling station number (may be blank) */
 			cp = &md_msg->calling_st[0];
 			for (i = 0; i < sizeof(md_msg->calling_st) - 1; i++) {
@@ -593,6 +633,10 @@
 			mwi_msg->fwd_st[sizeof(mwi_msg->fwd_st) - 1] = '\0';
 			cp = NULL;
 			
+			/* Put the fwd_st in the name field so that we can use ASTOBJ_FIND to look
+			 * up a message on this field */
+			ast_copy_string(mwi_msg->name, mwi_msg->fwd_st, sizeof(mwi_msg->name));
+
 			/* read the mwi failure cause */
 			for (i = 0; i < sizeof(mwi_msg->cause) - 1; i++)
 				mwi_msg->cause[i] = fgetc(iface->file);
@@ -1043,7 +1087,7 @@
 	struct ast_module_user *u;
 	AST_DECLARE_APP_ARGS(args,
 		AST_APP_ARG(port);
-		AST_APP_ARG(terminal);
+		AST_APP_ARG(station);
 		AST_APP_ARG(timeout);
 	);
 	unsigned int timeout = SMDI_RETRIEVE_TIMEOUT_DEFAULT;
@@ -1071,7 +1115,7 @@
 	parse = ast_strdupa(data);
 	AST_STANDARD_APP_ARGS(args, parse);
 
-	if (ast_strlen_zero(args.port) || ast_strlen_zero(args.terminal)) {
+	if (ast_strlen_zero(args.port) || ast_strlen_zero(args.station)) {
 		ast_log(LOG_ERROR, "Invalid arguments provided to SMDI_MSG_RETRIEVE\n");
 		goto return_error;
 	}
@@ -1088,9 +1132,9 @@
 		}
 	}
 
-	if (!(md_msg = smdi_message_wait(iface, timeout, SMDI_MD, args.terminal))) {
-		ast_log(LOG_WARNING, "No SMDI message retrieved for terminal '%s' after "
-			"waiting %u ms.\n", args.terminal, timeout);
+	if (!(md_msg = smdi_message_wait(iface, timeout, SMDI_MD, args.station))) {
+		ast_log(LOG_WARNING, "No SMDI message retrieved for station '%s' after "
+			"waiting %u ms.\n", args.station, timeout);
 		goto return_error;
 	}
 
@@ -1147,7 +1191,7 @@
 static struct ast_custom_function smdi_msg_retrieve_function = {
 	.name = "SMDI_MSG_RETRIEVE",
 	.synopsis = "Retrieve an SMDI message.",
-	.syntax = "SMDI_MSG_RETRIEVE(<smdi port>,<terminal>[,timeout])",
+	.syntax = "SMDI_MSG_RETRIEVE(<smdi port>,<station>[,timeout])",
 	.desc = 
 	"   This function is used to retrieve an incoming SMDI message.  It returns\n"
 	"an ID which can be used with the SMDI_MSG() function to access details of\n"




More information about the asterisk-commits mailing list