Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
main/endpoints.c File Reference

Asterisk endpoint API. More...

#include "asterisk.h"
#include "asterisk/astobj2.h"
#include "asterisk/endpoints.h"
#include "asterisk/stasis.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/stasis_endpoints.h"
#include "asterisk/stasis_message_router.h"
#include "asterisk/stringfields.h"
#include "asterisk/_private.h"
Include dependency graph for main/endpoints.c:

Go to the source code of this file.

Data Structures

struct  ast_endpoint
 

Macros

#define ENDPOINT_BUCKETS   127
 
#define ENDPOINT_CHANNEL_BUCKETS   127
 
#define TECH_ENDPOINT_BUCKETS   11
 

Functions

int ast_endpoint_add_channel (struct ast_endpoint *endpoint, struct ast_channel *chan)
 Adds a channel to the given endpoint. More...
 
struct ast_endpointast_endpoint_create (const char *tech, const char *resource)
 Create an endpoint struct. More...
 
struct ast_endpointast_endpoint_find_by_id (const char *id)
 Finds the endpoint with the given tech[/resource] id. More...
 
const char * ast_endpoint_get_id (const struct ast_endpoint *endpoint)
 Gets the tech/resource id of the given endpoint. More...
 
const char * ast_endpoint_get_resource (const struct ast_endpoint *endpoint)
 Gets the resource name of the given endpoint. More...
 
enum ast_endpoint_state ast_endpoint_get_state (const struct ast_endpoint *endpoint)
 Gets the state of the given endpoint. More...
 
const char * ast_endpoint_get_tech (const struct ast_endpoint *endpoint)
 Gets the technology of the given endpoint. More...
 
int ast_endpoint_init (void)
 Endpoint support initialization. More...
 
void ast_endpoint_set_max_channels (struct ast_endpoint *endpoint, int max_channels)
 Updates the maximum number of channels an endpoint supports. More...
 
void ast_endpoint_set_state (struct ast_endpoint *endpoint, enum ast_endpoint_state state)
 Updates the state of the given endpoint. More...
 
void ast_endpoint_shutdown (struct ast_endpoint *endpoint)
 Shutsdown an ast_endpoint. More...
 
struct ast_endpoint_snapshotast_endpoint_snapshot_create (struct ast_endpoint *endpoint)
 Create a snapshot of an endpoint. More...
 
const char * ast_endpoint_state_to_string (enum ast_endpoint_state state)
 Returns a string representation of the given endpoint state. More...
 
struct stasis_topicast_endpoint_topic (struct ast_endpoint *endpoint)
 Returns the topic for a specific endpoint. More...
 
struct stasis_topicast_endpoint_topic_cached (struct ast_endpoint *endpoint)
 Returns the topic for a specific endpoint. More...
 
static struct stasis_messagecreate_endpoint_snapshot_message (struct ast_endpoint *endpoint)
 
static void endpoint_cache_clear (void *data, struct stasis_subscription *sub, struct stasis_message *message)
 Handler for channel snapshot update. More...
 
static void endpoint_cleanup (void)
 
static void endpoint_dtor (void *obj)
 
static struct ast_endpointendpoint_internal_create (const char *tech, const char *resource)
 
static void endpoint_publish_snapshot (struct ast_endpoint *endpoint)
 
static void endpoint_snapshot_dtor (void *obj)
 
static void endpoint_subscription_change (void *data, struct stasis_subscription *sub, struct stasis_message *message)
 

Variables

static struct ao2_containerendpoints
 
static struct ao2_containertech_endpoints
 

Detailed Description

Asterisk endpoint API.

Author
David M. Lee, II dlee@.nosp@m.digi.nosp@m.um.co.nosp@m.m

Definition in file main/endpoints.c.

Macro Definition Documentation

◆ ENDPOINT_BUCKETS

#define ENDPOINT_BUCKETS   127

Buckets for endpoint hash. Keep it prime!

Definition at line 45 of file main/endpoints.c.

Referenced by ast_endpoint_init().

◆ ENDPOINT_CHANNEL_BUCKETS

#define ENDPOINT_CHANNEL_BUCKETS   127

Buckets for endpoint->channel mappings. Keep it prime!

Definition at line 42 of file main/endpoints.c.

Referenced by endpoint_internal_create().

◆ TECH_ENDPOINT_BUCKETS

#define TECH_ENDPOINT_BUCKETS   11

Buckets for technology endpoints.

