[asterisk-commits] file: branch file/pimp_sip_location r381357 - in /team/file/pimp_sip_location...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Feb 13 13:53:05 CST 2013


Author: file
Date: Wed Feb 13 13:53:01 2013
New Revision: 381357

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=381357
Log:
Flush out location a bit more!

This commit adds two new API calls:
1. ast_sip_location_retrieve - This retrieves a named location but takes into account location expiration if applicable
2. ast_sip_location_update - This updates the URI and expiration of a location if permitted

It also turns location into a sort of hybrid object. It can exist in both a static configuration file but parts of it
can also be stored in a persistence mechanism, like a database.

Next up I'll be writing a handler for the expiration time and also poking at dialing locations some more.

Modified:
    team/file/pimp_sip_location/include/asterisk/res_sip.h
    team/file/pimp_sip_location/res/res_sip.c
    team/file/pimp_sip_location/res/res_sip/location.c

Modified: team/file/pimp_sip_location/include/asterisk/res_sip.h
URL: http://svnview.digium.com/svn/asterisk/team/file/pimp_sip_location/include/asterisk/res_sip.h?view=diff&rev=381357&r1=381356&r2=381357
==============================================================================
--- team/file/pimp_sip_location/include/asterisk/res_sip.h (original)
+++ team/file/pimp_sip_location/include/asterisk/res_sip.h Wed Feb 13 13:53:01 2013
@@ -128,8 +128,10 @@
 	);
 	/*! Whether the user portion of the request URI is permitted to be changed or not */
 	unsigned int change_user;
+	/*! Whether the URI of this location can be updated/changed or not */
+	unsigned int update;
 	/*! Absolute time that this location is valid until */
-	time_t expiration_time;
+	struct timeval expiration;
 };
 
 /*!
@@ -425,6 +427,28 @@
 int ast_sip_initialize_sorcery_location(struct ast_sorcery *sorcery);
 
 /*!
+ * \brief Retrieve a named location
+ *
+ * \param name Name of the location
+ *
+ * \retval NULL if not found
+ * \retval non-NULL if found
+ */
+struct ast_sip_location *ast_sip_location_retrieve(const char *name);
+
+/*!
+ * \brief Update the URI and expiration time for a named location
+ *
+ * \param name Name of the location
+ * \param uri Optional fully formed URI
+ * \param expiration Optional expiration time
+ *
+ * \retval -1 failure
+ * \retval 0 success
+ */
+int ast_sip_location_update(const char *name, const char *uri, struct timeval expiration);
+
+/*!
  * \brief Create a new SIP work structure
  *
  * A SIP work is a means of grouping together SIP tasks. For instance, one

Modified: team/file/pimp_sip_location/res/res_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/file/pimp_sip_location/res/res_sip.c?view=diff&rev=381357&r1=381356&r2=381357
==============================================================================
--- team/file/pimp_sip_location/res/res_sip.c (original)
+++ team/file/pimp_sip_location/res/res_sip.c Wed Feb 13 13:53:01 2013
@@ -300,7 +300,7 @@
 	pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
 	const pj_str_t HCONTACT = { "Contact", 7 };
 
-	if (!ast_strlen_zero(location_name) && (location = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "location", location_name))) {
+	if (!ast_strlen_zero(location_name) && (location = ast_sip_location_retrieve(location_name))) {
 		pj_cstr(&remote_uri, location->uri);
 		if (!ast_strlen_zero(location->transport)) {
 			transport_name = location->transport;

Modified: team/file/pimp_sip_location/res/res_sip/location.c
URL: http://svnview.digium.com/svn/asterisk/team/file/pimp_sip_location/res/res_sip/location.c?view=diff&rev=381357&r1=381356&r2=381357
==============================================================================
--- team/file/pimp_sip_location/res/res_sip/location.c (original)
+++ team/file/pimp_sip_location/res/res_sip/location.c Wed Feb 13 13:53:01 2013
@@ -27,6 +27,7 @@
 #include "asterisk/astobj2.h"
 #include "asterisk/sorcery.h"
 
+
 /*! \brief Destructor for location */
  static void location_destroy(void *obj)
  {
@@ -52,20 +53,109 @@
 	return location;
 }
 
