stasis_message.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. /*! \internal */
  33. struct stasis_message_type {
  34. struct stasis_message_vtable *vtable;
  35. char *name;
  36. };
  37. static struct stasis_message_vtable null_vtable = {};
  38. static void message_type_dtor(void *obj)
  39. {
  40. struct stasis_message_type *type = obj;
  41. ast_free(type->name);
  42. type->name = NULL;
  43. }
  44. int stasis_message_type_create(const char *name,
  45. struct stasis_message_vtable *vtable,
  46. struct stasis_message_type **result)
  47. {
  48. struct stasis_message_type *type;
  49. /* Check for declination */
  50. if (name && stasis_message_type_declined(name)) {
  51. return STASIS_MESSAGE_TYPE_DECLINED;
  52. }
  53. type = ao2_t_alloc(sizeof(*type), message_type_dtor, name);
  54. if (!type) {
  55. return STASIS_MESSAGE_TYPE_ERROR;
  56. }
  57. if (!vtable) {
  58. /* Null object pattern, FTW! */
  59. vtable = &null_vtable;
  60. }
  61. type->name = ast_strdup(name);
  62. if (!type->name) {
  63. ao2_cleanup(type);
  64. return STASIS_MESSAGE_TYPE_ERROR;
  65. }
  66. type->vtable = vtable;
  67. *result = type;
  68. return STASIS_MESSAGE_TYPE_SUCCESS;
  69. }
  70. const char *stasis_message_type_name(const struct stasis_message_type *type)
  71. {
  72. return type->name;
  73. }
  74. /*! \internal */
  75. struct stasis_message {
  76. /*! Time the message was created */
  77. struct timeval timestamp;
  78. /*! Type of the message */
  79. struct stasis_message_type *type;
  80. /*! Where this message originated. NULL if aggregate message. */
  81. const struct ast_eid *eid_ptr;
  82. /*! Message content */
  83. void *data;
  84. /*! Where this message originated. */
  85. struct ast_eid eid;
  86. };
  87. static void stasis_message_dtor(void *obj)
  88. {
  89. struct stasis_message *message = obj;
  90. ao2_cleanup(message->type);
  91. ao2_cleanup(message->data);
  92. }
  93. struct stasis_message *stasis_message_create_full(struct stasis_message_type *type, void *data, const struct ast_eid *eid)
  94. {
  95. struct stasis_message *message;
  96. if (type == NULL || data == NULL) {
  97. return NULL;
  98. }
  99. message = ao2_t_alloc(sizeof(*message), stasis_message_dtor, type->name);
  100. if (message == NULL) {
  101. return NULL;
  102. }
  103. message->timestamp = ast_tvnow();
  104. ao2_ref(type, +1);
  105. message->type = type;
  106. ao2_ref(data, +1);
  107. message->data = data;
  108. if (eid) {
  109. message->eid_ptr = &message->eid;
  110. message->eid = *eid;
  111. }
  112. return message;
  113. }
  114. struct stasis_message *stasis_message_create(struct stasis_message_type *type, void *data)
  115. {
  116. return stasis_message_create_full(type, data, &ast_eid_default);
  117. }
  118. const struct ast_eid *stasis_message_eid(const struct stasis_message *msg)
  119. {
  120. if (msg == NULL) {
  121. return NULL;
  122. }
  123. return msg->eid_ptr;
  124. }
  125. struct stasis_message_type *stasis_message_type(const struct stasis_message *msg)
  126. {
  127. if (msg == NULL) {
  128. return NULL;
  129. }
  130. return msg->type;
  131. }
  132. void *stasis_message_data(const struct stasis_message *msg)
  133. {
  134. if (msg == NULL) {
  135. return NULL;
  136. }
  137. return msg->data;
  138. }
  139. const struct timeval *stasis_message_timestamp(const struct stasis_message *msg)
  140. {
  141. if (msg == NULL) {
  142. return NULL;
  143. }
  144. return &msg->timestamp;
  145. }
  146. #define INVOKE_VIRTUAL(fn, ...) \
  147. ({ \
  148. if (msg == NULL) { \
  149. return NULL; \
  150. } \
  151. ast_assert(msg->type != NULL); \
  152. ast_assert(msg->type->vtable != NULL); \
  153. if (msg->type->vtable->fn == NULL) { \
  154. return NULL; \
  155. } \
  156. msg->type->vtable->fn(__VA_ARGS__); \
  157. })
  158. struct ast_manager_event_blob *stasis_message_to_ami(struct stasis_message *msg)
  159. {
  160. return INVOKE_VIRTUAL(to_ami, msg);
  161. }
  162. struct ast_json *stasis_message_to_json(
  163. struct stasis_message *msg,
  164. struct stasis_message_sanitizer *sanitize)
  165. {
  166. return INVOKE_VIRTUAL(to_json, msg, sanitize);
  167. }
  168. struct ast_event *stasis_message_to_event(struct stasis_message *msg)
  169. {
  170. return INVOKE_VIRTUAL(to_event, msg);
  171. }