2013-01-25 14:01:04 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 - 2013, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Joshua Colp <jcolp@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 Data Access Layer API
|
|
|
|
*
|
|
|
|
* \author Joshua Colp <jcolp@digium.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*** MODULEINFO
|
|
|
|
<support_level>core</support_level>
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include "asterisk.h"
|
|
|
|
|
git migration: Refactor the ASTERISK_FILE_VERSION macro
Git does not support the ability to replace a token with a version
string during check-in. While it does have support for replacing a
token on clone, this is somewhat sub-optimal: the token is replaced
with the object hash, which is not particularly easy for human
consumption. What's more, in practice, the source file version was often
not terribly useful. Generally, when triaging bugs, the overall version
of Asterisk is far more useful than an individual SVN version of a file. As a
result, this patch removes Asterisk's support for showing source file
versions.
Specifically, it does the following:
* Rename ASTERISK_FILE_VERSION macro to ASTERISK_REGISTER_FILE, and
remove passing the version in with the macro. Other facilities
than 'core show file version' make use of the file names, such as
setting a debug level only on a specific file. As such, the act of
registering source files with the Asterisk core still has use. The
macro rename now reflects the new macro purpose.
* main/asterisk:
- Refactor the file_version structure to reflect that it no longer
tracks a version field.
- Remove the "core show file version" CLI command. Without the file
version, it is no longer useful.
- Remove the ast_file_version_find function. The file version is no
longer tracked.
- Rename ast_register_file_version/ast_unregister_file_version to
ast_register_file/ast_unregister_file, respectively.
* main/manager: Remove value from the Version key of the ModuleCheck
Action. The actual key itself has not been removed, as doing so would
absolutely constitute a backwards incompatible change. However, since
the file version is no longer tracked, there is no need to attempt to
include it in the Version key.
* UPGRADE: Add notes for:
- Modification to the ModuleCheck AMI Action
- Removal of the "core show file version" CLI command
Change-Id: I6cf0ff280e1668bf4957dc21f32a5ff43444a40e
2015-04-12 02:38:22 +00:00
|
|
|
ASTERISK_REGISTER_FILE()
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
#include "asterisk/logger.h"
|
|
|
|
#include "asterisk/sorcery.h"
|
|
|
|
#include "asterisk/astobj2.h"
|
2013-12-11 12:31:57 +00:00
|
|
|
#include "asterisk/format.h"
|
|
|
|
#include "asterisk/format_cap.h"
|
2013-01-25 14:01:04 +00:00
|
|
|
#include "asterisk/strings.h"
|
|
|
|
#include "asterisk/config_options.h"
|
|
|
|
#include "asterisk/netsock2.h"
|
|
|
|
#include "asterisk/module.h"
|
2013-05-06 13:04:08 +00:00
|
|
|
#include "asterisk/taskprocessor.h"
|
|
|
|
#include "asterisk/threadpool.h"
|
|
|
|
#include "asterisk/json.h"
|
2015-05-05 20:32:08 +00:00
|
|
|
#include "asterisk/vector.h"
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2013-01-29 20:19:28 +00:00
|
|
|
/* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
|
|
|
|
#undef open
|
|
|
|
#undef close
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
/*! \brief Number of buckets for wizards (should be prime for performance reasons) */
|
|
|
|
#define WIZARD_BUCKETS 7
|
|
|
|
|
|
|
|
/*! \brief Number of buckets for types (should be prime for performance reasons) */
|
|
|
|
#define TYPE_BUCKETS 53
|
|
|
|
|
2014-02-20 20:45:30 +00:00
|
|
|
/*! \brief Number of buckets for instances (should be prime for performance reasons) */
|
|
|
|
#define INSTANCE_BUCKETS 17
|
|
|
|
|
2014-03-07 21:23:39 +00:00
|
|
|
/*! \brief Number of buckets for object fields (should be prime for performance reasons) */
|
|
|
|
#define OBJECT_FIELD_BUCKETS 29
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
#define NOTIFY_GENERIC_OBSERVERS(container, type, callback, ...) ({ \
|
|
|
|
struct ao2_iterator i = ao2_iterator_init(container, 0); \
|
|
|
|
struct type *observer; \
|
|
|
|
ao2_rdlock(container); \
|
|
|
|
while ((observer = ao2_iterator_next(&i))) { \
|
|
|
|
if (observer->callbacks->callback) { \
|
|
|
|
observer->callbacks->callback(__VA_ARGS__); \
|
|
|
|
} \
|
|
|
|
ao2_cleanup(observer); \
|
|
|
|
} \
|
|
|
|
ao2_unlock(container); \
|
|
|
|
ao2_iterator_cleanup(&i); \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define NOTIFY_GLOBAL_OBSERVERS(container, callback, ...) \
|
|
|
|
NOTIFY_GENERIC_OBSERVERS(container, sorcery_global_observer, callback, __VA_ARGS__)
|
|
|
|
|
|
|
|
#define NOTIFY_INSTANCE_OBSERVERS(container, callback, ...) \
|
|
|
|
NOTIFY_GENERIC_OBSERVERS(container, sorcery_instance_observer, callback, __VA_ARGS__)
|
|
|
|
|
|
|
|
#define NOTIFY_WIZARD_OBSERVERS(container, callback, ...) \
|
|
|
|
NOTIFY_GENERIC_OBSERVERS(container, sorcery_wizard_observer, callback, __VA_ARGS__)
|
|
|
|
|
2013-05-06 13:04:08 +00:00
|
|
|
/*! \brief Thread pool for observers */
|
|
|
|
static struct ast_threadpool *threadpool;
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
/*! \brief Structure for an internal wizard instance */
|
|
|
|
struct ast_sorcery_internal_wizard {
|
|
|
|
/*!
|
|
|
|
* \brief Wizard interface itself
|
|
|
|
* \warning Callbacks must always be declared first in this structure
|
|
|
|
* so an ao2_ref on &callbacks will adjust the ref count on
|
|
|
|
* internal_wizard.
|
|
|
|
*/
|
|
|
|
struct ast_sorcery_wizard callbacks;
|
|
|
|
|
|
|
|
/*! \brief Observers */
|
|
|
|
struct ao2_container *observers;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief Structure for a wizard instance which operates on objects */
|
|
|
|
struct ast_sorcery_object_wizard {
|
|
|
|
/*! \brief Wizard interface itself */
|
|
|
|
struct ast_sorcery_internal_wizard *wizard;
|
|
|
|
|
|
|
|
/*! \brief Unique data for the wizard */
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
/*! \brief Wizard is acting as an object cache */
|
|
|
|
unsigned int caching:1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief Interface for a sorcery object type wizards */
|
|
|
|
AST_VECTOR_RW(ast_sorcery_object_wizards, struct ast_sorcery_object_wizard *);
|
|
|
|
|
2013-06-22 14:26:25 +00:00
|
|
|
/*! \brief Structure for internal sorcery object information */
|
|
|
|
struct ast_sorcery_object {
|
|
|
|
/*! \brief Unique identifier of this object */
|
2013-08-23 21:49:47 +00:00
|
|
|
char *id;
|
2013-06-22 14:26:25 +00:00
|
|
|
|
|
|
|
/*! \brief Type of object */
|
|
|
|
char type[MAX_OBJECT_TYPE];
|
|
|
|
|
|
|
|
/*! \brief Optional object destructor */
|
|
|
|
ao2_destructor_fn destructor;
|
|
|
|
|
|
|
|
/*! \brief Extended object fields */
|
|
|
|
struct ast_variable *extended;
|
2015-07-04 15:03:06 +00:00
|
|
|
|
|
|
|
/*! \brief Time that the object was created */
|
|
|
|
struct timeval created;
|
2013-06-22 14:26:25 +00:00
|
|
|
};
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
/*! \brief Structure for registered object type */
|
|
|
|
struct ast_sorcery_object_type {
|
|
|
|
/*! \brief Unique name of the object type */
|
|
|
|
char name[MAX_OBJECT_TYPE];
|
|
|
|
|
|
|
|
/*! \brief Optional transformation callback */
|
|
|
|
sorcery_transform_handler transform;
|
|
|
|
|
|
|
|
/*! \brief Optional object set apply callback */
|
|
|
|
sorcery_apply_handler apply;
|
|
|
|
|
2013-02-10 14:58:37 +00:00
|
|
|
/*! \brief Optional object copy callback */
|
|
|
|
sorcery_copy_handler copy;
|
|
|
|
|
|
|
|
/*! \brief Optional object diff callback */
|
|
|
|
sorcery_diff_handler diff;
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
/*! \brief Wizard instances */
|
2015-05-05 20:32:08 +00:00
|
|
|
struct ast_sorcery_object_wizards wizards;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
/*! \brief Object fields */
|
|
|
|
struct ao2_container *fields;
|
|
|
|
|
|
|
|
/*! \brief Configuration framework general information */
|
|
|
|
struct aco_info *info;
|
|
|
|
|
|
|
|
/*! \brief Configuration framework file information */
|
|
|
|
struct aco_file *file;
|
|
|
|
|
|
|
|
/*! \brief Type details */
|
|
|
|
struct aco_type type;
|
2013-05-06 13:04:08 +00:00
|
|
|
|
|
|
|
/*! \brief Observers */
|
|
|
|
struct ao2_container *observers;
|
|
|
|
|
|
|
|
/*! \brief Serializer for observers */
|
|
|
|
struct ast_taskprocessor *serializer;
|
2013-08-30 19:55:56 +00:00
|
|
|
|
|
|
|
/*! \brief Specifies if object type is reloadable or not */
|
|
|
|
unsigned int reloadable:1;
|
2013-05-06 13:04:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief Structure for registered object type observer */
|
|
|
|
struct ast_sorcery_object_type_observer {
|
|
|
|
/*! \brief Pointer to the observer implementation */
|
|
|
|
const struct ast_sorcery_observer *callbacks;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief Structure used for observer invocations */
|
|
|
|
struct sorcery_observer_invocation {
|
|
|
|
/*! \brief Pointer to the object type */
|
|
|
|
struct ast_sorcery_object_type *object_type;
|
|
|
|
|
|
|
|
/*! \brief Pointer to the object */
|
|
|
|
void *object;
|
2013-01-25 14:01:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief Structure for registered object field */
|
|
|
|
struct ast_sorcery_object_field {
|
|
|
|
/*! \brief Name of the field */
|
|
|
|
char name[MAX_OBJECT_FIELD];
|
|
|
|
|
2014-11-21 17:49:39 +00:00
|
|
|
/*! \brief The compiled name regex if name is a regex */
|
|
|
|
regex_t *name_regex;
|
|
|
|
|
2013-03-01 18:01:56 +00:00
|
|
|
/*! \brief Callback function for translation of a single value */
|
2013-01-25 14:01:04 +00:00
|
|
|
sorcery_field_handler handler;
|
|
|
|
|
2013-03-01 18:01:56 +00:00
|
|
|
/*! \brief Callback function for translation of multiple values */
|
|
|
|
sorcery_fields_handler multiple_handler;
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
/*! \brief Position of the field */
|
|
|
|
intptr_t args[];
|
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief Full structure for sorcery */
|
|
|
|
struct ast_sorcery {
|
|
|
|
/*! \brief Container for known object types */
|
|
|
|
struct ao2_container *types;
|
2014-12-05 17:08:27 +00:00
|
|
|
|
|
|
|
/*! \brief Observers */
|
|
|
|
struct ao2_container *observers;
|
|
|
|
|
2014-02-20 20:45:30 +00:00
|
|
|
/*! \brief The name of the module owning this sorcery instance */
|
|
|
|
char module_name[0];
|
2013-01-25 14:01:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*! \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 */
|
2014-02-20 20:45:30 +00:00
|
|
|
static struct ao2_container *wizards;
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
/* The following 3 observer wrappers must name their
|
|
|
|
* external observer 'callbacks' and it must be
|
|
|
|
* the first member of the structure. Common macros
|
|
|
|
* and container callbacks depend on it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \brief A global observer wrapper */
|
|
|
|
struct sorcery_global_observer {
|
|
|
|
const struct ast_sorcery_global_observer *callbacks;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief An instance observer wrapper */
|
|
|
|
struct sorcery_instance_observer {
|
|
|
|
const struct ast_sorcery_instance_observer *callbacks;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief A wizard observer wrapper */
|
|
|
|
struct sorcery_wizard_observer {
|
|
|
|
const struct ast_sorcery_wizard_observer *callbacks;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief Registered global observers */
|
|
|
|
struct ao2_container *observers;
|
|
|
|
|
2014-02-20 20:45:30 +00:00
|
|
|
/*! \brief Registered sorcery instances */
|
|
|
|
static struct ao2_container *instances;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
static int int_handler_fn(const void *obj, const intptr_t *args, char **buf)
|
|
|
|
{
|
|
|
|
int *field = (int *)(obj + args[0]);
|
|
|
|
return (ast_asprintf(buf, "%d", *field) < 0) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uint_handler_fn(const void *obj, const intptr_t *args, char **buf)
|
|
|
|
{
|
|
|
|
unsigned int *field = (unsigned int *)(obj + args[0]);
|
|
|
|
return (ast_asprintf(buf, "%u", *field) < 0) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int double_handler_fn(const void *obj, const intptr_t *args, char **buf)
|
|
|
|
{
|
|
|
|
double *field = (double *)(obj + args[0]);
|
|
|
|
return (ast_asprintf(buf, "%f", *field) < 0) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stringfield_handler_fn(const void *obj, const intptr_t *args, char **buf)
|
|
|
|
{
|
|
|
|
ast_string_field *field = (const char **)(obj + args[0]);
|
|
|
|
return !(*buf = ast_strdup(*field)) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bool_handler_fn(const void *obj, const intptr_t *args, char **buf)
|
|
|
|
{
|
|
|
|
unsigned int *field = (unsigned int *)(obj + args[0]);
|
|
|
|
return !(*buf = ast_strdup(*field ? "true" : "false")) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2016-06-20 18:21:52 +00:00
|
|
|
static int yesno_handler_fn(const void *obj, const intptr_t *args, char **buf)
|
|
|
|
{
|
|
|
|
unsigned int *field = (unsigned int *)(obj + args[0]);
|
|
|
|
return !(*buf = ast_strdup(*field ? "yes" : "no")) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
static int sockaddr_handler_fn(const void *obj, const intptr_t *args, char **buf)
|
|
|
|
{
|
|
|
|
struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + args[0]);
|
|
|
|
return !(*buf = ast_strdup(ast_sockaddr_stringify(field))) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int chararray_handler_fn(const void *obj, const intptr_t *args, char **buf)
|
|
|
|
{
|
|
|
|
char *field = (char *)(obj + args[0]);
|
|
|
|
return !(*buf = ast_strdup(field)) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2013-12-11 12:31:57 +00:00
|
|
|
static int codec_handler_fn(const void *obj, const intptr_t *args, char **buf)
|
|
|
|
{
|
2015-11-09 09:01:41 +00:00
|
|
|
struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
|
media formats: re-architect handling of media for performance improvements
In the old times media formats were represented using a bit field. This was
fast but had a few limitations.
1. Asterisk was limited in how many formats it could handle.
2. Formats, being a bit field, could not include any attribute information.
A format was strictly its type, e.g., "this is ulaw".
This was changed in Asterisk 10 (see
https://wiki.asterisk.org/wiki/display/AST/Media+Architecture+Proposal for
notes on that work) which led to the creation of the ast_format structure.
This structure allowed Asterisk to handle attributes and bundle information
with a format.
Additionally, ast_format_cap was created to act as a container for multiple
formats that, together, formed the capability of some entity. Another
mechanism was added to allow logic to be registered which performed format
attribute negotiation. Everywhere throughout the codebase Asterisk was
changed to use this strategy.
Unfortunately, in software, there is no free lunch. These new capabilities
came at a cost.
Performance analysis and profiling showed that we spend an inordinate
amount of time comparing, copying, and generally manipulating formats and
their related structures. Basic prototyping has shown that a reasonably
large performance improvement could be made in this area. This patch is the
result of that project, which overhauled the media format architecture
and its usage in Asterisk to improve performance.
Generally, the new philosophy for handling formats is as follows:
* The ast_format structure is reference counted. This removed a large amount
of the memory allocations and copying that was done in prior versions.
* In order to prevent race conditions while keeping things performant, the
ast_format structure is immutable by convention and lock-free. Violate this
tenet at your peril!
* Because formats are reference counted, codecs are also reference counted.
The Asterisk core generally provides built-in codecs and caches the
ast_format structures created to represent them. Generally, to prevent
inordinate amounts of module reference bumping, codecs and formats can be
added at run-time but cannot be removed.
* All compatibility with the bit field representation of codecs/formats has
been moved to a compatibility API. The primary user of this representation
is chan_iax2, which must continue to maintain its bit-field usage of formats
for interoperability concerns.
* When a format is negotiated with attributes, or when a format cannot be
represented by one of the cached formats, a new format object is created or
cloned from an existing format. That format may have the same codec
underlying it, but is a different format than a version of the format with
different attributes or without attributes.
* While formats are reference counted objects, the reference count maintained
on the format should be manipulated with care. Formats are generally cached
and will persist for the lifetime of Asterisk and do not explicitly need
to have their lifetime modified. An exception to this is when the user of a
format does not know where the format came from *and* the user may outlive
the provider of the format. This occurs, for example, when a format is read
from a channel: the channel may have a format with attributes (hence,
non-cached) and the user of the format may last longer than the channel (if
the reference to the channel is released prior to the format's reference).
For more information on this work, see the API design notes:
https://wiki.asterisk.org/wiki/display/AST/Media+Format+Rewrite
Finally, this work was the culmination of a large number of developer's
efforts. Extra thanks goes to Corey Farrell, who took on a large amount of the
work in the Asterisk core, chan_sip, and was an invaluable resource in peer
reviews throughout this project.
There were a substantial number of patches contributed during this work; the
following issues/patch names simply reflect some of the work (and will cause
the release scripts to give attribution to the individuals who work on them).
Reviews:
https://reviewboard.asterisk.org/r/3814
https://reviewboard.asterisk.org/r/3808
https://reviewboard.asterisk.org/r/3805
https://reviewboard.asterisk.org/r/3803
https://reviewboard.asterisk.org/r/3801
https://reviewboard.asterisk.org/r/3798
https://reviewboard.asterisk.org/r/3800
https://reviewboard.asterisk.org/r/3794
https://reviewboard.asterisk.org/r/3793
https://reviewboard.asterisk.org/r/3792
https://reviewboard.asterisk.org/r/3791
https://reviewboard.asterisk.org/r/3790
https://reviewboard.asterisk.org/r/3789
https://reviewboard.asterisk.org/r/3788
https://reviewboard.asterisk.org/r/3787
https://reviewboard.asterisk.org/r/3786
https://reviewboard.asterisk.org/r/3784
https://reviewboard.asterisk.org/r/3783
https://reviewboard.asterisk.org/r/3778
https://reviewboard.asterisk.org/r/3774
https://reviewboard.asterisk.org/r/3775
https://reviewboard.asterisk.org/r/3772
https://reviewboard.asterisk.org/r/3761
https://reviewboard.asterisk.org/r/3754
https://reviewboard.asterisk.org/r/3753
https://reviewboard.asterisk.org/r/3751
https://reviewboard.asterisk.org/r/3750
https://reviewboard.asterisk.org/r/3748
https://reviewboard.asterisk.org/r/3747
https://reviewboard.asterisk.org/r/3746
https://reviewboard.asterisk.org/r/3742
https://reviewboard.asterisk.org/r/3740
https://reviewboard.asterisk.org/r/3739
https://reviewboard.asterisk.org/r/3738
https://reviewboard.asterisk.org/r/3737
https://reviewboard.asterisk.org/r/3736
https://reviewboard.asterisk.org/r/3734
https://reviewboard.asterisk.org/r/3722
https://reviewboard.asterisk.org/r/3713
https://reviewboard.asterisk.org/r/3703
https://reviewboard.asterisk.org/r/3689
https://reviewboard.asterisk.org/r/3687
https://reviewboard.asterisk.org/r/3674
https://reviewboard.asterisk.org/r/3671
https://reviewboard.asterisk.org/r/3667
https://reviewboard.asterisk.org/r/3665
https://reviewboard.asterisk.org/r/3625
https://reviewboard.asterisk.org/r/3602
https://reviewboard.asterisk.org/r/3519
https://reviewboard.asterisk.org/r/3518
https://reviewboard.asterisk.org/r/3516
https://reviewboard.asterisk.org/r/3515
https://reviewboard.asterisk.org/r/3512
https://reviewboard.asterisk.org/r/3506
https://reviewboard.asterisk.org/r/3413
https://reviewboard.asterisk.org/r/3410
https://reviewboard.asterisk.org/r/3387
https://reviewboard.asterisk.org/r/3388
https://reviewboard.asterisk.org/r/3389
https://reviewboard.asterisk.org/r/3390
https://reviewboard.asterisk.org/r/3321
https://reviewboard.asterisk.org/r/3320
https://reviewboard.asterisk.org/r/3319
https://reviewboard.asterisk.org/r/3318
https://reviewboard.asterisk.org/r/3266
https://reviewboard.asterisk.org/r/3265
https://reviewboard.asterisk.org/r/3234
https://reviewboard.asterisk.org/r/3178
ASTERISK-23114 #close
Reported by: mjordan
media_formats_translation_core.diff uploaded by kharwell (License 6464)
rb3506.diff uploaded by mjordan (License 6283)
media_format_app_file.diff uploaded by kharwell (License 6464)
misc-2.diff uploaded by file (License 5000)
chan_mild-3.diff uploaded by file (License 5000)
chan_obscure.diff uploaded by file (License 5000)
jingle.diff uploaded by file (License 5000)
funcs.diff uploaded by file (License 5000)
formats.diff uploaded by file (License 5000)
core.diff uploaded by file (License 5000)
bridges.diff uploaded by file (License 5000)
mf-codecs-2.diff uploaded by file (License 5000)
mf-app_fax.diff uploaded by file (License 5000)
mf-apps-3.diff uploaded by file (License 5000)
media-formats-3.diff uploaded by file (License 5000)
ASTERISK-23715
rb3713.patch uploaded by coreyfarrell (License 5909)
rb3689.patch uploaded by mjordan (License 6283)
ASTERISK-23957
rb3722.patch uploaded by mjordan (License 6283)
mf-attributes-3.diff uploaded by file (License 5000)
ASTERISK-23958
Tested by: jrose
rb3822.patch uploaded by coreyfarrell (License 5909)
rb3800.patch uploaded by jrose (License 6182)
chan_sip.diff uploaded by mjordan (License 6283)
rb3747.patch uploaded by jrose (License 6182)
ASTERISK-23959 #close
Tested by: sgriepentrog, mjordan, coreyfarrell
sip_cleanup.diff uploaded by opticron (License 6273)
chan_sip_caps.diff uploaded by mjordan (License 6283)
rb3751.patch uploaded by coreyfarrell (License 5909)
chan_sip-3.diff uploaded by file (License 5000)
ASTERISK-23960 #close
Tested by: opticron
direct_media.diff uploaded by opticron (License 6273)
pjsip-direct-media.diff uploaded by file (License 5000)
format_cap_remove.diff uploaded by opticron (License 6273)
media_format_fixes.diff uploaded by opticron (License 6273)
chan_pjsip-2.diff uploaded by file (License 5000)
ASTERISK-23966 #close
Tested by: rmudgett
rb3803.patch uploaded by rmudgetti (License 5621)
chan_dahdi.diff uploaded by file (License 5000)
ASTERISK-24064 #close
Tested by: coreyfarrell, mjordan, opticron, file, rmudgett, sgriepentrog, jrose
rb3814.patch uploaded by rmudgett (License 5621)
moh_cleanup.diff uploaded by opticron (License 6273)
bridge_leak.diff uploaded by opticron (License 6273)
translate.diff uploaded by file (License 5000)
rb3795.patch uploaded by rmudgett (License 5621)
tls_fix.diff uploaded by mjordan (License 6283)
fax-mf-fix-2.diff uploaded by file (License 5000)
rtp_transfer_stuff uploaded by mjordan (License 6283)
rb3787.patch uploaded by rmudgett (License 5621)
media-formats-explicit-translate-format-3.diff uploaded by file (License 5000)
format_cache_case_fix.diff uploaded by opticron (License 6273)
rb3774.patch uploaded by rmudgett (License 5621)
rb3775.patch uploaded by rmudgett (License 5621)
rtp_engine_fix.diff uploaded by opticron (License 6273)
rtp_crash_fix.diff uploaded by opticron (License 6273)
rb3753.patch uploaded by mjordan (License 6283)
rb3750.patch uploaded by mjordan (License 6283)
rb3748.patch uploaded by rmudgett (License 5621)
media_format_fixes.diff uploaded by opticron (License 6273)
rb3740.patch uploaded by mjordan (License 6283)
rb3739.patch uploaded by mjordan (License 6283)
rb3734.patch uploaded by mjordan (License 6283)
rb3689.patch uploaded by mjordan (License 6283)
rb3674.patch uploaded by coreyfarrell (License 5909)
rb3671.patch uploaded by coreyfarrell (License 5909)
rb3667.patch uploaded by coreyfarrell (License 5909)
rb3665.patch uploaded by mjordan (License 6283)
rb3625.patch uploaded by coreyfarrell (License 5909)
rb3602.patch uploaded by coreyfarrell (License 5909)
format_compatibility-2.diff uploaded by file (License 5000)
core.diff uploaded by file (License 5000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@419044 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-07-20 22:06:33 +00:00
|
|
|
struct ast_format_cap **cap = (struct ast_format_cap **)(obj + args[0]);
|
|
|
|
return !(*buf = ast_strdup(ast_format_cap_get_names(*cap, &codec_buf)));
|
2013-12-11 12:31:57 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type type)
|
|
|
|
{
|
|
|
|
switch(type) {
|
|
|
|
case OPT_BOOL_T: return bool_handler_fn;
|
2016-06-20 18:21:52 +00:00
|
|
|
case OPT_YESNO_T: return yesno_handler_fn;
|
2013-01-25 14:01:04 +00:00
|
|
|
case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
|
2013-12-11 12:31:57 +00:00
|
|
|
case OPT_CODEC_T: return codec_handler_fn;
|
2013-01-25 14:01:04 +00:00
|
|
|
case OPT_DOUBLE_T: return double_handler_fn;
|
|
|
|
case OPT_INT_T: return int_handler_fn;
|
|
|
|
case OPT_SOCKADDR_T: return sockaddr_handler_fn;
|
|
|
|
case OPT_STRINGFIELD_T: return stringfield_handler_fn;
|
|
|
|
case OPT_UINT_T: return uint_handler_fn;
|
|
|
|
|
|
|
|
default:
|
|
|
|
case OPT_CUSTOM_T: return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Hashing function for sorcery wizards */
|
|
|
|
static int sorcery_wizard_hash(const void *obj, const int flags)
|
|
|
|
{
|
2014-12-05 17:08:27 +00:00
|
|
|
const struct ast_sorcery_internal_wizard *object;
|
2014-03-07 21:23:39 +00:00
|
|
|
const char *key;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2014-03-07 21:23:39 +00:00
|
|
|
switch (flags & OBJ_SEARCH_MASK) {
|
|
|
|
case OBJ_SEARCH_KEY:
|
|
|
|
key = obj;
|
|
|
|
break;
|
|
|
|
case OBJ_SEARCH_OBJECT:
|
|
|
|
object = obj;
|
2014-12-05 17:08:27 +00:00
|
|
|
key = object->callbacks.name;
|
2014-03-07 21:23:39 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ast_assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ast_str_hash(key);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Comparator function for sorcery wizards */
|
|
|
|
static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
|
|
|
|
{
|
2014-12-05 17:08:27 +00:00
|
|
|
const struct ast_sorcery_internal_wizard *object_left = obj;
|
|
|
|
const struct ast_sorcery_internal_wizard *object_right = arg;
|
2014-02-20 20:45:30 +00:00
|
|
|
const char *right_key = arg;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
switch (flags & OBJ_SEARCH_MASK) {
|
|
|
|
case OBJ_SEARCH_OBJECT:
|
2014-12-05 17:08:27 +00:00
|
|
|
right_key = object_right->callbacks.name;
|
2014-02-20 20:45:30 +00:00
|
|
|
/* Fall through */
|
|
|
|
case OBJ_SEARCH_KEY:
|
2014-12-05 17:08:27 +00:00
|
|
|
cmp = strcmp(object_left->callbacks.name, right_key);
|
2014-02-20 20:45:30 +00:00
|
|
|
break;
|
|
|
|
case OBJ_SEARCH_PARTIAL_KEY:
|
2014-12-05 17:08:27 +00:00
|
|
|
cmp = strncmp(object_left->callbacks.name, right_key, strlen(right_key));
|
2014-02-20 20:45:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cmp = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cmp) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return CMP_MATCH;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 21:23:39 +00:00
|
|
|
/*! \brief Hashing function for sorcery wizards */
|
|
|
|
static int object_type_field_hash(const void *obj, const int flags)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_field *object_field;
|
|
|
|
const char *key;
|
|
|
|
|
|
|
|
switch (flags & OBJ_SEARCH_MASK) {
|
|
|
|
case OBJ_SEARCH_KEY:
|
|
|
|
key = obj;
|
|
|
|
break;
|
|
|
|
case OBJ_SEARCH_OBJECT:
|
|
|
|
object_field = obj;
|
|
|
|
key = object_field->name;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ast_assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ast_str_hash(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int object_type_field_cmp(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_field *field_left = obj;
|
|
|
|
const struct ast_sorcery_object_field *field_right = arg;
|
|
|
|
const char *right_key = arg;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
switch (flags & OBJ_SEARCH_MASK) {
|
|
|
|
case OBJ_SEARCH_OBJECT:
|
|
|
|
right_key = field_right->name;
|
|
|
|
/* Fall through */
|
|
|
|
case OBJ_SEARCH_KEY:
|
|
|
|
cmp = strcmp(field_left->name, right_key);
|
|
|
|
break;
|
|
|
|
case OBJ_SEARCH_PARTIAL_KEY:
|
|
|
|
cmp = strncmp(field_left->name, right_key, strlen(right_key));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cmp = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cmp) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return CMP_MATCH;
|
|
|
|
}
|
|
|
|
|
2013-06-24 23:56:54 +00:00
|
|
|
/*! \brief Cleanup function for graceful shutdowns */
|
|
|
|
static void sorcery_cleanup(void)
|
|
|
|
{
|
2015-03-26 22:24:26 +00:00
|
|
|
ast_threadpool_shutdown(threadpool);
|
|
|
|
threadpool = NULL;
|
2013-06-24 23:56:54 +00:00
|
|
|
ao2_cleanup(wizards);
|
2014-02-20 20:45:30 +00:00
|
|
|
wizards = NULL;
|
2014-12-05 17:08:27 +00:00
|
|
|
ao2_cleanup(observers);
|
|
|
|
observers = NULL;
|
2014-02-20 20:45:30 +00:00
|
|
|
ao2_cleanup(instances);
|
|
|
|
instances = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Compare function for sorcery instances */
|
|
|
|
static int sorcery_instance_cmp(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery *object_left = obj;
|
|
|
|
const struct ast_sorcery *object_right = arg;
|
|
|
|
const char *right_key = arg;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
switch (flags & OBJ_SEARCH_MASK) {
|
|
|
|
case OBJ_SEARCH_OBJECT:
|
|
|
|
right_key = object_right->module_name;
|
|
|
|
/* Fall through */
|
|
|
|
case OBJ_SEARCH_KEY:
|
|
|
|
cmp = strcmp(object_left->module_name, right_key);
|
|
|
|
break;
|
|
|
|
case OBJ_SEARCH_PARTIAL_KEY:
|
|
|
|
cmp = strncmp(object_left->module_name, right_key, strlen(right_key));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cmp = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cmp) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return CMP_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Hashing function for sorcery instances */
|
|
|
|
static int sorcery_instance_hash(const void *obj, const int flags)
|
|
|
|
{
|
2014-03-07 21:23:39 +00:00
|
|
|
const struct ast_sorcery *object;
|
|
|
|
const char *key;
|
2014-02-20 20:45:30 +00:00
|
|
|
|
2014-03-07 21:23:39 +00:00
|
|
|
switch (flags & OBJ_SEARCH_MASK) {
|
|
|
|
case OBJ_SEARCH_KEY:
|
|
|
|
key = obj;
|
|
|
|
break;
|
|
|
|
case OBJ_SEARCH_OBJECT:
|
|
|
|
object = obj;
|
|
|
|
key = object->module_name;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ast_assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ast_str_hash(key);
|
2013-06-24 23:56:54 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
int ast_sorcery_init(void)
|
|
|
|
{
|
2013-05-06 13:04:08 +00:00
|
|
|
struct ast_threadpool_options options = {
|
|
|
|
.version = AST_THREADPOOL_OPTIONS_VERSION,
|
|
|
|
.auto_increment = 1,
|
|
|
|
.max_size = 0,
|
|
|
|
.idle_timeout = 60,
|
|
|
|
.initial_size = 0,
|
|
|
|
};
|
2013-01-25 14:01:04 +00:00
|
|
|
ast_assert(wizards == NULL);
|
|
|
|
|
2013-05-06 13:04:08 +00:00
|
|
|
if (!(threadpool = ast_threadpool_create("Sorcery", NULL, &options))) {
|
2013-06-24 23:56:54 +00:00
|
|
|
threadpool = NULL;
|
2013-05-06 13:04:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
|
2013-05-06 13:04:08 +00:00
|
|
|
ast_threadpool_shutdown(threadpool);
|
2013-01-25 14:01:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, NULL, NULL);
|
|
|
|
if (!observers) {
|
|
|
|
sorcery_cleanup();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-02-20 20:45:30 +00:00
|
|
|
instances = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, INSTANCE_BUCKETS,
|
|
|
|
sorcery_instance_hash, sorcery_instance_cmp);
|
|
|
|
if (!instances) {
|
|
|
|
sorcery_cleanup();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-24 23:56:54 +00:00
|
|
|
ast_register_cleanup(sorcery_cleanup);
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
static void sorcery_internal_wizard_destructor(void *obj)
|
|
|
|
{
|
|
|
|
struct ast_sorcery_internal_wizard *wizard = obj;
|
|
|
|
|
|
|
|
ao2_cleanup(wizard->observers);
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
|
|
|
|
{
|
2014-12-05 17:08:27 +00:00
|
|
|
struct ast_sorcery_internal_wizard *wizard;
|
2013-01-25 14:01:04 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
if (!(wizard = ao2_alloc(sizeof(*wizard), sorcery_internal_wizard_destructor))) {
|
2013-01-25 14:01:04 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
wizard->callbacks = *interface;
|
|
|
|
wizard->callbacks.module = module;
|
|
|
|
|
|
|
|
wizard->observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, NULL, NULL);
|
|
|
|
if (!wizard->observers) {
|
|
|
|
goto done;
|
|
|
|
}
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
ao2_link_flags(wizards, wizard, OBJ_NOLOCK);
|
|
|
|
res = 0;
|
|
|
|
|
|
|
|
ast_verb(2, "Sorcery registered wizard '%s'\n", interface->name);
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
NOTIFY_GLOBAL_OBSERVERS(observers, wizard_registered,
|
|
|
|
interface->name, interface);
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
done:
|
|
|
|
ao2_cleanup(wizard);
|
|
|
|
ao2_unlock(wizards);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
|
|
|
|
{
|
2014-12-05 17:08:27 +00:00
|
|
|
struct ast_sorcery_internal_wizard *wizard =
|
|
|
|
interface ? ao2_find(wizards, interface->name, OBJ_SEARCH_KEY) : NULL;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
if (wizard) {
|
2014-12-05 17:08:27 +00:00
|
|
|
NOTIFY_GLOBAL_OBSERVERS(observers, wizard_unregistering, wizard->callbacks.name, &wizard->callbacks);
|
|
|
|
ao2_unlink(wizards, wizard);
|
|
|
|
ao2_ref(wizard, -1);
|
2013-01-25 14:01:04 +00:00
|
|
|
ast_verb(2, "Sorcery unregistered wizard '%s'\n", interface->name);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
/*! \brief Internal callback function for removing a generic observer */
|
|
|
|
static int sorcery_generic_observer_remove(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
const struct sorcery_global_observer *observer = obj;
|
|
|
|
|
|
|
|
return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sorcery_global_observer_add(const struct ast_sorcery_global_observer *callbacks)
|
|
|
|
{
|
|
|
|
struct sorcery_global_observer *cb;
|
|
|
|
|
|
|
|
cb = ao2_alloc(sizeof(*cb), NULL);
|
|
|
|
if (!cb) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb->callbacks = callbacks;
|
|
|
|
ao2_link(observers, cb);
|
|
|
|
ao2_ref(cb, -1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_sorcery_global_observer_remove(
|
|
|
|
const struct ast_sorcery_global_observer *callbacks)
|
|
|
|
{
|
|
|
|
ao2_callback(observers, OBJ_NODATA | OBJ_UNLINK, sorcery_generic_observer_remove, (void *)callbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sorcery_instance_observer_add(struct ast_sorcery *sorcery,
|
|
|
|
const struct ast_sorcery_instance_observer *callbacks)
|
|
|
|
{
|
|
|
|
struct sorcery_instance_observer *cb;
|
|
|
|
|
|
|
|
cb = ao2_alloc(sizeof(*cb), NULL);
|
|
|
|
if (!cb) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb->callbacks = callbacks;
|
|
|
|
ao2_link(sorcery->observers, cb);
|
|
|
|
ao2_ref(cb, -1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_sorcery_instance_observer_remove(struct ast_sorcery *sorcery,
|
|
|
|
const struct ast_sorcery_instance_observer *callbacks)
|
|
|
|
{
|
|
|
|
ao2_callback(sorcery->observers, OBJ_NODATA | OBJ_UNLINK, sorcery_generic_observer_remove, (void *)callbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sorcery_wizard_observer_add(struct ast_sorcery_wizard *interface,
|
|
|
|
const struct ast_sorcery_wizard_observer *callbacks)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_internal_wizard *, wizard,
|
|
|
|
interface ? ao2_find(wizards, interface->name, OBJ_SEARCH_KEY) : NULL,
|
|
|
|
ao2_cleanup);
|
|
|
|
|
|
|
|
if (wizard) {
|
|
|
|
struct sorcery_wizard_observer *cb;
|
|
|
|
|
|
|
|
cb = ao2_alloc(sizeof(*cb), NULL);
|
|
|
|
if (!cb) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb->callbacks = callbacks;
|
|
|
|
ao2_link(wizard->observers, cb);
|
|
|
|
ao2_ref(cb, -1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_sorcery_wizard_observer_remove(struct ast_sorcery_wizard *interface,
|
|
|
|
const struct ast_sorcery_wizard_observer *callbacks)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_internal_wizard *, wizard,
|
|
|
|
interface ? ao2_find(wizards, interface->name, OBJ_SEARCH_KEY) : NULL,
|
|
|
|
ao2_cleanup);
|
|
|
|
|
|
|
|
if (wizard) {
|
|
|
|
ao2_callback(wizard->observers, OBJ_NODATA | OBJ_UNLINK, sorcery_generic_observer_remove, (void *)callbacks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
/*! \brief Destructor called when sorcery structure is destroyed */
|
|
|
|
static void sorcery_destructor(void *obj)
|
|
|
|
{
|
|
|
|
struct ast_sorcery *sorcery = obj;
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
if (sorcery->observers) {
|
|
|
|
NOTIFY_GLOBAL_OBSERVERS(observers, instance_destroying, sorcery->module_name, sorcery);
|
|
|
|
}
|
|
|
|
ao2_cleanup(sorcery->observers);
|
2013-01-25 14:01:04 +00:00
|
|
|
ao2_cleanup(sorcery->types);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Hashing function for sorcery types */
|
|
|
|
static int sorcery_type_hash(const void *obj, const int flags)
|
|
|
|
{
|
2014-03-07 21:23:39 +00:00
|
|
|
const struct ast_sorcery_object_type *object;
|
|
|
|
const char *key;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2014-03-07 21:23:39 +00:00
|
|
|
switch (flags & OBJ_SEARCH_MASK) {
|
|
|
|
case OBJ_SEARCH_KEY:
|
|
|
|
key = obj;
|
|
|
|
break;
|
|
|
|
case OBJ_SEARCH_OBJECT:
|
|
|
|
object = obj;
|
|
|
|
key = object->name;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ast_assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ast_str_hash(key);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Comparator function for sorcery types */
|
|
|
|
static int sorcery_type_cmp(void *obj, void *arg, int flags)
|
|
|
|
{
|
2014-02-20 20:45:30 +00:00
|
|
|
const struct ast_sorcery_object_type *object_left = obj;
|
|
|
|
const struct ast_sorcery_object_type *object_right = arg;
|
|
|
|
const char *right_key = arg;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
switch (flags & OBJ_SEARCH_MASK) {
|
|
|
|
case OBJ_SEARCH_OBJECT:
|
|
|
|
right_key = object_right->name;
|
|
|
|
/* Fall through */
|
|
|
|
case OBJ_SEARCH_KEY:
|
|
|
|
cmp = strcmp(object_left->name, right_key);
|
|
|
|
break;
|
|
|
|
case OBJ_SEARCH_PARTIAL_KEY:
|
|
|
|
cmp = strncmp(object_left->name, right_key, strlen(right_key));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cmp = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cmp) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return CMP_MATCH;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 20:45:30 +00:00
|
|
|
struct ast_sorcery *__ast_sorcery_open(const char *module_name)
|
2013-01-25 14:01:04 +00:00
|
|
|
{
|
|
|
|
struct ast_sorcery *sorcery;
|
|
|
|
|
2014-02-20 20:45:30 +00:00
|
|
|
ast_assert(module_name != NULL);
|
|
|
|
|
|
|
|
ao2_wrlock(instances);
|
|
|
|
if ((sorcery = ao2_find(instances, module_name, OBJ_SEARCH_KEY | OBJ_NOLOCK))) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(sorcery = ao2_alloc(sizeof(*sorcery) + strlen(module_name) + 1, sorcery_destructor))) {
|
|
|
|
goto done;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2013-02-10 14:58:37 +00:00
|
|
|
if (!(sorcery->types = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, TYPE_BUCKETS, sorcery_type_hash, sorcery_type_cmp))) {
|
2013-01-25 14:01:04 +00:00
|
|
|
ao2_ref(sorcery, -1);
|
|
|
|
sorcery = NULL;
|
2014-02-20 20:45:30 +00:00
|
|
|
goto done;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
if (!(sorcery->observers = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, NULL, NULL))) {
|
|
|
|
ao2_ref(sorcery, -1);
|
|
|
|
sorcery = NULL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2014-02-20 20:45:30 +00:00
|
|
|
strcpy(sorcery->module_name, module_name); /* Safe */
|
2014-04-02 18:57:29 +00:00
|
|
|
|
|
|
|
if (__ast_sorcery_apply_config(sorcery, module_name, module_name) == AST_SORCERY_APPLY_FAIL) {
|
|
|
|
ast_log(LOG_ERROR, "Error attempting to apply configuration %s to sorcery.\n", module_name);
|
|
|
|
ao2_cleanup(sorcery);
|
|
|
|
sorcery = NULL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2014-02-20 20:45:30 +00:00
|
|
|
ao2_link_flags(instances, sorcery, OBJ_NOLOCK);
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
NOTIFY_GLOBAL_OBSERVERS(observers, instance_created, module_name, sorcery);
|
|
|
|
|
2014-02-20 20:45:30 +00:00
|
|
|
done:
|
|
|
|
ao2_unlock(instances);
|
2013-01-25 14:01:04 +00:00
|
|
|
return sorcery;
|
|
|
|
}
|
|
|
|
|
2014-02-20 20:45:30 +00:00
|
|
|
/*! \brief Search function for sorcery instances */
|
|
|
|
struct ast_sorcery *ast_sorcery_retrieve_by_module_name(const char *module_name)
|
|
|
|
{
|
|
|
|
return ao2_find(instances, module_name, OBJ_SEARCH_KEY);
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
/*! \brief Destructor function for object types */
|
|
|
|
static void sorcery_object_type_destructor(void *obj)
|
|
|
|
{
|
|
|
|
struct ast_sorcery_object_type *object_type = obj;
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_WRLOCK(&object_type->wizards);
|
|
|
|
AST_VECTOR_CALLBACK_VOID(&object_type->wizards, ao2_cleanup);
|
|
|
|
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
|
|
|
|
AST_VECTOR_RW_FREE(&object_type->wizards);
|
2013-01-25 14:01:04 +00:00
|
|
|
ao2_cleanup(object_type->fields);
|
2013-05-06 13:04:08 +00:00
|
|
|
ao2_cleanup(object_type->observers);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
if (object_type->info) {
|
|
|
|
aco_info_destroy(object_type->info);
|
|
|
|
ast_free(object_type->info);
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_free(object_type->file);
|
2013-05-06 13:04:08 +00:00
|
|
|
|
|
|
|
ast_taskprocessor_unreference(object_type->serializer);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Internal function which allocates an object type structure */
|
2013-02-15 18:44:24 +00:00
|
|
|
static struct ast_sorcery_object_type *sorcery_object_type_alloc(const char *type, const char *module)
|
2013-01-25 14:01:04 +00:00
|
|
|
{
|
2015-05-05 20:32:08 +00:00
|
|
|
#define INITIAL_WIZARD_VECTOR_SIZE 5
|
2013-01-25 14:01:04 +00:00
|
|
|
struct ast_sorcery_object_type *object_type;
|
2016-01-07 01:09:43 +00:00
|
|
|
char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
if (!(object_type = ao2_alloc(sizeof(*object_type), sorcery_object_type_destructor))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Order matters for object wizards */
|
2015-05-05 20:32:08 +00:00
|
|
|
if (AST_VECTOR_RW_INIT(&object_type->wizards, INITIAL_WIZARD_VECTOR_SIZE) != 0) {
|
2013-01-25 14:01:04 +00:00
|
|
|
ao2_ref(object_type, -1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-07 21:23:39 +00:00
|
|
|
if (!(object_type->fields = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, OBJECT_FIELD_BUCKETS,
|
|
|
|
object_type_field_hash, object_type_field_cmp))) {
|
2013-01-25 14:01:04 +00:00
|
|
|
ao2_ref(object_type, -1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-06 13:04:08 +00:00
|
|
|
if (!(object_type->observers = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, 1, NULL, NULL))) {
|
|
|
|
ao2_ref(object_type, -1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
if (!(object_type->info = ast_calloc(1, sizeof(*object_type->info) + 2 * sizeof(object_type->info->files[0])))) {
|
|
|
|
ao2_ref(object_type, -1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(object_type->file = ast_calloc(1, sizeof(*object_type->file) + 2 * sizeof(object_type->file->types[0])))) {
|
|
|
|
ao2_ref(object_type, -1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-07 01:09:43 +00:00
|
|
|
/* Create name with seq number appended. */
|
|
|
|
ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "sorcery/%s", type);
|
2013-05-06 13:04:08 +00:00
|
|
|
|
2016-01-07 01:09:43 +00:00
|
|
|
if (!(object_type->serializer = ast_threadpool_serializer(tps_name, threadpool))) {
|
2013-05-06 13:04:08 +00:00
|
|
|
ao2_ref(object_type, -1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
object_type->info->files[0] = object_type->file;
|
|
|
|
object_type->info->files[1] = NULL;
|
2013-02-15 18:44:24 +00:00
|
|
|
object_type->info->module = module;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
ast_copy_string(object_type->name, type, sizeof(object_type->name));
|
|
|
|
|
|
|
|
return object_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Object wizard destructor */
|
|
|
|
static void sorcery_object_wizard_destructor(void *obj)
|
|
|
|
{
|
|
|
|
struct ast_sorcery_object_wizard *object_wizard = obj;
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
if (object_wizard->data && object_wizard->wizard->callbacks.close) {
|
2014-12-05 17:08:27 +00:00
|
|
|
object_wizard->wizard->callbacks.close(object_wizard->data);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (object_wizard->wizard) {
|
2014-12-05 17:08:27 +00:00
|
|
|
ast_module_unref(object_wizard->wizard->callbacks.module);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
2014-03-27 14:21:15 +00:00
|
|
|
|
|
|
|
ao2_cleanup(object_wizard->wizard);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
/*! \brief Return the number of wizards mapped to an object type */
|
|
|
|
int ast_sorcery_get_wizard_mapping_count(struct ast_sorcery *sorcery,
|
|
|
|
const char *type)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AST_VECTOR_SIZE(&object_type->wizards);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sorcery_get_wizard_mapping(struct ast_sorcery *sorcery,
|
|
|
|
const char *type, int index, struct ast_sorcery_wizard **wizard, void **data)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
struct ast_sorcery_object_wizard *owizard;
|
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index < 0 || index >= AST_VECTOR_SIZE(&object_type->wizards)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
owizard = AST_VECTOR_GET(&object_type->wizards, index);
|
|
|
|
|
|
|
|
if (wizard != NULL) {
|
|
|
|
*wizard = &(owizard->wizard->callbacks);
|
|
|
|
ao2_bump(owizard->wizard);
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data != NULL) {
|
|
|
|
*data = owizard->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Internal function removes a wizard mapping */
|
|
|
|
int __ast_sorcery_remove_wizard_mapping(struct ast_sorcery *sorcery,
|
|
|
|
const char *type, const char *module, const char *name)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_VECTOR_RW_WRLOCK(&object_type->wizards);
|
|
|
|
#define WIZARD_NAME_COMPARE(a, b) (strcmp((a)->wizard->callbacks.name, (b)) == 0)
|
|
|
|
res = AST_VECTOR_REMOVE_CMP_ORDERED(&object_type->wizards, name, WIZARD_NAME_COMPARE, ao2_cleanup);
|
|
|
|
#undef WIZARD_NAME_COMPARE
|
|
|
|
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Internal function which creates an object type and inserts a wizard mapping */
|
|
|
|
enum ast_sorcery_apply_result __ast_sorcery_insert_wizard_mapping(struct ast_sorcery *sorcery,
|
|
|
|
const char *type, const char *module, const char *name, const char *data,
|
|
|
|
unsigned int caching, int position)
|
2013-01-25 14:01:04 +00:00
|
|
|
{
|
2013-12-09 18:14:41 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
2014-12-05 17:08:27 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_internal_wizard *, wizard, ao2_find(wizards, name, OBJ_KEY), ao2_cleanup);
|
2013-01-25 14:01:04 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, ao2_alloc(sizeof(*object_wizard), sorcery_object_wizard_destructor), ao2_cleanup);
|
|
|
|
int created = 0;
|
|
|
|
|
2015-02-14 19:46:09 +00:00
|
|
|
if (!wizard) {
|
|
|
|
ast_log(LOG_ERROR, "Wizard '%s' could not be applied to object type '%s' as it was not found\n",
|
|
|
|
name, type);
|
|
|
|
return AST_SORCERY_APPLY_FAIL;
|
|
|
|
} else if (!object_wizard) {
|
2014-04-02 18:57:29 +00:00
|
|
|
return AST_SORCERY_APPLY_FAIL;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!object_type) {
|
2013-02-15 18:44:24 +00:00
|
|
|
if (!(object_type = sorcery_object_type_alloc(type, module))) {
|
2014-04-02 18:57:29 +00:00
|
|
|
return AST_SORCERY_APPLY_FAIL;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
created = 1;
|
|
|
|
}
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_WRLOCK(&object_type->wizards);
|
2014-04-02 18:57:29 +00:00
|
|
|
if (!created) {
|
2015-05-05 20:32:08 +00:00
|
|
|
struct ast_sorcery_object_wizard *found;
|
2014-04-02 18:57:29 +00:00
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
#define WIZARD_COMPARE(a, b) ((a)->wizard == (b))
|
|
|
|
found = AST_VECTOR_GET_CMP(&object_type->wizards, wizard, WIZARD_COMPARE);
|
|
|
|
#undef WIZARD_COMPARE
|
2014-04-02 18:57:29 +00:00
|
|
|
if (found) {
|
|
|
|
ast_debug(1, "Wizard %s already applied to object type %s\n",
|
2014-12-05 17:08:27 +00:00
|
|
|
wizard->callbacks.name, object_type->name);
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
|
2014-04-02 18:57:29 +00:00
|
|
|
return AST_SORCERY_APPLY_DUPLICATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 21:04:50 +00:00
|
|
|
ast_debug(5, "Calling wizard %s open callback on object type %s\n",
|
|
|
|
name, object_type->name);
|
2014-12-05 17:08:27 +00:00
|
|
|
if (wizard->callbacks.open && !(object_wizard->data = wizard->callbacks.open(data))) {
|
2015-07-01 21:04:50 +00:00
|
|
|
ast_log(LOG_WARNING, "Wizard '%s' failed to open mapping for object type '%s' with data: %s\n",
|
|
|
|
name, object_type->name, S_OR(data, ""));
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
|
2014-04-02 18:57:29 +00:00
|
|
|
return AST_SORCERY_APPLY_FAIL;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
ast_module_ref(wizard->callbacks.module);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2014-03-27 14:21:15 +00:00
|
|
|
object_wizard->wizard = ao2_bump(wizard);
|
2013-01-25 14:01:04 +00:00
|
|
|
object_wizard->caching = caching;
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
if (position == AST_SORCERY_WIZARD_POSITION_LAST) {
|
|
|
|
position = AST_VECTOR_SIZE(&object_type->wizards);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AST_VECTOR_INSERT_AT(&object_type->wizards, position, object_wizard) != 0) {
|
|
|
|
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
|
|
|
|
return AST_SORCERY_APPLY_FAIL;
|
|
|
|
}
|
|
|
|
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
|
|
|
|
ao2_bump(object_wizard);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
if (created) {
|
|
|
|
ao2_link(sorcery->types, object_type);
|
|
|
|
}
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, wizard_mapped,
|
|
|
|
sorcery->module_name, sorcery, type, &wizard->callbacks, data, object_wizard->data);
|
|
|
|
|
2014-04-02 18:57:29 +00:00
|
|
|
return AST_SORCERY_APPLY_SUCCESS;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
/*! \brief Internal function which creates an object type and adds a wizard mapping */
|
|
|
|
enum ast_sorcery_apply_result __ast_sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery,
|
|
|
|
const char *type, const char *module, const char *name, const char *data, unsigned int caching)
|
|
|
|
{
|
|
|
|
return __ast_sorcery_insert_wizard_mapping(sorcery, type, module, name, data,
|
|
|
|
caching, AST_SORCERY_WIZARD_POSITION_LAST);
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:57:29 +00:00
|
|
|
enum ast_sorcery_apply_result __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
|
2013-01-25 14:01:04 +00:00
|
|
|
{
|
|
|
|
struct ast_flags flags = { 0 };
|
|
|
|
struct ast_config *config = ast_config_load2("sorcery.conf", "sorcery", flags);
|
|
|
|
struct ast_variable *mapping;
|
2014-04-02 18:57:29 +00:00
|
|
|
int res = AST_SORCERY_APPLY_SUCCESS;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2014-04-02 18:57:29 +00:00
|
|
|
if (!config) {
|
|
|
|
return AST_SORCERY_APPLY_NO_CONFIGURATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config == CONFIG_STATUS_FILEINVALID) {
|
|
|
|
return AST_SORCERY_APPLY_FAIL;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (mapping = ast_variable_browse(config, name); mapping; mapping = mapping->next) {
|
|
|
|
RAII_VAR(char *, mapping_name, ast_strdup(mapping->name), ast_free);
|
|
|
|
RAII_VAR(char *, mapping_value, ast_strdup(mapping->value), ast_free);
|
2013-12-09 18:32:57 +00:00
|
|
|
char *options = mapping_name;
|
|
|
|
char *type = strsep(&options, "/");
|
|
|
|
char *data = mapping_value;
|
|
|
|
char *wizard = strsep(&data, ",");
|
2013-01-25 14:01:04 +00:00
|
|
|
unsigned int caching = 0;
|
|
|
|
|
2013-12-09 18:32:57 +00:00
|
|
|
/* If no object type or wizard exists just skip, nothing we can do */
|
|
|
|
if (ast_strlen_zero(type) || ast_strlen_zero(wizard)) {
|
2013-01-25 14:01:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the wizard is configured as a cache treat it as such */
|
|
|
|
if (!ast_strlen_zero(options) && strstr(options, "cache")) {
|
|
|
|
caching = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Any error immediately causes us to stop */
|
2014-12-05 17:08:27 +00:00
|
|
|
if (__ast_sorcery_apply_wizard_mapping(sorcery, type, module, wizard, data, caching) == AST_SORCERY_APPLY_FAIL) {
|
2014-04-02 18:57:29 +00:00
|
|
|
res = AST_SORCERY_APPLY_FAIL;
|
2013-01-25 14:01:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_config_destroy(config);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:57:29 +00:00
|
|
|
enum ast_sorcery_apply_result __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data)
|
2013-01-25 14:01:04 +00:00
|
|
|
{
|
2013-12-09 18:14:41 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
/* Defaults can not be added if any existing mapping exists */
|
|
|
|
if (object_type) {
|
2014-04-02 18:57:29 +00:00
|
|
|
return AST_SORCERY_APPLY_DEFAULT_UNNECESSARY;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
return __ast_sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2013-06-22 14:26:25 +00:00
|
|
|
static int sorcery_extended_config_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
|
|
|
|
{
|
|
|
|
return ast_sorcery_object_set_extended(obj, var->name, var->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sorcery_extended_fields_handler(const void *obj, struct ast_variable **fields)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_details *details = obj;
|
|
|
|
|
|
|
|
if (details->object->extended) {
|
|
|
|
*fields = ast_variables_dup(details->object->extended);
|
|
|
|
} else {
|
|
|
|
*fields = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-15 20:28:00 +00:00
|
|
|
int ast_sorcery_object_unregister(struct ast_sorcery *sorcery, const char *type)
|
|
|
|
{
|
|
|
|
struct ast_sorcery_object_type *object_type;
|
|
|
|
int res = -1;
|
|
|
|
|
|
|
|
ao2_wrlock(sorcery->types);
|
|
|
|
object_type = ao2_find(sorcery->types, type, OBJ_SEARCH_KEY | OBJ_NOLOCK);
|
|
|
|
if (object_type && object_type->type.type == ACO_ITEM) {
|
|
|
|
ao2_unlink_flags(sorcery->types, object_type, OBJ_NOLOCK);
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
ao2_unlock(sorcery->types);
|
|
|
|
|
|
|
|
/* XXX may need to add an instance unregister observer callback on success. */
|
|
|
|
|
|
|
|
ao2_cleanup(object_type);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-08-30 19:55:56 +00:00
|
|
|
int __ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type, unsigned int hidden, unsigned int reloadable, aco_type_item_alloc alloc, sorcery_transform_handler transform, sorcery_apply_handler apply)
|
2013-01-25 14:01:04 +00:00
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
|
2013-02-10 14:58:37 +00:00
|
|
|
if (!object_type || object_type->type.item_alloc) {
|
2013-01-25 14:01:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-02 12:18:50 +00:00
|
|
|
object_type->type.name = object_type->name;
|
2013-01-25 14:01:04 +00:00
|
|
|
object_type->type.type = ACO_ITEM;
|
2013-08-01 21:19:27 +00:00
|
|
|
object_type->type.category = ".?";
|
2013-01-25 14:01:04 +00:00
|
|
|
object_type->type.item_alloc = alloc;
|
2013-08-23 15:21:40 +00:00
|
|
|
object_type->type.hidden = hidden;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2013-08-30 19:55:56 +00:00
|
|
|
object_type->reloadable = reloadable;
|
2013-01-25 14:01:04 +00:00
|
|
|
object_type->transform = transform;
|
2013-01-27 17:13:22 +00:00
|
|
|
object_type->apply = apply;
|
2013-01-25 14:01:04 +00:00
|
|
|
object_type->file->types[0] = &object_type->type;
|
|
|
|
object_type->file->types[1] = NULL;
|
|
|
|
|
|
|
|
if (aco_info_init(object_type->info)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-22 14:26:25 +00:00
|
|
|
if (ast_sorcery_object_fields_register(sorcery, type, "^@", sorcery_extended_config_handler, sorcery_extended_fields_handler)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, object_type_registered,
|
|
|
|
sorcery->module_name, sorcery, type);
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-02 23:19:13 +00:00
|
|
|
int ast_sorcery_object_set_congestion_levels(struct ast_sorcery *sorcery, const char *type, long low_water, long high_water)
|
|
|
|
{
|
|
|
|
struct ast_sorcery_object_type *object_type;
|
|
|
|
int res = -1;
|
|
|
|
|
|
|
|
object_type = ao2_find(sorcery->types, type, OBJ_SEARCH_KEY);
|
|
|
|
if (object_type) {
|
|
|
|
res = ast_taskprocessor_alert_set_levels(object_type->serializer,
|
|
|
|
low_water, high_water);
|
|
|
|
ao2_ref(object_type, -1);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-02-10 14:58:37 +00:00
|
|
|
void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
object_type->copy = copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char *type, sorcery_diff_handler diff)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
object_type->diff = diff;
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:49:39 +00:00
|
|
|
static void sorcery_object_field_destructor(void *obj)
|
|
|
|
{
|
|
|
|
struct ast_sorcery_object_field *object_field = obj;
|
|
|
|
|
|
|
|
if (object_field->name_regex) {
|
|
|
|
regfree(object_field->name_regex);
|
2015-03-20 18:27:22 +00:00
|
|
|
ast_free(object_field->name_regex);
|
2014-11-21 17:49:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 18:01:56 +00:00
|
|
|
int ast_sorcery_object_fields_register(struct ast_sorcery *sorcery, const char *type, const char *regex, aco_option_handler config_handler, sorcery_fields_handler sorcery_handler)
|
|
|
|
{
|
2014-11-21 17:49:39 +00:00
|
|
|
#define MAX_REGEX_ERROR_LEN 128
|
2013-03-01 18:01:56 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
|
2014-11-21 17:49:39 +00:00
|
|
|
int rc;
|
2013-03-01 18:01:56 +00:00
|
|
|
|
2014-11-21 17:49:39 +00:00
|
|
|
if (!object_type || !object_type->type.item_alloc || !config_handler
|
|
|
|
|| !(object_field = ao2_alloc(sizeof(*object_field), sorcery_object_field_destructor))) {
|
2013-03-01 18:01:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_copy_string(object_field->name, regex, sizeof(object_field->name));
|
|
|
|
object_field->multiple_handler = sorcery_handler;
|
|
|
|
|
2014-11-21 17:49:39 +00:00
|
|
|
if (!(object_field->name_regex = ast_calloc(1, sizeof(regex_t)))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((rc = regcomp(object_field->name_regex, regex, REG_EXTENDED | REG_NOSUB))) {
|
|
|
|
char *regerr = ast_alloca(MAX_REGEX_ERROR_LEN);
|
|
|
|
regerror(rc, object_field->name_regex, regerr, MAX_REGEX_ERROR_LEN);
|
|
|
|
ast_log(LOG_ERROR, "Regular expression '%s' failed to compile: %s\n", regex, regerr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-03-01 18:01:56 +00:00
|
|
|
ao2_link(object_type->fields, object_field);
|
2013-08-23 15:21:40 +00:00
|
|
|
__aco_option_register(object_type->info, regex, ACO_REGEX, object_type->file->types, "", OPT_CUSTOM_T, config_handler, 0, 1, 0);
|
2013-03-01 18:01:56 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char *type, const char *name, const char *default_val, enum aco_option_type opt_type,
|
2014-03-07 21:11:49 +00:00
|
|
|
aco_option_handler config_handler, sorcery_field_handler sorcery_handler, sorcery_fields_handler multiple_handler, unsigned int flags, unsigned int no_doc, unsigned int alias, size_t argc, ...)
|
2013-01-25 14:01:04 +00:00
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
|
|
|
|
int pos;
|
|
|
|
va_list args;
|
|
|
|
|
2013-04-27 12:01:29 +00:00
|
|
|
if (!strcmp(type, "id") || !object_type || !object_type->type.item_alloc) {
|
2013-01-25 14:01:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sorcery_handler) {
|
|
|
|
sorcery_handler = sorcery_field_default_handler(opt_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(object_field = ao2_alloc(sizeof(*object_field) + argc * sizeof(object_field->args[0]), NULL))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_copy_string(object_field->name, name, sizeof(object_field->name));
|
|
|
|
object_field->handler = sorcery_handler;
|
2014-03-06 22:39:54 +00:00
|
|
|
object_field->multiple_handler = multiple_handler;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
va_start(args, argc);
|
|
|
|
for (pos = 0; pos < argc; pos++) {
|
|
|
|
object_field->args[pos] = va_arg(args, size_t);
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
2014-03-07 21:11:49 +00:00
|
|
|
if (!alias) {
|
|
|
|
ao2_link(object_type->fields, object_field);
|
|
|
|
}
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
/* TODO: Improve this hack */
|
|
|
|
if (!argc) {
|
2013-08-23 15:21:40 +00:00
|
|
|
__aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc);
|
2013-01-25 14:01:04 +00:00
|
|
|
} else if (argc == 1) {
|
2013-08-23 15:21:40 +00:00
|
|
|
__aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
|
2013-12-09 18:14:41 +00:00
|
|
|
object_field->args[0]);
|
2013-01-25 14:01:04 +00:00
|
|
|
} else if (argc == 2) {
|
2013-08-23 15:21:40 +00:00
|
|
|
__aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
|
2013-12-09 18:14:41 +00:00
|
|
|
object_field->args[0], object_field->args[1]);
|
2013-01-25 14:01:04 +00:00
|
|
|
} else if (argc == 3) {
|
2013-08-23 15:21:40 +00:00
|
|
|
__aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
|
2013-12-09 18:14:41 +00:00
|
|
|
object_field->args[0], object_field->args[1], object_field->args[2]);
|
2013-01-25 14:01:04 +00:00
|
|
|
} else {
|
|
|
|
ast_assert(0); /* The hack... she does us no good for this */
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-30 19:55:56 +00:00
|
|
|
/*! \brief Retrieves whether or not the type is reloadable */
|
|
|
|
static int sorcery_reloadable(const struct ast_sorcery *sorcery, const char *type)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type,
|
|
|
|
ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
return object_type && object_type->reloadable;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
static int sorcery_wizard_load(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
struct ast_sorcery_object_wizard *wizard = obj;
|
|
|
|
struct sorcery_load_details *details = arg;
|
|
|
|
void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
load = !details->reload ? wizard->wizard->callbacks.load : wizard->wizard->callbacks.reload;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
if (load) {
|
2014-12-05 17:08:27 +00:00
|
|
|
NOTIFY_WIZARD_OBSERVERS(wizard->wizard->observers, wizard_loading,
|
|
|
|
wizard->wizard->callbacks.name, &wizard->wizard->callbacks, details->type, details->reload);
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
load(wizard->data, details->sorcery, details->type);
|
2014-12-05 17:08:27 +00:00
|
|
|
|
|
|
|
NOTIFY_WIZARD_OBSERVERS(wizard->wizard->observers, wizard_loaded,
|
|
|
|
wizard->wizard->callbacks.name, &wizard->wizard->callbacks, details->type, details->reload);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-06 13:04:08 +00:00
|
|
|
/*! \brief Destructor for observer invocation */
|
|
|
|
static void sorcery_observer_invocation_destroy(void *obj)
|
|
|
|
{
|
|
|
|
struct sorcery_observer_invocation *invocation = obj;
|
|
|
|
|
|
|
|
ao2_cleanup(invocation->object_type);
|
|
|
|
ao2_cleanup(invocation->object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Allocator function for observer invocation */
|
|
|
|
static struct sorcery_observer_invocation *sorcery_observer_invocation_alloc(struct ast_sorcery_object_type *object_type, void *object)
|
|
|
|
{
|
|
|
|
struct sorcery_observer_invocation *invocation = ao2_alloc(sizeof(*invocation), sorcery_observer_invocation_destroy);
|
|
|
|
|
|
|
|
if (!invocation) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ao2_ref(object_type, +1);
|
|
|
|
invocation->object_type = object_type;
|
|
|
|
|
|
|
|
if (object) {
|
|
|
|
ao2_ref(object, +1);
|
|
|
|
invocation->object = object;
|
|
|
|
}
|
|
|
|
|
|
|
|
return invocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Internal callback function which notifies an individual observer that an object type has been loaded */
|
|
|
|
static int sorcery_observer_notify_loaded(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_type_observer *observer = obj;
|
|
|
|
|
|
|
|
if (observer->callbacks->loaded) {
|
|
|
|
observer->callbacks->loaded(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Internal callback function which notifies observers that an object type has been loaded */
|
|
|
|
static int sorcery_observers_notify_loaded(void *data)
|
|
|
|
{
|
|
|
|
struct sorcery_observer_invocation *invocation = data;
|
|
|
|
|
|
|
|
ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_loaded, invocation->object_type->name);
|
|
|
|
ao2_cleanup(invocation);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
static int sorcery_object_load(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
struct ast_sorcery_object_type *type = obj;
|
|
|
|
struct sorcery_load_details *details = arg;
|
|
|
|
|
2015-02-04 00:59:14 +00:00
|
|
|
if (!type->type.item_alloc) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
details->type = type->name;
|
2014-12-05 17:08:27 +00:00
|
|
|
|
2015-01-28 04:29:23 +00:00
|
|
|
if (details->reload && !sorcery_reloadable(details->sorcery, details->type)) {
|
|
|
|
ast_log(LOG_NOTICE, "Type '%s' is not reloadable, maintaining previous values\n",
|
|
|
|
details->type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
NOTIFY_INSTANCE_OBSERVERS(details->sorcery->observers, object_type_loading,
|
|
|
|
details->sorcery->module_name, details->sorcery, type->name, details->reload);
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_RDLOCK(&type->wizards);
|
|
|
|
AST_VECTOR_CALLBACK(&type->wizards, sorcery_wizard_load, NULL, details, 0);
|
|
|
|
AST_VECTOR_RW_UNLOCK(&type->wizards);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2015-01-28 04:29:23 +00:00
|
|
|
NOTIFY_INSTANCE_OBSERVERS(details->sorcery->observers, object_type_loaded,
|
|
|
|
details->sorcery->module_name, details->sorcery, type->name, details->reload);
|
|
|
|
|
2013-05-06 13:04:08 +00:00
|
|
|
if (ao2_container_count(type->observers)) {
|
2015-01-28 04:29:23 +00:00
|
|
|
struct sorcery_observer_invocation *invocation;
|
2013-05-06 13:04:08 +00:00
|
|
|
|
2015-01-28 04:29:23 +00:00
|
|
|
invocation = sorcery_observer_invocation_alloc(type, NULL);
|
|
|
|
if (invocation
|
|
|
|
&& ast_taskprocessor_push(type->serializer, sorcery_observers_notify_loaded,
|
|
|
|
invocation)) {
|
2013-05-06 13:04:08 +00:00
|
|
|
ao2_cleanup(invocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_sorcery_load(const struct ast_sorcery *sorcery)
|
|
|
|
{
|
|
|
|
struct sorcery_load_details details = {
|
|
|
|
.sorcery = sorcery,
|
|
|
|
.reload = 0,
|
|
|
|
};
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loading,
|
|
|
|
sorcery->module_name, sorcery, 0);
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
|
2014-12-05 17:08:27 +00:00
|
|
|
|
|
|
|
NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loaded,
|
|
|
|
sorcery->module_name, sorcery, 0);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2013-02-10 14:58:37 +00:00
|
|
|
void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
struct sorcery_load_details details = {
|
|
|
|
.sorcery = sorcery,
|
|
|
|
.reload = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sorcery_object_load(object_type, &details, 0);
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
void ast_sorcery_reload(const struct ast_sorcery *sorcery)
|
|
|
|
{
|
|
|
|
struct sorcery_load_details details = {
|
|
|
|
.sorcery = sorcery,
|
|
|
|
.reload = 1,
|
|
|
|
};
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loading,
|
|
|
|
sorcery->module_name, sorcery, 1);
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
|
2014-12-05 17:08:27 +00:00
|
|
|
|
|
|
|
NOTIFY_INSTANCE_OBSERVERS(sorcery->observers, instance_loaded,
|
|
|
|
sorcery->module_name, sorcery, 1);
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2013-02-10 14:58:37 +00:00
|
|
|
void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
struct sorcery_load_details details = {
|
|
|
|
.sorcery = sorcery,
|
|
|
|
.reload = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sorcery_object_load(object_type, &details, 0);
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
void ast_sorcery_ref(struct ast_sorcery *sorcery)
|
|
|
|
{
|
|
|
|
ao2_ref(sorcery, +1);
|
|
|
|
}
|
|
|
|
|
2014-03-06 22:39:54 +00:00
|
|
|
static struct ast_variable *get_single_field_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
|
|
|
|
{
|
|
|
|
struct ast_variable *tmp = NULL;
|
|
|
|
char *buf = NULL;
|
|
|
|
|
|
|
|
if (!object_field->handler) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(object_field->handler(object, object_field->args, &buf))) {
|
|
|
|
tmp = ast_variable_new(object_field->name, S_OR(buf, ""), "");
|
|
|
|
}
|
|
|
|
ast_free(buf);
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ast_variable *get_multiple_fields_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
|
|
|
|
{
|
|
|
|
struct ast_variable *tmp = NULL;
|
|
|
|
|
|
|
|
if (!object_field->multiple_handler) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object_field->multiple_handler(object, &tmp)) {
|
|
|
|
ast_variables_destroy(tmp);
|
|
|
|
tmp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ast_variable *ast_sorcery_objectset_create2(const struct ast_sorcery *sorcery,
|
|
|
|
const void *object, enum ast_sorcery_field_handler_flags flags)
|
2013-01-25 14:01:04 +00:00
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_details *details = object;
|
2013-06-22 14:26:25 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
|
2013-01-25 14:01:04 +00:00
|
|
|
struct ao2_iterator i;
|
|
|
|
struct ast_sorcery_object_field *object_field;
|
2014-03-06 23:47:16 +00:00
|
|
|
struct ast_variable *head = NULL;
|
|
|
|
struct ast_variable *tail = NULL;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = ao2_iterator_init(object_type->fields, 0);
|
|
|
|
|
2014-03-06 23:47:16 +00:00
|
|
|
for (; (object_field = ao2_iterator_next(&i)); ao2_ref(object_field, -1)) {
|
|
|
|
struct ast_variable *tmp;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2014-03-06 22:39:54 +00:00
|
|
|
switch (flags) {
|
|
|
|
case AST_HANDLER_PREFER_LIST:
|
|
|
|
if ((tmp = get_multiple_fields_as_var_list(object, object_field)) ||
|
|
|
|
(tmp = get_single_field_as_var_list(object, object_field))) {
|
|
|
|
break;
|
2013-03-01 18:01:56 +00:00
|
|
|
}
|
2014-03-06 22:39:54 +00:00
|
|
|
continue;
|
|
|
|
case AST_HANDLER_PREFER_STRING:
|
|
|
|
if ((tmp = get_single_field_as_var_list(object, object_field)) ||
|
|
|
|
(tmp = get_multiple_fields_as_var_list(object, object_field))) {
|
|
|
|
break;
|
2013-03-01 18:01:56 +00:00
|
|
|
}
|
2014-03-06 22:39:54 +00:00
|
|
|
continue;
|
|
|
|
case AST_HANDLER_ONLY_LIST:
|
|
|
|
if ((tmp = get_multiple_fields_as_var_list(object, object_field))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case AST_HANDLER_ONLY_STRING:
|
|
|
|
if ((tmp = get_single_field_as_var_list(object, object_field))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
default:
|
2013-01-25 14:01:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-06 22:39:54 +00:00
|
|
|
tail = ast_variable_list_append_hint(&head, tail, tmp);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ao2_iterator_destroy(&i);
|
|
|
|
|
2014-03-06 22:39:54 +00:00
|
|
|
return head;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2013-05-06 13:04:08 +00:00
|
|
|
struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_details *details = object;
|
2013-06-22 14:26:25 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
|
2013-05-06 13:04:08 +00:00
|
|
|
struct ao2_iterator i;
|
|
|
|
struct ast_sorcery_object_field *object_field;
|
|
|
|
struct ast_json *json = ast_json_object_create();
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
if (!object_type || !json) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = ao2_iterator_init(object_type->fields, 0);
|
|
|
|
|
2014-03-06 23:47:16 +00:00
|
|
|
for (; !res && (object_field = ao2_iterator_next(&i)); ao2_ref(object_field, -1)) {
|
2013-05-06 13:04:08 +00:00
|
|
|
if (object_field->multiple_handler) {
|
|
|
|
struct ast_variable *tmp = NULL;
|
|
|
|
struct ast_variable *field;
|
|
|
|
|
|
|
|
if ((res = object_field->multiple_handler(object, &tmp))) {
|
2014-03-06 23:47:16 +00:00
|
|
|
ast_variables_destroy(tmp);
|
2013-10-18 18:44:21 +00:00
|
|
|
ao2_ref(object_field, -1);
|
2013-05-06 13:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (field = tmp; field; field = field->next) {
|
|
|
|
struct ast_json *value = ast_json_string_create(field->value);
|
|
|
|
|
2014-02-21 18:04:54 +00:00
|
|
|
if (!value || ast_json_object_set(json, field->name, value)) {
|
2013-05-06 13:04:08 +00:00
|
|
|
res = -1;
|
2014-03-06 23:47:16 +00:00
|
|
|
break;
|
2013-05-06 13:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_variables_destroy(tmp);
|
|
|
|
} else if (object_field->handler) {
|
|
|
|
char *buf = NULL;
|
|
|
|
struct ast_json *value = NULL;
|
|
|
|
|
2015-07-08 21:28:13 +00:00
|
|
|
if (object_field->handler(object, object_field->args, &buf)
|
2014-02-21 18:04:54 +00:00
|
|
|
|| !(value = ast_json_string_create(buf))
|
|
|
|
|| ast_json_object_set(json, object_field->name, value)) {
|
2015-07-08 21:28:13 +00:00
|
|
|
ast_free(buf);
|
|
|
|
ast_debug(5, "Skipping field '%s' for object type '%s'\n",
|
|
|
|
object_field->name, object_type->name);
|
|
|
|
continue;
|
2013-05-06 13:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ast_free(buf);
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ao2_iterator_destroy(&i);
|
|
|
|
|
|
|
|
/* If any error occurs we destroy the JSON object so a partial objectset is not returned */
|
|
|
|
if (res) {
|
|
|
|
ast_json_unref(json);
|
|
|
|
json = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_details *details = object;
|
2013-06-22 14:26:25 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
|
2013-01-25 14:01:04 +00:00
|
|
|
RAII_VAR(struct ast_variable *, transformed, NULL, ast_variables_destroy);
|
|
|
|
struct ast_variable *field;
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object_type->transform && (transformed = object_type->transform(objectset))) {
|
|
|
|
field = transformed;
|
|
|
|
} else {
|
|
|
|
field = objectset;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; field; field = field->next) {
|
2013-06-22 14:26:25 +00:00
|
|
|
if ((res = aco_process_var(&object_type->type, details->object->id, field, object))) {
|
2013-01-25 14:01:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!res && object_type->apply) {
|
2013-04-25 18:25:31 +00:00
|
|
|
res = object_type->apply(sorcery, object);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes)
|
|
|
|
{
|
|
|
|
const struct ast_variable *field;
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
*changes = NULL;
|
|
|
|
|
|
|
|
/* Unless the ast_variable list changes when examined... it can't differ from itself */
|
|
|
|
if (original == modified) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (field = modified; field; field = field->next) {
|
2014-12-05 17:08:27 +00:00
|
|
|
const char *old_value = ast_variable_find_in_list(original, field->name);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
if (!old_value || strcmp(old_value, field->value)) {
|
2013-01-25 14:01:04 +00:00
|
|
|
struct ast_variable *tmp;
|
|
|
|
|
|
|
|
if (!(tmp = ast_variable_new(field->name, field->value, ""))) {
|
|
|
|
res = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp->next = *changes;
|
|
|
|
*changes = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If an error occurred do not return a partial changeset */
|
|
|
|
if (res) {
|
|
|
|
ast_variables_destroy(*changes);
|
|
|
|
*changes = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-06-22 14:26:25 +00:00
|
|
|
static void sorcery_object_destructor(void *object)
|
|
|
|
{
|
|
|
|
struct ast_sorcery_object_details *details = object;
|
|
|
|
|
|
|
|
if (details->object->destructor) {
|
|
|
|
details->object->destructor(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_variables_destroy(details->object->extended);
|
2013-08-23 21:49:47 +00:00
|
|
|
ast_free(details->object->id);
|
2013-06-22 14:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
|
|
|
|
{
|
|
|
|
void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object), sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
|
|
|
|
struct ast_sorcery_object_details *details = object;
|
|
|
|
|
|
|
|
if (!object) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
details->object = object + size;
|
|
|
|
details->object->destructor = destructor;
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
struct ast_sorcery_object_details *details;
|
|
|
|
|
|
|
|
if (!object_type || !object_type->type.item_alloc ||
|
2013-12-09 18:14:41 +00:00
|
|
|
!(details = object_type->type.item_alloc(id))) {
|
2013-01-25 14:01:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ast_strlen_zero(id)) {
|
2013-08-23 21:49:47 +00:00
|
|
|
char uuid[AST_UUID_STR_LEN];
|
|
|
|
|
|
|
|
ast_uuid_generate_str(uuid, sizeof(uuid));
|
|
|
|
details->object->id = ast_strdup(uuid);
|
2013-01-25 14:01:04 +00:00
|
|
|
} else {
|
2013-08-23 21:49:47 +00:00
|
|
|
details->object->id = ast_strdup(id);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
2015-08-17 16:00:53 +00:00
|
|
|
if (!details->object->id) {
|
|
|
|
ao2_ref(details, -1);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2015-07-04 15:03:06 +00:00
|
|
|
details->object->created = ast_tvnow();
|
2013-06-22 14:26:25 +00:00
|
|
|
ast_copy_string(details->object->type, type, sizeof(details->object->type));
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
if (aco_set_defaults(&object_type->type, id, details)) {
|
|
|
|
ao2_ref(details, -1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return details;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_details *details = object;
|
2013-06-22 14:26:25 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
|
|
|
|
struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->object->type, details->object->id);
|
2013-01-25 14:01:04 +00:00
|
|
|
RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
|
2013-02-10 14:58:37 +00:00
|
|
|
int res = 0;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2013-02-10 14:58:37 +00:00
|
|
|
if (!copy) {
|
2013-01-25 14:01:04 +00:00
|
|
|
return NULL;
|
2013-02-10 14:58:37 +00:00
|
|
|
} else if (object_type->copy) {
|
|
|
|
res = object_type->copy(object, copy);
|
|
|
|
} else if ((objectset = ast_sorcery_objectset_create(sorcery, object))) {
|
|
|
|
res = ast_sorcery_objectset_apply(sorcery, copy, objectset);
|
|
|
|
} else {
|
|
|
|
/* No native copy available and could not create an objectset, this copy has failed */
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
ao2_cleanup(copy);
|
|
|
|
copy = NULL;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes)
|
|
|
|
{
|
2013-02-10 14:58:37 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, ast_sorcery_object_get_type(original), OBJ_KEY), ao2_cleanup);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
*changes = NULL;
|
|
|
|
|
|
|
|
if (strcmp(ast_sorcery_object_get_type(original), ast_sorcery_object_get_type(modified))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-02-10 14:58:37 +00:00
|
|
|
if (original == modified) {
|
|
|
|
return 0;
|
|
|
|
} else if (!object_type->diff) {
|
|
|
|
RAII_VAR(struct ast_variable *, objectset1, NULL, ast_variables_destroy);
|
|
|
|
RAII_VAR(struct ast_variable *, objectset2, NULL, ast_variables_destroy);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2013-02-10 14:58:37 +00:00
|
|
|
objectset1 = ast_sorcery_objectset_create(sorcery, original);
|
|
|
|
objectset2 = ast_sorcery_objectset_create(sorcery, modified);
|
|
|
|
|
|
|
|
return ast_sorcery_changeset_create(objectset1, objectset2, changes);
|
|
|
|
} else {
|
|
|
|
return object_type->diff(original, modified, changes);
|
|
|
|
}
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2013-03-20 14:52:23 +00:00
|
|
|
/*! \brief Structure used when calling create, update, or delete */
|
|
|
|
struct sorcery_details {
|
|
|
|
/*! \brief Pointer to the sorcery instance */
|
|
|
|
const struct ast_sorcery *sorcery;
|
|
|
|
/*! \brief Pointer to the object itself */
|
|
|
|
void *obj;
|
|
|
|
};
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
/*! \brief Internal function used to create an object in caching wizards */
|
|
|
|
static int sorcery_cache_create(void *obj, void *arg, int flags)
|
|
|
|
{
|
2013-03-20 14:52:23 +00:00
|
|
|
const struct ast_sorcery_object_wizard *object_wizard = obj;
|
|
|
|
const struct sorcery_details *details = arg;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
if (!object_wizard->caching || !object_wizard->wizard->callbacks.create) {
|
2013-01-25 14:01:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
object_wizard->wizard->callbacks.create(details->sorcery, object_wizard->data, details->obj);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
void *object = NULL;
|
2015-05-05 20:32:08 +00:00
|
|
|
int i;
|
2013-01-25 14:01:04 +00:00
|
|
|
unsigned int cached = 0;
|
|
|
|
|
|
|
|
if (!object_type || ast_strlen_zero(id)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_RDLOCK(&object_type->wizards);
|
|
|
|
for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
|
|
|
|
struct ast_sorcery_object_wizard *wizard =
|
|
|
|
AST_VECTOR_GET(&object_type->wizards, i);
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
if (wizard->wizard->callbacks.retrieve_id &&
|
|
|
|
!(object = wizard->wizard->callbacks.retrieve_id(sorcery, wizard->data, object_type->name, id))) {
|
2013-01-25 14:01:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
cached = wizard->caching;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cached && object) {
|
2015-05-26 12:44:18 +00:00
|
|
|
struct sorcery_details sdetails = {
|
|
|
|
.sorcery = sorcery,
|
|
|
|
.obj = object,
|
|
|
|
};
|
|
|
|
|
|
|
|
AST_VECTOR_CALLBACK(&object_type->wizards, sorcery_cache_create, NULL, &sdetails, 0);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
void *object = NULL;
|
2015-05-05 20:32:08 +00:00
|
|
|
int i;
|
2013-01-25 14:01:04 +00:00
|
|
|
unsigned int cached = 0;
|
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If returning multiple objects create a container to store them in */
|
|
|
|
if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
|
|
|
|
if (!(object = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_RDLOCK(&object_type->wizards);
|
|
|
|
for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
|
|
|
|
struct ast_sorcery_object_wizard *wizard =
|
|
|
|
AST_VECTOR_GET(&object_type->wizards, i);
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
|
2014-12-05 17:08:27 +00:00
|
|
|
if (wizard->wizard->callbacks.retrieve_multiple) {
|
|
|
|
wizard->wizard->callbacks.retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
2014-12-05 17:08:27 +00:00
|
|
|
} else if (fields && wizard->wizard->callbacks.retrieve_fields) {
|
|
|
|
if (wizard->wizard->callbacks.retrieve_fields) {
|
|
|
|
object = wizard->wizard->callbacks.retrieve_fields(sorcery, wizard->data, object_type->name, fields);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-05 16:01:55 +00:00
|
|
|
if (((flags & AST_RETRIEVE_FLAG_MULTIPLE) && (!ao2_container_count(object) || !wizard->caching)) || !object) {
|
2013-01-25 14:01:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
cached = wizard->caching;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are returning a single object and it came from a non-cache source create it in any caches */
|
|
|
|
if (!(flags & AST_RETRIEVE_FLAG_MULTIPLE) && !cached && object) {
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_CALLBACK(&object_type->wizards, sorcery_cache_create, NULL, object, 0);
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2013-02-16 16:24:21 +00:00
|
|
|
struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
struct ao2_container *objects;
|
2015-05-05 20:32:08 +00:00
|
|
|
int i;
|
2013-02-16 16:24:21 +00:00
|
|
|
|
|
|
|
if (!object_type || !(objects = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_RDLOCK(&object_type->wizards);
|
|
|
|
for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
|
|
|
|
struct ast_sorcery_object_wizard *wizard =
|
|
|
|
AST_VECTOR_GET(&object_type->wizards, i);
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
if (!wizard->wizard->callbacks.retrieve_regex) {
|
2013-02-16 16:24:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
wizard->wizard->callbacks.retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
|
2015-12-05 16:01:55 +00:00
|
|
|
|
|
|
|
if (wizard->caching && ao2_container_count(objects)) {
|
|
|
|
break;
|
|
|
|
}
|
2013-02-16 16:24:21 +00:00
|
|
|
}
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
|
2013-02-16 16:24:21 +00:00
|
|
|
|
|
|
|
return objects;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
/*! \brief Internal function which returns if the wizard has created the object */
|
|
|
|
static int sorcery_wizard_create(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_wizard *object_wizard = obj;
|
2013-03-20 14:52:23 +00:00
|
|
|
const struct sorcery_details *details = arg;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2014-12-05 17:08:27 +00:00
|
|
|
if (!object_wizard->wizard->callbacks.create) {
|
2015-07-08 21:32:25 +00:00
|
|
|
ast_debug(5, "Sorcery wizard '%s' does not support creation\n", object_wizard->wizard->callbacks.name);
|
2014-10-03 15:55:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-02-25 21:13:19 +00:00
|
|
|
|
|
|
|
if (object_wizard->wizard->callbacks.create(details->sorcery, object_wizard->data, details->obj)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CMP_MATCH;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2013-05-06 13:04:08 +00:00
|
|
|
/*! \brief Internal callback function which notifies an individual observer that an object has been created */
|
|
|
|
static int sorcery_observer_notify_create(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_type_observer *observer = obj;
|
|
|
|
|
|
|
|
if (observer->callbacks->created) {
|
|
|
|
observer->callbacks->created(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Internal callback function which notifies observers that an object has been created */
|
|
|
|
static int sorcery_observers_notify_create(void *data)
|
|
|
|
{
|
|
|
|
struct sorcery_observer_invocation *invocation = data;
|
|
|
|
|
|
|
|
ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_create, invocation->object);
|
|
|
|
ao2_cleanup(invocation);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_details *details = object;
|
2013-06-22 14:26:25 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
|
2015-05-05 20:32:08 +00:00
|
|
|
struct ast_sorcery_object_wizard *object_wizard = NULL;
|
|
|
|
struct ast_sorcery_object_wizard *found_wizard;
|
|
|
|
int i;
|
2013-03-20 14:52:23 +00:00
|
|
|
struct sorcery_details sdetails = {
|
|
|
|
.sorcery = sorcery,
|
|
|
|
.obj = object,
|
|
|
|
};
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_RDLOCK(&object_type->wizards);
|
|
|
|
for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
|
|
|
|
found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
|
2016-02-25 21:13:19 +00:00
|
|
|
if (!found_wizard->caching && sorcery_wizard_create(found_wizard, &sdetails, 0) == CMP_MATCH) {
|
2015-05-05 20:32:08 +00:00
|
|
|
object_wizard = found_wizard;
|
2016-02-25 21:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-06 13:04:08 +00:00
|
|
|
|
2016-02-25 21:13:19 +00:00
|
|
|
if (object_wizard) {
|
|
|
|
for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
|
|
|
|
found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
|
|
|
|
if (found_wizard->caching) {
|
|
|
|
sorcery_wizard_create(found_wizard, &sdetails, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ao2_container_count(object_type->observers)) {
|
|
|
|
struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(
|
|
|
|
object_type, object);
|
|
|
|
|
|
|
|
if (invocation
|
|
|
|
&& ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create,
|
|
|
|
invocation)) {
|
|
|
|
ao2_cleanup(invocation);
|
2015-05-05 20:32:08 +00:00
|
|
|
}
|
2013-05-06 13:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-25 21:13:19 +00:00
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
return object_wizard ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2013-05-06 13:04:08 +00:00
|
|
|
/*! \brief Internal callback function which notifies an individual observer that an object has been updated */
|
|
|
|
static int sorcery_observer_notify_update(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_type_observer *observer = obj;
|
|
|
|
|
|
|
|
if (observer->callbacks->updated) {
|
|
|
|
observer->callbacks->updated(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Internal callback function which notifies observers that an object has been updated */
|
|
|
|
static int sorcery_observers_notify_update(void *data)
|
|
|
|
{
|
|
|
|
struct sorcery_observer_invocation *invocation = data;
|
|
|
|
|
|
|
|
ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_update, invocation->object);
|
|
|
|
ao2_cleanup(invocation);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
/*! \brief Internal function which returns if a wizard has updated the object */
|
|
|
|
static int sorcery_wizard_update(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_wizard *object_wizard = obj;
|
2013-03-20 14:52:23 +00:00
|
|
|
const struct sorcery_details *details = arg;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2015-07-08 21:32:25 +00:00
|
|
|
if (!object_wizard->wizard->callbacks.update) {
|
|
|
|
ast_debug(5, "Sorcery wizard '%s' does not support updating\n", object_wizard->wizard->callbacks.name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-25 21:13:19 +00:00
|
|
|
if (object_wizard->wizard->callbacks.update(details->sorcery, object_wizard->data, details->obj)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CMP_MATCH;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_details *details = object;
|
2013-06-22 14:26:25 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
|
2015-05-05 20:32:08 +00:00
|
|
|
struct ast_sorcery_object_wizard *object_wizard = NULL;
|
|
|
|
struct ast_sorcery_object_wizard *found_wizard;
|
|
|
|
int i;
|
2013-03-20 14:52:23 +00:00
|
|
|
struct sorcery_details sdetails = {
|
|
|
|
.sorcery = sorcery,
|
|
|
|
.obj = object,
|
|
|
|
};
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_RDLOCK(&object_type->wizards);
|
|
|
|
for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
|
|
|
|
found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
|
2016-02-25 21:13:19 +00:00
|
|
|
if (!found_wizard->caching && sorcery_wizard_update(found_wizard, &sdetails, 0) == CMP_MATCH) {
|
2015-05-05 20:32:08 +00:00
|
|
|
object_wizard = found_wizard;
|
2016-02-25 21:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-06 13:04:08 +00:00
|
|
|
|
2016-02-25 21:13:19 +00:00
|
|
|
if (object_wizard) {
|
|
|
|
for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
|
|
|
|
found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
|
|
|
|
if (found_wizard->caching) {
|
|
|
|
sorcery_wizard_update(found_wizard, &sdetails, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ao2_container_count(object_type->observers)) {
|
|
|
|
struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(
|
|
|
|
object_type, object);
|
|
|
|
|
|
|
|
if (invocation
|
|
|
|
&& ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update,
|
|
|
|
invocation)) {
|
|
|
|
ao2_cleanup(invocation);
|
2015-05-05 20:32:08 +00:00
|
|
|
}
|
2013-05-06 13:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-25 21:13:19 +00:00
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
return object_wizard ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2013-05-06 13:04:08 +00:00
|
|
|
/*! \brief Internal callback function which notifies an individual observer that an object has been deleted */
|
|
|
|
static int sorcery_observer_notify_delete(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_type_observer *observer = obj;
|
|
|
|
|
|
|
|
if (observer->callbacks->deleted) {
|
|
|
|
observer->callbacks->deleted(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Internal callback function which notifies observers that an object has been deleted */
|
|
|
|
static int sorcery_observers_notify_delete(void *data)
|
|
|
|
{
|
|
|
|
struct sorcery_observer_invocation *invocation = data;
|
|
|
|
|
|
|
|
ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_delete, invocation->object);
|
|
|
|
ao2_cleanup(invocation);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
/*! \brief Internal function which returns if a wizard has deleted the object */
|
|
|
|
static int sorcery_wizard_delete(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_wizard *object_wizard = obj;
|
2013-03-20 14:52:23 +00:00
|
|
|
const struct sorcery_details *details = arg;
|
2013-01-25 14:01:04 +00:00
|
|
|
|
2015-07-08 21:32:25 +00:00
|
|
|
if (!object_wizard->wizard->callbacks.delete) {
|
|
|
|
ast_debug(5, "Sorcery wizard '%s' does not support deletion\n", object_wizard->wizard->callbacks.name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-25 21:13:19 +00:00
|
|
|
if (object_wizard->wizard->callbacks.delete(details->sorcery, object_wizard->data, details->obj)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CMP_MATCH;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_details *details = object;
|
2013-06-22 14:26:25 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
|
2015-05-05 20:32:08 +00:00
|
|
|
struct ast_sorcery_object_wizard *object_wizard = NULL;
|
|
|
|
struct ast_sorcery_object_wizard *found_wizard;
|
|
|
|
int i;
|
2013-03-20 14:52:23 +00:00
|
|
|
struct sorcery_details sdetails = {
|
|
|
|
.sorcery = sorcery,
|
|
|
|
.obj = object,
|
|
|
|
};
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_RDLOCK(&object_type->wizards);
|
|
|
|
for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
|
|
|
|
found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
|
2016-02-25 21:13:19 +00:00
|
|
|
if (!found_wizard->caching && sorcery_wizard_delete(found_wizard, &sdetails, 0) == CMP_MATCH) {
|
2015-05-05 20:32:08 +00:00
|
|
|
object_wizard = found_wizard;
|
2016-02-25 21:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-06 13:04:08 +00:00
|
|
|
|
2016-02-25 21:13:19 +00:00
|
|
|
if (object_wizard) {
|
|
|
|
for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
|
|
|
|
found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
|
|
|
|
if (found_wizard->caching) {
|
|
|
|
sorcery_wizard_delete(found_wizard, &sdetails, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ao2_container_count(object_type->observers)) {
|
|
|
|
struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(
|
|
|
|
object_type, object);
|
|
|
|
|
|
|
|
if (invocation
|
|
|
|
&& ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete,
|
|
|
|
invocation)) {
|
|
|
|
ao2_cleanup(invocation);
|
2015-05-05 20:32:08 +00:00
|
|
|
}
|
2013-05-06 13:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-25 21:13:19 +00:00
|
|
|
|
2015-05-05 20:32:08 +00:00
|
|
|
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
|
2013-01-25 14:01:04 +00:00
|
|
|
|
|
|
|
return object_wizard ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2015-07-04 15:03:06 +00:00
|
|
|
int ast_sorcery_is_stale(const struct ast_sorcery *sorcery, void *object)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_details *details = object;
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
|
|
|
|
struct ast_sorcery_object_wizard *found_wizard;
|
|
|
|
int res = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!object_type) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_VECTOR_RW_RDLOCK(&object_type->wizards);
|
|
|
|
for (i = 0; i < AST_VECTOR_SIZE(&object_type->wizards); i++) {
|
|
|
|
found_wizard = AST_VECTOR_GET(&object_type->wizards, i);
|
|
|
|
|
|
|
|
if (found_wizard->wizard->callbacks.is_stale) {
|
|
|
|
res |= found_wizard->wizard->callbacks.is_stale(sorcery, found_wizard->data, object);
|
|
|
|
ast_debug(5, "After calling wizard '%s', object '%s' is %s\n",
|
|
|
|
found_wizard->wizard->callbacks.name,
|
|
|
|
ast_sorcery_object_get_id(object),
|
|
|
|
res ? "stale" : "not stale");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AST_VECTOR_RW_UNLOCK(&object_type->wizards);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
void ast_sorcery_unref(struct ast_sorcery *sorcery)
|
|
|
|
{
|
2014-02-20 20:45:30 +00:00
|
|
|
if (sorcery) {
|
|
|
|
/* One ref for what we just released, the other for the instances container. */
|
|
|
|
ao2_wrlock(instances);
|
|
|
|
if (ao2_ref(sorcery, -1) == 2) {
|
|
|
|
ao2_unlink_flags(instances, sorcery, OBJ_NOLOCK);
|
|
|
|
}
|
|
|
|
ao2_unlock(instances);
|
|
|
|
}
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *ast_sorcery_object_get_id(const void *object)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_details *details = object;
|
2013-06-22 14:26:25 +00:00
|
|
|
return details->object->id;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-07-04 15:03:06 +00:00
|
|
|
const struct timeval ast_sorcery_object_get_created(const void *object)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_details *details = object;
|
|
|
|
return details->object->created;
|
|
|
|
}
|
|
|
|
|
2013-01-25 14:01:04 +00:00
|
|
|
const char *ast_sorcery_object_get_type(const void *object)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_details *details = object;
|
2013-06-22 14:26:25 +00:00
|
|
|
return details->object->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ast_sorcery_object_get_extended(const void *object, const char *name)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_details *details = object;
|
|
|
|
struct ast_variable *field;
|
|
|
|
|
|
|
|
for (field = details->object->extended; field; field = field->next) {
|
|
|
|
if (!strcmp(field->name + 1, name)) {
|
|
|
|
return field->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_variable *, field, NULL, ast_variables_destroy);
|
|
|
|
struct ast_variable *extended = ast_variable_new(name, value, ""), *previous = NULL;
|
|
|
|
const struct ast_sorcery_object_details *details = object;
|
|
|
|
|
|
|
|
if (!extended) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (field = details->object->extended; field; previous = field, field = field->next) {
|
|
|
|
if (!strcmp(field->name, name)) {
|
|
|
|
if (previous) {
|
|
|
|
previous->next = field->next;
|
|
|
|
} else {
|
|
|
|
details->object->extended = field->next;
|
|
|
|
}
|
|
|
|
field->next = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extended->next = details->object->extended;
|
|
|
|
details->object->extended = extended;
|
|
|
|
|
|
|
|
return 0;
|
2013-01-25 14:01:04 +00:00
|
|
|
}
|
2013-05-06 13:04:08 +00:00
|
|
|
|
|
|
|
int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
|
|
|
|
struct ast_sorcery_object_type_observer *observer;
|
2013-12-03 17:35:54 +00:00
|
|
|
int res;
|
2013-05-06 13:04:08 +00:00
|
|
|
|
|
|
|
if (!object_type || !callbacks) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(observer = ao2_alloc(sizeof(*observer), NULL))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
observer->callbacks = callbacks;
|
2013-12-03 17:35:54 +00:00
|
|
|
res = 0;
|
|
|
|
if (!ao2_link(object_type->observers, observer)) {
|
|
|
|
res = -1;
|
|
|
|
}
|
2013-05-06 13:04:08 +00:00
|
|
|
ao2_ref(observer, -1);
|
|
|
|
|
2013-12-03 17:35:54 +00:00
|
|
|
return res;
|
2013-05-06 13:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief Internal callback function for removing an observer */
|
|
|
|
static int sorcery_observer_remove(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
const struct ast_sorcery_object_type_observer *observer = obj;
|
|
|
|
|
|
|
|
return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
|
|
|
|
}
|
|
|
|
|
2013-12-03 17:35:54 +00:00
|
|
|
void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
|
2013-05-06 13:04:08 +00:00
|
|
|
{
|
2013-12-03 17:35:54 +00:00
|
|
|
RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
|
|
|
|
struct ast_sorcery_observer *cbs = (struct ast_sorcery_observer *) callbacks;/* Remove const for traversal. */
|
2013-05-06 13:04:08 +00:00
|
|
|
|
2013-12-03 17:35:54 +00:00
|
|
|
if (!sorcery) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
object_type = ao2_find(sorcery->types, type, OBJ_KEY);
|
2013-05-06 13:04:08 +00:00
|
|
|
if (!object_type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-03 17:35:54 +00:00
|
|
|
ao2_callback(object_type->observers, OBJ_NODATA | OBJ_UNLINK,
|
|
|
|
sorcery_observer_remove, cbs);
|
2013-05-06 13:04:08 +00:00
|
|
|
}
|
2013-12-20 21:32:13 +00:00
|
|
|
|
2014-03-08 16:50:36 +00:00
|
|
|
int ast_sorcery_object_id_sort(const void *obj, const void *arg, int flags)
|
2013-12-20 21:32:13 +00:00
|
|
|
{
|
2014-03-08 16:50:36 +00:00
|
|
|
const char *right_key = arg;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
switch (flags & OBJ_SEARCH_MASK) {
|
|
|
|
case OBJ_SEARCH_OBJECT:
|
|
|
|
right_key = ast_sorcery_object_get_id(arg);
|
|
|
|
/* Fall through */
|
|
|
|
case OBJ_SEARCH_KEY:
|
|
|
|
cmp = strcmp(ast_sorcery_object_get_id(obj), right_key);
|
|
|
|
break;
|
|
|
|
case OBJ_SEARCH_PARTIAL_KEY:
|
|
|
|
cmp = strncmp(ast_sorcery_object_get_id(obj), right_key, strlen(right_key));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cmp = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sorcery_object_id_compare(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
const char *right_key = arg;
|
|
|
|
int cmp = 0;
|
|
|
|
|
|
|
|
switch (flags & OBJ_SEARCH_MASK) {
|
|
|
|
case OBJ_SEARCH_OBJECT:
|
|
|
|
right_key = ast_sorcery_object_get_id(arg);
|
|
|
|
/* Fall through */
|
|
|
|
case OBJ_SEARCH_KEY:
|
|
|
|
if (strcmp(ast_sorcery_object_get_id(obj), right_key) == 0) {
|
|
|
|
cmp = CMP_MATCH | CMP_STOP;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OBJ_SEARCH_PARTIAL_KEY:
|
|
|
|
if (strncmp(ast_sorcery_object_get_id(obj), right_key, strlen(right_key)) == 0) {
|
|
|
|
cmp = CMP_MATCH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cmp = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ast_sorcery_object_id_hash(const void *obj, int flags) {
|
|
|
|
if (flags & OBJ_SEARCH_OBJECT) {
|
|
|
|
return ast_str_hash(ast_sorcery_object_get_id(obj));
|
|
|
|
} else if (flags & OBJ_SEARCH_KEY) {
|
|
|
|
return ast_str_hash(obj);
|
2013-12-20 21:32:13 +00:00
|
|
|
}
|
2014-03-08 16:50:36 +00:00
|
|
|
return -1;
|
2013-12-20 21:32:13 +00:00
|
|
|
}
|
2014-03-07 21:23:39 +00:00
|
|
|
|
|
|
|
struct ast_sorcery_object_type *ast_sorcery_get_object_type(const struct ast_sorcery *sorcery,
|
|
|
|
const char *type)
|
|
|
|
{
|
|
|
|
return ao2_find(sorcery->types, type, OBJ_SEARCH_KEY);
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:49:39 +00:00
|
|
|
static int is_registered_cb(void *obj, void *arg, int flags)
|
|
|
|
{
|
|
|
|
struct ast_sorcery_object_field *object_field = obj;
|
|
|
|
char *name = arg;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
if (object_field->name_regex
|
|
|
|
&& !regexec(object_field->name_regex, name, 0, NULL, 0)) {
|
|
|
|
rc = CMP_MATCH | CMP_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-03-07 21:23:39 +00:00
|
|
|
int ast_sorcery_is_object_field_registered(const struct ast_sorcery_object_type *object_type,
|
|
|
|
const char *field_name)
|
|
|
|
{
|
|
|
|
struct ast_sorcery_object_field *object_field;
|
|
|
|
int res = 1;
|
|
|
|
|
|
|
|
ast_assert(object_type != NULL);
|
|
|
|
|
|
|
|
object_field = ao2_find(object_type->fields, field_name, OBJ_SEARCH_KEY);
|
2014-11-21 17:49:39 +00:00
|
|
|
|
|
|
|
if (!object_field) {
|
|
|
|
object_field = ao2_callback(object_type->fields, 0, is_registered_cb, (char *)field_name);
|
|
|
|
}
|
|
|
|
|
2014-03-07 21:23:39 +00:00
|
|
|
if (!object_field) {
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ao2_cleanup(object_field);
|
|
|
|
return res;
|
|
|
|
}
|
2015-05-16 22:02:50 +00:00
|
|
|
|
|
|
|
const char *ast_sorcery_get_module(const struct ast_sorcery *sorcery)
|
|
|
|
{
|
|
|
|
return sorcery->module_name;
|
2015-05-26 12:44:18 +00:00
|
|
|
}
|