Definition at line 48 of file main/endpoints.c.

Referenced by ast_endpoint_init().

Function Documentation

◆ ast_endpoint_add_channel()

int ast_endpoint_add_channel ( struct ast_endpoint endpoint,
struct ast_channel chan 
)

Adds a channel to the given endpoint.

Since
12 This updates the endpoint's statistics, as well as forwarding all of the channel's messages to the endpoint's topic.

The channel is automagically removed from the endpoint when it is disposed of.

Parameters
endpoint
chanChannel.
Return values
0on success.
Non-zeroon error.

Definition at line 164 of file main/endpoints.c.

References ao2_lock, ao2_unlock, ast_assert, ast_channel_forward_endpoint(), ast_channel_uniqueid(), ast_str_container_add(), ast_strlen_zero, ast_endpoint::channel_ids, endpoint_publish_snapshot(), NULL, and ast_endpoint::resource.

Referenced by __ast_channel_alloc_ap(), and AST_TEST_DEFINE().

166 {
167  ast_assert(chan != NULL);
168  ast_assert(endpoint != NULL);
169  ast_assert(!ast_strlen_zero(endpoint->resource));
170 
171  ast_channel_forward_endpoint(chan, endpoint);
172 
173  ao2_lock(endpoint);
175  ao2_unlock(endpoint);
176 
177  endpoint_publish_snapshot(endpoint);
178 
179  return 0;
180 }
struct ao2_container * channel_ids
int ast_channel_forward_endpoint(struct ast_channel *chan, struct ast_endpoint *endpoint)
Forward channel stasis messages to the given endpoint.
#define ast_assert(a)
Definition: utils.h:695
#define ao2_unlock(a)
Definition: astobj2.h:730
const ast_string_field resource
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
static void endpoint_publish_snapshot(struct ast_endpoint *endpoint)
#define ao2_lock(a)
Definition: astobj2.h:718
const char * ast_channel_uniqueid(const struct ast_channel *chan)
int ast_str_container_add(struct ao2_container *str_container, const char *add)
Adds a string to a string container allocated by ast_str_container_alloc.
Definition: strings.c:206

◆ ast_endpoint_create()

struct ast_endpoint* ast_endpoint_create ( const char *  tech,
const char *  resource 
)

Create an endpoint struct.

The endpoint is created with a state of UNKNOWN and max_channels of -1 (unlimited). While ast_endpoint is AO2 managed, you have to shut it down with ast_endpoint_shutdown() to clean up references from subscriptions.

Parameters
techTechnology for this endpoint.
resourceName of this endpoint.
Returns
Newly created endpoint.
NULL on error.
Since
12

Definition at line 319 of file main/endpoints.c.

References ast_log, ast_strlen_zero, endpoint_internal_create(), LOG_ERROR, and NULL.

Referenced by AST_TEST_DEFINE(), build_peer(), persistent_endpoint_find_or_create(), register_verify(), skinny_device_alloc(), and xmpp_client_alloc().

320 {
321  if (ast_strlen_zero(tech)) {
322  ast_log(LOG_ERROR, "Endpoint tech cannot be empty\n");
323  return NULL;
324  }
325 
326  if (ast_strlen_zero(resource)) {
327  ast_log(LOG_ERROR, "Endpoint resource cannot be empty\n");
328  return NULL;
329  }
330 
331  return endpoint_internal_create(tech, resource);
332 }
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
static struct ast_endpoint * endpoint_internal_create(const char *tech, const char *resource)

◆ ast_endpoint_find_by_id()

struct ast_endpoint* ast_endpoint_find_by_id ( const char *  id)

Finds the endpoint with the given tech[/resource] id.

Endpoints are refcounted, so ao2_cleanup() when you're done.

Note
The resource portion of an ID is optional. If not provided, an aggregate endpoint for the entire technology is returned. These endpoints must not be modified, but can be subscribed to in order to receive updates for all endpoints of a given technology.
Parameters
idTech[/resource] id to look for.
Returns
Associated endpoint.
NULL if not found.
Since
12

Definition at line 82 of file main/endpoints.c.

References ao2_find, and OBJ_KEY.

Referenced by ast_ari_endpoints_list_by_tech(), endpoint_find(), and messaging_app_unsubscribe_endpoint().

