[asterisk-commits] [svn-commits] russell: branch russell/events r110870 - in /team/russell/events: ./ include/...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Mar 26 10:54:38 CDT 2008


Author: russell
Date: Wed Mar 26 10:54:37 2008
New Revision: 110870

URL: http://svn.digium.com/view/asterisk?view=rev&rev=110870
Log:
update libxml2 check to use xml2-config

Added:
    team/russell/events/res/res_xmlevents.c   (with props)
Modified:
    team/russell/events/configure.ac
    team/russell/events/include/asterisk/event.h
    team/russell/events/include/asterisk/event_defs.h
    team/russell/events/include/asterisk/tcptls.h
    team/russell/events/main/devicestate.c
    team/russell/events/main/event.c
    team/russell/events/main/tcptls.c
    team/russell/events/res/res_ais.c

Modified: team/russell/events/configure.ac
URL: http://svn.digium.com/view/asterisk/team/russell/events/configure.ac?view=diff&rev=110870&r1=110869&r2=110870
==============================================================================
--- team/russell/events/configure.ac (original)
+++ team/russell/events/configure.ac Wed Mar 26 10:54:37 2008
@@ -1499,7 +1499,8 @@
 AST_EXT_LIB_CHECK([X11], [X11], [XOpenDisplay], [X11/Xlib.h],,, [standard_path])
 AST_EXT_LIB_CHECK([X11], [X11], [XOpenDisplay], [X11/Xlib.h],, [-I/usr/X11R6/include], [X11R6])
 
-AST_EXT_LIB_CHECK([XML2], [xml2], [xmlCreatePushParserCtxt], [])
+AST_EXT_TOOL_CHECK([XML2], [xml2])
+AST_EXT_LIB_CHECK([XML2], [xml2], [xmlCreatePushParserCtxt], [], [${XML2_LIB}], [${XML2_INCLUDE}])
 
 if test "${cross_compiling}" = "no";
 then

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=110870&r1=110869&r2=110870
==============================================================================
--- team/russell/events/include/asterisk/event.h (original)
+++ team/russell/events/include/asterisk/event.h Wed Mar 26 10:54:37 2008
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 2007, Digium, Inc.
+ * Copyright (C) 2007 - 2008, Digium, Inc.
  *
  * Russell Bryant <russell at digium.com>
  *
@@ -436,6 +436,10 @@
  */
 const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type);
 
+const char *ast_event_get_ie_type_name(enum ast_event_ie_type ie_type);
+
+enum ast_event_ie_pltype ast_event_get_ie_pltype(enum ast_event_ie_type ie_type);
+
 /*!
  * \brief Get the type for an event
  *
@@ -445,6 +449,8 @@
  *         ast_event_type enum
  */
 enum ast_event_type ast_event_get_type(const struct ast_event *event);
+
+const char *ast_event_get_type_name(const struct ast_event *event);
 
 /*!
  * \brief Get the size of an event

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=110870&r1=110869&r2=110870
==============================================================================
--- team/russell/events/include/asterisk/event_defs.h (original)
+++ team/russell/events/include/asterisk/event_defs.h Wed Mar 26 10:54:37 2008
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 2007, Digium, Inc.
+ * Copyright (C) 2007 - 2008, Digium, Inc.
  *
  * Russell Bryant <russell at digium.com>
  *
@@ -30,26 +30,26 @@
 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,
+	AST_EVENT_ALL                 = 0x00,
 	/*! This event type is reserved for use by third-party modules to create
 	 *  custom events without having to modify this file. 
 	 *  \note There are no "custom" IE types, because IEs only have to be
 	 *  unique to the event itself, not necessarily across all events. */
-	AST_EVENT_CUSTOM = 0x01,
+	AST_EVENT_CUSTOM              = 0x01,
 	/*! Voicemail message waiting indication */
-	AST_EVENT_MWI          = 0x02,
+	AST_EVENT_MWI                 = 0x02,
 	/*! Someone has subscribed to events */
-	AST_EVENT_SUB          = 0x03,
+	AST_EVENT_SUB                 = 0x03,
 	/*! Someone has unsubscribed from events */
