[svn-commits] russell: branch russell/events r59059 - in /team/russell/events: apps/ includ...

svn-commits at lists.digium.com svn-commits at lists.digium.com
Mon Mar 19 18:18:13 MST 2007


Author: russell
Date: Mon Mar 19 20:18:13 2007
New Revision: 59059

URL: http://svn.digium.com/view/asterisk?view=rev&rev=59059
Log:
I was bored on a plane today and decided to being working on an event system
for Asterisk.  I have made some significant progress on what I consider
"phase 1" of this project.

Vision:
 * phase 1: create an internal generic event system and convert some existing
   architecture to use it: MWI, extension and device state, etc.
 * phase 2: allow subscriptions between servers over IAX2
 * phase 3: create a peer-to-peer method (like DUNDi) to allow a federation of
   Asterisk servers to share events.

This is what I got done before my battery died:
 * Create an internal event system.  Events can be queued up in a non-blocking
   way, and they are dispatched to event subscribers by a thread pool.
 * Begin working on the ability to cache events so that the system knows the
   last known value for "state"-like events such as device state.
 * Modify app_voicemail to generate an event for MWI.
 * Create res_eventtest that can generate events, and subscribes to events for
   the purpose of testing this code.

Next steps:
 * Finish event caching, since it should be used for MWI.
 * Modify chan_sip (and other channels) to subscribe to MWI events instead of
   having to poll for it.  The load on voicemail will be much smaller, and
   MWI will be near-instant, instead of delayed until the next time the channel
   driver decides to check.

Added:
    team/russell/events/include/asterisk/event.h   (with props)
    team/russell/events/include/asterisk/event_defs.h   (with props)
    team/russell/events/main/event.c   (with props)
    team/russell/events/res/res_eventtest.c   (with props)
Modified:
    team/russell/events/apps/app_voicemail.c
    team/russell/events/include/asterisk.h
    team/russell/events/main/Makefile
    team/russell/events/main/asterisk.c
    team/russell/events/main/dial.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=59059&r1=59058&r2=59059
==============================================================================
--- team/russell/events/apps/app_voicemail.c (original)
+++ team/russell/events/apps/app_voicemail.c Mon Mar 19 20:18:13 2007
@@ -104,6 +104,7 @@
 #include "asterisk/utils.h"
 #include "asterisk/stringfields.h"
 #include "asterisk/smdi.h"
+#include "asterisk/event.h"
 
 #ifdef ODBC_STORAGE
 #include "asterisk/res_odbc.h"
@@ -3934,6 +3935,22 @@
 	return cmd;
 }
 
+static void queue_mwi_event(const char *mailbox, int new, int old)
+{
+	struct ast_event *event;
+	
+	if (!(event = ast_event_new(AST_EVENT_MWI)))
+		return;
+	if (ast_event_append_ie_str(&event, AST_EVENT_IE_MAILBOX, mailbox))
+		return;
+	if (ast_event_append_ie_uint(&event, AST_EVENT_IE_NEWMSGS, new))
+		return;
+	if (ast_event_append_ie_uint(&event, AST_EVENT_IE_OLDMSGS, old))
+		return;
+
+	ast_event_queue(event);
+}
+
 static int notify_new_message(struct ast_channel *chan, struct ast_vm_user *vmu, int msgnum, long duration, char *fmt, char *cidnum, char *cidname)
 {
 	char todir[PATH_MAX], fn[PATH_MAX], ext_context[PATH_MAX], *stringp;
@@ -3981,6 +3998,8 @@
 	/* Leave voicemail for someone */
 	if (ast_app_has_voicemail(ext_context, NULL)) 
 		ast_app_inboxcount(ext_context, &newmsgs, &oldmsgs);
+
+	queue_mwi_event(ext_context, newmsgs, oldmsgs);
 
 	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);
@@ -6759,9 +6778,12 @@
 	if (vmu)
 		close_mailbox(&vms, vmu);
 	if (valid) {
+		int new = 0, old = 0;
 		snprintf(ext_context, sizeof(ext_context), "%s@%s", vms.username, vmu->context);
 		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);
 	}
 #ifdef IMAP_STORAGE
 	/* expunge message - use UID Expunge if supported on IMAP server*/