83 {
84  struct ast_endpoint *endpoint = ao2_find(endpoints, id, OBJ_KEY);
85 
86  if (!endpoint) {
87  endpoint = ao2_find(tech_endpoints, id, OBJ_KEY);
88  }
89 
90  return endpoint;
91 }
#define OBJ_KEY
Definition: astobj2.h:1155
static struct ao2_container * endpoints
static struct ao2_container * tech_endpoints
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756

◆ ast_endpoint_get_id()

const char* ast_endpoint_get_id ( const struct ast_endpoint endpoint)

Gets the tech/resource id of the given endpoint.

This is unique across all endpoints, and immutable.

Parameters
endpointThe endpoint.
Returns
Tech/resource id of the endpoint.
NULL if endpoint is NULL.
Since
12

Definition at line 391 of file main/endpoints.c.

References ast_endpoint::id, and NULL.

Referenced by app_subscribe_endpoint(), forwards_create_endpoint(), get_or_create_subscription(), get_subscription(), messaging_app_subscribe_endpoint(), and messaging_app_unsubscribe_endpoint().

392 {
393  if (!endpoint) {
394  return NULL;
395  }
396  return endpoint->id;
397 }
#define NULL
Definition: resample.c:96
const ast_string_field id

◆ ast_endpoint_get_resource()

const char* ast_endpoint_get_resource ( const struct ast_endpoint endpoint)

Gets the resource name of the given endpoint.

This is unique for the endpoint's technology, and immutable.

Note
If the endpoint being queried is a technology aggregate endpoint, this will be an empty string.
Parameters
endpointThe endpoint.
Returns
Resource name of the endpoint.
NULL if endpoint is NULL.
Since
12

Definition at line 383 of file main/endpoints.c.

References NULL, and ast_endpoint::resource.

Referenced by add_to_regcontext(), ast_sip_get_endpoint_snapshot(), ast_sip_persistent_endpoint_publish_contact_state(), ast_sip_persistent_endpoint_update_state(), AST_TEST_DEFINE(), chan_pjsip_devicestate(), endpoint_deleted_observer(), get_or_create_subscription(), get_subscription(), messaging_app_unsubscribe_endpoint(), persistent_endpoint_cmp(), and persistent_endpoint_hash().

384 {
385  if (!endpoint) {
386  return NULL;
387  }
388  return endpoint->resource;
389 }
const ast_string_field resource
#define NULL
Definition: resample.c:96

◆ ast_endpoint_get_state()

enum ast_endpoint_state ast_endpoint_get_state ( const struct ast_endpoint endpoint)

Gets the state of the given endpoint.

Parameters
endpointThe endpoint.
Returns
state.
AST_ENDPOINT_UNKNOWN if endpoint is NULL.
Since
13.4

Definition at line 399 of file main/endpoints.c.

References AST_ENDPOINT_UNKNOWN, and ast_endpoint::state.

Referenced by add_to_regcontext(), and ast_sip_persistent_endpoint_update_state().

400 {
401  if (!endpoint) {
402  return AST_ENDPOINT_UNKNOWN;
403  }
404  return endpoint->state;
405 }
enum ast_endpoint_state state

◆ ast_endpoint_get_tech()

const char* ast_endpoint_get_tech ( const struct ast_endpoint endpoint)

Gets the technology of the given endpoint.

This is an immutable string describing the channel provider technology (SIP, IAX2, etc.).

Parameters
endpointThe endpoint.
Returns
Tec of the endpoint.
NULL if endpoint is NULL.
Since
12

Definition at line 375 of file main/endpoints.c.

References NULL, and ast_endpoint::tech.

Referenced by ast_sip_get_endpoint_snapshot(), AST_TEST_DEFINE(), chan_pjsip_devicestate(), and get_subscription().

376 {
377  if (!endpoint) {
378  return NULL;
379  }
380  return endpoint->tech;
381 }
#define NULL
Definition: resample.c:96
const ast_string_field tech

◆ ast_endpoint_init()

int ast_endpoint_init ( void  )

Endpoint support initialization.

Returns
0 on success.
Non-zero on error.

Definition at line 496 of file main/endpoints.c.

References AO2_ALLOC_OPT_LOCK_MUTEX, ao2_container_alloc_hash, ast_register_cleanup(), ENDPOINT_BUCKETS, endpoint_cleanup(), NULL, and TECH_ENDPOINT_BUCKETS.

Referenced by asterisk_daemon().

