[svn-commits] russell: branch russell/events r59243 - in /team/russell/events: apps/ main/

svn-commits at lists.digium.com svn-commits at lists.digium.com
Mon Mar 26 16:49:15 MST 2007


Author: russell
Date: Mon Mar 26 18:49:14 2007
New Revision: 59243

URL: http://svn.digium.com/view/asterisk?view=rev&rev=59243
Log:
Merge a bunch of fixes from testing event based MWI in chan_sip.  
It works great!  The fixes were mostly endianness related.  However, I did
make app_voicemail fire off an initial event for each mailbox at load time to
populate the event cache and allow chan_sip to send its initial MWI to peers.

Modified:
    team/russell/events/apps/app_voicemail.c
    team/russell/events/main/event.c

Modified: team/russell/events/apps/app_voicemail.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/apps/app_voicemail.c?view=diff&rev=59243&r1=59242&r2=59243
==============================================================================
--- team/russell/events/apps/app_voicemail.c (original)
+++ team/russell/events/apps/app_voicemail.c Mon Mar 26 18:49:14 2007
@@ -2221,12 +2221,14 @@
 
 static void free_user(struct ast_vm_user *vmu)
 {
+	if (!ast_test_flag(vmu, VM_ALLOCED))
+		return;
+
 	if (vmu->event_pub) {
 		ast_event_unreg_publisher(vmu->event_pub);
 		vmu->event_pub = NULL;
 	}
-	if (ast_test_flag(vmu, VM_ALLOCED))
-		free(vmu);
+	free(vmu);
 }
 
 static void free_zone(struct vm_zone *z)
@@ -3943,14 +3945,21 @@
 	return cmd;
 }
 
-static void queue_mwi_event(const char *mailbox, int new, int old)
+static void queue_mwi_event(const char *mbox, int new, int old, int with_sub_only)
 {
 	struct ast_event *event;
-
-	if (ast_event_check_subscriber(AST_EVENT_MWI,
+	char *mailbox;
+
+	/* Strip off @default */
+	mailbox = ast_strdupa(mbox);
+	if (strstr(mailbox, "@default"))
+		mailbox = strsep(&mailbox, "@");
+
+	if (with_sub_only && ast_event_check_subscriber(AST_EVENT_MWI,
 		AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
-		AST_EVENT_IE_END) == AST_EVENT_SUB_NONE)
+		AST_EVENT_IE_END) == AST_EVENT_SUB_NONE) {
 		return;
+	}
 
 	if (!(event = ast_event_new(AST_EVENT_MWI)))
 		return;
@@ -4014,7 +4023,7 @@
 	if (ast_app_has_voicemail(ext_context, NULL)) 
 		ast_app_inboxcount(ext_context, &newmsgs, &oldmsgs);
 
-	queue_mwi_event(ext_context, newmsgs, oldmsgs);
+	queue_mwi_event(ext_context, newmsgs, oldmsgs, 1);
 
 	manager_event(EVENT_FLAG_CALL, "MessageWaiting", "Mailbox: %s@%s\r\nWaiting: %d\r\nNew: %d\r\nOld: %d\r\n", vmu->mailbox, vmu->context, ast_app_has_voicemail(ext_context, NULL), newmsgs, oldmsgs);
 	run_externnotify(vmu->context, vmu->mailbox);
@@ -6798,7 +6807,7 @@
 		manager_event(EVENT_FLAG_CALL, "MessageWaiting", "Mailbox: %s\r\nWaiting: %d\r\n", ext_context, has_voicemail(ext_context, NULL));
 		run_externnotify(vmu->context, vmu->mailbox);
 		ast_app_inboxcount(ext_context, &new, &old);
-		queue_mwi_event(ext_context, new, old);
+		queue_mwi_event(ext_context, new, old, 1);
 	}
 #ifdef IMAP_STORAGE
 	/* expunge message - use UID Expunge if supported on IMAP server*/
@@ -6940,24 +6949,36 @@
 	char *stringp;
 	char *s;
 	struct ast_vm_user *vmu;
+	char *mailbox_full;
+	int new = 0, old = 0;
 
 	tmp = ast_strdupa(data);
 
