[asterisk-commits] russell: branch russell/events r103448 - in /team/russell/events: include/ast...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Feb 12 14:04:07 CST 2008


Author: russell
Date: Tue Feb 12 14:04:06 2008
New Revision: 103448

URL: http://svn.digium.com/view/asterisk?view=rev&rev=103448
Log:
 - clean up some code for clarification and reducing duplication
 - cache device state change events not just on device name, but also the server
   that they originated from
 - add various raw IE handling stuff in event.c

Modified:
    team/russell/events/include/asterisk/event.h
    team/russell/events/include/asterisk/event_defs.h
    team/russell/events/main/devicestate.c
    team/russell/events/main/event.c
    team/russell/events/pbx/pbx_dundi.c

Modified: team/russell/events/include/asterisk/event.h
URL: http://svn.digium.com/view/asterisk/team/russell/events/include/asterisk/event.h?view=diff&rev=103448&r1=103447&r2=103448
==============================================================================
--- team/russell/events/include/asterisk/event.h (original)
+++ team/russell/events/include/asterisk/event.h Tue Feb 12 14:04:06 2008
@@ -122,6 +122,9 @@
 int ast_event_sub_append_ie_str(struct ast_event_sub *sub, 	
 	enum ast_event_ie_type, const char *str);
 
+int ast_event_sub_append_ie_raw(struct ast_event_sub *sub, 	
+	enum ast_event_ie_type, void *data, size_t raw_datalen);
+
 int ast_event_sub_append_ie_exists(struct ast_event_sub *sub, 	
 	enum ast_event_ie_type);
 

Modified: team/russell/events/include/asterisk/event_defs.h
URL: http://svn.digium.com/view/asterisk/team/russell/events/include/asterisk/event_defs.h?view=diff&rev=103448&r1=103447&r2=103448
==============================================================================
--- team/russell/events/include/asterisk/event_defs.h (original)
+++ team/russell/events/include/asterisk/event_defs.h Tue Feb 12 14:04:06 2008
@@ -113,7 +113,7 @@
 	 /*!
 	  * \brief Entity ID
 	  * Used by All events
-	  * Payload type: STR
+	  * Payload type: RAW
 	  * This IE indicates which server the event originated from
 	  */
 	 AST_EVENT_IE_EID      = 0x0A,
@@ -130,6 +130,8 @@
 	AST_EVENT_IE_PLTYPE_UINT,
 	/*! String */
 	AST_EVENT_IE_PLTYPE_STR,
+	/*! Raw data, compared with memcmp */
+	AST_EVENT_IE_PLTYPE_RAW,
 };
 
 /*!

Modified: team/russell/events/main/devicestate.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/devicestate.c?view=diff&rev=103448&r1=103447&r2=103448
==============================================================================
--- team/russell/events/main/devicestate.c (original)
+++ team/russell/events/main/devicestate.c Tue Feb 12 14:04:06 2008
@@ -383,7 +383,6 @@
 	struct devstate_prov *devprov;
 	int res = AST_DEVICE_INVALID;
 
-
 	AST_RWLIST_RDLOCK(&devstate_provs);
 	AST_RWLIST_TRAVERSE(&devstate_provs, devprov, list) {
 		ast_debug(5, "Checking provider %s with %s\n", devprov->label, provider);
@@ -394,6 +393,7 @@
 		}
 	}
 	AST_RWLIST_UNLOCK(&devstate_provs);
+
 	return res;
 }
 
@@ -413,6 +413,7 @@
 		 * device name if it exists. */
 		ast_event_queue_and_cache(event,
 			AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR,
+			AST_EVENT_IE_EID, AST_EVENT_IE_PLTYPE_RAW,
 			AST_EVENT_IE_END);
 	} else {
 		ast_event_queue(event);

Modified: team/russell/events/main/event.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/event.c?view=diff&rev=103448&r1=103447&r2=103448
==============================================================================
--- team/russell/events/main/event.c (original)
+++ team/russell/events/main/event.c Tue Feb 12 14:04:06 2008
@@ -100,7 +100,9 @@
 	union {
 		uint32_t uint;
 		const char *str;
+		void *raw;
 	} payload;
+	size_t raw_datalen;
 };
 
 /*! \brief Event subscription */