497 {
499 
501  ast_endpoint_hash_fn, NULL, ast_endpoint_cmp_fn);
502  if (!endpoints) {
503  return -1;
504  }
505 
507  TECH_ENDPOINT_BUCKETS, ast_endpoint_hash_fn, NULL, ast_endpoint_cmp_fn);
508  if (!tech_endpoints) {
509  return -1;
510  }
511 
512  return 0;
513 }
#define NULL
Definition: resample.c:96
#define ENDPOINT_BUCKETS
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
static struct ao2_container * endpoints
static struct ao2_container * tech_endpoints
#define TECH_ENDPOINT_BUCKETS
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
static void endpoint_cleanup(void)

◆ ast_endpoint_set_max_channels()

void ast_endpoint_set_max_channels ( struct ast_endpoint endpoint,
int  max_channels 
)

Updates the maximum number of channels an endpoint supports.

Set to -1 for unlimited channels.

Parameters
endpointEndpoint to modify.
max_channelsMaximum number of concurrent channels this endpoint supports.

Definition at line 419 of file main/endpoints.c.

References ao2_lock, ao2_unlock, ast_assert, ast_strlen_zero, endpoint_publish_snapshot(), ast_endpoint::max_channels, NULL, and ast_endpoint::resource.

Referenced by AST_TEST_DEFINE().

421 {
422  ast_assert(endpoint != NULL);
423  ast_assert(!ast_strlen_zero(endpoint->resource));
424 
425  ao2_lock(endpoint);
426  endpoint->max_channels = max_channels;
427  ao2_unlock(endpoint);
428  endpoint_publish_snapshot(endpoint);
429 }
#define ast_assert(a)
Definition: utils.h:695
#define ao2_unlock(a)
Definition: astobj2.h:730
const ast_string_field resource
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
static void endpoint_publish_snapshot(struct ast_endpoint *endpoint)
#define ao2_lock(a)
Definition: astobj2.h:718
int max_channels
Max channels for this endpoint. -1 means unlimited or unknown.

◆ ast_endpoint_set_state()

void ast_endpoint_set_state ( struct ast_endpoint endpoint,
enum ast_endpoint_state  state 
)

Updates the state of the given endpoint.

Parameters
endpointEndpoint to modify.
stateNew state.
Since
12

Definition at line 407 of file main/endpoints.c.

References ao2_lock, ao2_unlock, ast_assert, ast_strlen_zero, endpoint_publish_snapshot(), NULL, ast_endpoint::resource, and ast_endpoint::state.

Referenced by __expire_registry(), __iax2_poke_noanswer(), ast_sip_persistent_endpoint_update_state(), AST_TEST_DEFINE(), expire_register(), handle_response_peerpoke(), parse_register_contact(), persistent_endpoint_find_or_create(), register_verify(), sip_poke_noanswer(), skinny_register(), skinny_session_cleanup(), socket_process_helper(), unload_module(), update_registry(), and xmpp_client_change_state().

409 {
410  ast_assert(endpoint != NULL);
411  ast_assert(!ast_strlen_zero(endpoint->resource));
412 
413  ao2_lock(endpoint);
414  endpoint->state = state;
415  ao2_unlock(endpoint);
416  endpoint_publish_snapshot(endpoint);
417 }
enum sip_cc_notify_state state
Definition: chan_sip.c:959
#define ast_assert(a)
Definition: utils.h:695
#define ao2_unlock(a)
Definition: astobj2.h:730
const ast_string_field resource
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
static void endpoint_publish_snapshot(struct ast_endpoint *endpoint)
#define ao2_lock(a)
Definition: astobj2.h:718
enum ast_endpoint_state state

◆ ast_endpoint_shutdown()

void ast_endpoint_shutdown ( struct ast_endpoint endpoint)

Shutsdown an ast_endpoint.

Parameters
endpointEndpoint to shut down.
Since
12

Definition at line 350 of file main/endpoints.c.

References ao2_cleanup, ao2_ref, ao2_unlink, ast_endpoint_topic(), create_endpoint_snapshot_message(), NULL, RAII_VAR, ast_endpoint::router, stasis_cache_clear_create(), stasis_forward_cancel(), stasis_message_router_unsubscribe(), stasis_publish(), and ast_endpoint::tech_forward.

Referenced by AST_TEST_DEFINE(), peer_destructor(), persistent_endpoint_destroy(), sip_destroy_peer(), skinny_device_destroy(), and xmpp_client_destructor().