Modified: team/russell/events/include/asterisk.h
URL: http://svn.digium.com/view/asterisk/team/russell/events/include/asterisk.h?view=diff&rev=59059&r1=59058&r2=59059
==============================================================================
--- team/russell/events/include/asterisk.h (original)
+++ team/russell/events/include/asterisk.h Mon Mar 19 20:18:13 2007
@@ -82,6 +82,7 @@
 void dnsmgr_start_refresh(void);		/*!< Provided by dnsmgr.c */
 int dnsmgr_reload(void);			/*!< Provided by dnsmgr.c */
 void threadstorage_init(void);			/*!< Provided by threadstorage.c */
+void ast_event_init(void);          /*!< Provided by event.c */
 
 /* Many headers need 'ast_channel' to be defined */
 struct ast_channel;

Added: team/russell/events/include/asterisk/event.h
URL: http://svn.digium.com/view/asterisk/team/russell/events/include/asterisk/event.h?view=auto&rev=59059
==============================================================================
--- team/russell/events/include/asterisk/event.h (added)
+++ team/russell/events/include/asterisk/event.h Mon Mar 19 20:18:13 2007
@@ -1,0 +1,59 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \author Russell Bryant <russell at digium.com>
+ * \brief Generic event system
+ */
+
+#ifndef AST_EVENT_H
+#define AST_EVENT_H
+
+#include "asterisk/event_defs.h"
+
+typedef void (*ast_event_cb_t)(const struct ast_event *);
+
+int ast_event_subscribe(enum ast_event_type, ast_event_cb_t);
+
+void ast_event_unsubscribe(enum ast_event_type, ast_event_cb_t);
+
+struct ast_event *ast_event_new(enum ast_event_type);
+
+int ast_event_queue(struct ast_event *);
+
+int ast_event_queue_and_cache(struct ast_event *, ...);
+
+struct ast_event *ast_event_get_cached(enum ast_event_type, ...);
+
+int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
+	const char *str);
+
+int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
+	uint32_t data);
+
+int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
+	const void *data, size_t data_len);
+
+uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type);
+
+const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type);
+
+const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type);
+
+#endif /* AST_EVENT_H */

Propchange: team/russell/events/include/asterisk/event.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/russell/events/include/asterisk/event.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/russell/events/include/asterisk/event.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/russell/events/include/asterisk/event_defs.h
URL: http://svn.digium.com/view/asterisk/team/russell/events/include/asterisk/event_defs.h?view=auto&rev=59059
==============================================================================
--- team/russell/events/include/asterisk/event_defs.h (added)
+++ team/russell/events/include/asterisk/event_defs.h Mon Mar 19 20:18:13 2007
@@ -1,0 +1,64 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \author Russell Bryant <russell at digium.com>
+ * \brief Generic event system
+ */
+
+#ifndef AST_EVENT_DEFS_H
+#define AST_EVENT_DEFS_H
+
+/*! \brief Event types
+ * \note These values can *never* change. */
+enum ast_event_type {
+	/*! Reserved to provide the ability to subscribe to all events.  A specific
+	    event should never have a payload of 0. */
+	AST_EVENT_ALL   = 0x00,
+	/*! Voicemail message waiting indication */
+	AST_EVENT_MWI   = 0x01,
+	/*! Number of event types.  This should be the last event type + 1 */
+	AST_EVENT_TOTAL = 0x02,
+};
+
+/*! \brief Event Information Element types */
+enum ast_event_ie_type {
+	/*! Used with AST_EVENT_MWI */
+	AST_EVENT_IE_NEWMSGS,
+	/*! Used with AST_EVENT_MWI */
+	AST_EVENT_IE_OLDMSGS,
+	/*! Used with AST_EVENT_MWI */
+	AST_EVENT_IE_MAILBOX,
+};
+
+struct ast_event_ie {
+	enum ast_event_ie_type ie_type:16;
+	/*! Total length of the IE payload */
+	uint16_t ie_payload_len;
+	unsigned char ie_payload[0];
+} __attribute__ ((packed));
+
+struct ast_event {
+	enum ast_event_type type:16;
+	/*! Total length of the event */
+	uint16_t event_len:16;
+	unsigned char payload[0];
+} __attribute__ ((packed));
+
+#endif /* AST_EVENT_DEFS_H */

