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