351 {
352  RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
353 
354  if (endpoint == NULL) {
355  return;
356  }
357 
358  ao2_unlink(endpoints, endpoint);
359  endpoint->tech_forward = stasis_forward_cancel(endpoint->tech_forward);
360 
361  clear_msg = create_endpoint_snapshot_message(endpoint);
362  if (clear_msg) {
364  message = stasis_cache_clear_create(clear_msg);
365  if (message) {
367  }
368  }
369 
370  /* Bump refcount to hold on to the router */
371  ao2_ref(endpoint->router, +1);
373 }
struct stasis_forward * tech_forward
struct stasis_topic * ast_endpoint_topic(struct ast_endpoint *endpoint)
Returns the topic for a specific endpoint.
struct stasis_message * stasis_cache_clear_create(struct stasis_message *message)
A message which instructs the caching topic to remove an entry from its cache.
Definition: stasis_cache.c:778
#define NULL
Definition: resample.c:96
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
static struct ao2_container * endpoints
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_unlink(container, obj)
Definition: astobj2.h:1598
struct stasis_message_router * router
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic's subscribers.
Definition: stasis.c:1511
struct stasis_forward * stasis_forward_cancel(struct stasis_forward *forward)
Definition: stasis.c:1548
static struct stasis_message * create_endpoint_snapshot_message(struct ast_endpoint *endpoint)
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
void stasis_message_router_unsubscribe(struct stasis_message_router *router)
Unsubscribe the router from the upstream topic.

◆ ast_endpoint_state_to_string()

const char* ast_endpoint_state_to_string ( enum ast_endpoint_state  state)

Returns a string representation of the given endpoint state.

Parameters
stateEndpoint state.
Returns
String representation of state.
"?" if state isn't in ast_endpoint_state.

Definition at line 109 of file main/endpoints.c.

References AST_ENDPOINT_OFFLINE, AST_ENDPOINT_ONLINE, and AST_ENDPOINT_UNKNOWN.

Referenced by ast_endpoint_snapshot_to_json().

110 {
111  switch (state) {
113  return "unknown";
115  return "offline";
116  case AST_ENDPOINT_ONLINE:
117  return "online";
118  }
119  return "?";
120 }

◆ create_endpoint_snapshot_message()

static struct stasis_message* create_endpoint_snapshot_message ( struct ast_endpoint endpoint)
static

Definition at line 334 of file main/endpoints.c.

References ao2_cleanup, ast_endpoint_snapshot_create(), ast_endpoint_snapshot_type(), NULL, RAII_VAR, and stasis_message_create().

Referenced by ast_endpoint_shutdown().

335 {
336  RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
337 
339  return NULL;
340  }
341 
342  snapshot = ast_endpoint_snapshot_create(endpoint);
343  if (!snapshot) {
344  return NULL;
345  }
346 
348 }
struct ast_endpoint_snapshot * ast_endpoint_snapshot_create(struct ast_endpoint *endpoint)
Create a snapshot of an endpoint.
struct stasis_message_type * ast_endpoint_snapshot_type(void)
Message type for ast_endpoint_snapshot.
#define NULL
Definition: resample.c:96
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
A snapshot of an endpoint's state.
struct stasis_message * stasis_message_create(struct stasis_message_type *type, void *data)
Create a new message.
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ endpoint_cache_clear()

static void endpoint_cache_clear ( void *  data,
struct stasis_subscription sub,
struct stasis_message message 
)
static

Handler for channel snapshot update.

Definition at line 183 of file main/endpoints.c.

References ao2_lock, ao2_unlock, ast_assert, AST_FLAG_DEAD, ast_str_container_remove(), ast_test_flag, ast_channel_snapshot::base, ast_endpoint::channel_ids, endpoint_publish_snapshot(), ast_channel_snapshot::flags, ast_channel_snapshot_update::new_snapshot, NULL, stasis_message_data(), ast_channel_snapshot_base::uniqueid, and update().

Referenced by endpoint_internal_create().

