[svn-commits] kharwell: branch kharwell/pimp_sip_qualify r390435 - in /team/kharwell/pimp_s...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Jun 4 16:13:44 CDT 2013


Author: kharwell
Date: Tue Jun  4 16:13:42 2013
New Revision: 390435

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=390435
Log:
addressed review issues

Modified:
    team/kharwell/pimp_sip_qualify/res/res_sip.c
    team/kharwell/pimp_sip_qualify/res/res_sip/sip_options.c

Modified: team/kharwell/pimp_sip_qualify/res/res_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/pimp_sip_qualify/res/res_sip.c?view=diff&rev=390435&r1=390434&r2=390435
==============================================================================
--- team/kharwell/pimp_sip_qualify/res/res_sip.c (original)
+++ team/kharwell/pimp_sip_qualify/res/res_sip.c Tue Jun  4 16:13:42 2013
@@ -223,13 +223,6 @@
 				<configOption name="outbound_proxy">
 					<synopsis>Proxy through which to send requests</synopsis>
 				</configOption>
-				<configOption name="qualify_frequency" default="0">
-					<synopsis>Interval at which to qualify an endpoint</synopsis>
-					<description><para>
-						Interval between attempts to qualify the endpoint for reachability.
-						If <literal>0</literal> never qualify. Time in seconds.
-					</para></description>
-				</configOption>
 				<configOption name="rewrite_contact">
 					<synopsis>Allow Contact header to be rewritten with the source IP address-port</synopsis>
 				</configOption>
@@ -511,6 +504,13 @@
 					<synopsis>Time to keep alive a contact</synopsis>
 					<description><para>
 						Time to keep alive a contact. String style specification.
+					</para></description>
+				</configOption>
+				<configOption name="qualify_frequency" default="0">
+					<synopsis>Interval at which to qualify a contact</synopsis>
+					<description><para>
+						Interval between attempts to qualify the contact for reachability.
+						If <literal>0</literal> never qualify. Time in seconds.
 					</para></description>
 				</configOption>
 			</configObject>
@@ -583,6 +583,13 @@
 				<configOption name="type">
 					<synopsis>Must be of type 'aor'.</synopsis>
 				</configOption>
+				<configOption name="qualify_frequency" default="0">
+					<synopsis>Interval at which to qualify an AoR</synopsis>
+					<description><para>
+						Interval between attempts to qualify the AoR for reachability.
+						If <literal>0</literal> never qualify. Time in seconds.
+					</para></description>
+				</configOption>
 			</configObject>
 		</configFile>
 	</configInfo>

Modified: team/kharwell/pimp_sip_qualify/res/res_sip/sip_options.c
URL: http://svnview.digium.com/svn/asterisk/team/kharwell/pimp_sip_qualify/res/res_sip/sip_options.c?view=diff&rev=390435&r1=390434&r2=390435
==============================================================================
--- team/kharwell/pimp_sip_qualify/res/res_sip/sip_options.c (original)
+++ team/kharwell/pimp_sip_qualify/res/res_sip/sip_options.c Tue Jun  4 16:13:42 2013
@@ -37,6 +37,11 @@
 
 static int qualify_contact(struct ast_sip_contact *contact);
 
+enum contact_status_type {
+	UNAVAILABLE,
+	AVAILABLE
+};
+
 /*!
  * \internal
  * \brief A contact's status.
@@ -47,9 +52,9 @@
 struct contact_status {
 	SORCERY_OBJECT(details);
 	/*! 0 = unavailable, 1 = available (default) */
-	int status;
-	/*! The start time set before sending a qualify request */
-	struct timeval start;
+	enum contact_status_type status;
+	/*! The round trip start time set before sending a qualify request */
+	struct timeval rtt_start;
 	/*! The round trip time in microseconds */
 	int64_t rtt;
 };
@@ -67,10 +72,6 @@
 		ast_log(LOG_ERROR, "Unable to allocate contact_status\n");
 		return NULL;
 	}
-
-	status->status = 0;
-	status->start = ast_tv(0, 0);
-	status->rtt = 0;
 
 	return status;
 }
@@ -112,7 +113,8 @@
  * \internal
  * \brief Set a contact_status's status to the given value.
  */