-	if ((vmu = find_or_create(context, mbox))) {
-		populate_defaults(vmu);
-
-		stringp = tmp;
-		if ((s = strsep(&stringp, ","))) 
-			ast_copy_string(vmu->password, s, sizeof(vmu->password));
-		if (stringp && (s = strsep(&stringp, ","))) 
-			ast_copy_string(vmu->fullname, s, sizeof(vmu->fullname));
-		if (stringp && (s = strsep(&stringp, ","))) 
-			ast_copy_string(vmu->email, s, sizeof(vmu->email));
-		if (stringp && (s = strsep(&stringp, ","))) 
-			ast_copy_string(vmu->pager, s, sizeof(vmu->pager));
-		if (stringp && (s = strsep(&stringp, ","))) 
-			apply_options(vmu, s);
-	}
+	if (!(vmu = find_or_create(context, mbox)))
+		return -1;
+	
+	populate_defaults(vmu);
+
+	stringp = tmp;
+	if ((s = strsep(&stringp, ","))) 
+		ast_copy_string(vmu->password, s, sizeof(vmu->password));
+	if (stringp && (s = strsep(&stringp, ","))) 
+		ast_copy_string(vmu->fullname, s, sizeof(vmu->fullname));
+	if (stringp && (s = strsep(&stringp, ","))) 
+		ast_copy_string(vmu->email, s, sizeof(vmu->email));
+	if (stringp && (s = strsep(&stringp, ","))) 
+		ast_copy_string(vmu->pager, s, sizeof(vmu->pager));
+	if (stringp && (s = strsep(&stringp, ","))) 
+		apply_options(vmu, s);
+
+	mailbox_full = alloca(strlen(mbox) + strlen(context) + 1);
+	strcpy(mailbox_full, mbox);
+	strcat(mailbox_full, "@");
+	strcat(mailbox_full, context);
+
+	inboxcount(mailbox_full, &new, &old);
+	queue_mwi_event(mailbox_full, new, old, 0);
+
 	return 0;
 }
 
@@ -7798,7 +7819,7 @@
 
 static int reload(void)
 {
-	return(load_config());
+	return load_config();
 }
 
 static int unload_module(void)