186 {
187  struct ast_endpoint *endpoint = data;
189 
190  /* Only when the channel is dead do we remove it */
191  if (!ast_test_flag(&update->new_snapshot->flags, AST_FLAG_DEAD)) {
192  return;
193  }
194 
195  ast_assert(endpoint != NULL);
196 
197  ao2_lock(endpoint);
199  ao2_unlock(endpoint);
200  endpoint_publish_snapshot(endpoint);
201 }
struct ast_channel_snapshot_base * base
#define ast_test_flag(p, flag)
Definition: utils.h:63
static void update(int code_size, int y, int wi, int fi, int dq, int sr, int dqsez, struct g726_state *state_ptr)
Definition: codec_g726.c:367
struct ao2_container * channel_ids
void ast_str_container_remove(struct ao2_container *str_container, const char *remove)
Removes a string from a string container allocated by ast_str_container_alloc.
Definition: strings.c:222
const ast_string_field uniqueid
#define ast_assert(a)
Definition: utils.h:695
#define ao2_unlock(a)
Definition: astobj2.h:730
#define NULL
Definition: resample.c:96
Structure representing a change of snapshot of channel state.
static void endpoint_publish_snapshot(struct ast_endpoint *endpoint)
#define ao2_lock(a)
Definition: astobj2.h:718
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
struct ast_channel_snapshot * new_snapshot
struct ast_flags flags

◆ endpoint_cleanup()

static void endpoint_cleanup ( void  )
static

Definition at line 487 of file main/endpoints.c.

References ao2_cleanup, and NULL.

Referenced by ast_endpoint_init().

488 {
490  endpoints = NULL;
491 
494 }
#define NULL
Definition: resample.c:96
static struct ao2_container * endpoints
static struct ao2_container * tech_endpoints
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ endpoint_dtor()

static void endpoint_dtor ( void *  obj)
static

Definition at line 145 of file main/endpoints.c.

References ao2_cleanup, ast_assert, ast_string_field_free_memory, ast_endpoint::channel_ids, NULL, ast_endpoint::router, stasis_cp_single_unsubscribe(), stasis_message_router_is_done(), and ast_endpoint::topics.

Referenced by endpoint_internal_create().

146 {
147  struct ast_endpoint *endpoint = obj;
148 
149  /* The router should be shut down already */
151  ao2_cleanup(endpoint->router);
152  endpoint->router = NULL;
153 
155  endpoint->topics = NULL;
156 
157  ao2_cleanup(endpoint->channel_ids);
158  endpoint->channel_ids = NULL;
159 
161 }
struct ao2_container * channel_ids
void stasis_cp_single_unsubscribe(struct stasis_cp_single *one)
Stops caching and forwarding messages.
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
int stasis_message_router_is_done(struct stasis_message_router *router)
Returns whether router has received its final message.
struct stasis_message_router * router
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:368
struct stasis_cp_single * topics

◆ endpoint_internal_create()

static struct ast_endpoint* endpoint_internal_create ( const char *  tech,
const char *  resource 
)
static

Definition at line 214 of file main/endpoints.c.

References ao2_alloc, AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_cleanup, ao2_find, ao2_link, ao2_ref, ast_asprintf, ast_channel_snapshot_type(), ast_endpoint_cache_all(), ast_endpoint_snapshot_type(), ast_endpoint_topic(), AST_ENDPOINT_UNKNOWN, ast_free, ast_str_container_alloc_options(), ast_string_field_build, ast_string_field_init, ast_string_field_set, ast_strlen_zero, endpoint_cache_clear(), ENDPOINT_CHANNEL_BUCKETS, endpoint_dtor(), endpoint_publish_snapshot(), endpoint_subscription_change(), NULL, OBJ_KEY, RAII_VAR, S_OR, stasis_cp_single_accept_message_type(), stasis_cp_single_create(), stasis_cp_single_set_filter(), stasis_cp_single_topic(), stasis_cp_sink_create(), stasis_forward_all(), stasis_message_router_add(), stasis_message_router_create_pool, stasis_subscription_change_type(), and STASIS_SUBSCRIPTION_FILTER_SELECTIVE.

Referenced by ast_endpoint_create().

