stasis_message.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@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. /*! \file
  19. *
  20. * \brief Stasis Message API.
  21. *
  22. * \author David M. Lee, II <dlee@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/astobj2.h"
  30. #include "asterisk/stasis.h"
  31. #include "asterisk/utils.h"
  32. #include "asterisk/hashtab.h"
  33. /*! \internal */
  34. struct stasis_message_type {
  35. struct stasis_message_vtable *vtable;
  36. char *name;
  37. unsigned int hash;
  38. int id;
  39. enum stasis_subscription_message_formatters available_formatters;
  40. };
  41. static struct stasis_message_vtable null_vtable = {};
  42. static int message_type_id;
  43. static void message_type_dtor(void *obj)
  44. {
  45. struct stasis_message_type *type = obj;
  46. ast_free(type->name);
  47. type->name = NULL;
  48. }
  49. int stasis_message_type_create(const char *name,
  50. struct stasis_message_vtable *vtable,
  51. struct stasis_message_type **result)
  52. {
  53. struct stasis_message_type *type;
  54. /* Check for declination */
  55. if (name && stasis_message_type_declined(name)) {
  56. return STASIS_MESSAGE_TYPE_DECLINED;
  57. }
  58. type = ao2_t_alloc_options(sizeof(*type), message_type_dtor,
  59. AO2_ALLOC_OPT_LOCK_NOLOCK, name ?: "");
  60. if (!type) {
  61. return STASIS_MESSAGE_TYPE_ERROR;
  62. }
  63. if (!vtable) {
  64. /* Null object pattern, FTW! */
  65. vtable = &null_vtable;
  66. }
  67. type->name = ast_strdup(name);
  68. if (!type->name) {
  69. ao2_cleanup(type);
  70. return STASIS_MESSAGE_TYPE_ERROR;
  71. }
  72. type->hash = ast_hashtab_hash_string(name);
  73. type->vtable = vtable;
  74. if (vtable->to_json) {
  75. type->available_formatters |= STASIS_SUBSCRIPTION_FORMATTER_JSON;
  76. }
  77. if (vtable->to_ami) {
  78. type->available_formatters |= STASIS_SUBSCRIPTION_FORMATTER_AMI;
  79. }
  80. if (vtable->to_event) {
  81. type->available_formatters |= STASIS_SUBSCRIPTION_FORMATTER_EVENT;
  82. }
  83. type->id = ast_atomic_fetchadd_int(&message_type_id, +1);
  84. *result = type;
  85. return STASIS_MESSAGE_TYPE_SUCCESS;
  86. }
  87. const char *stasis_message_type_name(const struct stasis_message_type *type)
  88. {
  89. return type->name;
  90. }
  91. unsigned int stasis_message_type_hash(const struct stasis_message_type *type)
  92. {
  93. return type->hash;
  94. }
  95. int stasis_message_type_id(const struct stasis_message_type *type)
  96. {
  97. return type->id;
  98. }
  99. enum stasis_subscription_message_formatters stasis_message_type_available_formatters(
  100. const struct stasis_message_type *type)
  101. {
  102. return type->available_formatters;
  103. }
  104. /*! \internal */
  105. struct stasis_message {
  106. /*! Time the message was created */
  107. struct timeval timestamp;
  108. /*! Type of the message */
  109. struct stasis_message_type *type;
  110. /*! Where this message originated. NULL if aggregate message. */
  111. const struct ast_eid *eid_ptr;
  112. /*! Message content */
  113. void *data;
  114. /*! Where this message originated. */
  115. struct ast_eid eid;
  116. };
  117. static void stasis_message_dtor(void *obj)
  118. {
  119. struct stasis_message *message = obj;
  120. ao2_cleanup(message->data);
  121. }
  122. struct stasis_message *stasis_message_create_full(struct stasis_message_type *type, void *data, const struct ast_eid *eid)
  123. {
  124. struct stasis_message *message;
  125. if (type == NULL || data == NULL) {
  126. return NULL;
  127. }
  128. message = ao2_t_alloc_options(sizeof(*message), stasis_message_dtor,
  129. AO2_ALLOC_OPT_LOCK_NOLOCK, type->name);
  130. if (message == NULL) {
  131. return NULL;
  132. }
  133. message->timestamp = ast_tvnow();
  134. /*
  135. * XXX Normal ao2 ref counting rules says we should increment the message
  136. * type ref here and decrement it in stasis_message_dtor(). However, the
  137. * stasis message could be cached and legitimately cause the type ref count
  138. * to hit the excessive ref count assertion. Since the message type
  139. * practically has to be a global object anyway, we can get away with not
  140. * holding a ref in the stasis message.
  141. */
  142. message->type = type;
  143. ao2_ref(data, +1);
  144. message->data = data;
  145. if (eid) {
  146. message->eid_ptr = &message->eid;
  147. message->eid = *eid;
  148. }
  149. return message;
  150. }
  151. struct stasis_message *stasis_message_create(struct stasis_message_type *type, void *data)
  152. {
  153. return stasis_message_create_full(type, data, &ast_eid_default);
  154. }
  155. const struct ast_eid *stasis_message_eid(const struct stasis_message *msg)
  156. {
  157. if (msg == NULL) {
  158. return NULL;
  159. }
  160. return msg->eid_ptr;
  161. }
  162. struct stasis_message_type *stasis_message_type(const struct stasis_message *msg)
  163. {
  164. if (msg == NULL) {
  165. return NULL;
  166. }
  167. return msg->type;
  168. }
  169. void *stasis_message_data(const struct stasis_message *msg)
  170. {
  171. if (msg == NULL) {
  172. return NULL;
  173. }
  174. return msg->data;
  175. }
  176. const struct timeval *stasis_message_timestamp(const struct stasis_message *msg)
  177. {
  178. if (msg == NULL) {
  179. return NULL;
  180. }
  181. return &msg->timestamp;
  182. }
  183. #define INVOKE_VIRTUAL(fn, ...) \
  184. ({ \
  185. if (!msg) { \
  186. return NULL; \
  187. } \
  188. ast_assert(msg->type != NULL); \
  189. ast_assert(msg->type->vtable != NULL); \
  190. if (!msg->type->vtable->fn) { \
  191. return NULL; \
  192. } \
  193. msg->type->vtable->fn(__VA_ARGS__); \
  194. })
  195. struct ast_manager_event_blob *stasis_message_to_ami(struct stasis_message *msg)
  196. {
  197. return INVOKE_VIRTUAL(to_ami, msg);
  198. }
  199. struct ast_json *stasis_message_to_json(
  200. struct stasis_message *msg,
  201. struct stasis_message_sanitizer *sanitize)
  202. {
  203. return INVOKE_VIRTUAL(to_json, msg, sanitize);
  204. }
  205. struct ast_event *stasis_message_to_event(struct stasis_message *msg)
  206. {
  207. return INVOKE_VIRTUAL(to_event, msg);
  208. }
  209. #define HAS_VIRTUAL(fn, msg) \
  210. ({ \
  211. if (!msg) { \
  212. return 0; \
  213. } \
  214. ast_assert(msg->type != NULL); \
  215. ast_assert(msg->type->vtable != NULL); \
  216. !!msg->type->vtable->fn; \
  217. })
  218. int stasis_message_can_be_ami(struct stasis_message *msg)
  219. {
  220. return HAS_VIRTUAL(to_ami, msg);
  221. }