@@ -7826,22 +7847,22 @@
 static int load_module(void)
 {
 	int res;
+
+	/* compute the location of the voicemail spool directory */
+	snprintf(VM_SPOOL_DIR, sizeof(VM_SPOOL_DIR), "%s/voicemail/", ast_config_AST_SPOOL_DIR);
+
+	if ((res = load_config()))
+		return res;
+
 	res = ast_register_application(app, vm_exec, synopsis_vm, descrip_vm);
 	res |= ast_register_application(app2, vm_execmain, synopsis_vmain, descrip_vmain);
 	res |= ast_register_application(app3, vm_box_exists, synopsis_vm_box_exists, descrip_vm_box_exists);
 	res |= ast_register_application(app4, vmauthenticate, synopsis_vmauthenticate, descrip_vmauthenticate);
 	res |= ast_custom_function_register(&mailbox_exists_acf);
 	if (res)
-		return(res);
-
-	if ((res=load_config())) {
-		return(res);
-	}
+		return res;
 
 	ast_cli_register_multiple(cli_voicemail, sizeof(cli_voicemail) / sizeof(struct ast_cli_entry));
-
-	/* compute the location of the voicemail spool directory */
-	snprintf(VM_SPOOL_DIR, sizeof(VM_SPOOL_DIR), "%s/voicemail/", ast_config_AST_SPOOL_DIR);
 
 	ast_install_vm_functions(has_voicemail, inboxcount, messagecount);
 

Modified: team/russell/events/main/event.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/event.c?view=diff&rev=59243&r1=59242&r2=59243
==============================================================================
--- team/russell/events/main/event.c (original)
+++ team/russell/events/main/event.c Mon Mar 26 18:49:14 2007
@@ -385,7 +385,7 @@
 
 enum ast_event_type ast_event_get_type(const struct ast_event *event)
 {
-	return event->type;
+	return ntohs(event->type);
 }
 
 uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
@@ -405,13 +405,17 @@
 const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
 {
 	struct ast_event_ie *ie;
+	uint16_t event_len;
+
+	ie_type = ntohs(ie_type);
+	event_len = ntohs(event->event_len);
 
 	ie = ((void *) event) + sizeof(*event);
 
-	while ((((void *) ie) - ((void *) event)) < event->event_len) {
+	while ((((void *) ie) - ((void *) event)) < event_len) {
 		if (ie->ie_type == ie_type)
 			return ie->ie_payload;
-		ie = ((void *) ie) + sizeof(*ie) + ie->ie_payload_len;
+		ie = ((void *) ie) + sizeof(*ie) + ntohs(ie->ie_payload_len);
 	}
 
 	return NULL;
@@ -435,18 +439,20 @@
 {
 	struct ast_event_ie *ie;
 	unsigned int extra_len;
-
+	uint16_t event_len;
+
+	event_len = ntohs((*event)->event_len);
 	extra_len = sizeof(*ie) + data_len;
 
-	if (!(*event = ast_realloc(*event, (*event)->event_len + extra_len)))
+	if (!(*event = ast_realloc(*event, event_len + extra_len)))
 		return -1;
 
-	ie = ((void *) *event) + (*event)->event_len;
+	ie = ((void *) *event) + event_len;
 	ie->ie_type = htons(ie_type);
 	ie->ie_payload_len = htons(data_len);
 	memcpy(ie->ie_payload, data, data_len);
 
-	(*event)->event_len += extra_len;
+	(*event)->event_len = htons(event_len + extra_len);
 
 	return 0;
 }
@@ -485,11 +491,14 @@
 static struct ast_event *ast_event_dup(const struct ast_event *event)
 {
 	struct ast_event *dup_event;
-
-	if (!(dup_event = ast_calloc(1, event->event_len)))
+	uint16_t event_len;
+
+	event_len = ntohs(event->event_len);
+
+	if (!(dup_event = ast_calloc(1, event_len)))
 		return NULL;
 	
-	memcpy(dup_event, event, event->event_len);
+	memcpy(dup_event, event, event_len);
 
 	return dup_event;
 }
@@ -582,7 +591,7 @@
 	
 	event_ref->event = dup_event;
 
-	AST_LIST_INSERT_TAIL(&ast_event_cache[event->type], event_ref, entry);
+	AST_LIST_INSERT_TAIL(&ast_event_cache[ntohs(event->type)], event_ref, entry);
 
 	return 0;
 }
@@ -591,6 +600,7 @@
 {
 	va_list ap;
 	enum ast_event_type ie_type;
+	uint16_t host_event_type;
 	struct ast_event_ref *event_ref;
 	int res;
 	struct cache_arg {
@@ -600,10 +610,12 @@
 	} *cache_arg;
 	AST_LIST_HEAD_NOLOCK_STATIC(cache_args, cache_arg);
 
+	host_event_type = ntohs(event->type);
+
 	/* Invalid type */
-	if (event->type >= AST_EVENT_TOTAL) {
+	if (host_event_type >= AST_EVENT_TOTAL) {
 		ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
-			"type '%d'!\n", event->type);
+			"type '%d'!\n", host_event_type);
 		return -1;
 	}
 
@@ -626,8 +638,8 @@
 		return ast_event_queue(event);
 	}
  
-	AST_RWLIST_WRLOCK(&ast_event_cache[event->type]);
-	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[event->type], event_ref, entry) {
+	AST_RWLIST_WRLOCK(&ast_event_cache[host_event_type]);
+	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[host_event_type], event_ref, entry) {
 		AST_LIST_TRAVERSE(&cache_args, cache_arg, entry) {
 			if ( ! ( (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
 			   (ast_event_get_ie_uint(event, cache_arg->ie_type) ==
@@ -645,13 +657,13 @@
 		}
 		if (!cache_arg) {
 			/* All parameters were matched on this cache entry, so remove it */
-			AST_LIST_REMOVE_CURRENT(&ast_event_cache[event->type], entry);
+			AST_LIST_REMOVE_CURRENT(&ast_event_cache[host_event_type], entry);
 			ast_event_ref_destroy(event_ref);
 		}
 	}
 	AST_RWLIST_TRAVERSE_SAFE_END
 	res = ast_event_dup_and_cache(event);
-	AST_RWLIST_UNLOCK(&ast_event_cache[event->type]);
+	AST_RWLIST_UNLOCK(&ast_event_cache[host_event_type]);
 
 	return (ast_event_queue(event) || res) ? -1 : 0;
 }
@@ -659,16 +671,19 @@
 int ast_event_queue(struct ast_event *event)
 {
 	struct ast_event_ref *event_ref;
+	uint16_t host_event_type;
+
+	host_event_type = ntohs(event->type);
 
 	/* Invalid type */
-	if (event->type >= AST_EVENT_TOTAL) {
+	if (host_event_type >= AST_EVENT_TOTAL) {
 		ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
-			"type '%d'!\n", event->type);
+			"type '%d'!\n", host_event_type);
 		return -1;
 	}
 
 	/* If nobody has subscribed to this event type, throw it away now */
-	if (ast_event_check_subscriber(event->type, AST_EVENT_IE_END) 
+	if (ast_event_check_subscriber(host_event_type, AST_EVENT_IE_END) 
 		== AST_EVENT_SUB_NONE) {
 		ast_event_destroy(event);
 		return 0;
@@ -692,17 +707,21 @@
 	for (;;) {
 		struct ast_event_ref *event_ref;
 		struct ast_event_sub *sub;
+		uint16_t host_event_type;
 
 		ast_mutex_lock(&event_thread.lock);
 		while (!(event_ref = AST_LIST_REMOVE_HEAD(&event_thread.event_q, entry)))
 			ast_cond_wait(&event_thread.cond, &event_thread.lock);
 		ast_mutex_unlock(&event_thread.lock);
 
+		host_event_type = ntohs(event_ref->event->type);
+
 		/* Subscribers to this specific event first */
-		AST_RWLIST_RDLOCK(&ast_event_subs[event_ref->event->type]);
-		AST_RWLIST_TRAVERSE(&ast_event_subs[event_ref->event->type], sub, entry)
+		AST_RWLIST_RDLOCK(&ast_event_subs[host_event_type]);
+		/* XXX Check subscription parameters! */
+		AST_RWLIST_TRAVERSE(&ast_event_subs[host_event_type], sub, entry)
 			sub->cb(event_ref->event);
-		AST_RWLIST_UNLOCK(&ast_event_subs[event_ref->event->type]);
+		AST_RWLIST_UNLOCK(&ast_event_subs[host_event_type]);
 
 		/* Now to subscribers to all event types */
 		AST_RWLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);



More information about the svn-commits mailing list