215 {
216  RAII_VAR(struct ast_endpoint *, endpoint, NULL, ao2_cleanup);
217  RAII_VAR(struct ast_endpoint *, tech_endpoint, NULL, ao2_cleanup);
218  int r = 0;
219 
220  /* Get/create the technology endpoint */
221  if (!ast_strlen_zero(resource)) {
222  tech_endpoint = ao2_find(tech_endpoints, tech, OBJ_KEY);
223  if (!tech_endpoint) {
224  tech_endpoint = endpoint_internal_create(tech, NULL);
225  if (!tech_endpoint) {
226  return NULL;
227  }
228  }
229  }
230 
231  endpoint = ao2_alloc(sizeof(*endpoint), endpoint_dtor);
232  if (!endpoint) {
233  return NULL;
234  }
235 
236  endpoint->max_channels = -1;
237  endpoint->state = AST_ENDPOINT_UNKNOWN;
238 
239  if (ast_string_field_init(endpoint, 80) != 0) {
240  return NULL;
241  }
242  ast_string_field_set(endpoint, tech, tech);
243  ast_string_field_set(endpoint, resource, S_OR(resource, ""));
244  ast_string_field_build(endpoint, id, "%s%s%s",
245  tech,
246  !ast_strlen_zero(resource) ? "/" : "",
247  S_OR(resource, ""));
248 
249  /* All access to channel_ids should be covered by the endpoint's
250  * lock; no extra lock needed. */
251  endpoint->channel_ids = ast_str_container_alloc_options(
253  if (!endpoint->channel_ids) {
254  return NULL;
255  }
256 
257  if (!ast_strlen_zero(resource)) {
258  char *topic_name;
259  int ret;
260 
261  ret = ast_asprintf(&topic_name, "endpoint:%s", endpoint->id);
262  if (ret < 0) {
263  return NULL;
264  }
265 
266  endpoint->topics = stasis_cp_single_create(ast_endpoint_cache_all(),
267  topic_name);
268  ast_free(topic_name);
269  if (!endpoint->topics) {
270  return NULL;
271  }
274 
275  endpoint->router = stasis_message_router_create_pool(ast_endpoint_topic(endpoint));
276  if (!endpoint->router) {
277  return NULL;
278  }
279  r |= stasis_message_router_add(endpoint->router,
281  endpoint);
282  r |= stasis_message_router_add(endpoint->router,
284  endpoint);
285  if (r) {
286  return NULL;
287  }
288 
289  endpoint->tech_forward = stasis_forward_all(stasis_cp_single_topic(endpoint->topics),
290  stasis_cp_single_topic(tech_endpoint->topics));
291 
292  endpoint_publish_snapshot(endpoint);
293  ao2_link(endpoints, endpoint);
294  } else {
295  char *topic_name;
296  int ret;
297 
298  ret = ast_asprintf(&topic_name, "endpoint:%s", endpoint->id);
299  if (ret < 0) {
300  return NULL;
301  }
302 
303  endpoint->topics = stasis_cp_sink_create(ast_endpoint_cache_all(),
304  topic_name);
305  ast_free(topic_name);
306  if (!endpoint->topics) {
307  return NULL;
308  }
311 
312  ao2_link(tech_endpoints, endpoint);
313  }
314 
315  ao2_ref(endpoint, +1);
316  return endpoint;
317 }
#define stasis_message_router_create_pool(topic)
#define OBJ_KEY
Definition: astobj2.h:1155
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.
struct stasis_cp_all * ast_endpoint_cache_all(void)
struct stasis_message_type * ast_endpoint_snapshot_type(void)
Message type for ast_endpoint_snapshot.
#define ENDPOINT_CHANNEL_BUCKETS
struct stasis_topic * ast_endpoint_topic(struct ast_endpoint *endpoint)
Returns the topic for a specific endpoint.
const ast_string_field resource
#define NULL
Definition: resample.c:96
static void endpoint_subscription_change(void *data, struct stasis_subscription *sub, struct stasis_message *message)
struct stasis_cp_single * stasis_cp_single_create(struct stasis_cp_all *all, const char *name)
Create the &#39;one&#39; side of the cache pattern.
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:269
#define ast_strlen_zero(foo)
Definition: strings.h:52
struct ao2_container * ast_str_container_alloc_options(enum ao2_alloc_opts opts, int buckets)
Allocates a hash container for bare strings.
Definition: strings.c:201
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:353
static void endpoint_publish_snapshot(struct ast_endpoint *endpoint)
static struct ao2_container * endpoints
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static struct ao2_container * tech_endpoints
static void endpoint_dtor(void *obj)
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
const ast_string_field tech
#define ast_free(a)
Definition: astmm.h:182
struct stasis_topic * stasis_cp_single_topic(struct stasis_cp_single *one)
Get the topic for this instance.
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
struct stasis_message_type * ast_channel_snapshot_type(void)
Message type for ast_channel_snapshot_update.
#define ast_string_field_build(x, field, fmt, args...)
Set a field to a complex (built) value.
Definition: stringfields.h:550
int stasis_cp_single_set_filter(struct stasis_cp_single *one, enum stasis_subscription_message_filter filter)
Set the message type filtering level on a cache.
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
static struct ast_endpoint * endpoint_internal_create(const char *tech, const char *resource)
struct stasis_message_type * stasis_subscription_change_type(void)
Gets the message type for subscription change notices.
int stasis_cp_single_accept_message_type(struct stasis_cp_single *one, struct stasis_message_type *type)
Indicate to an instance that we are interested in a message type.
struct stasis_forward * stasis_forward_all(struct stasis_topic *from_topic, struct stasis_topic *to_topic)
Create a subscription which forwards all messages from one topic to another.
Definition: stasis.c:1578
static void endpoint_cache_clear(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Handler for channel snapshot update.
struct stasis_cp_single * stasis_cp_sink_create(struct stasis_cp_all *all, const char *name)
Create a sink in the cache pattern.
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514
#define ao2_link(container, obj)
Definition: astobj2.h:1549

◆ endpoint_publish_snapshot()

static void endpoint_publish_snapshot ( struct ast_endpoint endpoint)
static

Definition at line 122 of file main/endpoints.c.

References ao2_cleanup, ast_assert, ast_endpoint_snapshot_create(), ast_endpoint_snapshot_type(), ast_endpoint_topic(), NULL, RAII_VAR, stasis_message_create(), stasis_publish(), and ast_endpoint::topics.

Referenced by ast_endpoint_add_channel(), ast_endpoint_set_max_channels(), ast_endpoint_set_state(), endpoint_cache_clear(), and endpoint_internal_create().

123 {
124  RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
126 
127  ast_assert(endpoint != NULL);
128  ast_assert(endpoint->topics != NULL);
129 
131  return;
132  }
133 
134  snapshot = ast_endpoint_snapshot_create(endpoint);
135  if (!snapshot) {
136  return;
137  }
139  if (!message) {
140  return;
141  }
143 }
struct ast_endpoint_snapshot * ast_endpoint_snapshot_create(struct ast_endpoint *endpoint)
Create a snapshot of an endpoint.
struct stasis_message_type * ast_endpoint_snapshot_type(void)
Message type for ast_endpoint_snapshot.
struct stasis_topic * ast_endpoint_topic(struct ast_endpoint *endpoint)
Returns the topic for a specific endpoint.
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
A snapshot of an endpoint&#39;s state.
struct stasis_message * stasis_message_create(struct stasis_message_type *type, void *data)
Create a new message.
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic&#39;s subscribers.
Definition: stasis.c:1511
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct stasis_cp_single * topics

◆ endpoint_snapshot_dtor()

static void endpoint_snapshot_dtor ( void *  obj)
static

Definition at line 431 of file main/endpoints.c.

References ao2_ref, ast_assert, ast_string_field_free_memory, ast_endpoint_snapshot::channel_ids, NULL, and ast_endpoint_snapshot::num_channels.

Referenced by ast_endpoint_snapshot_create().

432 {
433  struct ast_endpoint_snapshot *snapshot = obj;
434  int channel;
435 
436  ast_assert(snapshot != NULL);
437 
438  for (channel = 0; channel < snapshot->num_channels; channel++) {
439  ao2_ref(snapshot->channel_ids[channel], -1);
440  }
441 
443 }
#define ast_assert(a)
Definition: utils.h:695
Definition: muted.c:95
#define NULL
Definition: resample.c:96
#define ao2_ref(o, delta)
Definition: astobj2.h:464
A snapshot of an endpoint&#39;s state.
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:368

◆ endpoint_subscription_change()

static void endpoint_subscription_change ( void *  data,
struct stasis_subscription sub,
struct stasis_message message 
)
static

Definition at line 203 of file main/endpoints.c.

References ao2_cleanup, and stasis_subscription_final_message().

Referenced by endpoint_internal_create().

206 {
207  struct stasis_endpoint *endpoint = data;
208 
209  if (stasis_subscription_final_message(sub, message)) {
210  ao2_cleanup(endpoint);
211  }
212 }
int stasis_subscription_final_message(struct stasis_subscription *sub, struct stasis_message *msg)
Determine whether a message is the final message to be received on a subscription.
Definition: stasis.c:1176
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

Variable Documentation

◆ endpoints

struct ao2_container* endpoints
static

◆ tech_endpoints

struct ao2_container* tech_endpoints
static

Definition at line 52 of file main/endpoints.c.