+int ast_sip_location_update(const char *name, const char *uri, struct timeval expiration)
+{
+	RAII_VAR(struct ast_sip_location *, existing, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "location", name), ao2_cleanup);
+	RAII_VAR(struct ast_sip_location *, updated, NULL, ao2_cleanup);
+
+	/* If the location can not be updated with a new URI... HALT! CEASE! DESIST! */
+	if (!existing->update || !(updated = ast_sorcery_copy(ast_sip_get_sorcery(), existing))) {
+		return -1;
+	}
+
+	ast_string_field_set(updated, uri, uri);
+	updated->expiration = expiration;
+
+	return ast_sorcery_update(ast_sip_get_sorcery(), updated);
+}
+
+struct ast_sip_location *ast_sip_location_retrieve(const char *name)
+{
+	struct ast_sip_location *location = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "location_dynamic", name);
+
+	/* If this location has expired update it and return nothing */
+	if (location && !ast_tvzero(location->expiration) && (ast_tvdiff_ms(location->expiration, ast_tvnow()) <= 0)) {
+		ast_sip_location_update(name, NULL, ast_tv(0, 0));
+		ao2_ref(location, -1);
+		return NULL;
+	} else if (!location) {
+		/* If no dynamic objects could be found fall back to static */
+		location = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "location_static", name);
+	}
+
+	return location;
+}
+
+/*! \brief Callback for when a static location is applied */
+static void location_apply(const struct ast_sorcery *sorcery, void *obj)
+{
+	struct ast_sip_location *location_static = obj;
+	RAII_VAR(struct ast_sip_location *, existing, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "location_dynamic", ast_sorcery_object_get_id(obj)), ao2_cleanup);
+	RAII_VAR(struct ast_sip_location *, copy, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
+
+	if (!existing) {
+		if (location_static->update) {
+			/* If this location can be updated we need to create a dynamic version, or try to at least */
+			if (!(existing = ast_sorcery_alloc(sorcery, "location_dynamic", ast_sorcery_object_get_id(obj))) ||
+				ast_sorcery_create(sorcery, existing)) {
+				return;
+			}
+		} else {
+			/* Static and dynamic sides are both in-sync */
+			return;
+		}
+	} else if (!location_static->update) {
+		/* Okay, we have an existing dynamic location but it should only be static so therefore delete it */
+		ast_sorcery_delete(sorcery, existing);
+		return;
+	}
+
+	/* So! Create a copy of the existing object and apply all relevant parts from the static object to it */
+	if (!(copy = ast_sorcery_copy(sorcery, existing)) ||
+		!(objset = ast_sorcery_objectset_create(sorcery, location_static)) ||
+		ast_sorcery_objectset_apply(sorcery, copy, objset)) {
+		return;
+	}
+
+	/* Once the state of the copy is that of the static version set the few bits from the dynamic part we care about */
+	ast_string_field_set(copy, uri, existing->uri);
+	copy->expiration = existing->expiration;
+
+	/* Because we know that both location_dynamic and location_static are really the same object we can change the object type without harm */
+	ast_copy_string(copy->details.type, "location_dynamic", sizeof(copy->details.type));
+
+	/* Now once we update the information in location_dynamic it should mirror the static aspect */
+	ast_sorcery_update(sorcery, copy);
+}
+
 /*! \brief Initialize sorcery with location support */
 int ast_sip_initialize_sorcery_location(struct ast_sorcery *sorcery)
 {
-	ast_sorcery_apply_default(sorcery, "location", "config", "res_sip.conf,criteria=type=location");
+	/* Location is actually a hybrid object potentially existing across multiple wizards so we address
+	 * the static and dynamic versions individually so we can ensure they remain in sync
+	 */
+	ast_sorcery_apply_default(sorcery, "location_dynamic", "memory", NULL);
+	ast_sorcery_apply_default(sorcery, "location_static", "config", "res_sip.conf,criteria=type=location");
 
-	if (ast_sorcery_object_register(sorcery, "location", location_alloc, NULL, NULL)) {
+	if (ast_sorcery_object_register(sorcery, "location_static", location_alloc, NULL, location_apply) ||
+		ast_sorcery_object_register(sorcery, "location_dynamic", location_alloc, NULL, NULL)) {
 		return -1;
 	}
 
-	ast_sorcery_object_field_register(sorcery, "location", "type", "", OPT_NOOP_T, 0, 0);
-	ast_sorcery_object_field_register(sorcery, "location", "uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, uri));
-	ast_sorcery_object_field_register(sorcery, "location", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, transport));
-	ast_sorcery_object_field_register(sorcery, "location", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, outbound_proxy));
-	ast_sorcery_object_field_register(sorcery, "location", "change_user", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_location, change_user));
+	ast_sorcery_object_field_register(sorcery, "location_static", "type", "", OPT_NOOP_T, 0, 0);
+	ast_sorcery_object_field_register(sorcery, "location_static", "uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, uri));
+	ast_sorcery_object_field_register(sorcery, "location_static", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, transport));
+	ast_sorcery_object_field_register(sorcery, "location_static", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, outbound_proxy));
+	ast_sorcery_object_field_register(sorcery, "location_static", "change_user", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_location, change_user));
+	ast_sorcery_object_field_register(sorcery, "location_static", "update", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_location, update));
+
+	ast_sorcery_object_field_register(sorcery, "location_dynamic", "type", "", OPT_NOOP_T, 0, 0);
+	ast_sorcery_object_field_register(sorcery, "location_dynamic", "uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, uri));
+	ast_sorcery_object_field_register(sorcery, "location_dynamic", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, transport));
+	ast_sorcery_object_field_register(sorcery, "location_dynamic", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_location, outbound_proxy));
+	ast_sorcery_object_field_register(sorcery, "location_dynamic", "change_user", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_location, change_user));
+	ast_sorcery_object_field_register(sorcery, "location_dynamic", "update", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_location, update));
 
 	return 0;
 }




More information about the asterisk-commits mailing list