[asterisk-commits] file: branch file/sorcery r377967 - in /team/file/sorcery: include/asterisk/ ...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Dec 13 08:48:31 CST 2012


Author: file
Date: Thu Dec 13 08:48:30 2012
New Revision: 377967

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=377967
Log:
Add some more code. Still very much in flux.

Added:
    team/file/sorcery/tests/test_sorcery.c   (with props)
Modified:
    team/file/sorcery/include/asterisk/sorcery.h
    team/file/sorcery/main/asterisk.c
    team/file/sorcery/main/sorcery.c

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=377967&r1=377966&r2=377967
==============================================================================
--- team/file/sorcery/include/asterisk/sorcery.h (original)
+++ team/file/sorcery/include/asterisk/sorcery.h Thu Dec 13 08:48:30 2012
@@ -36,45 +36,80 @@
 extern "C" {
 #endif
 
+/*! \brief Maximum size of an object unique identifier */
+#define MAX_OBJECT_ID 64
+
+/*! \brief Maximum size of an object type */
+#define MAX_OBJECT_TYPE 64
+
 /*! \brief Interface for a sorcery wizard */
 struct ast_sorcery_wizard {
+	/*! \brief Name of the wizard */
+	const char *name;
+
+	/*! \brief Pointer to the Asterisk module this wizard is implemented by */
+	struct ast_module *module;
 };
 
 /*! \brief Forward declaration for the sorcery main structure */
 struct ast_sorcery;
 
+/*! \brief Structure which contains details about a sorcery object */
+struct ast_sorcery_object_details {
+	/*! \brief Unique identifier of this object */
+	char id[MAX_OBJECT_ID];
+
+	/*! \brief Type of object */
+	char type[MAX_OBJECT_TYPE];
+
+	/*! \brief Wizard which has conjured this object */
+	struct ast_sorcery_wizard *wizard;
+};
+
+/*! \brief Macro which must be used at the beginning of each sorcery capable object */
+#define SORCERY_OBJECT(details)                    \
+struct {                                           \
+	struct ast_sorcery_object_details details; \
+}                                                  \
+
+/*!
+ * \brief Initialize the sorcery API
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+int ast_sorcery_init(void);
+
 /*!
  * \brief Register a sorcery wizard
  *
- * \param name Name of the provider
- * \param provider Pointer to a wizard interface
+ * \param interface Pointer to a wizard interface
  * \param module Pointer to the module implementing the interface
  *
  * \retval 0 success
  * \retval -1 failure
  */
-int ast_sorcery_wizard_register(const char *name, const struct ast_sorcery_wizard *wizard, struct ast_module *module);
+int ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module);
 
 /*!
  * \brief Unregister a sorcery wizard
  *
- * \param name Name of the wizard
- * \param provider Pointer to the wizard interface
+ * \param interface Pointer to the wizard interface
  *
  * \retval 0 success
  * \retval -1 failure
  */
-int ast_sorcery_wizard_unregister(const char *name, const struct ast_sorcery_wizard *wizard);
+int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface);
 
 /*!
- * \brief Allocate a new sorcery structure
+ * \brief Open a new sorcery structure
  *
  * \param name Optional name of the module using the sorcery API, used for explicit object->wizard mappings
  *
  * \retval non-NULL success
  * \retval NULL if allocation failed
  */
-struct ast_sorcery *ast_sorcery_alloc(const char *name);
+struct ast_sorcery *ast_sorcery_open(const char *name);
 
 /*!
  * \brief Increase the reference count of a sorcery structure
@@ -84,12 +119,75 @@
 void ast_sorcery_ref(struct ast_sorcery *sorcery);
 
 /*!
+ * \brief Allocate an object
+ *
+ * \param sorcery Pointer to a sorcery structure
+ * \param type Type of object to allocate
+ * \param id Optional unique identifier, if none is provided one will be generated
+ *
+ * \retval non-NULL success
+ * \retval NULL if allocation failed
+ */
+void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id);
+
+/*!
+ * \brief Persist an object
+ *
+ * \param sorcery Pointer to a sorcery structure
+ * \param object Pointer to a sorcery object
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+int ast_sorcery_create(struct ast_sorcery *sorcery, void *object);
+
+/*!
+ * \brief Update an object
+ *
+ * \param sorcery Pointer to a sorcery structure
+ * \param object Pointer to a sorcery object
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+int ast_sorcery_update(struct ast_sorcery *sorcery, void *object);
+
+/*!
+ * \brief Delete an object
+ *
+ * \param sorcery Pointer to a sorcery structure
+ * \param object Pointer to a sorcery object
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+int ast_sorcery_delete(struct ast_sorcery *sorcery, void *object);
+
+/*!
  * \brief Decrease the reference count of a sorcery structure
  *
  * \param sorcery Pointer to a sorcery structure
  */
 void ast_sorcery_unref(struct ast_sorcery *sorcery);
 