-	AST_EVENT_UNSUB        = 0x04,
+	AST_EVENT_UNSUB               = 0x04,
 	/*! The aggregate state of a device across all servers configured to be
 	 *  a part of a device state cluster has changed. */
-	AST_EVENT_DEVICE_STATE = 0x05,
+	AST_EVENT_DEVICE_STATE        = 0x05,
 	/*! The state of a device has changed on _one_ server.  This should not be used
 	 *  directly, in general.  Use AST_EVENT_DEVICE_STATE instead. */
 	AST_EVENT_DEVICE_STATE_CHANGE = 0x06,
 	/*! Number of event types.  This should be the last event type + 1 */
-	AST_EVENT_TOTAL        = 0x07,
+	AST_EVENT_TOTAL               = 0x07,
 };
 
 /*! \brief Event Information Element types */
@@ -122,6 +122,8 @@
 	 AST_EVENT_IE_EID      = 0x0A,
 };
 
+#define AST_EVENT_IE_MAX AST_EVENT_IE_EID
+
 /*!
  * \brief Payload types for event information elements
  */
@@ -154,4 +156,15 @@
 struct ast_event_sub;
 struct ast_event_iterator;
 
+/*!
+ * \brief supposed to be an opaque type
+ *
+ * This is only here so that it can be declared on the stack.
+ */
+struct ast_event_iterator {
+	uint16_t event_len;
+	const struct ast_event *event;
+	struct ast_event_ie *ie;
+};
+
 #endif /* AST_EVENT_DEFS_H */

Modified: team/russell/events/include/asterisk/tcptls.h
URL: http://svn.digium.com/view/asterisk/team/russell/events/include/asterisk/tcptls.h?view=diff&rev=110870&r1=110869&r2=110870
==============================================================================
--- team/russell/events/include/asterisk/tcptls.h (original)
+++ team/russell/events/include/asterisk/tcptls.h Wed Mar 26 10:54:37 2008
@@ -48,6 +48,8 @@
 
 #ifndef _ASTERISK_SERVER_H
 #define _ASTERISK_SERVER_H
+
+#include <fcntl.h>
 
 #include "asterisk/utils.h"
 
@@ -127,6 +129,7 @@
 	int client;
 	struct sockaddr_in requestor;
 	struct server_args *parent;
+	pthread_t worker_thread;
 };
 
 /*! \brief

Modified: team/russell/events/main/devicestate.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/devicestate.c?view=diff&rev=110870&r1=110869&r2=110870
==============================================================================
--- team/russell/events/main/devicestate.c (original)
+++ team/russell/events/main/devicestate.c Wed Mar 26 10:54:37 2008
@@ -1,9 +1,10 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 1999 - 2007, Digium, Inc.
+ * Copyright (C) 1999 - 2008, Digium, Inc.
  *
  * Mark Spencer <markster at digium.com>
+ * Russell Bryant <russell at digium.com>
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
@@ -21,11 +22,16 @@
  * \brief Device state management
  *
  * \author Mark Spencer <markster at digium.com> 
+ * \author Russell Bryant <russell at digium.com>
  *
  *	\arg \ref AstExtState
  */
 
 /*! \page AstExtState Extension and device states in Asterisk
+ *
+ * (Note that these descriptions of device states and extension
+ * states have not been updated to the way things work
+ * in Asterisk 1.6.)
  *
  *	Asterisk has an internal system that reports states
  *	for an extension. By using the dialplan priority -1,
@@ -417,6 +423,8 @@
 {
 	struct ast_event *event;
 
+	ast_debug(1, "device '%s' state '%d'\n", device, state);
+
 	if (!(event = ast_event_new(AST_EVENT_DEVICE_STATE_CHANGE,
 			AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
 			AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
@@ -697,6 +705,8 @@
 
 	state = ast_devstate_aggregate_result(&agg);
 
+	ast_debug(1, "Aggregate devstate result is %d\n", state);
+
 	event = ast_event_get_cached(AST_EVENT_DEVICE_STATE,
 		AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
 		AST_EVENT_IE_END);

Modified: team/russell/events/main/event.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/event.c?view=diff&rev=110870&r1=110869&r2=110870
==============================================================================
--- team/russell/events/main/event.c (original)
+++ team/russell/events/main/event.c Wed Mar 26 10:54:37 2008
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 2007, Digium, Inc.
+ * Copyright (C) 2007 - 2008, Digium, Inc.
  *
  * Russell Bryant <russell at digium.com>
  *
@@ -78,12 +78,6 @@
 	AST_LIST_ENTRY(ast_event_ref) entry;
 };
 
-struct ast_event_iterator {
-	uint16_t event_len;
-	const struct ast_event *event;
-	struct ast_event_ie *ie;
-};
-
 /*! \brief data shared between event dispatching threads */
 static struct {
 	ast_cond_t cond;
@@ -126,6 +120,77 @@
  * 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];
