stasis_cache_pattern.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #ifndef _ASTERISK_STASIS_CACHE_PATTERN_H
  19. #define _ASTERISK_STASIS_CACHE_PATTERN_H
  20. /*! \file
  21. *
  22. * \brief Caching pattern for \ref stasis topics.
  23. *
  24. * A typical pattern for Stasis objects is to have individual objects, which
  25. * have their own topic and caching topic. These individual topics feed an
  26. * upstream aggregate topics, and a shared cache.
  27. *
  28. * The \ref stasis_cp_all object contains the aggregate topics and shared cache.
  29. * This is built with the base name for the topics, and the identity function to
  30. * identify messages in the cache.
  31. *
  32. * The \ref stasis_cp_single object contains the \ref stasis_topic for a single
  33. * instance, and the corresponding \ref stasis_caching_topic.
  34. *
  35. * Since the \ref stasis_cp_single object has subscriptions for forwarding
  36. * and caching, it must be disposed of using stasis_cp_single_unsubscribe()
  37. * instead of simply ao2_cleanup().
  38. */
  39. #include "asterisk/stasis.h"
  40. /*!
  41. * \brief The 'all' side of the cache pattern. These are typically built as
  42. * global objects for specific modules.
  43. */
  44. struct stasis_cp_all;
  45. /*!
  46. * \brief Create an all instance of the cache pattern.
  47. *
  48. * This object is AO2 managed, so dispose of it with ao2_cleanup().
  49. *
  50. * \param name Base name of the topics.
  51. * \param id_fn Identity function for the cache.
  52. * \return All side instance.
  53. * \return \c NULL on error.
  54. */
  55. struct stasis_cp_all *stasis_cp_all_create(const char *name,
  56. snapshot_get_id id_fn);
  57. /*!
  58. * \brief Get the aggregate topic.
  59. *
  60. * This topic aggregates all messages published to corresponding
  61. * stasis_cp_single_topic() topics.
  62. *
  63. * \param all All side caching pattern object.
  64. * \return The aggregate topic.
  65. * \return \c NULL if \a all is \c NULL
  66. */
  67. struct stasis_topic *stasis_cp_all_topic(struct stasis_cp_all *all);
  68. /*!
  69. * \brief Get the caching topic.
  70. *
  71. * This topic aggregates all messages from the corresponding
  72. * stasis_cp_single_topic_cached() topics.
  73. *
  74. * Note that one normally only subscribes to the caching topic, since data
  75. * is fed to it from its upstream topic.
  76. *
  77. * \param all All side caching pattern object.
  78. * \return The aggregate caching topic.
  79. * \return \c NULL if \a all is \c NULL
  80. */
  81. struct stasis_topic *stasis_cp_all_topic_cached(
  82. struct stasis_cp_all *all);
  83. /*!
  84. * \brief Get the cache.
  85. *
  86. * This is the shared cache for all corresponding \ref stasis_cp_single objects.
  87. *
  88. * \param all All side caching pattern object.
  89. * \return The cache.
  90. * \return \c NULL if \a all is \c NULL
  91. */
  92. struct stasis_cache *stasis_cp_all_cache(struct stasis_cp_all *all);
  93. /*!
  94. * \brief The 'one' side of the cache pattern. These are built per-instance for
  95. * some corresponding object, and must be explicitly disposed of using
  96. * stasis_cp_single_unsubscribe().
  97. */
  98. struct stasis_cp_single;
  99. /*!
  100. * \brief Create the 'one' side of the cache pattern.
  101. *
  102. * Create the 'one' and forward to all's topic and topic_cached.
  103. *
  104. * Dispose of using stasis_cp_single_unsubscribe().
  105. *
  106. * \param all Corresponding all side.
  107. * \param name Base name for the topics.
  108. * \return One side instance
  109. */
  110. struct stasis_cp_single *stasis_cp_single_create(struct stasis_cp_all *all,
  111. const char *name);
  112. /*!
  113. * \brief Create a sink in the cache pattern
  114. *
  115. * Create the 'one' but do not automatically forward to the all's topic.
  116. * This is useful when aggregating other topic's messages created with
  117. * \c stasis_cp_single_create in another caching topic without replicating
  118. * those messages in the all's topics.
  119. *
  120. * Dispose of using stasis_cp_single_unsubscribe().
  121. *
  122. * \param all Corresponding all side.
  123. * \param name Base name for the topics.
  124. * \return One side instance
  125. */
  126. struct stasis_cp_single *stasis_cp_sink_create(struct stasis_cp_all *all,
  127. const char *name);
  128. /*!
  129. * \brief Stops caching and forwarding messages.
  130. *
  131. * \param one One side of the cache pattern.
  132. */
  133. void stasis_cp_single_unsubscribe(struct stasis_cp_single *one);
  134. /*!
  135. * \brief Get the topic for this instance.
  136. *
  137. * This is the topic to which one would post instance-specific messages, or
  138. * subscribe for single-instance, uncached messages.
  139. *
  140. * \param one One side of the cache pattern.
  141. * \return The main topic.
  142. * \return \c NULL if \a one is \c NULL
  143. */
  144. struct stasis_topic *stasis_cp_single_topic(struct stasis_cp_single *one);
  145. /*!
  146. * \brief Get the caching topic for this instance.
  147. *
  148. * Note that one normally only subscribes to the caching topic, since data
  149. * is fed to it from its upstream topic.
  150. *
  151. * \param one One side of the cache pattern.
  152. * \return The caching topic.
  153. * \return \c NULL if \a one is \c NULL
  154. */
  155. struct stasis_topic *stasis_cp_single_topic_cached(
  156. struct stasis_cp_single *one);
  157. /*!
  158. * \brief Indicate to an instance that we are interested in a message type.
  159. *
  160. * This will cause the caching topic to receive messages of the given message
  161. * type. This enables internal filtering in the stasis message bus to reduce
  162. * messages.
  163. *
  164. * \param one One side of the cache pattern.
  165. * \param type The message type we wish to receive.
  166. * \retval 0 on success
  167. * \retval -1 failure
  168. *
  169. * \since 17.0.0
  170. */
  171. int stasis_cp_single_accept_message_type(struct stasis_cp_single *one,
  172. struct stasis_message_type *type);
  173. /*!
  174. * \brief Set the message type filtering level on a cache
  175. *
  176. * This will cause the underlying subscription to filter messages according to the
  177. * provided filter level. For example if selective is used then only
  178. * messages matching those provided to \ref stasis_subscription_accept_message_type
  179. * will be raised to the subscription callback.
  180. *
  181. * \param one One side of the cache pattern.
  182. * \param filter What filter to use
  183. * \retval 0 on success
  184. * \retval -1 failure
  185. *
  186. * \since 17.0.0
  187. */
  188. int stasis_cp_single_set_filter(struct stasis_cp_single *one,
  189. enum stasis_subscription_message_filter filter);
  190. #endif /* _ASTERISK_STASIS_CACHE_PATTERN_H */