Propchange: team/russell/events/include/asterisk/event_defs.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/russell/events/include/asterisk/event_defs.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/russell/events/include/asterisk/event_defs.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/russell/events/main/Makefile
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/Makefile?view=diff&rev=59059&r1=59058&r2=59059
==============================================================================
--- team/russell/events/main/Makefile (original)
+++ team/russell/events/main/Makefile Mon Mar 19 20:18:13 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
+	strcompat.o threadstorage.o dial.o event.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/russell/events/main/asterisk.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/asterisk.c?view=diff&rev=59059&r1=59058&r2=59059
==============================================================================
--- team/russell/events/main/asterisk.c (original)
+++ team/russell/events/main/asterisk.c Mon Mar 19 20:18:13 2007
@@ -2545,6 +2545,7 @@
 	ast_alaw_init();
 	callerid_init();
 	ast_builtins_init();
+	ast_event_init();
 	ast_utils_init();
 	tdd_init();
 	/* When Asterisk restarts after it has dropped the root privileges,

Modified: team/russell/events/main/dial.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/dial.c?view=diff&rev=59059&r1=59058&r2=59059
==============================================================================
--- team/russell/events/main/dial.c (original)
+++ team/russell/events/main/dial.c Mon Mar 19 20:18:13 2007
@@ -46,23 +46,23 @@
 
 /*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
 struct ast_dial {
-	int num;                                           /*! Current number to give to next dialed channel */
-	enum ast_dial_result state;                       /*! Status of dial */
-	void *options[AST_DIAL_OPTION_MAX];                /*! Global options */
-	ast_dial_state_callback state_callback;          /*! Status callback */
-	AST_LIST_HEAD_NOLOCK(, ast_dial_channel) channels; /*! Channels being dialed */
-	pthread_t thread;                                  /*! Thread (if running in async) */
+	int num;                                           /*!< Current number to give to next dialed channel */
+	enum ast_dial_result state;                        /*!< Status of dial */
+	void *options[AST_DIAL_OPTION_MAX];                /*!< Global options */
+	ast_dial_state_callback state_callback;            /*!< Status callback */
+	AST_LIST_HEAD_NOLOCK(, ast_dial_channel) channels; /*!< Channels being dialed */
+	pthread_t thread;                                  /*!< Thread (if running in async) */
 };
 
 /*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
 struct ast_dial_channel {
-	int num;                               /*! Unique number for dialed channel */
-	const char *tech;                      /*! Technology being dialed */
-	const char *device;                    /*! Device being dialed */
-	void *options[AST_DIAL_OPTION_MAX];    /*! Channel specific options */
-	int cause;                             /*! Cause code in case of failure */
-	struct ast_channel *owner;             /*! Asterisk channel */
-	AST_LIST_ENTRY(ast_dial_channel) list; /*! Linked list information */
+	int num;                               /*!< Unique number for dialed channel */
+	const char *tech;                      /*!< Technology being dialed */
+	const char *device;                    /*!< Device being dialed */
+	void *options[AST_DIAL_OPTION_MAX];    /*!< Channel specific options */
+	int cause;                             /*!< Cause code in case of failure */
+	struct ast_channel *owner;             /*!< Asterisk channel */
+	AST_LIST_ENTRY(ast_dial_channel) list; /*!< Linked list information */
 };
 
 /*! \brief Typedef for dial option enable */

