[svn-commits] russell: branch russell/sla_updates r57009 - /team/russell/sla_updates/apps/

svn-commits at lists.digium.com svn-commits at lists.digium.com
Tue Feb 27 13:36:25 MST 2007


Author: russell
Date: Tue Feb 27 14:36:24 2007
New Revision: 57009

URL: http://svn.digium.com/view/asterisk?view=rev&rev=57009
Log:
Break out even more logic into functions and add comments in various places to
explain what is going on.

Modified:
    team/russell/sla_updates/apps/app_meetme.c

Modified: team/russell/sla_updates/apps/app_meetme.c
URL: http://svn.digium.com/view/asterisk/team/russell/sla_updates/apps/app_meetme.c?view=diff&rev=57009&r1=57008&r2=57009
==============================================================================
--- team/russell/sla_updates/apps/app_meetme.c (original)
+++ team/russell/sla_updates/apps/app_meetme.c Tue Feb 27 14:36:24 2007
@@ -3221,6 +3221,61 @@
 	sla_queue_event(SLA_EVENT_DIAL_STATE);
 }
 
+/*! \brief Check to see if dialing this station already timed out for this ringing trunk
+ * \note Assumes sla.lock is locked
+ */
+static int sla_check_timed_out_station(const struct sla_ringing_trunk *ringing_trunk,
+	const struct sla_station *station)
+{
+	struct sla_station_ref *timed_out_station;
+
+	AST_LIST_TRAVERSE(&ringing_trunk->timed_out_stations, timed_out_station, entry) {
+		if (station == timed_out_station->station)
+			return 1;
+	}
+
+	return 0;
+}
+
+/*! \brief Choose the highest priority ringing trunk for a station
+ * \param station the station
+ * \param remove remove the ringing trunk once selected
+ * \return a pointer to the selected ringing trunk, or NULL if none found
+ */
+static struct sla_ringing_trunk *sla_choose_ringing_trunk(struct sla_station *station, int remove)
+{
+	struct sla_trunk_ref *s_trunk_ref;
+	struct sla_ringing_trunk *ringing_trunk;
+
+	ast_mutex_lock(&sla.lock);
+
+	AST_LIST_TRAVERSE(&station->trunks, s_trunk_ref, entry) {
+		AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {
+			/* Make sure this is the trunk we're looking for */
+			if (s_trunk_ref->trunk != ringing_trunk->trunk)
+				continue;
+
+			/* This trunk on the station is ringing.  But, make sure this station
+			 * didn't already time out while this trunk was ringing. */
+			if (sla_check_timed_out_station(ringing_trunk, station))
+				continue;
+
+			if (remove)
+				AST_LIST_REMOVE_CURRENT(&sla.ringing_trunks, entry);
+
+			break;
+		}
+		AST_LIST_TRAVERSE_SAFE_END
+	
+		if (ringing_trunk)
+			break;
+	}
+
+	ast_mutex_unlock(&sla.lock);
+
+	return ringing_trunk;
+}
+
 static void sla_handle_dial_state_event(void)
 {
 	struct sla_ringing_station *ringing_station;
@@ -3247,36 +3302,20 @@
 		case AST_DIAL_RESULT_ANSWERED:
 			AST_LIST_REMOVE_CURRENT(&sla.ringing_stations, entry);
 			/* Find the appropriate trunk to answer. */
-			AST_LIST_TRAVERSE(&ringing_station->station->trunks, s_trunk_ref, entry) {
-				ast_mutex_lock(&sla.lock);
-				AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {
-					struct sla_station_ref *station_ref;
-					if (s_trunk_ref->trunk != ringing_trunk->trunk)
-						continue;
-					/* This trunk on the station is ringing.  But, make sure this station
-					 * didn't already time out while this trunk was ringing. */
-					AST_LIST_TRAVERSE(&ringing_trunk->timed_out_stations, station_ref, entry) {
-						if (station_ref->station == ringing_station->station)
-							break;
-					}
-					if (station_ref)
-						continue;
-					AST_LIST_REMOVE_CURRENT(&sla.ringing_trunks, entry);
-					break;
-				}
-				AST_LIST_TRAVERSE_SAFE_END
-				ast_mutex_unlock(&sla.lock);
-				if (ringing_trunk)
-					break;
-			}
+			ringing_trunk = sla_choose_ringing_trunk(ringing_station->station, 1);
 			if (!ringing_trunk) {
 				ast_log(LOG_DEBUG, "Found no ringing trunk for station '%s' to answer!\n",
 					ringing_station->station->name);
 				break;
 			}
+			/* Track the channel that answered this trunk */
 			s_trunk_ref->chan = ast_dial_answered(ringing_station->station->dial);
+			/* Actually answer the trunk */
 			ast_answer(ringing_trunk->trunk->chan);
 			sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS);
+			/* Now, start a thread that will connect this station to the trunk.  The rest of
+			 * the code here sets up the thread and ensures that it is able to save the arguments
+			 * before they are no longer valid since they are allocated on the stack. */
 			args.trunk_ref = s_trunk_ref;
 			args.station = ringing_station->station;
 			args.cond = &cond;
@@ -3349,22 +3388,6 @@
 	return res;
 }
 
-/*! \brief Check to see if dialing this station already timed out for this ringing trunk
- * \note Assumes sla.lock is locked
- */
-static int sla_check_timed_out_station(const struct sla_ringing_trunk *ringing_trunk,
-	const struct sla_station *station)
-{
-	struct sla_station_ref *timed_out_station;
-
-	AST_LIST_TRAVERSE(&ringing_trunk->timed_out_stations, timed_out_station, entry) {
-		if (station == timed_out_station->station)
-			return 1;
-	}
-
-	return 0;
-}
-
 /*! \brief Ring a station
  * \note Assumes sla.lock is locked
  */
@@ -3497,20 +3520,6 @@
 		ast_device_state_changed("SLA:%s_%s",
 			event->station->name, event->trunk_ref->trunk->name);
 	}
-}
-
-/*! \brief For a given station, choose the highest priority idle trunk
- */
-static struct sla_trunk_ref *sla_choose_idle_trunk(const struct sla_station *station)
-{
-	struct sla_trunk_ref *trunk_ref = NULL;
-
-	AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
-		if (trunk_ref->state == SLA_TRUNK_STATE_IDLE)
-			break;
-	}
-
-	return trunk_ref;
 }
 
 /*! \brief Process trunk ring timeouts
@@ -3635,6 +3644,7 @@
 	if (change_made)
 		sla_queue_event_nolock(SLA_EVENT_RINGING_TRUNK);
 
+	/* No timeout */
 	if (timeout == UINT_MAX)
 		return 0;
 
@@ -3800,6 +3810,20 @@
 	ast_dial_destroy(dial);
 
 	return NULL;
+}
+
+/*! \brief For a given station, choose the highest priority idle trunk
+ */
+static struct sla_trunk_ref *sla_choose_idle_trunk(const struct sla_station *station)
+{
+	struct sla_trunk_ref *trunk_ref = NULL;
+
+	AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
+		if (trunk_ref->state == SLA_TRUNK_STATE_IDLE)
+			break;
+	}
+
+	return trunk_ref;
 }
 
 static int sla_station_exec(struct ast_channel *chan, void *data)



More information about the svn-commits mailing list