+
+/*!
+ * The index of each entry _must_ match the event type number!
+ */
+static struct event_name {
+	enum ast_event_type type;
+	const char *name;
+} event_names[] = {
+	{ 0, },
+	{ AST_EVENT_CUSTOM,              "Custom" },
+	{ AST_EVENT_MWI,                 "MWI" },
+	{ AST_EVENT_SUB,                 "Subscription" },
+	{ AST_EVENT_UNSUB,               "Unsubscription" },
+	{ AST_EVENT_DEVICE_STATE,        "DeviceState" },
+	{ AST_EVENT_DEVICE_STATE_CHANGE, "DeviceStateChange" },
+};
+
+/*!
+ * The index of each entry _must_ match the event ie number!
+ */
+static struct ie_map {
+	enum ast_event_ie_type ie_type;
+	enum ast_event_ie_pltype ie_pltype;
+	const char *name;
+} ie_maps[] = {
+	{ 0, },
+	{ AST_EVENT_IE_NEWMSGS,   AST_EVENT_IE_PLTYPE_UINT, "NewMessages" },
+	{ AST_EVENT_IE_OLDMSGS,   AST_EVENT_IE_PLTYPE_UINT, "OldMessages" },
+	{ AST_EVENT_IE_MAILBOX,   AST_EVENT_IE_PLTYPE_STR,  "Mailbox" },
+	{ AST_EVENT_IE_UNIQUEID,  AST_EVENT_IE_PLTYPE_UINT, "UniqueID" },
+	{ AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, "EventType" },
+	{ AST_EVENT_IE_EXISTS,    AST_EVENT_IE_PLTYPE_UINT, "Exists" },
+	{ AST_EVENT_IE_DEVICE,    AST_EVENT_IE_PLTYPE_STR,  "Device" },
+	{ AST_EVENT_IE_STATE,     AST_EVENT_IE_PLTYPE_UINT, "State" },
+	{ AST_EVENT_IE_CONTEXT,   AST_EVENT_IE_PLTYPE_STR,  "Context" },
+	{ AST_EVENT_IE_EID,       AST_EVENT_IE_PLTYPE_RAW,  "EntityID" },
+};
+
+const char *ast_event_get_type_name(const struct ast_event *event)
+{
+	enum ast_event_type type;
+
+	type = ast_event_get_type(event);
+
+	if (type >= AST_EVENT_TOTAL || type < 0) {
+		ast_log(LOG_ERROR, "Invalid event type - '%d'\n", type);
+		return "";
+	}
+
+	return event_names[type].name;
+}
+
+const char *ast_event_get_ie_type_name(enum ast_event_ie_type ie_type)
+{
+	if (ie_type <= 0 || ie_type > AST_EVENT_IE_MAX) {
+		ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
+		return "";
+	}
+
+	return ie_maps[ie_type].name;
+}
+
+enum ast_event_ie_pltype ast_event_get_ie_pltype(enum ast_event_ie_type ie_type)
+{
+	if (ie_type <= 0 || ie_type > AST_EVENT_IE_MAX) {
+		ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
+		return AST_EVENT_IE_PLTYPE_UNKNOWN;
+	}
+
+	return ie_maps[ie_type].ie_pltype;
+}
 
 size_t ast_event_get_size(const struct ast_event *event)
 {
@@ -547,7 +612,7 @@
 	struct ast_event *event;
 
 	AST_RWLIST_WRLOCK(&ast_event_subs[sub->type]);
-	AST_LIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
+	AST_RWLIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
 	AST_RWLIST_UNLOCK(&ast_event_subs[sub->type]);
 
 	if (ast_event_check_subscriber(AST_EVENT_UNSUB,
@@ -752,7 +817,7 @@
 	struct ast_event *dup_event;
 	uint16_t event_len;
 
-	event_len = ntohs(event->event_len);
+	event_len = ast_event_get_size(event);
 
 	if (!(dup_event = ast_calloc(1, event_len)))
 		return NULL;

Modified: team/russell/events/main/tcptls.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/tcptls.c?view=diff&rev=110870&r1=110869&r2=110870
==============================================================================
--- team/russell/events/main/tcptls.c (original)
+++ team/russell/events/main/tcptls.c Wed Mar 26 10:54:37 2008
@@ -106,7 +106,6 @@
 	struct sockaddr_in sin;
 	socklen_t sinlen;
 	struct ast_tcptls_session_instance *ser;
-	pthread_t launched;
 	
 	for (;;) {
 		int i, flags;
@@ -137,7 +136,7 @@
 
 		ser->client = 0;
 			
-		if (ast_pthread_create_detached_background(&launched, NULL, ast_make_file_from_fd, ser)) {
+		if (ast_pthread_create_detached_background(&ser->worker_thread, NULL, ast_make_file_from_fd, ser)) {
 			ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
 			close(ser->fd);
 			ast_free(ser);

Modified: team/russell/events/res/res_ais.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/res/res_ais.c?view=diff&rev=110870&r1=110869&r2=110870
==============================================================================
--- team/russell/events/res/res_ais.c (original)
+++ team/russell/events/res/res_ais.c Wed Mar 26 10:54:37 2008
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 2007, Digium, Inc.
+ * Copyright (C) 2007 - 2008, Digium, Inc.
  *
  * Russell Bryant <russell at digium.com>
  *

Added: team/russell/events/res/res_xmlevents.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/res/res_xmlevents.c?view=auto&rev=110870
==============================================================================
--- team/russell/events/res/res_xmlevents.c (added)
+++ team/russell/events/res/res_xmlevents.c Wed Mar 26 10:54:37 2008
@@ -1,0 +1,467 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2008, 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 An XML encoded socket interface to Asterisk events accessible
+ *        via TCP and TCP/TLS.
+ *
+ * \todo account configuration, authentication
+ * \todo subscription support instead of spamming all events
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
+
+#include "asterisk/logger.h"
+#include "asterisk/module.h"
+#include "asterisk/tcptls.h"
+#include "asterisk/config.h"
+#include "asterisk/event.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/linkedlists.h"
+
+#define DEFAULT_XML_EVENTS_TCP_PORT 7103
+#define DEFAULT_XML_EVENTS_TLS_PORT 7104
+
+#define XML_EVENTS_CONFIG_FILE "xml_events.conf"
+
+#define NUM_SESSIONS_BUCKETS 1
+
+static struct xml_events_config {
+	unsigned int enabled:1;
+} xml_events_config_defaults = {
+	.enabled = 0,
+};
+
+struct pending_event {
+	struct ast_event *event;
+	AST_LIST_ENTRY(pending_event) entry;
+};
+
+struct xml_events_session {
+	struct ast_event_sub *sub;
+	struct ast_tcptls_session_instance *ser;
+	AST_LIST_HEAD_NOLOCK(, pending_event) pending_events;
+	unsigned int stop:1;
+};
+
+static struct ao2_container *xml_events_sessions;
+
+static struct xml_events_config xml_events_config;
+
+static struct ast_tls_config xml_events_tls_cfg;
+
+static void *session_do(void *data);
+
+static struct server_args xml_events_tcp_serv = {
+	.accept_fd = -1,
+	.master = AST_PTHREADT_NULL,
+	.name = "XML events TCP server",
+	.accept_fn = ast_tcptls_server_root,	/* thread doing the accept() */
+	.worker_fn = session_do,	/* thread handling the session */
+};
+
+static struct server_args xml_events_tls_serv = {
+	.accept_fd = -1,
+	.master = AST_PTHREADT_NULL,
+	.tls_cfg = &xml_events_tls_cfg, 
+	.poll_timeout = -1,	/* the other does the periodic cleanup */
+	.name = "XML events TLS server",
+	.accept_fn = ast_tcptls_server_root,	/* thread doing the accept() */
+	.worker_fn = session_do,	/* thread handling the session */
+};
+
+static void xml_events_session_destructor(void *obj)
+{
+	struct xml_events_session *session = obj;
+	struct pending_event *pending_event;
+
+	if (session->ser) {
+		fclose(session->ser->f);
+		ast_tcptls_session_instance_destroy(session->ser);
+		session->ser = NULL;
+	}
+
+	if (session->sub) {
+		ast_event_unsubscribe(session->sub);
+		session->sub = NULL;
+	}
+
+	while ((pending_event = AST_LIST_REMOVE_HEAD(&session->pending_events, entry))) {
+		ast_free(pending_event);
+	}
+}
+
+static inline struct xml_events_session *unref_session(struct xml_events_session *session)
+{
+	ao2_ref(session, -1);
+	return NULL;
+}
+
+static void event_cb(const struct ast_event *event, void *data)
+{
+	struct xml_events_session *session = data;
+	struct pending_event *pending_event;
+	size_t event_len;
+
+	event_len = ast_event_get_size(event);
+
+	if (!(pending_event = ast_calloc(1, sizeof(*pending_event) + event_len)))
+		return;
+
+	pending_event->event = (struct ast_event *) (pending_event + 1);
+	memcpy(pending_event->event, event, event_len);
+
+	ao2_lock(session);
+	AST_LIST_INSERT_TAIL(&session->pending_events, pending_event, entry);
+	pthread_kill(session->ser->worker_thread, SIGURG);
+	ao2_unlock(session);
+}
+
+AST_THREADSTORAGE(event_buf);
+#define EVENT_BUF_INIT_LEN 256
+
+static void begin_event_tag(struct ast_str **buf, const struct ast_event *event)
+{
+	const char *name;
+
+	ast_str_set(buf, 0, "<event type=\"%d\"", ast_event_get_type(event));
+
+	name = ast_event_get_type_name(event);
+	if (!ast_strlen_zero(name)) {
+		ast_str_append(buf, 0, " name=\"%s\"", name);
+	}
+
+	ast_str_append(buf, 0, ">\n");
+}
+
+static void end_event_tag(struct ast_str **buf)
+{
+	ast_str_append(buf, 0, "</event>\n");
+}
+
+static int handle_raw_ie(struct ast_str **buf, enum ast_event_ie_type ie_type, struct ast_event_iterator *iter)
+{
+	switch (ie_type) {
+	case AST_EVENT_IE_EID:
+	{
+		char eid_str[32] = "";
+		struct ast_eid *eid;
+
+		eid = ast_event_iterator_get_ie_raw(iter);
+		ast_eid_to_str(eid_str, sizeof(eid_str), eid);
+
+		ast_str_append(buf, 0, ">%s</ie>\n", eid_str);
+
+		return 1;
+	}
+	default:
+		return 0;
+	}
+}
+
+static void add_ie_tags(struct ast_str **buf, struct ast_event_iterator *iter)
+{
+	enum ast_event_ie_type ie_type;
+	enum ast_event_ie_pltype ie_pltype;
+	const char *name;
+
+	ie_type = ast_event_iterator_get_ie_type(iter);
+
+	ast_str_append(buf, 0, "   <ie type=\"%d\"", ie_type);
+
+	name = ast_event_get_ie_type_name(ie_type);
+	if (!ast_strlen_zero(name)) {
+		ast_str_append(buf, 0, " name=\"%s\"", name);
+	}
+
+	ie_pltype = ast_event_get_ie_pltype(ie_type);
+	switch (ie_pltype) {
+	case AST_EVENT_IE_PLTYPE_UINT:
+		ast_str_append(buf, 0, ">%u</ie>\n",
+			ast_event_iterator_get_ie_uint(iter));
+		break;
+	case AST_EVENT_IE_PLTYPE_STR:
+		ast_str_append(buf, 0, ">%s</ie>\n",
+			ast_event_iterator_get_ie_str(iter));
+		break;
+	case AST_EVENT_IE_PLTYPE_RAW:
+		if (handle_raw_ie(buf, ie_type, iter))
+			break;
+	case AST_EVENT_IE_PLTYPE_UNKNOWN:
+	case AST_EVENT_IE_PLTYPE_EXISTS:
+		ast_str_append(buf, 0, "/>\n");
+		break;
+	}
+}
+
+static int handle_pending_event(struct xml_events_session *session, struct pending_event *pending_event)
+{
+	struct ast_event *event;
+	struct ast_event_iterator iter;
+	struct ast_str *buf;
+
+	if (!(buf = ast_str_thread_get(&event_buf, EVENT_BUF_INIT_LEN)))
+		return -1;
+
+	event = pending_event->event;
+
+	begin_event_tag(&buf, event);
+
+	ast_event_iterator_init(&iter, event);
+	for (;;) {
+		add_ie_tags(&buf, &iter);
+
+		if (ast_event_iterator_next(&iter))
+			break;
+	}
+
+	end_event_tag(&buf);
+
+	if (fwrite(buf->str, 1, buf->used, session->ser->f) < buf->used)
+		return -1;
+	
+	fflush(session->ser->f);
+
+	return 0;
+}
+
+static int handle_events(struct xml_events_session *session)
+{
+	struct pending_event *pending_event;
+
+	ao2_lock(session);
+	while (!session->stop && (pending_event = AST_LIST_REMOVE_HEAD(&session->pending_events, entry))) {
+		int res;
+
+		ao2_unlock(session);
+		if ((res = handle_pending_event(session, pending_event)))
+			return res;
+		ao2_lock(session);
+	}
+	ao2_unlock(session);
+
+	return 0;
+}
+
+static void *session_do(void *data)
+{
+	struct xml_events_session *session;
+	int flags;
+
+	if (!(session = ao2_alloc(sizeof(*session), xml_events_session_destructor))) {
+		return NULL;
+	}
+
+	session->ser = data;
+
+	flags = fcntl(session->ser->fd, F_GETFL);
+	flags |= O_NONBLOCK;
+	fcntl(session->ser->fd, F_SETFL, flags);
+
+	/* XXX temporary hack.  Eventually add subscription support. */
+	session->sub = ast_event_subscribe(AST_EVENT_ALL, event_cb, session, AST_EVENT_IE_END);
+	if (!session->sub) {
+		session = unref_session(session);
+		return NULL;
+	}
+
+	while (!session->stop) {	
+		int res;
+
+		res = handle_events(session);
+		if (res < 0) {
+			ast_debug(1, "handle_events() returned < 1, breaking out of session loop.\n");
+			break;
+		}
+
+		res = ast_wait_for_input(session->ser->fd, 50);
+		if (res < 0) {
+			if (errno == EINTR || errno == EAGAIN)
+				continue;
+			ast_log(LOG_NOTICE, "poll() returned error: %s\n", strerror(errno));
+			break;
+		}
+
+		if (res == 0) {
+			/* No input, move on. */
+			continue;
+		}
+
+		res = fread(buf, 1, sizeof(buf), session->ser->f);
+		if (res < 1) {
+			ast_debug(1, "fread() returned < 1, breaking out of session loop.\n");
+			break;
+		}
+
+		/* XXX Eventually support subscriptions ... */
+	}
+
+	session = unref_session(session);
+
+	return NULL;
+}
+
+
+static void store_port(const char *val, struct sockaddr_in *sin)
+{
+	uint16_t port;
+
+	if (sscanf(val, "%hu", &port) != 1) {
+		ast_log(LOG_WARNING, "Invalid port specified - '%s'\n", val);
+		return;
+	}
+
+	sin->sin_port = htons(port);
+}
+
+static void store_addr(const char *val, struct sockaddr_in *sin)
+{
+	if (ast_parse_arg(val, PARSE_INADDR, sin))
+		ast_log(LOG_WARNING, "Invalid bindaddr specified - '%s'\n", val);
+}
+
+static void store_config_core(const char *var, const char *value)
+{
+	CV_START(var, value);
+
+	CV_BOOL("enabled", xml_events_config.enabled);
+	CV_F("bindport", store_port(value, &xml_events_tcp_serv.sin));
+	CV_F("bindaddr", store_addr(value, &xml_events_tcp_serv.sin));
+
+	CV_BOOL("tlsenable", xml_events_tls_cfg.enabled)
+	CV_BOOL("sslenable", xml_events_tls_cfg.enabled)
+	CV_F("tlsbindport", store_port(value, &xml_events_tls_serv.sin));
+	CV_F("sslbindport", store_port(value, &xml_events_tls_serv.sin));
+	CV_F("tlsbindaddr", store_addr(value, &xml_events_tls_serv.sin));
+	CV_F("sslbindaddr", store_addr(value, &xml_events_tls_serv.sin));
+	CV_DSTR("tlscert", xml_events_tls_cfg.certfile);
+	CV_DSTR("sslcert", xml_events_tls_cfg.certfile);
+	CV_DSTR("tlscipher", xml_events_tls_cfg.cipher);
+	CV_DSTR("sslcipher", xml_events_tls_cfg.cipher);
+
+	ast_log(LOG_WARNING, "Unknown option '%s'\n", var);
+
+	CV_END;
+}
+
+static int load_config(int reload)
+{
+	struct ast_config *cfg;
+	struct ast_variable *var;
+	struct ast_flags cfg_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
+
+	if ((cfg = ast_config_load(XML_EVENTS_CONFIG_FILE, cfg_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
+		return 0;
+	}
+
+	if (!cfg) {
+		ast_log(LOG_NOTICE, "Unable to open XML events configuration file.\n");
+		return -1;
+	}
+
+	xml_events_config = xml_events_config_defaults;
+
+	memset(&xml_events_tcp_serv.sin, 0, sizeof(xml_events_tcp_serv.sin));
+	xml_events_tcp_serv.sin.sin_port = htons(DEFAULT_XML_EVENTS_TCP_PORT);
+	
+	memset(&xml_events_tls_serv.sin, 0, sizeof(xml_events_tls_serv.sin));
+	xml_events_tls_serv.sin.sin_port = htons(DEFAULT_XML_EVENTS_TLS_PORT);
+
+	xml_events_tls_cfg.enabled = 0;
+
+	if (xml_events_tls_cfg.certfile) {
+		ast_free(xml_events_tls_cfg.certfile);
+		xml_events_tls_cfg.certfile = NULL;
+	}
+	if (!(xml_events_tls_cfg.certfile = ast_strdup(AST_CERTFILE))) {
+		return -1;
+	}
+
+	if (xml_events_tls_cfg.cipher) {
+		ast_free(xml_events_tls_cfg.cipher);
+		xml_events_tls_cfg.cipher = NULL;
+	}
+	if (!(xml_events_tls_cfg.cipher = ast_strdup(""))) {
+		return -1;
+	}
+
+	for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
+		store_config_core(var->name, var->value);
+	}
+
+	ast_config_destroy(cfg);
+
+	/* These are used as hints as to whether the servers are enabled or not */
+	if (xml_events_config.enabled) {
+		xml_events_tcp_serv.sin.sin_family = AF_INET;
+	}
+	if (xml_events_tls_cfg.enabled) {
+		xml_events_tls_serv.sin.sin_family = AF_INET;
+	}
+
+	ast_tcptls_server_start(&xml_events_tcp_serv);
+	if (ast_ssl_setup(xml_events_tls_serv.tls_cfg))
+		ast_tcptls_server_start(&xml_events_tls_serv);
+
+	return 0;
+}
+
+static int xml_events_session_hash(const void *obj, const int flags)
+{
+	/* unused */
+	return 0;
+}
+
+static int xml_events_session_cmp(void *obj, void *arg, int flags)
+{
+	/* unused */
+	return 0;
+}
+
+static int load_module(void)
+{
+	if (!(xml_events_sessions = ao2_container_alloc(NUM_SESSIONS_BUCKETS, 
+		xml_events_session_hash, xml_events_session_cmp))) {
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	if (load_config(0)) {
+		ao2_ref(xml_events_sessions, -1);
+		xml_events_sessions = NULL;
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+	if (xml_events_sessions) {
+		ao2_ref(xml_events_sessions, -1);
+		xml_events_sessions = NULL;
+	}
+
+	return 0;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "XML Event Interface");

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

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

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


_______________________________________________
--Bandwidth and Colocation Provided by http://www.api-digital.com--

svn-commits mailing list
To UNSUBSCRIBE or update options visit:
   http://lists.digium.com/mailman/listinfo/svn-commits

-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.




More information about the asterisk-commits mailing list