Added: team/russell/events/main/event.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/event.c?view=auto&rev=59059
==============================================================================
--- team/russell/events/main/event.c (added)
+++ team/russell/events/main/event.c Mon Mar 19 20:18:13 2007
@@ -1,0 +1,265 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Internal event system
+ *
+ * \author Russell Bryant <russell at digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "asterisk/event.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/lock.h"
+#include "asterisk/utils.h"
+
+#define NUM_EVENT_THREADS 5
+
+struct ast_event_ref {
+	struct ast_event *event;
+	AST_LIST_ENTRY(ast_event_ref) entry;
+};
+
+/*! \brief data shared between event dispatching threads */
+static struct {
+	ast_cond_t cond;
+	ast_mutex_t lock;
+	AST_LIST_HEAD_NOLOCK(, ast_event_ref) event_q;
+} event_thread = {
+	.lock = AST_MUTEX_INIT_VALUE,
+};
+
+/*! \brief Event subscription */
+struct ast_event_sub {
+	ast_event_cb_t cb;
+	AST_RWLIST_ENTRY(ast_event_sub) entry;
+};
+
+/*! \brief Event subscriptions
+ * The event subscribers are indexed by which event they are subscribed to */
+static AST_RWLIST_HEAD(ast_event_sub_list, ast_event_sub) ast_event_subs[AST_EVENT_TOTAL];
+
+/*! \brief Cached events
+ * The event cache is indexed on the event type.  The purpose of this is 
+ * for events that express some sort of state.  So, when someone first
+ * needs to know this state, it can get the last known state from the cache. */
+static AST_RWLIST_HEAD(ast_event_ref_list, ast_event_ref) ast_event_cache[AST_EVENT_TOTAL];
+
+int ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb)
+{
+	struct ast_event_sub *sub;
+
+	if (type >= AST_EVENT_TOTAL)
+		return -1;
+
+	if (!(sub = ast_calloc(1, sizeof(*sub))))
+		return -1;
+
+	sub->cb = cb;
+
+	AST_RWLIST_WRLOCK(&ast_event_subs[type]);
+	AST_RWLIST_INSERT_TAIL(&ast_event_subs[type], sub, entry);
+	AST_RWLIST_UNLOCK(&ast_event_subs[type]);
+
+	return 0;
+}
+
+void ast_event_unsubscribe(enum ast_event_type type, ast_event_cb_t cb)
+{
+	struct ast_event_sub *sub;
+
+	AST_RWLIST_WRLOCK(&ast_event_subs[type]);
+	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_subs[type], sub, entry) {
+		if (sub->cb == cb) {
+			AST_LIST_REMOVE_CURRENT(&ast_event_subs[type], entry);
+			free(sub);
+			break;
+		}
+	}
+	AST_RWLIST_TRAVERSE_SAFE_END
+	AST_RWLIST_UNLOCK(&ast_event_subs[type]);
+}
+
+uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
+{
+	const uint32_t *ie_val;
+
+	ie_val = ast_event_get_ie_raw(event, ie_type);
+
+	return ie_val ? ntohl(*ie_val) : 0;
+}
+
+const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
+{
+	return ast_event_get_ie_raw(event, ie_type);
+}
+
+const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
+{
+	struct ast_event_ie *ie;
+
+	ie = ((void *) event) + sizeof(*event);
+
+	while ((((void *) ie) - ((void *) event)) < event->event_len) {
+		if (ie->ie_type == ie_type)
+			return ie->ie_payload;
+		ie = ((void *) ie) + sizeof(*ie) + ie->ie_payload_len;
+	}
+
+	return NULL;
+}
+
+int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
+	const char *str)
+{
+	return ast_event_append_ie_raw(event, ie_type, str, strlen(str) + 1);
+}
+
+int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
+	uint32_t data)
+{
+	data = htonl(data);
+	return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
+}
+
+int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
+	const void *data, size_t data_len)
+{
+	struct ast_event_ie *ie;
+	unsigned int extra_len;
+
+	extra_len = sizeof(*ie) + data_len;
+
+	if (!(*event = ast_realloc(*event, (*event)->event_len + extra_len)))
+		return -1;
+
+	ie = ((void *) *event) + (*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;
+
+	return 0;
+}
+
+struct ast_event *ast_event_new(enum ast_event_type type)
+{
+	struct ast_event *event;
+
+	/* Invalid type */
+	if (type >= AST_EVENT_TOTAL) {
+		ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
+			"type '%d'!\n", type);
+		return NULL;
+	}
+
+	if (!(event = ast_calloc(1, sizeof(*event))))
+		return NULL;
+
+	event->type = htons(type);
+	event->event_len = htons(sizeof(*event));
+
+	return event;
+}
+
+struct ast_event *ast_event_get_cached(enum ast_event_type type, ...)
+{
+	/* XXX Get event from cache matching specified IE parameters */
+
+	return NULL;
+}
+
+int ast_event_queue_and_cache(struct ast_event *event, ...)
+{
+	/* XXX Add/Replace event in cache */
+
+	return ast_event_queue(event);
+}
+
+int ast_event_queue(struct ast_event *event)
+{
+	struct ast_event_ref *event_ref;
+
+	if (!(event_ref = ast_calloc(1, sizeof(*event_ref))))
+		return -1;
+
+	event_ref->event = event;
+
+	ast_mutex_lock(&event_thread.lock);
+	AST_LIST_INSERT_TAIL(&event_thread.event_q, event_ref, entry);
+	ast_cond_signal(&event_thread.cond);
+	ast_mutex_unlock(&event_thread.lock);
+
+	return 0;
+}
+
+static void *ast_event_dispatcher(void *unused)
+{
+	for (;;) {
+		struct ast_event_ref *event_ref;
+		struct ast_event_sub *sub;
+
+		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);
+
+		/* 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)
+			sub->cb(event_ref->event);
+		AST_RWLIST_UNLOCK(&ast_event_subs[event_ref->event->type]);
+
+		/* Now to subscribers to all event types */
+		AST_RWLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
+		AST_RWLIST_TRAVERSE(&ast_event_subs[AST_EVENT_ALL], sub, entry)
+			sub->cb(event_ref->event);
+		AST_RWLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
+
+		free(event_ref->event);
+		free(event_ref);
+	}
+
+	return NULL;
+}
+
+void ast_event_init(void)
+{
+	int i;
+
+	for (i = 0; i < AST_EVENT_TOTAL; i++)
+		AST_RWLIST_HEAD_INIT(&ast_event_subs[i]);
+
+	for (i = 0; i < AST_EVENT_TOTAL; i++)
+		AST_RWLIST_HEAD_INIT(&ast_event_cache[i]);
+
+	ast_cond_init(&event_thread.cond, NULL);
+
+	for (i = 0; i < NUM_EVENT_THREADS; i++) {
+		pthread_t dont_care;
+		ast_pthread_create_background(&dont_care, NULL, ast_event_dispatcher, NULL);
+	}
+}

