[asterisk-commits] mmichelson: branch mmichelson/forward-loop r87017 - in /team/mmichelson/forwa...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Oct 24 17:11:33 CDT 2007


Author: mmichelson
Date: Wed Oct 24 17:11:33 2007
New Revision: 87017

URL: http://svn.digium.com/view/asterisk?view=rev&rev=87017
Log:
Changed the forwarding-loop solution to use a channel datastore instead of modifying the ast_channel structure.
This datastore is a bit of an anomaly since it isn't used by any one particular module. It needs to be accessed
by several, so I created global_datastores.c and global_datastores.h to be used for such datastores.


Added:
    team/mmichelson/forward-loop/include/asterisk/global_datastores.h   (with props)
    team/mmichelson/forward-loop/main/global_datastores.c   (with props)
Modified:
    team/mmichelson/forward-loop/apps/app_dial.c
    team/mmichelson/forward-loop/channels/chan_local.c
    team/mmichelson/forward-loop/include/asterisk/channel.h
    team/mmichelson/forward-loop/main/Makefile
    team/mmichelson/forward-loop/main/channel.c

Modified: team/mmichelson/forward-loop/apps/app_dial.c
URL: http://svn.digium.com/view/asterisk/team/mmichelson/forward-loop/apps/app_dial.c?view=diff&rev=87017&r1=87016&r2=87017
==============================================================================
--- team/mmichelson/forward-loop/apps/app_dial.c (original)
+++ team/mmichelson/forward-loop/apps/app_dial.c Wed Oct 24 17:11:33 2007
@@ -60,6 +60,7 @@
 #include "asterisk/manager.h"
 #include "asterisk/privacy.h"
 #include "asterisk/stringfields.h"
+#include "asterisk/global_datastores.h"
 
 static char *app = "Dial";
 
@@ -500,20 +501,12 @@
 					} else {
 						/* Setup parameters */
 						if ((c = o->chan = ast_request(tech, in->nativeformats, stuff, &cause))) {
-							struct ast_dialed_interface *cur;
 							ast_log(LOG_DEBUG, "Requester 2 got channel %p\n", c);
 							ast_log(LOG_DEBUG, "Is it bridged? %s %p\n", ast_bridged_channel(c) ? "Yes" : "No", ast_bridged_channel(c));
 							if (single)
 								ast_channel_make_compatible(o->chan, in);
 							ast_channel_inherit_variables(in, o->chan);
-							AST_LIST_LOCK(&in->dialed_interfaces);
-							AST_LIST_TRAVERSE(&in->dialed_interfaces, cur, list) {
-								struct ast_dialed_interface *tmp = ast_calloc(1, sizeof(*tmp));
-								ast_copy_string(tmp->interface, cur->interface, sizeof(tmp->interface));
-								ast_log(LOG_DEBUG, "Copying '%s' to the outgoing channel '%s': %p\n", cur->interface, c->name, c);
-								AST_LIST_INSERT_TAIL(&c->dialed_interfaces, tmp, list);
-							}
-							AST_LIST_UNLOCK(&in->dialed_interfaces);
+							ast_channel_datastore_inherit(in, o->chan);
 						} else
 							ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s' (cause = %d)\n", tech, stuff, cause);
 					}
@@ -1102,6 +1095,8 @@
 		/* find if we already dialed this interface */
 		int dialed = 0;
 		struct ast_dialed_interface *cur;
+		struct ast_datastore *datastore;
+		AST_LIST_HEAD(, ast_dialed_interface) *dialed_interfaces;
 		if (!number) {
 			ast_log(LOG_WARNING, "Dial argument takes format (technology/[device:]number1)\n");
 			goto out;
@@ -1130,8 +1125,19 @@
 			continue;
 		}
 		ast_log(LOG_DEBUG, "Dialing from interface %s: %p\n", chan->name, chan);