-static void update_contact_status(const struct ast_sip_contact *contact, int value)
+static void update_contact_status(const struct ast_sip_contact *contact,
+				  enum contact_status_type value)
 {
 	RAII_VAR(struct contact_status *, status,
 		 get_contact_status(contact), ao2_cleanup);
@@ -128,11 +130,14 @@
 
 	copy->status = value;
 
+	/* status is set to unavailable just before sending, so intialize the rrt 
+	   start time to "now", so when we receive a response (status = available)
+	   we can calculate the rtt */
 	if (copy->status) {
-		copy->rtt = ast_tvdiff_us(ast_tvnow(), status->start);
-		copy->start = ast_tv(0, 0);
+		copy->rtt = ast_tvdiff_us(ast_tvnow(), status->rtt_start);
+		copy->rtt_start = ast_tv(0, 0);
 	} else {
-		copy->start = ast_tvnow();
+		copy->rtt_start = ast_tvnow();
 		copy->rtt = 0;
 	}
 
@@ -216,7 +221,7 @@
 	pjsip_tx_data *tdata;
 
 	if (tsx->status_code != 401 && tsx->status_code != 407) {
-		update_contact_status(contact, 1);
+		update_contact_status(contact, AVAILABLE);
 		return;
 	}
 
@@ -249,8 +254,9 @@
 static int qualify_contact(struct ast_sip_contact *contact)
 {
 	pjsip_tx_data *tdata;
-	/* assume we can't connect so if no reply comes back */
-	update_contact_status(contact, 0);
+	/* assume we can't connect so if no reply comes back the contact is
+	   considered unavailable */
+	update_contact_status(contact, UNAVAILABLE);
 
 	if (ast_sip_create_request("OPTIONS", NULL, NULL, contact->uri, &tdata)) {
 		ast_log(LOG_ERROR, "Unable to create request to qualify contact %s\n",
@@ -288,10 +294,10 @@
  * \brief Structure to hold qualify contact scheduling information.
  */
 struct sched_data {
-	/*! The contact being checked */
-	struct ast_sip_contact *contact;
 	/*! The scheduling id */
 	int id;
+	/*! The the contact being checked */
+	struct ast_sip_contact *contact;
 };
 
 /*!
@@ -304,11 +310,29 @@
 
 	if (!AST_SCHED_DEL(sched, data->id)) {
 		/* If we successfully deleted the qualify, we got it before it
-		 * fired. We can safely unref the data that was passed to it.
-		 * Otherwise, we're getting deleted while this is firing, so
-		 * don't unref! */
-		ao2_cleanup(data->contact);
-	}
+		   fired. We can safely unref the data that was passed to it.
+		   Otherwise, we're getting deleted while this is firing, so
+		   don't unref! */
+		ao2_ref(data->contact, -1);
+	}
+}
+/*!
+ * \internal
+ * \brief Create the scheduling data object.
+ */
+static struct sched_data *sched_data_create(struct ast_sip_contact *contact)
+{
+	struct sched_data *data = ao2_alloc(sizeof(*data), sched_data_destructor);
+
+	if (!data) {
+		ast_log(LOG_ERROR, "Unable to create schedule qualify data\n");
+		return NULL;
+	}
+
+	data->contact = contact;
+	ao2_ref(data->contact, +1);
+
+	return data;
 }
 
 /*!
@@ -327,7 +351,13 @@
  */
 static int qualify_contact_sched(const void *obj)
 {
-	struct sched_data *data = (struct sched_data *)obj;
+	RAII_VAR(struct sched_data *, data, (struct sched_data *)obj, ao2_cleanup);
+
+	if (!data) {
+		return 0;
+	}
+
+	ao2_ref(data, +1);
 
 	if (!data->contact) {
 		/* contact is gone - so remove from scheduler */
@@ -349,23 +379,18 @@
  */
 static void schedule_qualify(struct ast_sip_contact *contact)
 {
-	RAII_VAR(struct sched_data *, data,  ao2_alloc(
-			 sizeof(*data), sched_data_destructor), ao2_cleanup);
+	RAII_VAR(struct sched_data *, data, sched_data_create(contact), ao2_cleanup);
 
 	if (!data) {
-		ast_log(LOG_ERROR, "Unable to create schedule qualify data\n");
 		return;
 	}
 
-	data->contact = contact;
-	ao2_ref(data->contact, +1);
-	if ((data->id = ast_sched_add_variable(
-		sched, contact->qualify_frequency * 1000,
-		qualify_contact_sched, data, 1) <= 0)) {
-
+	data->id = ast_sched_add_variable(
+		sched, contact->qualify_frequency * 1000, qualify_contact_sched, data, 1);
+
+	if (data->id < 0) {
 		ast_log(LOG_ERROR, "Unable to schedule qualify for contact %s\n",
 			contact->uri);
-		ao2_ref(data->contact, -1);
 		return;
 	}
 
@@ -378,28 +403,21 @@
  */
 static void unschedule_qualify(struct ast_sip_contact *contact)
 {
-	/* make sure the contact isn't already in the scheduler */
-	struct sched_data *data =
-		ao2_find(sched_qualifies, contact, OBJ_UNLINK);
-
-	if (data) {
-		/* if it does exist remove */
-		ao2_ref(data, -1);
-	}
-}
-
-/*!
- * \internal
- * \brief Qualify the given contact and sets up scheduling if configured.
+	ao2_find(sched_qualifies, contact, OBJ_UNLINK | OBJ_NODATA);
+}
+
+/*!
+ * \internal
+ * \brief Qualify the given contact and set up scheduling if configured.
  */
 static void qualify_and_schedule(struct ast_sip_contact *contact)
 {
 	unschedule_qualify(contact);
 
-	ao2_ref(contact, +1);
-	ast_sip_push_task(NULL, qualify_contact_task, contact);
-
 	if (contact->qualify_frequency) {
+		ao2_ref(contact, +1);
+		ast_sip_push_task(NULL, qualify_contact_task, contact);
+
 		schedule_qualify(contact);
 	}
 }
@@ -415,25 +433,20 @@
 
 /*!
  * \internal
- * \brief A contact has been updated make sure it is still available.
- */
-static void contact_updated(const void *obj)
-{
-	qualify_and_schedule((struct ast_sip_contact *)obj);
-}
-
-/*!
- * \internal
  * \brief A contact has been deleted remove status tracking.
  */
 static void contact_deleted(const void *obj)
 {
 	struct ast_sip_contact *contact = (struct ast_sip_contact *)obj;
-
-	RAII_VAR(struct contact_status *, status,
-		 get_contact_status(contact), ao2_cleanup);
+	RAII_VAR(struct contact_status *, status, NULL, ao2_cleanup);
 
 	unschedule_qualify(contact);
+
+	if (!(status = ast_sorcery_retrieve_by_id(
+		      ast_sip_get_sorcery(), CONTACT_STATUS,
+		      ast_sorcery_object_get_id(contact)))) {
+		return;
+	}
 
 	if (ast_sorcery_delete(ast_sip_get_sorcery(), status)) {
 		ast_log(LOG_ERROR, "Unable to delete contact_status for contact %s\n",
@@ -443,8 +456,7 @@
 
 struct ast_sorcery_observer contact_observer = {
 	.created = contact_created,
-	.updated = contact_updated,
-	.deleted = contact_deleted,
+	.deleted = contact_deleted
 };
 
 static pj_bool_t options_start(void)
@@ -705,9 +717,9 @@
 static int sched_qualifies_cmp_fn(void *obj, void *arg, int flags)
 {
 	struct sched_data *data = obj;
-	const char *id = flags & OBJ_KEY ? arg : ast_sorcery_object_get_id(arg);
-
-	return !strcmp(ast_sorcery_object_get_id(data->contact), id);
+
+	return !strcmp(ast_sorcery_object_get_id(data->contact),
+		       ast_sorcery_object_get_id(arg));
 }
 
 int ast_sip_initialize_sorcery_qualify(struct ast_sorcery *sorcery)
@@ -721,8 +733,8 @@
 		return -1;
 	}
 
-	ast_sorcery_object_field_register(sorcery, CONTACT_STATUS, "status", "no", OPT_BOOL_T,
-					  1, FLDSET(struct contact_status, status));
+	ast_sorcery_object_field_register(sorcery, CONTACT_STATUS, "rtt", "0", OPT_UINT_T,
+					  1, FLDSET(struct contact_status, rtt));
 
 	if (ast_sorcery_observer_add(sorcery, "contact", &contact_observer)) {
 		ast_log(LOG_WARNING, "Unable to add contact observer\n");
@@ -738,8 +750,8 @@
 	struct ast_sip_aor *aor = arg;
 
 	contact->qualify_frequency = aor->qualify_frequency;
-
 	qualify_and_schedule(contact);
+
 	return 0;
 }
 
@@ -764,12 +776,10 @@
 	while ((aor_name = strsep(&aors, ","))) {
 		RAII_VAR(struct ast_sip_aor *, aor,
 			 ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
-		RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
-
-		if (!aor) {
+
+		if (!aor || !aor->permanent_contacts) {
 			continue;
 		}
-
 		ao2_callback(aor->permanent_contacts, OBJ_NODATA, qualify_and_schedule_cb, aor);
 	}
 




More information about the svn-commits mailing list