[asterisk-commits] file: branch file/sorcery r378619 - in /team/file/sorcery: configs/ include/a...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Jan 5 08:25:07 CST 2013


Author: file
Date: Sat Jan  5 08:25:02 2013
New Revision: 378619

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=378619
Log:
Incorporate review feedback and make it easier for dynamic wizards to create objects.

Modified:
    team/file/sorcery/configs/sorcery.conf.sample
    team/file/sorcery/include/asterisk/sorcery.h
    team/file/sorcery/main/sorcery.c
    team/file/sorcery/res/res_sorcery_memory.c
    team/file/sorcery/tests/test_sorcery.c

Modified: team/file/sorcery/configs/sorcery.conf.sample
URL: http://svnview.digium.com/svn/asterisk/team/file/sorcery/configs/sorcery.conf.sample?view=diff&rev=378619&r1=378618&r2=378619
==============================================================================
--- team/file/sorcery/configs/sorcery.conf.sample (original)
+++ team/file/sorcery/configs/sorcery.conf.sample Sat Jan  5 08:25:02 2013
@@ -3,7 +3,7 @@
 ;
 ; Wizards
 ;
-; Wizards are the object persistence mechanism for objects. They are loaded as Asterisk modules and register
+; Wizards are the persistence mechanism for objects. They are loaded as Asterisk modules and register
 ; themselves with the sorcery core. All implementation specific details of how objects are persisted is isolated
 ; within wizards.
 ;

Modified: team/file/sorcery/include/asterisk/sorcery.h
URL: http://svnview.digium.com/svn/asterisk/team/file/sorcery/include/asterisk/sorcery.h?view=diff&rev=378619&r1=378618&r2=378619
==============================================================================
--- team/file/sorcery/include/asterisk/sorcery.h (original)
+++ team/file/sorcery/include/asterisk/sorcery.h Sat Jan  5 08:25:02 2013
@@ -172,22 +172,22 @@
 	void *(*open)(const char *data);
 
 	/*! \brief Optional callback for loading persistent objects */
-	void (*load)(void *data);
+	void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
 
 	/*! \brief Optional callback for reloading persistent objects */
-	void (*reload)(void *data);
+	void (*reload)(void *data, const struct ast_sorcery *sorcery, const char *type);
 
 	/*! \brief Callback for creating an object */
 	int (*create)(void *data, void *object);
 
 	/*! \brief Callback for retrieving an object using an id */
-	void *(*retrieve_id)(const struct ast_sorcery *sorcery, void *data, const char *id);
+	void *(*retrieve_id)(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
 
 	/*! \brief Optional callback for retrieving an object using fields */
-	void *(*retrieve_fields)(const struct ast_sorcery *sorcery, void *data, const struct ast_variable *fields);
+	void *(*retrieve_fields)(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
 
 	/*! \brief Optional callback for retrieving multiple objects using some optional field criteria */
-	void (*retrieve_multiple)(const struct ast_sorcery *sorcery, void *data, struct ao2_container *objects, const struct ast_variable *fields);
+	void (*retrieve_multiple)(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields);
 
 	/*! \brief Callback for updating an object */
 	int (*update)(void *data, void *object);
@@ -231,7 +231,12 @@
  * \retval 0 success
  * \retval -1 failure
  */
-int ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module);
+int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module);
+
+/*!
+ * \brief See \ref __ast_sorcery_wizard_register()
+ */
+#define ast_sorcery_wizard_register(interface) __ast_sorcery_wizard_register(interface, ast_module_info ? ast_module_info->self : NULL)
 
 /*!
  * \brief Unregister a sorcery wizard

Modified: team/file/sorcery/main/sorcery.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorcery/main/sorcery.c?view=diff&rev=378619&r1=378618&r2=378619
==============================================================================
--- team/file/sorcery/main/sorcery.c (original)
+++ team/file/sorcery/main/sorcery.c Sat Jan  5 08:25:02 2013
@@ -105,6 +105,18 @@
 	struct ao2_container *types;
 };
 
+/*! \brief Structure for passing load/reload details */
+struct sorcery_load_details {
+	/*! \brief Sorcery structure in use */
+	const struct ast_sorcery *sorcery;
+
+	/*! \brief Type of object being loaded */
+	const char *type;
+
+	/*! \brief Whether this is a reload or not */
+	unsigned int reload:1;
+};
+
 /*! \brief Registered sorcery wizards */
 struct ao2_container *wizards;
 