+/*!
+ * \brief Get the unique identifier of a sorcery object
+ *
+ * \param object Pointer to a sorcery object
+ *
+ * \retval unique identifier
+ */
+const char *ast_sorcery_object_get_id(void *object);
+
+/*!
+ * \brief Get the type of a sorcery object
+ *
+ * \param object Pointer to a sorcery object
+ *
+ * \retval type of object
+ */
+const char *ast_sorcery_object_get_type(void *object);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

Modified: team/file/sorcery/main/asterisk.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorcery/main/asterisk.c?view=diff&rev=377967&r1=377966&r2=377967
==============================================================================
--- team/file/sorcery/main/asterisk.c (original)
+++ team/file/sorcery/main/asterisk.c Thu Dec 13 08:48:30 2012
@@ -239,6 +239,7 @@
 #include "asterisk/format.h"
 #include "asterisk/aoc.h"
 #include "asterisk/uuid.h"
+#include "asterisk/sorcery.h"
 
 #include "../defaults.h"
 
@@ -4115,6 +4116,11 @@
 	ast_aoc_cli_init();
 	ast_uuid_init();
 
+	if (ast_sorcery_init()) {
+		printf("%s", term_quit());
+		exit(1);
+	}
+
 	ast_makesocket();
 	sigemptyset(&sigs);
 	sigaddset(&sigs, SIGHUP);

Modified: team/file/sorcery/main/sorcery.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorcery/main/sorcery.c?view=diff&rev=377967&r1=377966&r2=377967
==============================================================================
--- team/file/sorcery/main/sorcery.c (original)
+++ team/file/sorcery/main/sorcery.c Thu Dec 13 08:48:30 2012
@@ -34,23 +34,118 @@
 #include "asterisk/logger.h"
 #include "asterisk/sorcery.h"
 #include "asterisk/astobj2.h"
+#include "asterisk/strings.h"
+
+/*! \brief Number of buckets for wizards */
+#define WIZARD_BUCKETS 7
+
+/*! \brief Number of buckets for types */
+#define TYPE_BUCKETS 53
+
+/*! \brief Structure for registered object type */
+struct ast_sorcery_object_type {
+	/*! \brief Unqiue name of the object type */
+	char name[MAX_OBJECT_TYPE];
+};
+
+/*! \brief Full structure for sorcery */
+struct ast_sorcery {
+	/*! \brief Container for known object types */
+	struct ao2_container *types;
+};
 
 /*! \brief Registered sorcery wizards */
 struct ao2_container *wizards;
 
-int ast_sorcery_wizard_register(const char *name, const struct ast_sorcery_wizard *wizard, struct ast_module *module)
+/*! \brief Hashing function for sorcery wizards */
+static int sorcery_wizard_hash(const void *obj, const int flags)
 {
-	return -1;
+	const struct ast_sorcery_wizard *wizard = obj;
+	const char *name = obj;
+
+	return ast_str_hash(flags & OBJ_KEY ? name : wizard->name);
 }
 
-int ast_sorcery_wizard_unregister(const char *name, const struct ast_sorcery_wizard *wizard)
+/*! \brief Comparator function for sorcery wizards */
+static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
 {
-	return -1;
+	struct ast_sorcery_wizard *wizard1 = obj, *wizard2 = arg;
+	const char *name = arg;
+
+	return !strcmp(wizard1->name, flags & OBJ_KEY ? name : wizard2->name) ? CMP_MATCH | CMP_STOP : 0;
 }
 
-struct ast_sorcery *ast_sorcery_alloc(const char *name)
+int ast_sorcery_init(void)
 {
-	return NULL;
+	if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
+		return -1;
+	}
+
+	return 0;
+}
+
+int ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
+{
+	struct ast_sorcery_wizard *wizard;
+	int res = -1;
+
+	ast_assert(!ast_strlen_zero(interface->name));
+
+	ao2_lock(wizards);
+
+	if ((wizard = ao2_find(wizards, interface->name, OBJ_KEY | OBJ_NOLOCK))) {
+		ast_log(LOG_WARNING, "Attempted to register sorcery wizard '%s' twice\n",
+			interface->name);
+		goto done;
+	}
+
+	if (!(wizard = ao2_alloc(sizeof(*wizard), NULL))) {
+		goto done;
+	}
+
+	*wizard = *interface;
+	wizard->module = module;
+
+	ao2_link_flags(wizards, wizard, OBJ_NOLOCK);
+	res = 0;
+
+	ast_verb(2, "Sorcery registered wizard '%s'\n", interface->name);
+
+done:
+	if (wizard) {
+		ao2_ref(wizard, -1);
+	}
+	ao2_unlock(wizards);
+
+	return res;
+}
+
+int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
+{
+	RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, interface->name, OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
+
+	if (wizard) {
+		ast_verb(2, "Sorcery unregistered wizard '%s'\n", interface->name);
+		return 0;
+	} else {
+		return -1;
+	}
+}
+
+/*! \brief Destructor called when sorcery structure is destroyed */
+static void sorcery_destructor(void *obj)
+{
+}
+
+struct ast_sorcery *ast_sorcery_open(const char *name)
+{
+	struct ast_sorcery *sorcery;
+
+	if (!(sorcery = ao2_alloc(sizeof(*sorcery), sorcery_destructor))) {
+		return NULL;
+	}
+
+	return sorcery;
 }
 
 void ast_sorcery_ref(struct ast_sorcery *sorcery)
