Asterisk - The Open Source Telephony Project  21.4.1
Functions | Variables
stasis_endpoints.c File Reference

Stasis endpoint API. More...

#include "asterisk.h"
#include "asterisk/astobj2.h"
#include "asterisk/stasis.h"
#include "asterisk/stasis_endpoints.h"

Go to the source code of this file.

Functions

struct stasis_messageast_endpoint_blob_create (struct ast_endpoint *endpoint, struct stasis_message_type *type, struct ast_json *blob)
 Creates a ast_endpoint_blob message. More...
 
void ast_endpoint_blob_publish (struct ast_endpoint *endpoint, struct stasis_message_type *type, struct ast_json *blob)
 Creates and publishes a ast_endpoint_blob message. More...
 
struct stasis_cacheast_endpoint_cache (void)
 Backend cache for ast_endpoint_topic_all_cached(). More...
 
struct stasis_cp_allast_endpoint_cache_all (void)
 
struct ast_endpoint_snapshotast_endpoint_latest_snapshot (const char *tech, const char *name)
 Retrieve the most recent snapshot for the endpoint with the given name. More...
 
struct ast_jsonast_endpoint_snapshot_to_json (const struct ast_endpoint_snapshot *snapshot, const struct stasis_message_sanitizer *sanitize)
 Build a JSON object from a ast_endpoint_snapshot. More...
 
int ast_endpoint_stasis_init (void)
 Initialization function for endpoint stasis support. More...
 
struct stasis_topicast_endpoint_topic_all (void)
 Topic for all endpoint related messages. More...
 
struct stasis_topicast_endpoint_topic_all_cached (void)
 Cached topic for all endpoint related messages. More...
 
static struct ast_manager_event_blobcontactstatus_to_ami (struct stasis_message *msg)
 
static struct ast_jsoncontactstatus_to_json (struct stasis_message *msg, const struct stasis_message_sanitizer *sanitize)
 
static void endpoint_blob_dtor (void *obj)
 
static const char * endpoint_snapshot_get_id (struct stasis_message *message)
 Callback extract a unique identity from a snapshot message. More...
 
static void endpoints_stasis_cleanup (void)
 
static struct ast_manager_event_blobpeerstatus_to_ami (struct stasis_message *msg)
 
static struct ast_jsonpeerstatus_to_json (struct stasis_message *msg, const struct stasis_message_sanitizer *sanitize)
 
 STASIS_MESSAGE_TYPE_DEFN (ast_endpoint_snapshot_type)
 
 STASIS_MESSAGE_TYPE_DEFN (ast_endpoint_state_type,.to_ami=peerstatus_to_ami,.to_json=peerstatus_to_json,)
 
 STASIS_MESSAGE_TYPE_DEFN (ast_endpoint_contact_state_type,.to_ami=contactstatus_to_ami,.to_json=contactstatus_to_json)
 

Variables

static struct stasis_cp_allendpoint_cache_all
 

Detailed Description

Stasis endpoint API.

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

Definition in file stasis_endpoints.c.

Function Documentation

struct ast_json* ast_endpoint_snapshot_to_json ( const struct ast_endpoint_snapshot snapshot,
const struct stasis_message_sanitizer sanitize 
)

Build a JSON object from a ast_endpoint_snapshot.

Parameters
snapshotEndpoint snapshot.
sanitizeThe message sanitizer to use on the snapshot
Returns
JSON object representing endpoint snapshot.
Return values
NULLon error

Definition at line 398 of file stasis_endpoints.c.

References ast_endpoint_state_to_string(), ast_json_array_append(), ast_json_integer_create(), ast_json_object_get(), ast_json_object_set(), ast_json_pack(), ast_json_string_create(), ast_json_unref(), stasis_message_sanitizer::channel_id, ast_endpoint_snapshot::channel_ids, ast_endpoint_snapshot::max_channels, ast_endpoint_snapshot::num_channels, ast_endpoint_snapshot::resource, ast_endpoint_snapshot::state, and ast_endpoint_snapshot::tech.

Referenced by ast_ari_endpoints_get(), ast_ari_endpoints_list(), and ast_ari_endpoints_list_by_tech().

