stasis_message_router.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_MESSAGE_ROUTER_H
  19. #define _ASTERISK_STASIS_MESSAGE_ROUTER_H
  20. /*!
  21. * \brief A simplistic router for \ref stasis_message's.
  22. *
  23. * Often times, when subscribing to a topic, one wants to handle different
  24. * message types differently. While one could cascade if/else statements through
  25. * the subscription handler, it is much cleaner to specify a different callback
  26. * for each message type. The \ref stasis_message_router is here to help!
  27. *
  28. * A \ref stasis_message_router is constructed for a particular \ref
  29. * stasis_topic, which is subscribes to. Call
  30. * stasis_message_router_unsubscribe() to cancel that subscription.
  31. *
  32. * Once constructed, routes can be added using stasis_message_router_add() (or
  33. * stasis_message_router_set_default() for any messages not handled by other
  34. * routes). There may be only one route per \ref stasis_message_type. The
  35. * route's \a callback is invoked just as if it were a callback for a
  36. * subscription; but it only gets called for messages of the specified type.
  37. *
  38. * \since 12
  39. */
  40. #include "asterisk/stasis.h"
  41. /*! \brief Stasis message routing object */
  42. struct stasis_message_router;
  43. /*!
  44. * \brief Create a new message router object.
  45. *
  46. * \param topic Topic to subscribe route to.
  47. *
  48. * \return New \ref stasis_message_router.
  49. * \return \c NULL on error.
  50. *
  51. * \since 12
  52. */
  53. struct stasis_message_router *__stasis_message_router_create(
  54. struct stasis_topic *topic, const char *file, int lineno, const char *func);
  55. #ifdef AST_DEVMODE
  56. #define stasis_message_router_create(topic) __stasis_message_router_create(topic, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  57. #else
  58. struct stasis_message_router *stasis_message_router_create(
  59. struct stasis_topic *topic);
  60. #endif
  61. /*!
  62. * \brief Create a new message router object.
  63. *
  64. * The subscription created for this message router will dispatch
  65. * callbacks on a thread pool.
  66. *
  67. * \param topic Topic to subscribe route to.
  68. *
  69. * \return New \ref stasis_message_router.
  70. * \return \c NULL on error.
  71. *
  72. * \since 12.8.0
  73. */
  74. struct stasis_message_router *__stasis_message_router_create_pool(
  75. struct stasis_topic *topic, const char *file, int lineno, const char *func);
  76. #ifdef AST_DEVMODE
  77. #define stasis_message_router_create_pool(topic) __stasis_message_router_create_pool(topic, __FILE__, __LINE__, __PRETTY_FUNCTION__)
  78. #else
  79. struct stasis_message_router *stasis_message_router_create_pool(
  80. struct stasis_topic *topic);
  81. #endif
  82. /*!
  83. * \brief Unsubscribe the router from the upstream topic.
  84. *
  85. * \param router Router to unsubscribe.
  86. *
  87. * \since 12
  88. */
  89. void stasis_message_router_unsubscribe(struct stasis_message_router *router);
  90. /*!
  91. * \brief Unsubscribe the router from the upstream topic, blocking until the
  92. * final message has been processed.
  93. *
  94. * See stasis_unsubscribe_and_join() for info on when to use this
  95. * vs. stasis_message_router_unsubscribe().
  96. *
  97. * \param router Router to unsubscribe.
  98. *
  99. * \since 12
  100. */
  101. void stasis_message_router_unsubscribe_and_join(
  102. struct stasis_message_router *router);
  103. /*!
  104. * \brief Returns whether \a router has received its final message.
  105. *
  106. * \param router Router.
  107. *
  108. * \return True (non-zero) if stasis_subscription_final_message() has been
  109. * received.
  110. * \return False (zero) if waiting for the end.
  111. */
  112. int stasis_message_router_is_done(struct stasis_message_router *router);
  113. /*!
  114. * \brief Publish a message to a message router's subscription synchronously
  115. *
  116. * \param router Router
  117. * \param message The \ref stasis message
  118. *
  119. * This should be used when a message needs to be published synchronously to
  120. * the underlying subscription created by a message router. This is analagous
  121. * to \ref stasis_publish_sync.
  122. *
  123. * Note that the caller will be blocked until the thread servicing the message
  124. * on the message router's subscription completes handling of the message.
  125. *
  126. * \since 12.1.0
  127. */
  128. void stasis_message_router_publish_sync(struct stasis_message_router *router,
  129. struct stasis_message *message);
  130. /*!
  131. * \brief Set the high and low alert water marks of the stasis message router.
  132. * \since 13.10.0
  133. *
  134. * \param router Pointer to a stasis message router
  135. * \param low_water New queue low water mark. (-1 to set as 90% of high_water)
  136. * \param high_water New queue high water mark.
  137. *
  138. * \retval 0 on success.
  139. * \retval -1 on error (water marks not changed).
  140. */
  141. int stasis_message_router_set_congestion_limits(struct stasis_message_router *router,
  142. long low_water, long high_water);
  143. /*!
  144. * \brief Add a route to a message router.
  145. *
  146. * A particular \a message_type may have at most one route per \a router. If
  147. * you route \ref stasis_cache_update messages, the callback will only receive
  148. * updates for types not handled by routes added with
  149. * stasis_message_router_add_cache_update().
  150. *
  151. * Adding multiple routes for the same message type results in undefined
  152. * behavior.
  153. *
  154. * \param router Router to add the route to.
  155. * \param message_type Type of message to route.
  156. * \param callback Callback to forard messages of \a message_type to.
  157. * \param data Data pointer to pass to \a callback.
  158. *
  159. * \retval 0 on success
  160. * \retval -1 on failure
  161. *
  162. * \since 12
  163. */
  164. int stasis_message_router_add(struct stasis_message_router *router,
  165. struct stasis_message_type *message_type,
  166. stasis_subscription_cb callback, void *data);
  167. /*!
  168. * \brief Add a route for \ref stasis_cache_update messages to a message router.
  169. *
  170. * A particular \a message_type may have at most one cache route per \a router.
  171. * These are distinct from regular routes, so one could have both a regular
  172. * route and a cache route for the same \a message_type.
  173. *
  174. * Adding multiple routes for the same message type results in undefined
  175. * behavior.
  176. *
  177. * \param router Router to add the route to.
  178. * \param message_type Subtype of cache update to route.
  179. * \param callback Callback to forard messages of \a message_type to.
  180. * \param data Data pointer to pass to \a callback.
  181. *
  182. * \retval 0 on success
  183. * \retval -1 on failure
  184. *
  185. * \since 12
  186. */
  187. int stasis_message_router_add_cache_update(struct stasis_message_router *router,
  188. struct stasis_message_type *message_type,
  189. stasis_subscription_cb callback, void *data);
  190. /*!
  191. * \brief Remove a route from a message router.
  192. *
  193. * If a route is removed from another thread, there is no notification that
  194. * all messages using this route have been processed. This typically means that
  195. * the associated \c data pointer for this route must be kept until the
  196. * route itself is disposed of.
  197. *
  198. * \param router Router to remove the route from.
  199. * \param message_type Type of message to route.
  200. *
  201. * \since 12
  202. */
  203. void stasis_message_router_remove(struct stasis_message_router *router,
  204. struct stasis_message_type *message_type);
  205. /*!
  206. * \brief Remove a cache route from a message router.
  207. *
  208. * If a route is removed from another thread, there is no notification that
  209. * all messages using this route have been processed. This typically means that
  210. * the associated \c data pointer for this route must be kept until the
  211. * route itself is disposed of.
  212. *
  213. * \param router Router to remove the route from.
  214. * \param message_type Type of message to route.
  215. *
  216. * \since 12
  217. */
  218. void stasis_message_router_remove_cache_update(
  219. struct stasis_message_router *router,
  220. struct stasis_message_type *message_type);
  221. /*!
  222. * \brief Sets the default route of a router.
  223. *
  224. * \param router Router to set the default route of.
  225. * \param callback Callback to forward messages which otherwise have no home.
  226. * \param data Data pointer to pass to \a callback.
  227. *
  228. * \retval 0 on success
  229. * \retval -1 on failure
  230. *
  231. * \since 12
  232. *
  233. * \note Setting a default callback will automatically cause the underlying
  234. * subscription to receive all messages and not be filtered. If filtering is
  235. * desired then a specific route for each message type should be provided.
  236. */
  237. int stasis_message_router_set_default(struct stasis_message_router *router,
  238. stasis_subscription_cb callback,
  239. void *data);
  240. /*!
  241. * \brief Sets the default route of a router with formatters.
  242. *
  243. * \param router Router to set the default route of.
  244. * \param callback Callback to forward messages which otherwise have no home.
  245. * \param data Data pointer to pass to \a callback.
  246. * \param formatters A bitmap of \ref stasis_subscription_message_formatters we wish to receive.
  247. *
  248. * \since 13.26.0
  249. * \since 16.3.0
  250. *
  251. * \note If formatters are specified then the message router will remain in a selective
  252. * filtering state. Any explicit routes will receive messages of their message type and
  253. * the default callback will only receive messages that have one of the given formatters.
  254. * Explicit routes will not be filtered according to the given formatters.
  255. */
  256. void stasis_message_router_set_formatters_default(struct stasis_message_router *router,
  257. stasis_subscription_cb callback,
  258. void *data,
  259. enum stasis_subscription_message_formatters formatters);
  260. /*!
  261. * \brief Indicate to a message router that we are interested in messages with one or more formatters.
  262. *
  263. * The formatters are passed on to the underlying subscription.
  264. *
  265. * \warning With direct subscriptions, adding a formatter filter is an OR operation
  266. * with any message type filters. In the current implementation of message router however,
  267. * it's an AND operation. Even when setting a default route, the callback will only get
  268. * messages that have the formatters provides in this call.
  269. *
  270. * \param router Router to set the formatters of.
  271. * \param formatters A bitmap of \ref stasis_subscription_message_formatters we wish to receive.
  272. *
  273. * \since 13.25.0
  274. * \since 16.2.0
  275. */
  276. void stasis_message_router_accept_formatters(struct stasis_message_router *router,
  277. enum stasis_subscription_message_formatters formatters);
  278. #endif /* _ASTERISK_STASIS_MESSAGE_ROUTER_H */