@@ -208,7 +220,7 @@
 	return 0;
 }
 
-int ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
+int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
 {
 	struct ast_sorcery_wizard *wizard;
 	int res = -1;
@@ -311,9 +323,7 @@
 		ast_free(object_type->info);
 	}
 
-	if (object_type->file) {
-		ast_free(object_type->file);
-	}
+	ast_free(object_type->file);
 }
 
 /*! \brief Internal function which allocates an object type structure */
@@ -363,9 +373,7 @@
 		object_wizard->wizard->close(object_wizard->data);
 	}
 
-	if (object_wizard->wizard->module) {
-		ast_module_unref(object_wizard->wizard->module);
-	}
+	ast_module_unref(object_wizard->wizard->module);
 }
 
 /*! \brief Internal function which creates an object type and adds a wizard mapping */
@@ -395,9 +403,7 @@
 		return -1;
 	}
 
-	if (wizard->module) {
-		ast_module_ref(wizard->module);
-	}
+	ast_module_ref(wizard->module);
 
 	object_wizard->wizard = wizard;
 	object_wizard->caching = caching;
@@ -537,9 +543,13 @@
 static int sorcery_wizard_load(void *obj, void *arg, int flags)
 {
 	struct ast_sorcery_object_wizard *wizard = obj;
-
-	if (wizard->wizard->load) {
-		wizard->wizard->load(wizard->data);
+	struct sorcery_load_details *details = arg;
+	void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
+
+	load = !details->reload ? wizard->wizard->load : wizard->wizard->reload;
+
+	if (load) {
+		load(wizard->data, details->sorcery, details->type);
 	}
 
 	return 0;
@@ -548,40 +558,32 @@
 static int sorcery_object_load(void *obj, void *arg, int flags)
 {
 	struct ast_sorcery_object_type *type = obj;
-
-	ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, NULL);
+	struct sorcery_load_details *details = arg;
+
+	details->type = type->name;
+	ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, details);
 
 	return 0;
 }
 
 void ast_sorcery_load(const struct ast_sorcery *sorcery)
 {
-	ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, NULL);
-}
-
-static int sorcery_wizard_reload(void *obj, void *arg, int flags)
-{
-	struct ast_sorcery_object_wizard *wizard = obj;
-
-	if (wizard->wizard->reload) {
-		wizard->wizard->reload(wizard->data);
-	}
-
-	return 0;
-}
-
-static int sorcery_object_reload(void *obj, void *arg, int flags)
-{
-	struct ast_sorcery_object_type *type = obj;
-
-	ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_reload, NULL);
-
-	return 0;
+	struct sorcery_load_details details = {
+		.sorcery = sorcery,
+		.reload = 0,
+	};
+
+	ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
 }
 
 void ast_sorcery_reload(const struct ast_sorcery *sorcery)
 {
-	ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_reload, NULL);
+	struct sorcery_load_details details = {
+		.sorcery = sorcery,
+		.reload = 1,
+	};
+
+	ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
 }
 
 void ast_sorcery_ref(struct ast_sorcery *sorcery)
@@ -847,15 +849,15 @@
 	for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
 		if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
 			if (wizard->wizard->retrieve_multiple) {
-				wizard->wizard->retrieve_multiple(sorcery, wizard->data, object, fields);
+				wizard->wizard->retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
 			}
 		} else if (!ast_strlen_zero(id)) {
 			if (wizard->wizard->retrieve_id) {
-				object = wizard->wizard->retrieve_id(sorcery, wizard->data, id);
+				object = wizard->wizard->retrieve_id(sorcery, wizard->data, object_type->name, id);
 			}
 		} else if (fields && wizard->wizard->retrieve_fields) {
 			if (wizard->wizard->retrieve_fields) {
-				object = wizard->wizard->retrieve_fields(sorcery, wizard->data, fields);
+				object = wizard->wizard->retrieve_fields(sorcery, wizard->data, object_type->name, fields);
 			}
 		}
 