401 {
402  struct ast_json *json;
403  struct ast_json *channel_array;
404  int i;
405 
406  json = ast_json_pack("{s: s, s: s, s: s, s: []}",
407  "technology", snapshot->tech,
408  "resource", snapshot->resource,
409  "state", ast_endpoint_state_to_string(snapshot->state),
410  "channel_ids");
411 
412  if (json == NULL) {
413  return NULL;
414  }
415 
416  if (snapshot->max_channels != -1) {
417  int res = ast_json_object_set(json, "max_channels",
419  if (res != 0) {
420  ast_json_unref(json);
421 
422  return NULL;
423  }
424  }
425 
426  channel_array = ast_json_object_get(json, "channel_ids");
427  ast_assert(channel_array != NULL);
428  for (i = 0; i < snapshot->num_channels; ++i) {
429  int res;
430 
431  if (sanitize && sanitize->channel_id
432  && sanitize->channel_id(snapshot->channel_ids[i])) {
433  continue;
434  }
435 
436  res = ast_json_array_append(channel_array,
437  ast_json_string_create(snapshot->channel_ids[i]));
438  if (res != 0) {
439  ast_json_unref(json);
440 
441  return NULL;
442  }
443  }
444 
445  return json;
446 }
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
int(* channel_id)(const char *channel_id)
Callback which determines whether a channel should be sanitized from a message based on the channel's...
Definition: stasis.h:210
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition: json.c:414
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:278
const ast_string_field resource
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:378
const ast_string_field tech
const char * ast_endpoint_state_to_string(enum ast_endpoint_state state)
Returns a string representation of the given endpoint state.
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:407
Abstract JSON element (object, array, string, int, ...).
enum ast_endpoint_state state
struct ast_json * ast_json_integer_create(intmax_t value)
Create a JSON integer.
Definition: json.c:327
int ast_endpoint_stasis_init ( void  )

Initialization function for endpoint stasis support.

Returns
0 on success.
non-zero on error.
Since
12

Definition at line 458 of file stasis_endpoints.c.

References ast_endpoint_contact_state_type(), ast_endpoint_snapshot_type(), ast_endpoint_state_type(), ast_register_cleanup(), endpoint_snapshot_get_id(), stasis_cp_all_create(), and STASIS_MESSAGE_TYPE_INIT.

459 {
460  int res = 0;
461  ast_register_cleanup(endpoints_stasis_cleanup);
462 
463  endpoint_cache_all = stasis_cp_all_create("endpoint:all",
465  if (!endpoint_cache_all) {
466  return -1;
467  }
468 
472 
473  return res;
474 }
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1493
struct stasis_message_type * ast_endpoint_snapshot_type(void)
Message type for ast_endpoint_snapshot.
static const char * endpoint_snapshot_get_id(struct stasis_message *message)
Callback extract a unique identity from a snapshot message.
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
struct stasis_message_type * ast_endpoint_state_type(void)
Message type for endpoint state changes.
struct stasis_message_type * ast_endpoint_contact_state_type(void)
Message type for endpoint contact state changes.
struct stasis_cp_all * stasis_cp_all_create(const char *name, snapshot_get_id id_fn)
Create an all instance of the cache pattern.
static const char* endpoint_snapshot_get_id ( struct stasis_message message)
static

Callback extract a unique identity from a snapshot message.

This identity is unique to the underlying object of the snapshot, such as the UniqueId field of a channel.

Parameters
messageMessage to extract id from.
Returns
String representing the snapshot's id.
Return values
NULLif the message_type of the message isn't a handled snapshot.
Since
12

Definition at line 384 of file stasis_endpoints.c.

References ast_endpoint_snapshot_type(), ast_endpoint_snapshot::id, stasis_message_data(), and stasis_message_type().

Referenced by ast_endpoint_stasis_init().

385 {
386  struct ast_endpoint_snapshot *snapshot;
387 
389  return NULL;
390  }
391 
392  snapshot = stasis_message_data(message);
393 
394  return snapshot->id;
395 }
struct stasis_message_type * ast_endpoint_snapshot_type(void)
Message type for ast_endpoint_snapshot.
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
const ast_string_field id
A snapshot of an endpoint's state.
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.