@@ -58,7 +153,39 @@
 	ao2_ref(sorcery, +1);
 }
 
+void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
+{
+	return NULL;
+}
+
+int ast_sorcery_create(struct ast_sorcery *sorcery, void *object)
+{
+	return -1;
+}
+
+int ast_sorcery_update(struct ast_sorcery *sorcery, void *object)
+{
+	return -1;
+}
+
+int ast_sorcery_delete(struct ast_sorcery *sorcery, void *object)
+{
+	return -1;
+}
+
 void ast_sorcery_unref(struct ast_sorcery *sorcery)
 {
 	ao2_ref(sorcery, -1);
 }
+
+const char *ast_sorcery_object_get_id(void *object)
+{
+	struct ast_sorcery_object_details *details = object;
+	return details->id;
+}
+
+const char *ast_sorcery_object_get_type(void *object)
+{
+	struct ast_sorcery_object_details *details = object;
+	return details->type;
+}

Added: team/file/sorcery/tests/test_sorcery.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorcery/tests/test_sorcery.c?view=auto&rev=377967
==============================================================================
--- team/file/sorcery/tests/test_sorcery.c (added)
+++ team/file/sorcery/tests/test_sorcery.c Thu Dec 13 08:48:30 2012
@@ -1,0 +1,131 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2012, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Sorcery Unit Tests
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ *
+ */
+
+/*** MODULEINFO
+	<depend>TEST_FRAMEWORK</depend>
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "")
+
+#include "asterisk/test.h"
+#include "asterisk/module.h"
+#include "asterisk/sorcery.h"
+#include "asterisk/logger.h"
+
+/*! \brief Dummy sorcery wizard, not actually used so we only populate the name and nothing else */
+static struct ast_sorcery_wizard test_wizard = {
+	.name = "test",
+};
+
+AST_TEST_DEFINE(wizard_registration)
+{
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "wizard_registration";
+		info->category = "/main/sorcery/";
+		info->summary = "sorcery wizard registration and unregistration unit test";
+		info->description =
+			"Test registration and unregistration of a sorcery wizard";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	if (ast_sorcery_wizard_register(&test_wizard, NULL)) {
+		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)) {
+		ast_test_status_update(test, "Successfully registered a sorcery wizard twice, which is bad\n");
+		return AST_TEST_FAIL;
+	}
+
+	if (ast_sorcery_wizard_unregister(&test_wizard)) {
+		ast_test_status_update(test, "Failed to unregister a perfectly valid sorcery wizard\n");
+		return AST_TEST_FAIL;
+	}
+
+	if (!ast_sorcery_wizard_unregister(&test_wizard)) {
+		ast_test_status_update(test, "Successfully unregistered a sorcery wizard twice, which is bad\n");
+		return AST_TEST_FAIL;
+	}
+
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(open)
+{
+	int res = AST_TEST_PASS;
+	struct ast_sorcery *sorcery;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "open";
+		info->category = "/main/sorcery/";
+		info->summary = "sorcery open unit test";
+		info->description =
+			"Test opening of sorcery";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	if (!(sorcery = ast_sorcery_open(NULL))) {
+		ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+		res = AST_TEST_FAIL;
+	}
+
+	ast_sorcery_unref(sorcery);
+
+	if (!(sorcery = ast_sorcery_open("test"))) {
+		ast_test_status_update(test, "Failed to open sorcery with populated name\n");
+		res = AST_TEST_FAIL;
+	}
+
+	ast_sorcery_unref(sorcery);
+
+	return res;
+}
+
+static int unload_module(void)
+{
+	AST_TEST_UNREGISTER(wizard_registration);
+	AST_TEST_UNREGISTER(open);
+	return 0;
+}
+
+static int load_module(void)
+{
+	AST_TEST_REGISTER(wizard_registration);
+	AST_TEST_REGISTER(open);
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Sorcery test module");

Propchange: team/file/sorcery/tests/test_sorcery.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/file/sorcery/tests/test_sorcery.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/file/sorcery/tests/test_sorcery.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list