[asterisk-commits] mmichelson: branch group/manager2 r114071 - /team/group/manager2/res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Apr 11 16:33:42 CDT 2008


Author: mmichelson
Date: Fri Apr 11 16:33:41 2008
New Revision: 114071

URL: http://svn.digium.com/view/asterisk?view=rev&rev=114071
Log:
Add skeleton functions for subscribing to events based on defined filters.
Tested with a single event type and it worked fine.


Modified:
    team/group/manager2/res/res_manager2.c

Modified: team/group/manager2/res/res_manager2.c
URL: http://svn.digium.com/view/asterisk/team/group/manager2/res/res_manager2.c?view=diff&rev=114071&r1=114070&r2=114071
==============================================================================
--- team/group/manager2/res/res_manager2.c (original)
+++ team/group/manager2/res/res_manager2.c Fri Apr 11 16:33:41 2008
@@ -46,6 +46,11 @@
 struct info_event {
 	AST_LIST_ENTRY(info_event) list;
 	struct ast_event *event;
+};
+
+struct info_event_sub {
+	AST_LIST_ENTRY(info_event_sub) list;
+	struct ast_event_sub *event_sub;
 };
 
 static void *subscribe_start(void *);
@@ -60,6 +65,8 @@
 	.worker_fn = subscribe_start,
 };
 
+
+
 /* Each of these represents a logged in subscriber
  * Each of these will have its own thread in which it
  * executes.
@@ -72,6 +79,7 @@
 	ast_cond_t cond;
 	ast_mutex_t lock;
 	AST_LIST_HEAD_NOLOCK(, info_event) info_events;
+	AST_LIST_HEAD_NOLOCK(, info_event_sub) event_subscriptions;
 };
 
 struct subscriber_filter {
@@ -91,6 +99,20 @@
 };
 
 AST_LIST_HEAD_STATIC(subscribers, subscriber);
+
+static struct subscriber *get_subscriber_by_name(const char *name)
+{
+	struct subscriber *subscriber = NULL;
+
+	AST_LIST_LOCK(&subscribers);
+	AST_LIST_TRAVERSE(&subscribers, subscriber, list) {
+		if (!strcasecmp(subscriber->name, name)) {
+			break;
+		}
+	}
+
+	return subscriber;
+}
 
 /*! \brief Take our event and put it into our own queue
  * to ease the load of the main event queue
@@ -195,10 +217,54 @@
 	ast_cond_init(&sub->cond, NULL);
 }
 
+/*! \brief Subscribe to a specified event and add it to the subscriber_instance's
+ * list of event subscriptions.
+ */
+static void subscribe_to_single_event(struct subscriber_instance *sub_inst, enum ast_event_type event_type)
+{
+	struct ast_event_sub *event_sub = NULL;
+	struct info_event_sub *info_sub = NULL;
+
+	ast_mutex_lock(&sub_inst->lock);
+	if ((event_sub = ast_event_subscribe(event_type, info_cb, sub_inst, AST_EVENT_IE_END))) {
+		if ((info_sub = ast_calloc(1, sizeof(*info_sub)))) {
+			info_sub->event_sub = event_sub;
+			AST_LIST_INSERT_TAIL(&(sub_inst->event_subscriptions), info_sub, list);
+		} else if (event_sub) {
+			ast_event_unsubscribe(event_sub);
+		}
+	}
+	ast_mutex_unlock(&sub_inst->lock);
+
+	/* TODO: Make failures not-so-silent. */
+}
+
+/*! \brief This iterates through the subscriber's filter to see which events to
+ * subscribe to. This uses subscribe_to_single_event as a helper to subscribe
+ * to each individual event
+ */
+static void subscribe_to_filtered_events(struct subscriber_instance *sub_inst)
+{
+	struct subscriber_filter *filter = sub_inst->sub->filter;
+	enum ast_event_type event_type;
+
+	/* If they're subscribed to all events, subscribe and return */
+	if (filter->events[AST_EVENT_ALL]) {
+		subscribe_to_single_event(sub_inst, AST_EVENT_ALL);
+		return;
+	}
+
+	for (event_type = 1; event_type < AST_EVENT_TOTAL; ++event_type) {
+		if (filter->events[event_type]) {
+			subscribe_to_single_event(sub_inst, event_type);
+		}
+	}
+}
+
 /*! \brief This is the worker function which is called when
  * a someone logs into the event subscription interface. It tells us that someone
  * wants to receive event notification from us. This function initializes
- * the subscriber struct and starts the thread which waits for events so
+ * the subscriber_instance struct and starts the thread which waits for events so
  * it can spit them out in string form
  *
  * \param event the event which caused this thread to execute. ATM the only
@@ -219,15 +285,19 @@
 	/* TODO: Find a way to get the corresponding subscriber 
 	 * A simple way is to read from ser->f and retrieve a subscriber from
 	 * our list of subscribers based on the input.
+	 *
+	 * For now, we will hard-code "mark" as the subscriber
 	 */
 	sub->f = ser->f;
 
+	if (!(sub->sub = get_subscriber_by_name("mark"))) {
+		ast_log(LOG_WARNING, "Subscriber '%s' not defined in list of subscribers.\n", "mark");
+		return NULL;
+	}
+
 	setup_thread(sub);
-
-	/* Just for testing purposes, let's subscribe to queue member device state changes */
-	/* TODO: Change this to use what has been set up in the subscriber's filter */
-	ast_event_subscribe(AST_EVENT_QUEUE_MEMBER_STATUS, info_cb, sub, AST_EVENT_IE_END);
-	ast_pthread_create_background(&sub->thread, NULL, event_spitter, sub);
+	if (!ast_pthread_create_background(&sub->thread, NULL, event_spitter, sub))
+		subscribe_to_filtered_events(sub);
 	return NULL;
 }
 




More information about the asterisk-commits mailing list