Propchange: team/russell/events/main/event.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/russell/events/main/event.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/russell/events/main/event.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/russell/events/res/res_eventtest.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/res/res_eventtest.c?view=auto&rev=59059
==============================================================================
--- team/russell/events/res/res_eventtest.c (added)
+++ team/russell/events/res/res_eventtest.c Mon Mar 19 20:18:13 2007
@@ -1,0 +1,130 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Russell Bryant <russell at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! 
+ * \file
+ * \author Russell Bryant <russell at digium.com>
+ *
+ * \brief Test code for the internal event system
+ * 
+ */
+
+/*** MODULEINFO
+	<defaultenabled>no</defaultenabled>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "asterisk/module.h"
+#include "asterisk/event.h"
+#include "asterisk/cli.h"
+
+static void process_event_generic(const struct ast_event *event)
+{
+	ast_log(LOG_DEBUG, "Event type: %u\n", event->type);
+}
+
+static void process_event_mwi(const struct ast_event *event)
+{
+	const char *mailbox;
+	unsigned int new;
+	unsigned int old;
+
+	mailbox = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
+	new = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
+	old = ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
+
+	ast_log(LOG_DEBUG, "MWI Event.  Mailbox: %s  New: %u  Old: %u\n",
+		mailbox, new, old);
+}
+
+static void ast_event_process(const struct ast_event *event)
+{
+	switch (event->type) {
+	case AST_EVENT_MWI:
+		process_event_mwi(event);
+		break;
+	default:
+		process_event_generic(event);
+	}
+}
+
+static char *event_gen(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	struct ast_event *event;
+	const char *mailbox = "1234";
+	unsigned int new = 5;
+	unsigned int old = 12;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "event generate";
+		e->usage =
+			"Usage: event generate\n"
+			"       Generate a test event.\n";
+		return NULL;
+
+	case CLI_GENERATE:
+		return NULL;	/* no completion */
+	}
+	if (a->argc != e->args)
+		return CLI_SHOWUSAGE;
+
+	if (!(event = ast_event_new(AST_EVENT_MWI)))
+		return CLI_FAILURE;
+	if (ast_event_append_ie_str(&event, AST_EVENT_IE_MAILBOX, mailbox))
+		return CLI_FAILURE;
+	if (ast_event_append_ie_uint(&event, AST_EVENT_IE_NEWMSGS, new))
+		return CLI_FAILURE;
+	if (ast_event_append_ie_uint(&event, AST_EVENT_IE_OLDMSGS, old))
+		return CLI_FAILURE;
+
+	ast_event_queue(event);
+
+	return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry cli_commands[] = {
+	NEW_CLI(event_gen, "Generate a test event"),
+};
+
+static int load_module(void)
+{
+	ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
+
+	ast_event_subscribe(AST_EVENT_ALL, ast_event_process);
+
+	return 0;
+}
+
+static int unload_module(void)
+{
+	ast_event_unsubscribe(AST_EVENT_ALL, ast_event_process);
+
+	ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
+
+	return 0;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Test code for the internal event system");

Propchange: team/russell/events/res/res_eventtest.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/russell/events/res/res_eventtest.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/russell/events/res/res_eventtest.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain



More information about the svn-commits mailing list