@@ -136,8 +138,16 @@
 
 static void ast_event_ie_val_destroy(struct ast_event_ie_val *ie_val)
 {
-	if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
-		ast_free((void *) ie_val->payload.str);
+	switch (ie_val->ie_pltype) {
+	case AST_EVENT_IE_PLTYPE_STR:
+	case AST_EVENT_IE_PLTYPE_RAW:
+		ast_free(ie_val->payload.raw);
+		break;
+	case AST_EVENT_IE_PLTYPE_UINT:
+	case AST_EVENT_IE_PLTYPE_EXISTS:
+	case AST_EVENT_IE_PLTYPE_UNKNOWN:
+		break;
+	}
 
 	ast_free(ie_val);
 }
@@ -211,6 +221,29 @@
 	return res;
 }
 
+static int match_ie_val(struct ast_event *event, struct ast_event_ie_val *ie_val)
+{
+	if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT) {
+		if (ie_val->payload.uint == ast_event_get_ie_uint(event, ie_val->ie_type))
+			return 1;
+		return 0;
+	}
+
+	if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR) {
+		if (!strcmp(ie_val->payload.str, ast_event_get_ie_str(event, ie_val->ie_type)))
+			return 1;
+		return 0;
+	}
+
+	if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS) {
+		if (ast_event_get_ie_raw(event, ie_val->ie_type))
+			return 1;
+		return 0;
+	}
+
+	return 0;
+}
+
 /*! \brief Dump the event cache for the subscribed event type */
 void ast_event_dump_cache(const struct ast_event_sub *event_sub)
 {
@@ -221,19 +254,8 @@
 	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[type], event_ref, entry) {
 		struct ast_event_ie_val *ie_val;
 		AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
-			if ( ! ( (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
-			   (ie_val->payload.uint ==
-			    ast_event_get_ie_uint(event_ref->event, ie_val->ie_type))) ||
-
-			   (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
-			   (!strcmp(ie_val->payload.str,
-			     ast_event_get_ie_str(event_ref->event, ie_val->ie_type)))) ||
-
-			   (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS &&
-			    ast_event_get_ie_raw(event_ref->event, ie_val->ie_type)) ) ) 
-			{
+			if (!match_ie_val(event_ref->event, ie_val))
 				break;
-			}
 		}
 		if (!ie_val) {
 			/* All parameters were matched on this cache entry, so dump it */
@@ -242,6 +264,43 @@
 	}
 	AST_RWLIST_TRAVERSE_SAFE_END
 	AST_RWLIST_UNLOCK(&ast_event_cache[type]);
+}
+
+static struct ast_event *gen_sub_event(struct ast_event_sub *sub)
+{
+	struct ast_event_ie_val *ie_val;
+	struct ast_event *event;
+
+	event = ast_event_new(AST_EVENT_SUB,
+		AST_EVENT_IE_UNIQUEID,  AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
+		AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
+		AST_EVENT_IE_END);
+
+	if (!event)
+		return NULL;
+
+	AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
+		switch (ie_val->ie_pltype) {
+		case AST_EVENT_IE_PLTYPE_UNKNOWN:
+			break;
+		case AST_EVENT_IE_PLTYPE_EXISTS:
+			ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
+			break;
+		case AST_EVENT_IE_PLTYPE_UINT:
+			ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
+			break;
+		case AST_EVENT_IE_PLTYPE_STR:
+			ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
+			break;
+		case AST_EVENT_IE_PLTYPE_RAW:
+			ast_event_append_ie_raw(&event, ie_val->ie_type, ie_val->payload.raw, ie_val->raw_datalen);
+			break;
+		}
+		if (!event)
+			break;
+	}
+
+	return event;
 }
 
 /*! \brief Send AST_EVENT_SUB events to this subscriber of ... subscriber events */
@@ -270,28 +329,7 @@
 		if (event_sub == sub)
 			continue;
 
-		event = ast_event_new(AST_EVENT_SUB,
-			AST_EVENT_IE_UNIQUEID,  AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
-			AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
-			AST_EVENT_IE_END);
-
-		AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
-			switch (ie_val->ie_pltype) {
-			case AST_EVENT_IE_PLTYPE_UNKNOWN:
-				break;
-			case AST_EVENT_IE_PLTYPE_EXISTS:
-				ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
-				break;
-			case AST_EVENT_IE_PLTYPE_UINT:
-				ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
-				break;
-			case AST_EVENT_IE_PLTYPE_STR:
-				ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
-				break;
-			}
-			if (!event)
-				break;
-		}
+		event = gen_sub_event(sub);
 
 		if (!event)
 			continue;