-		AST_LIST_LOCK(&chan->dialed_interfaces);
-		AST_LIST_TRAVERSE(&chan->dialed_interfaces, cur, list) {
+
+		if (!(datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL))) {
+			ast_log(LOG_DEBUG, "Creating datastore for channel '%s'\n", chan->name);
+			datastore = ast_channel_datastore_alloc(&dialed_interface_info, NULL);
+			datastore->inheritance = DATASTORE_INHERIT_FOREVER;
+			dialed_interfaces = ast_calloc(1, sizeof(*dialed_interfaces));
+			datastore->data = dialed_interfaces;
+			AST_LIST_HEAD_INIT(dialed_interfaces);
+			ast_channel_datastore_add(chan, datastore);
+		} else 
+			dialed_interfaces = datastore->data;
+		AST_LIST_LOCK(dialed_interfaces);
+		AST_LIST_TRAVERSE(dialed_interfaces, cur, list) {
 			char *dash;
 			char *name = ast_strdupa(tmp->chan->name);
 			if ((dash = strchr(name, '-')))
@@ -1143,7 +1149,7 @@
 				break;
 			}
 		}
-		AST_LIST_UNLOCK(&chan->dialed_interfaces);
+		AST_LIST_UNLOCK(dialed_interfaces);
 		if(!dialed) {
 			char *blah;
 			char *tech;
@@ -1158,7 +1164,7 @@
 					goto out;
 				ast_copy_string(cur->interface, interface, sizeof(cur->interface));
 				ast_log(LOG_DEBUG, "Adding interface '%s' to the list of dialed interfaces\n", cur->interface);
-				AST_LIST_INSERT_TAIL(&chan->dialed_interfaces, cur, list);
+				AST_LIST_INSERT_TAIL(dialed_interfaces, cur, list);
 			}
 		} else {
 			ast_log(LOG_DEBUG, "Skipping dialing interface '%s' since it has already been dialed\n", cur->interface);

Modified: team/mmichelson/forward-loop/channels/chan_local.c
URL: http://svn.digium.com/view/asterisk/team/mmichelson/forward-loop/channels/chan_local.c?view=diff&rev=87017&r1=87016&r2=87017
==============================================================================
--- team/mmichelson/forward-loop/channels/chan_local.c (original)
+++ team/mmichelson/forward-loop/channels/chan_local.c Wed Oct 24 17:11:33 2007
@@ -436,7 +436,6 @@
 	struct local_pvt *p = ast->tech_pvt;
 	int res;
 	struct ast_var_t *varptr = NULL, *new;
-	struct ast_dialed_interface *di;
 	size_t len, namelen;
 
 	if (!p)
@@ -468,11 +467,7 @@
 			AST_LIST_INSERT_TAIL(&p->chan->varshead, new, entries);
 		}
 	}
-	AST_LIST_TRAVERSE(&p->owner->dialed_interfaces, di, list) {
-		struct ast_dialed_interface *tmp = ast_calloc(1, sizeof(*tmp));
-		ast_copy_string(tmp->interface, di->interface, sizeof(tmp->interface));
-		AST_LIST_INSERT_TAIL(&p->chan->dialed_interfaces, tmp, list);
-	}
+	ast_channel_datastore_inherit(p->owner, p->chan);
 
 	/* Start switch on sub channel */
 	if (!(res = ast_pbx_start(p->chan)))

Modified: team/mmichelson/forward-loop/include/asterisk/channel.h
URL: http://svn.digium.com/view/asterisk/team/mmichelson/forward-loop/include/asterisk/channel.h?view=diff&rev=87017&r1=87016&r2=87017
==============================================================================
--- team/mmichelson/forward-loop/include/asterisk/channel.h (original)
+++ team/mmichelson/forward-loop/include/asterisk/channel.h Wed Oct 24 17:11:33 2007
@@ -118,6 +118,7 @@
 #include "asterisk/stringfields.h"
 #include "asterisk/compiler.h"
 
+#define DATASTORE_INHERIT_FOREVER	INT_MAX
 
 #define AST_MAX_FDS		8
 /*
@@ -148,6 +149,7 @@
 /*! \brief Structure for a data store type */
 struct ast_datastore_info {
 	const char *type;		/*!< Type of data store */
+	void *(*duplicate)(void *data); /*!< Duplicate item data (used for inheritance) */
 	void (*destroy)(void *data);	/*!< Destroy function */
 };
 
@@ -156,6 +158,7 @@
 	char *uid;		/*!< Unique data store identifier */
 	void *data;		/*!< Contained data */
 	const struct ast_datastore_info *info;	/*!< Data store type information */
