[asterisk-commits] mmichelson: branch group/pimp_my_sip r379068 - in /team/group/pimp_my_sip: in...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Jan 14 15:04:08 CST 2013


Author: mmichelson
Date: Mon Jan 14 15:04:05 2013
New Revision: 379068

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=379068
Log:
Add SIP session datastore API guts.


Modified:
    team/group/pimp_my_sip/include/asterisk/res_sip_session.h
    team/group/pimp_my_sip/res/res_sip_session.c

Modified: team/group/pimp_my_sip/include/asterisk/res_sip_session.h
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/include/asterisk/res_sip_session.h?view=diff&rev=379068&r1=379067&r2=379068
==============================================================================
--- team/group/pimp_my_sip/include/asterisk/res_sip_session.h (original)
+++ team/group/pimp_my_sip/include/asterisk/res_sip_session.h Mon Jan 14 15:04:05 2013
@@ -158,11 +158,15 @@
 void ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement);
 
 /*!
- * \brief Wrapper for ast_datastore_alloc()
+ * \brief Alternative for ast_datastore_alloc()
  *
  * There are two major differences between this and ast_datastore_alloc()
  * 1) This allocates a refcounted object
  * 2) This will fill in a uid if one is not provided
+ *
+ * DO NOT call ast_datastore_free() on a datastore allocated in this
+ * way since that function will attempt to free the datastore rather
+ * than play nicely with its refcount.
  *
  * \param info Callbacks for datastore
  * \param uid Identifier for datastore
@@ -178,7 +182,7 @@
  * must have been allocated using ao2_alloc() or there will be serious problems.
  *
  * \param session The session to add the datastore to
- * \param cookie The datastore to be added to the session
+ * \param datastore The datastore to be added to the session
  * \retval 0 Success
  * \retval -1 Failure
  */
@@ -192,8 +196,8 @@
  *
  * \param session The session from which to retrieve the datastore
  * \param name The name of the datastore to retrieve
- * \retval NULL Failed to find the specified cookie
- * \retval non-NULL The specified cookie
+ * \retval NULL Failed to find the specified datastore
+ * \retval non-NULL The specified datastore
  */
 struct ast_datastore *ast_sip_session_get_datastore(struct ast_sip_session *session, const char *name);
 
@@ -203,12 +207,10 @@
  * This operation may cause the datastore's free() callback to be called if the reference
  * count reaches zero.
  *
- * \param session The session to remove the cookie from
- * \param name The name of the cookie to remove
- * \retval 0 Successfully removed the cookie
- * \retval -1 Failed to remove the cookie
- */
-int ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name);
+ * \param session The session to remove the datastore from
+ * \param name The name of the datastore to remove
+ */
+void ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name);
 
 /*!
  * \brief Retrieve identifying information from an incoming request

Modified: team/group/pimp_my_sip/res/res_sip_session.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pimp_my_sip/res/res_sip_session.c?view=diff&rev=379068&r1=379067&r2=379068
==============================================================================
--- team/group/pimp_my_sip/res/res_sip_session.c (original)
+++ team/group/pimp_my_sip/res/res_sip_session.c Mon Jan 14 15:04:05 2013
@@ -31,6 +31,7 @@
 #include "asterisk/res_sip.h"
 #include "asterisk/astobj2.h"
 #include "asterisk/lock.h"
+#include "asterisk/uuid.h"
 
 #define SDP_HANDLER_BUCKETS 11
 /*!
@@ -155,28 +156,75 @@
 	AST_RWLIST_TRAVERSE_SAFE_END;
 }
 
+static void session_datastore_destroy(void *obj)
+{
+	struct ast_datastore *datastore = obj;
+
+	/* Using the destroy function (if present) destroy the data */
+	if (datastore->info->destroy != NULL && datastore->data != NULL) {
+		datastore->info->destroy(datastore->data);
+		datastore->data = NULL;
+	}
+
+	ast_free((void *) datastore->uid);
+	datastore->uid = NULL;
+}
+
 struct ast_datastore *ast_sip_session_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
 {
-	/* XXX STUB */
-	return NULL;
+	RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
+	const char *uid_ptr = uid;
+	char uuid_buf[AST_UUID_STR_LEN];
+
+	if (!info) {
+		return NULL;
+	}
+
+	datastore = ao2_alloc(sizeof(*datastore), session_datastore_destroy);
+	if (!datastore) {
+		return NULL;
+	}
+
+	datastore->info = info;
+	if (ast_strlen_zero(uid)) {
+		/* They didn't provide an ID so we'll provide one ourself */
+		struct ast_uuid *uuid = ast_uuid_generate();
+		if (!uuid) {
+			return NULL;
+		}
+		uid_ptr = ast_uuid_to_str(uuid, uuid_buf, sizeof(uuid_buf));
+		ast_free(uuid);
+	}
+
+	datastore->uid = ast_strdup(uid_ptr);
+	if (!datastore->uid) {
+		return NULL;
+	}
+
+	ao2_ref(datastore, +1);
+	return datastore;
 }
 
 int ast_sip_session_add_datastore(struct ast_sip_session *session, struct ast_datastore *datastore)
 {
-	/* XXX STUB */
+	ast_assert(datastore != NULL);
+	ast_assert(datastore->info != NULL);
+	ast_assert(ast_strlen_zero(datastore->uid) == 0);
+
+	if (!ao2_link(session->datastores, datastore)) {
+		return -1;
+	}
 	return 0;
 }
 
 struct ast_datastore *ast_sip_session_get_datastore(struct ast_sip_session *session, const char *name)
 {
-	/* XXX STUB */
-	return NULL;
-}
-
-int ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name)
-{
-	/* XXX STUB */
-	return 0;
+	return ao2_find(session->datastores, name, OBJ_KEY);
+}
+
+void ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name)
+{
+	ao2_callback(session->datastores, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA, NULL, (void *) name);
 }
 
 int ast_sip_session_get_identity(struct pjsip_rx_data *rdata, struct ast_party_id *id)




More information about the asterisk-commits mailing list