res_sorcery_memory.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. *
  21. * \brief Sorcery In-Memory Object Wizard
  22. *
  23. * \author Joshua Colp <jcolp@digium.com>
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include <regex.h>
  31. #include "asterisk/module.h"
  32. #include "asterisk/sorcery.h"
  33. #include "asterisk/astobj2.h"
  34. /*! \brief Number of buckets for sorcery objects */
  35. #define OBJECT_BUCKETS 53
  36. static void *sorcery_memory_open(const char *data);
  37. static int sorcery_memory_create(const struct ast_sorcery *sorcery, void *data, void *object);
  38. static void *sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
  39. static void *sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
  40. static void sorcery_memory_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
  41. const struct ast_variable *fields);
  42. static void sorcery_memory_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
  43. static int sorcery_memory_update(const struct ast_sorcery *sorcery, void *data, void *object);
  44. static int sorcery_memory_delete(const struct ast_sorcery *sorcery, void *data, void *object);
  45. static void sorcery_memory_close(void *data);
  46. static struct ast_sorcery_wizard memory_object_wizard = {
  47. .name = "memory",
  48. .open = sorcery_memory_open,
  49. .create = sorcery_memory_create,
  50. .retrieve_id = sorcery_memory_retrieve_id,
  51. .retrieve_fields = sorcery_memory_retrieve_fields,
  52. .retrieve_multiple = sorcery_memory_retrieve_multiple,
  53. .retrieve_regex = sorcery_memory_retrieve_regex,
  54. .update = sorcery_memory_update,
  55. .delete = sorcery_memory_delete,
  56. .close = sorcery_memory_close,
  57. };
  58. /*! \brief Structure used for fields comparison */
  59. struct sorcery_memory_fields_cmp_params {
  60. /*! \brief Pointer to the sorcery structure */
  61. const struct ast_sorcery *sorcery;
  62. /*! \brief Pointer to the fields to check */
  63. const struct ast_variable *fields;
  64. /*! \brief Regular expression for checking object id */
  65. regex_t *regex;
  66. /*! \brief Optional container to put object into */
  67. struct ao2_container *container;
  68. };
  69. /*! \brief Hashing function for sorcery objects */
  70. static int sorcery_memory_hash(const void *obj, const int flags)
  71. {
  72. const char *id = obj;
  73. return ast_str_hash(flags & OBJ_KEY ? id : ast_sorcery_object_get_id(obj));
  74. }
  75. /*! \brief Comparator function for sorcery objects */
  76. static int sorcery_memory_cmp(void *obj, void *arg, int flags)
  77. {
  78. const char *id = arg;
  79. return !strcmp(ast_sorcery_object_get_id(obj), flags & OBJ_KEY ? id : ast_sorcery_object_get_id(arg)) ? CMP_MATCH | CMP_STOP : 0;
  80. }
  81. static int sorcery_memory_create(const struct ast_sorcery *sorcery, void *data, void *object)
  82. {
  83. ao2_link(data, object);
  84. return 0;
  85. }
  86. static int sorcery_memory_fields_cmp(void *obj, void *arg, int flags)
  87. {
  88. const struct sorcery_memory_fields_cmp_params *params = arg;
  89. RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
  90. RAII_VAR(struct ast_variable *, diff, NULL, ast_variables_destroy);
  91. if (params->regex) {
  92. /* If a regular expression has been provided see if it matches, otherwise move on */
  93. if (!regexec(params->regex, ast_sorcery_object_get_id(obj), 0, NULL, 0)) {
  94. ao2_link(params->container, obj);
  95. }
  96. return 0;
  97. } else if (params->fields &&
  98. (!(objset = ast_sorcery_objectset_create(params->sorcery, obj)) ||
  99. (ast_sorcery_changeset_create(objset, params->fields, &diff)) ||
  100. diff)) {
  101. /* If we can't turn the object into an object set OR if differences exist between the fields
  102. * passed in and what are present on the object they are not a match.
  103. */
  104. return 0;
  105. }
  106. if (params->container) {
  107. ao2_link(params->container, obj);
  108. /* As multiple objects are being returned keep going */
  109. return 0;
  110. } else {
  111. /* Immediately stop and return, we only want a single object */
  112. return CMP_MATCH | CMP_STOP;
  113. }
  114. }
  115. static void *sorcery_memory_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
  116. {
  117. struct sorcery_memory_fields_cmp_params params = {
  118. .sorcery = sorcery,
  119. .fields = fields,
  120. .container = NULL,
  121. };
  122. /* If no fields are present return nothing, we require *something* */
  123. if (!fields) {
  124. return NULL;
  125. }
  126. return ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
  127. }
  128. static void *sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
  129. {
  130. return ao2_find(data, id, OBJ_KEY);
  131. }
  132. static void sorcery_memory_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
  133. {
  134. struct sorcery_memory_fields_cmp_params params = {
  135. .sorcery = sorcery,
  136. .fields = fields,
  137. .container = objects,
  138. };
  139. ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
  140. }
  141. static void sorcery_memory_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
  142. {
  143. regex_t expression;
  144. struct sorcery_memory_fields_cmp_params params = {
  145. .sorcery = sorcery,
  146. .container = objects,
  147. .regex = &expression,
  148. };
  149. if (regcomp(&expression, regex, REG_EXTENDED | REG_NOSUB)) {
  150. return;
  151. }
  152. ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
  153. regfree(&expression);
  154. }
  155. static int sorcery_memory_update(const struct ast_sorcery *sorcery, void *data, void *object)
  156. {
  157. RAII_VAR(void *, existing, NULL, ao2_cleanup);
  158. ao2_lock(data);
  159. if (!(existing = ao2_find(data, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_UNLINK))) {
  160. ao2_unlock(data);
  161. return -1;
  162. }
  163. ao2_link(data, object);
  164. ao2_unlock(data);
  165. return 0;
  166. }
  167. static int sorcery_memory_delete(const struct ast_sorcery *sorcery, void *data, void *object)
  168. {
  169. RAII_VAR(void *, existing, ao2_find(data, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
  170. return existing ? 0 : -1;
  171. }
  172. static void *sorcery_memory_open(const char *data)
  173. {
  174. return ao2_container_alloc(OBJECT_BUCKETS, sorcery_memory_hash, sorcery_memory_cmp);
  175. }
  176. static void sorcery_memory_close(void *data)
  177. {
  178. ao2_ref(data, -1);
  179. }
  180. static int load_module(void)
  181. {
  182. if (ast_sorcery_wizard_register(&memory_object_wizard)) {
  183. return AST_MODULE_LOAD_DECLINE;
  184. }
  185. return AST_MODULE_LOAD_SUCCESS;
  186. }
  187. static int unload_module(void)
  188. {
  189. ast_sorcery_wizard_unregister(&memory_object_wizard);
  190. return 0;
  191. }
  192. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery In-Memory Object Wizard",
  193. .support_level = AST_MODULE_SUPPORT_CORE,
  194. .load = load_module,
  195. .unload = unload_module,
  196. .load_pri = AST_MODPRI_REALTIME_DRIVER,
  197. );