+	unsigned int inheritance;	/*!Number of levels this item will continue to be inherited */
 	AST_LIST_ENTRY(ast_datastore) entry; /*!< Used for easy linking */
 };
 
@@ -174,11 +177,6 @@
 	int cid_ani2;		/*!< Callerid ANI 2 (Info digits) */
 	int cid_ton;		/*!< Callerid Type of Number */
 	int cid_tns;		/*!< Callerid Transit Network Select */
-};
-
-struct ast_dialed_interface {
-	char interface[80];
-	AST_LIST_ENTRY(ast_dialed_interface) list;
 };
 
 /*! \brief 
@@ -441,8 +439,6 @@
 	char emulate_dtmf_digit;			/*!< Digit being emulated */
 	unsigned int emulate_dtmf_duration;	/*!< Number of ms left to emulate DTMF for */
 	struct timeval dtmf_tv;       /*!< The time that an in process digit began, or the last digit ended */
-
-	AST_LIST_HEAD(dialed_interfaces, ast_dialed_interface) dialed_interfaces;
 
 	/*! \brief Data stores on the channel */
 	AST_LIST_HEAD_NOLOCK(datastores, ast_datastore) datastores;
@@ -580,6 +576,9 @@
 /*! \brief Free a channel datastore structure */
 int ast_channel_datastore_free(struct ast_datastore *datastore);
 
+/*! \brief Inherit datastores from a parent to a child. */
+int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to);
+
 /*! \brief Add a datastore to a channel */
 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore);
 

Added: team/mmichelson/forward-loop/include/asterisk/global_datastores.h
URL: http://svn.digium.com/view/asterisk/team/mmichelson/forward-loop/include/asterisk/global_datastores.h?view=auto&rev=87017
==============================================================================
--- team/mmichelson/forward-loop/include/asterisk/global_datastores.h (added)
+++ team/mmichelson/forward-loop/include/asterisk/global_datastores.h Wed Oct 24 17:11:33 2007
@@ -1,0 +1,21 @@
+
+#ifndef _ASTERISK_GLOBAL_DATASTORE_H
+#define _ASTERISK_GLOBAL_DATASTORE_H
+
+#include "asterisk/channel.h"
+
+void dialed_interface_destroy(void *data);
+void *dialed_interface_duplicate(void *data);
+
+const struct ast_datastore_info dialed_interface_info = {
+	.type ="dialed-interface",
+	.destroy = dialed_interface_destroy,
+	.duplicate = dialed_interface_duplicate,
+};
+
+struct ast_dialed_interface {
+	char interface[80];
+	AST_LIST_ENTRY(ast_dialed_interface) list;
+};
+
+#endif

Propchange: team/mmichelson/forward-loop/include/asterisk/global_datastores.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/mmichelson/forward-loop/include/asterisk/global_datastores.h
------------------------------------------------------------------------------
    svn:keywords = Mark Michelson 2007-10-24 mmichelson 87017

Propchange: team/mmichelson/forward-loop/include/asterisk/global_datastores.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/mmichelson/forward-loop/main/Makefile
URL: http://svn.digium.com/view/asterisk/team/mmichelson/forward-loop/main/Makefile?view=diff&rev=87017&r1=87016&r2=87017
==============================================================================
--- team/mmichelson/forward-loop/main/Makefile (original)
+++ team/mmichelson/forward-loop/main/Makefile Wed Oct 24 17:11:33 2007
@@ -26,7 +26,7 @@
 	utils.o plc.o jitterbuf.o dnsmgr.o devicestate.o \
 	netsock.o slinfactory.o ast_expr2.o ast_expr2f.o \
 	cryptostub.o sha1.o http.o fixedjitterbuf.o abstract_jb.o \
-	strcompat.o threadstorage.o dial.o astobj2.o
+	strcompat.o threadstorage.o dial.o astobj2.o global_datastores.o
 
 # we need to link in the objects statically, not as a library, because
 # otherwise modules will not have them available if none of the static