@@ -334,6 +372,7 @@
 
 	ie_val->ie_type = ie_type;
 	ie_val->payload.uint = uint;
+	ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_UINT;
 
 	AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
 
@@ -349,6 +388,7 @@
 		return -1;
 
 	ie_val->ie_type = ie_type;
+	ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_EXISTS;
 
 	AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
 
@@ -364,10 +404,36 @@
 		return -1;
 
 	ie_val->ie_type = ie_type;
+	ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_STR;
+
 	if (!(ie_val->payload.str = ast_strdup(str))) {
 		ast_free(ie_val);
 		return -1;
 	}
+
+	AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
+
+	return 0;
+}
+
+int ast_event_sub_append_ie_raw(struct ast_event_sub *sub, 	
+	enum ast_event_ie_type ie_type, void *data, size_t raw_datalen)
+{
+	struct ast_event_ie_val *ie_val;
+
+	if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
+		return -1;
+
+	ie_val->ie_type = ie_type;
+	ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_RAW;
+	ie_val->raw_datalen = raw_datalen;
+
+	if (!(ie_val->payload.raw = ast_malloc(raw_datalen))) {
+		ast_free(ie_val);
+		return -1;
+	}
+
+	memcpy(ie_val->payload.raw, data, raw_datalen);
 
 	AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
 
@@ -379,31 +445,9 @@
 	if (ast_event_check_subscriber(AST_EVENT_SUB,
 		AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
 		AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
-		struct ast_event_ie_val *ie_val;
 		struct ast_event *event;
 
-		event = ast_event_new(AST_EVENT_SUB,
-			AST_EVENT_IE_UNIQUEID,  AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
-			AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
-			AST_EVENT_IE_END);
-
-		AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
-			switch (ie_val->ie_pltype) {
-			case AST_EVENT_IE_PLTYPE_UNKNOWN:
-				break;
-			case AST_EVENT_IE_PLTYPE_EXISTS:
-				ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
-				break;
-			case AST_EVENT_IE_PLTYPE_UINT:
-				ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
-				break;
-			case AST_EVENT_IE_PLTYPE_STR:
-				ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
-				break;
-			}
-			if (!event)
-				break;
-		}
+		event = gen_sub_event(sub);
 
 		if (event)
 			ast_event_queue(event);
@@ -448,6 +492,13 @@
 		{
 			const char *str = va_arg(ap, const char *);
 			ast_event_sub_append_ie_str(sub, ie_type, str);
+			break;
+		}
+		case AST_EVENT_IE_PLTYPE_RAW:
+		{
+			void *data = va_arg(ap, void *);
+			size_t data_len = va_arg(ap, size_t);
+			ast_event_sub_append_ie_raw(sub, ie_type, data, data_len);
 			break;
 		}
 		case AST_EVENT_IE_PLTYPE_EXISTS:
@@ -689,16 +740,8 @@
 	enum ast_event_ie_type ie_type;
 	struct ast_event *dup_event = NULL;
 	struct ast_event_ref *event_ref;
-	struct cache_arg {
-		AST_LIST_ENTRY(cache_arg) entry;
-		enum ast_event_ie_type ie_type;
-		enum ast_event_ie_pltype ie_pltype;
-		union {
-			uint32_t uint;
-			const char *str;
-		} payload;
-	} *cache_arg;
-	AST_LIST_HEAD_NOLOCK_STATIC(cache_args, cache_arg);
+	struct ast_event_ie_val *cache_arg;
+	AST_LIST_HEAD_NOLOCK_STATIC(cache_args, ast_event_ie_val);
 
 	if (type >= AST_EVENT_TOTAL) {
 		ast_log(LOG_ERROR, "%u is an invalid type!\n", type);

Modified: team/russell/events/pbx/pbx_dundi.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/pbx/pbx_dundi.c?view=diff&rev=103448&r1=103447&r2=103448
==============================================================================
--- team/russell/events/pbx/pbx_dundi.c (original)
+++ team/russell/events/pbx/pbx_dundi.c Tue Feb 12 14:04:06 2008
@@ -5021,6 +5021,7 @@
 	case AST_EVENT_IE_PLTYPE_EXISTS:
 		ast_event_sub_append_ie_exists(sub, ie_type);
 		break;
+	case AST_EVENT_IE_PLTYPE_RAW:
 	case -1:
 		ast_log(LOG_ERROR, "Invalid value '%s' for parameter in event "
 			"mapping for DUNDi context '%s\n", 




More information about the asterisk-commits mailing list