[asterisk-commits] russell: branch russell/events r74118 - /team/russell/events/main/devicestate.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Jul 9 13:17:59 CDT 2007


Author: russell
Date: Mon Jul  9 13:17:58 2007
New Revision: 74118

URL: http://svn.digium.com/view/asterisk?view=rev&rev=74118
Log:
re-enable device state caching, but only for device state providers which are
explicitly providing state information.  If they are, the code assumes the code
is being responsible enough to call ast_devstate_changed() every time the state
changes, and not assuming that it will get polled for.
(*cough* app_queue/chan_agent *cough*)

Modified:
    team/russell/events/main/devicestate.c

Modified: team/russell/events/main/devicestate.c
URL: http://svn.digium.com/view/asterisk/team/russell/events/main/devicestate.c?view=diff&rev=74118&r1=74117&r2=74118
==============================================================================
--- team/russell/events/main/devicestate.c (original)
+++ team/russell/events/main/devicestate.c Mon Jul  9 13:17:58 2007
@@ -166,6 +166,17 @@
 
 /*! \brief Flag for the queue */
 static ast_cond_t change_pending;
+
+/*! \brief Whether or not to cache this device state value */
+enum devstate_cache {
+	/*! Cache this value as it is coming from a device state provider which is
+	 *  pushing up state change events to us as they happen */
+	CACHE_ON,
+	/*! Don't cache this result, since it was pulled from the device state provider.
+	 *  We only want to cache results from device state providers that are being nice
+	 *  and pushing state change events up to us as they happen. */
+	CACHE_OFF,
+};
 
 /* Forward declarations */
 static int getproviderstate(const char *provider, const char *address);
@@ -262,15 +273,6 @@
 	return res;
 }
 
-/* The last known device state is cached.  However, this can't be enabled until
- * we are confident that the cache contains the correct value.  There are some
- * places that this would break.  For example, chan_agent previously never called
- * ast_device_state_changed() when an agent's state changed to BUSY.  It only
- * worked because app_queue polled for the device state, which made it call into
- * the channel driver on a regular basis.  chan_agent now explicitly changes state
- * to busy, but I'd like to review all of the channel drivers before enabling use
- * of the cache. */
-#if 0
 static enum ast_device_state devstate_cached(const char *device)
 {
 	enum ast_device_state res = AST_DEVICE_UNKNOWN;
@@ -289,7 +291,6 @@
 
 	return res;
 }
-#endif
 
 /*! \brief Check device state through channel specific function or generic function */
 enum ast_device_state ast_device_state(const char *device)
@@ -303,13 +304,10 @@
 	/*! \brief Another provider of device state */
 	char *provider = NULL;
 
-	/* See comment by devstate_cached */
-#if 0
 	/* If the last known state is cached, just return that */
 	res = devstate_cached(device);
 	if (res != AST_DEVICE_UNKNOWN)
 		return res;
-#endif
 
 	buf = ast_strdupa(device);
 	tech = strsep(&buf, "/");
@@ -406,7 +404,7 @@
 	return res;
 }
 
-static void devstate_event(const char *device, enum ast_device_state state)
+static void devstate_event(const char *device, enum ast_device_state state, enum devstate_cache cache)
 {
 	struct ast_event *event;
 
@@ -417,9 +415,15 @@
 		return;
 	}
 
-	ast_event_queue_and_cache(event,
-		AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR,
-		AST_EVENT_IE_END);
+	if (cache == CACHE_ON) {
+		/* Cache this event, replacing an event in the cache with the same
+		 * device name if it exists. */
+		ast_event_queue_and_cache(event,
+			AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR,
+			AST_EVENT_IE_END);
+	} else {
+		ast_event_queue(event);
+	}
 }
 
 /*! Called by the state change thread to find out what the state is, and then
@@ -432,7 +436,7 @@
 
 	ast_debug(3, "Changing state for %s - state %d (%s)\n", device, state, devstate2str(state));
 
-	devstate_event(device, state);
+	devstate_event(device, state, CACHE_OFF);
 }
 
 static int __ast_devstate_changed_literal(enum ast_device_state state, char *buf)
@@ -450,7 +454,7 @@
 		*tmp = '\0';
 
 	if (state != AST_DEVICE_UNKNOWN) {
-		devstate_event(device, state);
+		devstate_event(device, state, CACHE_ON);
 	} else if (change_thread == AST_PTHREADT_NULL || !(change = ast_calloc(1, sizeof(*change) + strlen(device)))) {
 		/* we could not allocate a change struct, or */
 		/* there is no background thread, so process the change now */




More information about the asterisk-commits mailing list