Modified: team/file/sorcery/res/res_sorcery_memory.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorcery/res/res_sorcery_memory.c?view=diff&rev=378619&r1=378618&r2=378619
==============================================================================
--- team/file/sorcery/res/res_sorcery_memory.c (original)
+++ team/file/sorcery/res/res_sorcery_memory.c Sat Jan  5 08:25:02 2013
@@ -41,9 +41,10 @@
 
 static void *sorcery_memory_open(const char *data);
 static int sorcery_memory_create(void *data, void *object);
-static void *sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *id);
-static void *sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const struct ast_variable *fields);
-static void sorcery_memory_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, struct ao2_container *objects, const struct ast_variable *fields);
+static void *sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
+static void *sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
+static void sorcery_memory_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
+					     const struct ast_variable *fields);
 static int sorcery_memory_update(void *data, void *object);
 static int sorcery_memory_delete(void *data, void *object);
 static void sorcery_memory_close(void *data);
@@ -120,7 +121,7 @@
 	}
 }
 
-static void *sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const struct ast_variable *fields)
+static void *sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
 {
 	struct sorcery_memory_fields_cmp_params params = {
 		.sorcery = sorcery,
@@ -136,12 +137,12 @@
 	return ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
 }
 
-static void *sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *id)
+static void *sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
 {
 	return ao2_find(data, id, OBJ_KEY);
 }
 
-static void sorcery_memory_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, struct ao2_container *objects, const struct ast_variable *fields)
+static void sorcery_memory_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
 {
 	struct sorcery_memory_fields_cmp_params params = {
 		.sorcery = sorcery,
@@ -189,7 +190,7 @@
 
 static int load_module(void)
 {
-	if (ast_sorcery_wizard_register(&memory_object_wizard, NULL)) {
+	if (ast_sorcery_wizard_register(&memory_object_wizard)) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
 

Modified: team/file/sorcery/tests/test_sorcery.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorcery/tests/test_sorcery.c?view=diff&rev=378619&r1=378618&r2=378619
==============================================================================
--- team/file/sorcery/tests/test_sorcery.c (original)
+++ team/file/sorcery/tests/test_sorcery.c Sat Jan  5 08:25:02 2013
@@ -111,9 +111,9 @@
 	return 0;
 }
 
-static void *sorcery_test_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *id)
-{
-	return (cache.created && !cache.deleted) ? ast_sorcery_alloc(sorcery, "test", id) : NULL;
+static void *sorcery_test_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
+{
+	return (cache.created && !cache.deleted) ? ast_sorcery_alloc(sorcery, type, id) : NULL;
 }
 
 static int sorcery_test_update(void *data, void *object)
@@ -151,12 +151,12 @@
 		break;
 	}
 
-	if (ast_sorcery_wizard_register(&test_wizard, NULL)) {
+	if (ast_sorcery_wizard_register(&test_wizard)) {
 		ast_test_status_update(test, "Failed to register a perfectly valid sorcery wizard\n");
 		return AST_TEST_FAIL;
 	}
 
-	if (!ast_sorcery_wizard_register(&test_wizard, NULL)) {
+	if (!ast_sorcery_wizard_register(&test_wizard)) {
 		ast_test_status_update(test, "Successfully registered a sorcery wizard twice, which is bad\n");
 		return AST_TEST_FAIL;
 	}
@@ -1541,7 +1541,7 @@
 
 	ast_config_destroy(config);
 
-	if (ast_sorcery_wizard_register(&test_wizard, NULL)) {
+	if (ast_sorcery_wizard_register(&test_wizard)) {
 		ast_test_status_update(test, "Failed to register a perfectly valid sorcery wizard\n");
 		return AST_TEST_FAIL;
 	}




More information about the asterisk-commits mailing list