[asterisk-commits] russell: branch russell/events r81160 - in /team/russell/events: configs/ inc...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Aug 27 17:45:23 CDT 2007


Author: russell
Date: Mon Aug 27 17:45:23 2007
New Revision: 81160

URL: http://svn.digium.com/view/asterisk?view=rev&rev=81160
Log:
Allow building a subscription through a series of funciton calls for
dynamically building subscriptions as an alternative to the single function
call which works fine when the parameters of the subscription are static.

Added:
    team/russell/events/configs/dundi.conf.sample
      - copied unchanged from r81156, team/russell/ais/configs/dundi.conf.sample
Modified:
    team/russell/events/include/asterisk/event.h
    team/russell/events/main/event.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=81160&r1=81159&r2=81160
==============================================================================
--- team/russell/events/include/asterisk/event.h (original)
+++ team/russell/events/include/asterisk/event.h Mon Aug 27 17:45:23 2007
@@ -105,6 +105,20 @@
 struct ast_event_sub *ast_event_subscribe(enum ast_event_type event_type, 
 	ast_event_cb_t cb, void *userdata, ...);
 
+struct ast_event_sub *ast_event_subscribe_new(enum ast_event_type type, 
+	ast_event_cb_t cb, void *userdata);
+
+int ast_event_sub_append_ie_uint(struct ast_event_sub *sub,
+	enum ast_event_ie_type, uint32_t uint);
+
+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_exists(struct ast_event_sub *sub, 	
+	enum ast_event_ie_type);
+
+int ast_event_sub_activate(struct ast_event_sub *sub);
+
 /*!
  * \brief Un-subscribe from events
  *

Modified: team/russell/events/main/event.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/event.c?view=diff&rev=81160&r1=81159&r2=81160
==============================================================================
--- team/russell/events/main/event.c (original)
+++ team/russell/events/main/event.c Mon Aug 27 17:45:23 2007
@@ -25,7 +25,7 @@
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+ASTERISK_FILE_VERSION(__FILE__, "$Revision: 79039 $")
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -286,20 +286,125 @@
 	AST_RWLIST_UNLOCK(&ast_event_subs[event_type]);
 }
 
+struct ast_event_sub *ast_event_subscribe_new(enum ast_event_type type, 
+	ast_event_cb_t cb, void *userdata)
+{
+	struct ast_event_sub *sub;
+
+	if (type >= AST_EVENT_TOTAL) {
+		ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
+		return NULL;
+	}
+
+	if (!(sub = ast_calloc(1, sizeof(*sub))))
+		return NULL;
+
+	sub->type = type;
+	sub->cb = cb;
+	sub->userdata = userdata;
+	sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
+
+	return sub;
+}
+
+int ast_event_sub_append_ie_uint(struct ast_event_sub *sub,
+	enum ast_event_ie_type ie_type, uint32_t uint)
+{
+	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->payload.uint = uint;
+
+	AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
+
+	return 0;
+}
+
+int ast_event_sub_append_ie_exists(struct ast_event_sub *sub,
+	enum ast_event_ie_type ie_type)
+{
+	struct ast_event_ie_val *ie_val;
+
+	if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
+		return -1;
+
+	ie_val->ie_type = ie_type;
+
+	AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
+
+	return 0;
+}
+
+int ast_event_sub_append_ie_str(struct ast_event_sub *sub, 	
+	enum ast_event_ie_type ie_type, const char *str)
+{
+	struct ast_event_ie_val *ie_val;
+
+	if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
+		return -1;
+
+	ie_val->ie_type = ie_type;
+	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_activate(struct ast_event_sub *sub)
+{
+	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_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;
+		}
+
+		if (event)
+			ast_event_queue(event);
+	}
+
+	AST_RWLIST_WRLOCK(&ast_event_subs[sub->type]);
+	AST_RWLIST_INSERT_TAIL(&ast_event_subs[sub->type], sub, entry);
+	AST_RWLIST_UNLOCK(&ast_event_subs[sub->type]);
+
+	return 0;
+}
+
 struct ast_event_sub *ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb, 
 	void *userdata, ...)
 {
 	va_list ap;
 	enum ast_event_ie_type ie_type;
 	struct ast_event_sub *sub;
-	struct ast_event *event;
-
-	if (type >= AST_EVENT_TOTAL) {
-		ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
-		return NULL;
-	}
-
-	if (!(sub = ast_calloc(1, sizeof(*sub))))
+
+	if (!(sub = ast_event_subscribe_new(type, cb, userdata)))
 		return NULL;
 
 	va_start(ap, userdata);
@@ -307,61 +412,31 @@
 		ie_type != AST_EVENT_IE_END;
 		ie_type = va_arg(ap, enum ast_event_type))
 	{
-		struct ast_event_ie_val *ie_val;
-		if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
-			continue;
-		ie_val->ie_type = ie_type;
-		ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
-		if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
-			ie_val->payload.uint = va_arg(ap, uint32_t);
-		else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR) {
-			if (!(ie_val->payload.str = ast_strdup(va_arg(ap, const char *)))) {
-				ast_free(ie_val);
-				continue;
-			}
-		}
-		AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
+		enum ast_event_ie_pltype ie_pltype;
+
+		ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
+
+		switch (ie_pltype) {
+		case AST_EVENT_IE_PLTYPE_UINT:
+		{
+			uint32_t uint = va_arg(ap, uint32_t);
+			ast_event_sub_append_ie_uint(sub, ie_type, uint);
+			break;
+		}
+		case AST_EVENT_IE_PLTYPE_STR:
+		{
+			const char *str = va_arg(ap, const char *);
+			ast_event_sub_append_ie_str(sub, ie_type, str);
+			break;
+		}
+		case AST_EVENT_IE_PLTYPE_EXISTS:
+			ast_event_sub_append_ie_exists(sub, ie_type);
+			break;
+		}
 	}
 	va_end(ap);
 
-	sub->type = type;
-	sub->cb = cb;
-	sub->userdata = userdata;
-	sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
-
-	if (ast_event_check_subscriber(AST_EVENT_SUB,
-		AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, type,
-		AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
-		struct ast_event_ie_val *ie_val;
-
-		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_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;
-		}
-
-		if (event)
-			ast_event_queue(event);
-	}
-
-	AST_RWLIST_WRLOCK(&ast_event_subs[type]);
-	AST_RWLIST_INSERT_TAIL(&ast_event_subs[type], sub, entry);
-	AST_RWLIST_UNLOCK(&ast_event_subs[type]);
+	ast_event_sub_activate(sub);
 
 	return sub;
 }




More information about the asterisk-commits mailing list