Modified: team/mmichelson/forward-loop/main/channel.c
URL: http://svn.digium.com/view/asterisk/team/mmichelson/forward-loop/main/channel.c?view=diff&rev=87017&r1=87016&r2=87017
==============================================================================
--- team/mmichelson/forward-loop/main/channel.c (original)
+++ team/mmichelson/forward-loop/main/channel.c Wed Oct 24 17:11:33 2007
@@ -888,9 +888,6 @@
 	AST_LIST_INSERT_HEAD(&channels, tmp, chan_list);
 	AST_LIST_UNLOCK(&channels);
 
-	ast_log(LOG_DEBUG, "Created new channel %p\n", tmp);
-	ast_backtrace();
-
 	return tmp;
 }
 
@@ -1197,7 +1194,6 @@
 	struct ast_frame *f;
 	struct varshead *headp;
 	struct ast_datastore *datastore = NULL;
-	struct ast_dialed_interface *di = NULL;
 	char name[AST_CHANNEL_NAME];
 	
 	headp=&chan->varshead;
@@ -1264,9 +1260,6 @@
 	while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
 		ast_var_delete(vardata);
 
-	while ((di = AST_LIST_REMOVE_HEAD(&chan->dialed_interfaces, list)))
-		free(di);
-
 	ast_app_group_discard(chan);
 
 	/* Destroy the jitterbuffer */
@@ -1321,6 +1314,23 @@
 	free(datastore);
 
 	return res;
+}
+
+int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
+{
+	struct ast_datastore *datastore = NULL, *datastore2;
+
+	AST_LIST_TRAVERSE(&from->datastores, datastore, entry) {
+		if (datastore->inheritance > 0) {
+			datastore2 = ast_channel_datastore_alloc(datastore->info, datastore->uid);
+			if (datastore2) {
+				datastore2->data = datastore->info->duplicate(datastore->data);
+				datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
+				AST_LIST_INSERT_TAIL(&to->datastores, datastore2, entry);
+			}
+		}
+	}
+	return 0;
 }
 
 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)

Added: team/mmichelson/forward-loop/main/global_datastores.c
URL: http://svn.digium.com/view/asterisk/team/mmichelson/forward-loop/main/global_datastores.c?view=auto&rev=87017
==============================================================================
--- team/mmichelson/forward-loop/main/global_datastores.c (added)
+++ team/mmichelson/forward-loop/main/global_datastores.c Wed Oct 24 17:11:33 2007
@@ -1,0 +1,41 @@
+#include "asterisk/global_datastores.h"
+#include "asterisk/linkedlists.h"
+
+void dialed_interface_destroy(void *data)
+{
+	struct ast_dialed_interface *di = NULL;
+	AST_LIST_HEAD(, ast_dialed_interface) *dialed_interface_list = data;
+	
+	if (!dialed_interface_list)
+		return;
+
+	AST_LIST_LOCK(dialed_interface_list);
+	while ((di = AST_LIST_REMOVE_HEAD(dialed_interface_list, list)))
+		free(di);
+	AST_LIST_UNLOCK(dialed_interface_list);
+
+	AST_LIST_HEAD_DESTROY(dialed_interface_list);
+	ast_free(dialed_interface_list);
+}
+
+void *dialed_interface_duplicate(void *data)
+{
+	struct ast_dialed_interface *di = NULL;
+	AST_LIST_HEAD(, ast_dialed_interface) *old_list;
+	AST_LIST_HEAD(, ast_dialed_interface) *new_list = NULL;
+
+	if(!(old_list = data))
+		return NULL;
+
+	if(!(new_list = ast_calloc(1, sizeof(*new_list))))
+		return NULL;
+
+	AST_LIST_HEAD_INIT(new_list);
+	AST_LIST_LOCK(old_list);
+	AST_LIST_TRAVERSE(old_list, di, list) {
+		AST_LIST_INSERT_TAIL(new_list, di, list);
+	}
+	AST_LIST_UNLOCK(old_list);
+
+	return new_list;
+}

Propchange: team/mmichelson/forward-loop/main/global_datastores.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/mmichelson/forward-loop/main/global_datastores.c
------------------------------------------------------------------------------
    svn:keywords = Mark Michelson 2007-10-24 mmichelson 87017

Propchange: team/mmichelson/forward-loop/main/global_datastores.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list