Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions
stasis_state.c File Reference
#include "asterisk.h"
#include "asterisk/stasis_state.h"
Include dependency graph for stasis_state.c:

Go to the source code of this file.

Data Structures

struct  stasis_state
 
struct  stasis_state_manager
 
struct  stasis_state_proxy
 
struct  stasis_state_publisher
 
struct  stasis_state_subscriber
 

Macros

#define STATE_BUCKETS   57
 
#define state_find_or_add(mgr, top, id)   __state_find_or_add(mgr, top, id, __FILE__, __LINE__, __PRETTY_FUNCTION__)
 

Functions

static struct stasis_state__state_find_or_add (struct stasis_state_manager *manager, struct stasis_topic *state_topic, const char *id, const char *file, int line, const char *func)
 
 AO2_STRING_FIELD_CMP_FN (stasis_state_proxy, id)
 
 AO2_STRING_FIELD_HASH_FN (stasis_state_proxy, id)
 
static int handle_stasis_state (struct stasis_state *state, on_stasis_state handler, void *data)
 
static int handle_stasis_state_proxy (void *obj, void *arg, void *data, int flags)
 
static int handle_stasis_state_subscribed (void *obj, void *arg, void *data, int flags)
 
static void publisher_dtor (void *obj)
 
int stasis_state_add_observer (struct stasis_state_manager *manager, struct stasis_state_observer *observer)
 Add an observer to receive managed state related events. More...
 
struct stasis_state_publisherstasis_state_add_publisher (struct stasis_state_manager *manager, const char *id)
 Add a publisher to the managed state for the given id. More...
 
struct stasis_state_subscriberstasis_state_add_subscriber (struct stasis_state_manager *manager, const char *id)
 Add a subscriber to the managed stasis state for the given id. More...
 
struct stasis_topicstasis_state_all_topic (struct stasis_state_manager *manager)
 Retrieve the manager's topic (the topic that all state topics get forwarded to) More...
 
void stasis_state_callback_all (struct stasis_state_manager *manager, on_stasis_state handler, void *data)
 For each managed state call the given handler. More...
 
void stasis_state_callback_subscribed (struct stasis_state_manager *manager, on_stasis_state handler, void *data)
 For each managed, and explicitly subscribed state call the given handler. More...
 
struct stasis_state_managerstasis_state_manager_create (const char *topic_name)
 Create a stasis state manager. More...
 
void stasis_state_publish (struct stasis_state_publisher *pub, struct stasis_message *msg)
 Publish to a managed state (topic) using a publisher. More...
 
void stasis_state_publish_by_id (struct stasis_state_manager *manager, const char *id, const struct ast_eid *eid, struct stasis_message *msg)
 Publish to a managed named by id topic, and add an implicit subscriber. More...
 
const char * stasis_state_publisher_id (const struct stasis_state_publisher *pub)
 Retrieve the publisher's underlying state's unique id. More...
 
struct stasis_topicstasis_state_publisher_topic (struct stasis_state_publisher *pub)
 Retrieve the publisher's topic. More...
 
void stasis_state_remove_observer (struct stasis_state_manager *manager, struct stasis_state_observer *observer)
 Remove an observer (will no longer receive managed state related events). More...
 
void stasis_state_remove_publish_by_id (struct stasis_state_manager *manager, const char *id, const struct ast_eid *eid, struct stasis_message *msg)
 Publish to a managed named by id topic, and remove an implicit publisher. More...
 
struct stasis_state_subscriberstasis_state_subscribe_pool (struct stasis_state_manager *manager, const char *id, stasis_subscription_cb callback, void *data)
 Add a subscriber, and subscribe to its underlying stasis topic. More...
 
void * stasis_state_subscriber_data (struct stasis_state_subscriber *sub)
 Retrieve the last known state stasis message payload for the subscriber. More...
 
const char * stasis_state_subscriber_id (const struct stasis_state_subscriber *sub)
 Retrieve the underlying subscribed to state's unique id. More...
 
struct stasis_subscriptionstasis_state_subscriber_subscription (struct stasis_state_subscriber *sub)
 Retrieve the stasis topic subscription if available. More...
 
struct stasis_topicstasis_state_subscriber_topic (struct stasis_state_subscriber *sub)
 Retrieve the subscriber's topic. More...
 
struct stasis_topicstasis_state_topic (struct stasis_state_manager *manager, const char *id)
 Retrieve a managed topic creating one if not currently managed. More...
 
void * stasis_state_unsubscribe (struct stasis_state_subscriber *sub)
 Unsubscribe from the stasis topic and stasis state. More...
 
void * stasis_state_unsubscribe_and_join (struct stasis_state_subscriber *sub)
 Unsubscribe from the stasis topic, block until the final message is received, and then unsubscribe from stasis state. More...
 
static struct stasis_statestate_alloc (struct stasis_state_manager *manager, struct stasis_topic *state_topic, const char *id, const char *file, int line, const char *func)
 
static void state_dtor (void *obj)
 
static void state_find_and_remove_eid (struct stasis_state *state, const struct ast_eid *eid)
 
static void state_find_or_add_eid (struct stasis_state *state, const struct ast_eid *eid)
 
static const char * state_id_by_topic (struct stasis_topic *manager_topic, const struct stasis_topic *state_topic)
 
static void state_manager_dtor (void *obj)
 
static void state_proxy_dtor (void *obj)
 
static void state_proxy_sub_cb (void *obj, void *data)
 
static void subscriber_dtor (void *obj)
 

Macro Definition Documentation

◆ STATE_BUCKETS

#define STATE_BUCKETS   57

The number of buckets to use for managed states

Definition at line 77 of file stasis_state.c.

Referenced by stasis_state_manager_create().

◆ state_find_or_add

#define state_find_or_add (   mgr,
  top,
  id 
)    __state_find_or_add(mgr, top, id, __FILE__, __LINE__, __PRETTY_FUNCTION__)

Function Documentation

◆ __state_find_or_add()

static struct stasis_state* __state_find_or_add ( struct stasis_state_manager manager,
struct stasis_topic state_topic,
const char *  id,
const char *  file,
int  line,
const char *  func 
)
static

Definition at line 271 of file stasis_state.c.

References stasis_state_manager::all_topic, ao2_lock, ao2_unlock, ao2_weakproxy_find, ast_strlen_zero, OBJ_NOLOCK, OBJ_SEARCH_KEY, state, state_alloc(), state_id_by_topic(), and stasis_state_manager::states.

274 {
275  struct stasis_state *state;
276 
277  ao2_lock(manager->states);
278  if (ast_strlen_zero(id)) {
279  id = state_id_by_topic(manager->all_topic, state_topic);
280  }
281 
282  state = ao2_weakproxy_find(manager->states, id, OBJ_SEARCH_KEY | OBJ_NOLOCK, "");
283  if (!state) {
284  state = state_alloc(manager, state_topic, id, file, line, func);
285  }
286 
287  ao2_unlock(manager->states);
288 
289  return state;
290 }
enum sip_cc_notify_state state
Definition: chan_sip.c:959
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
#define ao2_unlock(a)
Definition: astobj2.h:730
static const char * state_id_by_topic(struct stasis_topic *manager_topic, const struct stasis_topic *state_topic)
Definition: stasis_state.c:104
#define ast_strlen_zero(foo)
Definition: strings.h:52
struct stasis_topic * all_topic
Definition: stasis_state.c:83
static struct stasis_state * state_alloc(struct stasis_state_manager *manager, struct stasis_topic *state_topic, const char *id, const char *file, int line, const char *func)
Definition: stasis_state.c:167
#define ao2_lock(a)
Definition: astobj2.h:718
#define ao2_weakproxy_find(c, arg, flags, tag)
Perform an ao2_find on a container with ao2_weakproxy objects, returning the real object...
Definition: astobj2.h:1768
struct ao2_container * states
Definition: stasis_state.c:81

◆ AO2_STRING_FIELD_CMP_FN()

AO2_STRING_FIELD_CMP_FN ( stasis_state_proxy  ,
id   
)

◆ AO2_STRING_FIELD_HASH_FN()

AO2_STRING_FIELD_HASH_FN ( stasis_state_proxy  ,
id   
)

◆ handle_stasis_state()

static int handle_stasis_state ( struct stasis_state state,
on_stasis_state  handler,
void *  data 
)
static

Definition at line 708 of file stasis_state.c.

References ao2_bump, ao2_cleanup, ao2_lock, ao2_unlock, handler(), and stasis_state::msg.

Referenced by handle_stasis_state_proxy(), and handle_stasis_state_subscribed().

709 {
710  struct stasis_message *msg;
711  int res;
712 
713  /*
714  * State needs to be locked here while we retrieve and bump the reference on its message
715  * object. Doing so guarantees the message object will live throughout its handling.
716  */
717  ao2_lock(state);
718  msg = ao2_bump(state->msg);
719  ao2_unlock(state);
720 
721  res = handler(state->id, msg, data);
722  ao2_cleanup(msg);
723  return res;
724 }
struct stasis_message * msg
Definition: stasis_state.c:63
#define ao2_unlock(a)
Definition: astobj2.h:730
#define ao2_bump(obj)
Definition: astobj2.h:491
#define ao2_lock(a)
Definition: astobj2.h:718
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static void handler(const char *name, int response_code, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Definition: test_ari.c:59

◆ handle_stasis_state_proxy()

static int handle_stasis_state_proxy ( void *  obj,
void *  arg,
void *  data,
int  flags 
)
static

Definition at line 726 of file stasis_state.c.

References ao2_ref, ao2_weakproxy_get_object, and handle_stasis_state().

Referenced by stasis_state_callback_all().

727 {
728  struct stasis_state *state = ao2_weakproxy_get_object(obj, 0);
729 
730  if (state) {
731  int res;
732  res = handle_stasis_state(state, arg, data);
733  ao2_ref(state, -1);
734  return res;
735  }
736 
737  return 0;
738 }
static int handle_stasis_state(struct stasis_state *state, on_stasis_state handler, void *data)
Definition: stasis_state.c:708
#define ao2_weakproxy_get_object(weakproxy, flags)
Definition: astobj2.h:625
#define ao2_ref(o, delta)
Definition: astobj2.h:464

◆ handle_stasis_state_subscribed()

static int handle_stasis_state_subscribed ( void *  obj,
void *  arg,
void *  data,
int  flags 
)
static

Definition at line 749 of file stasis_state.c.

References ao2_cleanup, ao2_weakproxy_get_object, handle_stasis_state(), and stasis_state::num_subscribers.

Referenced by stasis_state_callback_subscribed().

750 {
751  struct stasis_state *state = ao2_weakproxy_get_object(obj, 0);
752  int res = 0;
753 
754  if (state && state->num_subscribers) {
755  res = handle_stasis_state(state, arg, data);
756  }
757 
758  ao2_cleanup(state);
759 
760  return res;
761 }
static int handle_stasis_state(struct stasis_state *state, on_stasis_state handler, void *data)
Definition: stasis_state.c:708
#define ao2_weakproxy_get_object(weakproxy, flags)
Definition: astobj2.h:625
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
unsigned int num_subscribers
Definition: stasis_state.c:52

◆ publisher_dtor()

static void publisher_dtor ( void *  obj)
static

Definition at line 524 of file stasis_state.c.

References ao2_ref, and stasis_state_publisher::state.

Referenced by stasis_state_add_publisher().

525 {
526  struct stasis_state_publisher *pub = obj;
527 
528  ao2_ref(pub->state, -1);
529 }
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct stasis_state * state
Definition: stasis_state.c:521

◆ stasis_state_add_observer()

int stasis_state_add_observer ( struct stasis_state_manager manager,
struct stasis_state_observer observer 
)

Add an observer to receive managed state related events.

Parameters
managerThe state manager
observerThe observer handling events
Return values
0if successfully registered
-1on failure
Since
13.28.0
16.5.0

Definition at line 688 of file stasis_state.c.

References AST_VECTOR_APPEND, AST_VECTOR_RW_UNLOCK, and AST_VECTOR_RW_WRLOCK.

Referenced by ast_mwi_add_observer(), and subscriptions_create().

690 {
691  int res;
692 
693  AST_VECTOR_RW_WRLOCK(&manager->observers);
694  res = AST_VECTOR_APPEND(&manager->observers, observer);
695  AST_VECTOR_RW_UNLOCK(&manager->observers);
696 
697  return res;
698 }
#define AST_VECTOR_RW_UNLOCK(vec)
Unlock vector.
Definition: vector.h:900
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
#define AST_VECTOR_RW_WRLOCK(vec)
Obtain write lock on vector.
Definition: vector.h:890

◆ stasis_state_add_publisher()

struct stasis_state_publisher* stasis_state_add_publisher ( struct stasis_state_manager manager,
const char *  id 
)

Add a publisher to the managed state for the given id.

Adds a publisher to a managed state based on id. If managed state does not already exists for the given id then new managed state is created. Otherwise the existing state is used.

Parameters
managerThe manager object
idThe unique id of a managed state
Return values
Astasis state publisher
NULLif an error occurred
Since
13.28.0
16.5.0

Definition at line 531 of file stasis_state.c.

References stasis_state_manager::all_topic, AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_alloc_options, ao2_ref, ast_log, LOG_ERROR, NULL, publisher_dtor(), stasis_topic_name(), stasis_state_publisher::state, and state_find_or_add.

Referenced by ast_mwi_add_publisher(), and publishers_create().

533 {
536 
537  if (!pub) {
538  ast_log(LOG_ERROR, "Unable to create publisher to %s/%s\n",
539  stasis_topic_name(manager->all_topic), id);
540  return NULL;
541  }
542 
543  pub->state = state_find_or_add(manager, NULL, id);
544  if (!pub->state) {
545  ao2_ref(pub, -1);
546  return NULL;
547  }
548 
549  return pub;
550 }
#define state_find_or_add(mgr, top, id)
Definition: stasis_state.c:270
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
#define NULL
Definition: resample.c:96
struct stasis_topic * all_topic
Definition: stasis_state.c:83
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static void publisher_dtor(void *obj)
Definition: stasis_state.c:524
#define LOG_ERROR
Definition: logger.h:285
const char * stasis_topic_name(const struct stasis_topic *topic)
Return the name of a topic.
Definition: stasis.c:628
struct stasis_state * state
Definition: stasis_state.c:521

◆ stasis_state_add_subscriber()

struct stasis_state_subscriber* stasis_state_add_subscriber ( struct stasis_state_manager manager,
const char *  id 
)

Add a subscriber to the managed stasis state for the given id.

Adds a subscriber to a managed state based on id. If managed state does not already exists for the given id then new managed state is created. Otherwise the existing state is subscribed to.

Parameters
managerThe manager object
idThe unique id of a managed state
Return values
Astasis state subscriber
NULLif an error occurred
Since
13.28.0
16.5.0

Definition at line 412 of file stasis_state.c.

References stasis_state_manager::all_topic, AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_alloc_options, ao2_lock, ao2_ref, ao2_unlock, ast_log, AST_VECTOR_GET, AST_VECTOR_RW_RDLOCK, AST_VECTOR_RW_UNLOCK, AST_VECTOR_SIZE, LOG_ERROR, NULL, stasis_state::num_subscribers, stasis_topic_name(), stasis_state_subscriber::state, state_find_or_add, sub, and subscriber_dtor().

Referenced by ast_mwi_add_subscriber(), and stasis_state_subscribe_pool().

414 {
415  size_t i;
418 
419  if (!sub) {
420  ast_log(LOG_ERROR, "Unable to create subscriber to %s/%s\n",
421  stasis_topic_name(manager->all_topic), id);
422  return NULL;
423  }
424 
425  sub->state = state_find_or_add(manager, NULL, id);
426  if (!sub->state) {
427  ao2_ref(sub, -1);
428  return NULL;
429  }
430 
431  ao2_lock(sub->state);
432  ++sub->state->num_subscribers;
433  ao2_unlock(sub->state);
434 
435  AST_VECTOR_RW_RDLOCK(&manager->observers);
436  for (i = 0; i < AST_VECTOR_SIZE(&manager->observers); ++i) {
437  if (AST_VECTOR_GET(&manager->observers, i)->on_subscribe) {
438  AST_VECTOR_GET(&manager->observers, i)->on_subscribe(id, sub);
439  }
440  }
441  AST_VECTOR_RW_UNLOCK(&manager->observers);
442 
443  return sub;
444 }
struct stasis_state * state
Definition: stasis_state.c:386
#define state_find_or_add(mgr, top, id)
Definition: stasis_state.c:270
#define AST_VECTOR_RW_UNLOCK(vec)
Unlock vector.
Definition: vector.h:900
#define AST_VECTOR_RW_RDLOCK(vec)
Obtain read lock on vector.
Definition: vector.h:880
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
#define ao2_unlock(a)
Definition: astobj2.h:730
#define NULL
Definition: resample.c:96
struct stasis_topic * all_topic
Definition: stasis_state.c:83
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_lock(a)
Definition: astobj2.h:718
#define LOG_ERROR
Definition: logger.h:285
static void subscriber_dtor(void *obj)
Definition: stasis_state.c:391
const char * stasis_topic_name(const struct stasis_topic *topic)
Return the name of a topic.
Definition: stasis.c:628
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:682
struct stasis_forward * sub
Definition: res_corosync.c:240
unsigned int num_subscribers
Definition: stasis_state.c:52
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611

◆ stasis_state_all_topic()

struct stasis_topic* stasis_state_all_topic ( struct stasis_state_manager manager)

Retrieve the manager's topic (the topic that all state topics get forwarded to)

Parameters
managerThe manager object
Return values
Themanager's topic.
Since
13.28.0
16.5.0

Definition at line 364 of file stasis_state.c.

References stasis_state_manager::all_topic.

Referenced by ast_mwi_topic_all().

365 {
366  return manager->all_topic;
367 }
struct stasis_topic * all_topic
Definition: stasis_state.c:83

◆ stasis_state_callback_all()

void stasis_state_callback_all ( struct stasis_state_manager manager,
on_stasis_state  handler,
void *  data 
)

For each managed state call the given handler.

Parameters
managerThe state manager
handlerThe handler to call for each managed state
dataUser to data to pass on to the handler
Since
13.28.0
16.5.0

Definition at line 740 of file stasis_state.c.

References ao2_callback_data, ast_assert, handle_stasis_state_proxy(), NULL, OBJ_MULTIPLE, OBJ_NODATA, and stasis_state_manager::states.

Referenced by ast_mwi_state_callback_all(), and publish().

742 {
743  ast_assert(handler != NULL);
744 
747 }
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
#define ao2_callback_data(container, flags, cb_fn, arg, data)
Definition: astobj2.h:1743
static int handle_stasis_state_proxy(void *obj, void *arg, void *data, int flags)
Definition: stasis_state.c:726
static void handler(const char *name, int response_code, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Definition: test_ari.c:59
struct ao2_container * states
Definition: stasis_state.c:81

◆ stasis_state_callback_subscribed()

void stasis_state_callback_subscribed ( struct stasis_state_manager manager,
on_stasis_state  handler,
void *  data 
)

For each managed, and explicitly subscribed state call the given handler.

Parameters
managerThe state manager
handlerThe handler to call for each managed state
dataUser to data to pass on to the handler
Since
13.28.0
16.5.0

Definition at line 763 of file stasis_state.c.

References ao2_callback_data, ast_assert, handle_stasis_state_subscribed(), NULL, OBJ_MULTIPLE, OBJ_NODATA, and stasis_state_manager::states.

Referenced by ast_mwi_state_callback_subscribed().

765 {
766  ast_assert(handler != NULL);
767 
770 }
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
static int handle_stasis_state_subscribed(void *obj, void *arg, void *data, int flags)
Definition: stasis_state.c:749
#define ao2_callback_data(container, flags, cb_fn, arg, data)
Definition: astobj2.h:1743
static void handler(const char *name, int response_code, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Definition: test_ari.c:59
struct ao2_container * states
Definition: stasis_state.c:81

◆ stasis_state_manager_create()

struct stasis_state_manager* stasis_state_manager_create ( const char *  topic_name)

Create a stasis state manager.

Note
The state manager is an ao2_object. When done simply decrement its reference for object cleanup.
Parameters
topic_nameThe name of the topic to create that all state topics get forwarded to
Return values
Astasis state manager
NULLif an error occurred
Since
13.28.0
16.5.0

Definition at line 324 of file stasis_state.c.

References stasis_state_manager::all_topic, AO2_ALLOC_OPT_LOCK_MUTEX, AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_alloc_options, ao2_container_alloc_hash, ao2_container_register(), ao2_ref, ast_alloca, AST_VECTOR_RW_INIT, stasis_state_proxy::manager, NULL, stasis_topic_create(), stasis_topic_name(), STATE_BUCKETS, state_manager_dtor(), and stasis_state_manager::states.

Referenced by AST_TEST_DEFINE(), and mwi_init().

325 {
326  struct stasis_state_manager *manager;
327 
328  manager = ao2_alloc_options(sizeof(*manager), state_manager_dtor,
330  if (!manager) {
331  return NULL;
332  }
333 
335  STATE_BUCKETS, stasis_state_proxy_hash_fn, NULL, stasis_state_proxy_cmp_fn);
336  if (!manager->states) {
337  ao2_ref(manager, -1);
338  return NULL;
339  }
340 
341  manager->all_topic = stasis_topic_create(topic_name);
342  if (!manager->all_topic) {
343  ao2_ref(manager, -1);
344  return NULL;
345  }
346 
347  if (AST_VECTOR_RW_INIT(&manager->observers, 2) != 0) {
348  ao2_ref(manager, -1);
349  return NULL;
350  }
351 
352 #ifdef AO2_DEBUG
353  {
354  char *container_name =
355  ast_alloca(strlen(stasis_topic_name(manager->all_topic)) + strlen("-manager") + 1);
356  sprintf(container_name, "%s-manager", stasis_topic_name(manager->all_topic));
357  ao2_container_register(container_name, manager->states, state_prnt_obj);
358  }
359 #endif
360 
361  return manager;
362 }
#define AST_VECTOR_RW_INIT(vec, size)
Initialize a vector with a read/write lock.
Definition: vector.h:158
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
#define NULL
Definition: resample.c:96
struct stasis_topic * all_topic
Definition: stasis_state.c:83
int ao2_container_register(const char *name, struct ao2_container *self, ao2_prnt_obj_fn *prnt_obj)
Register a container for CLI stats and integrity check.
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct stasis_topic * stasis_topic_create(const char *name)
Create a new topic.
Definition: stasis.c:618
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
const char * stasis_topic_name(const struct stasis_topic *topic)
Return the name of a topic.
Definition: stasis.c:628
#define STATE_BUCKETS
Definition: stasis_state.c:77
static void state_manager_dtor(void *obj)
Definition: stasis_state.c:292
struct ao2_container * states
Definition: stasis_state.c:81

◆ stasis_state_publish()

void stasis_state_publish ( struct stasis_state_publisher pub,
struct stasis_message msg 
)

Publish to a managed state (topic) using a publisher.

Parameters
pubThe publisher to use to publish the message
msgThe message to publish
Since
13.28.0
16.5.0

Definition at line 562 of file stasis_state.c.

References ao2_lock, ao2_replace, ao2_unlock, stasis_state::msg, stasis_publish(), stasis_state_publisher::state, and stasis_state::topic.

Referenced by ast_mwi_publish(), and explicit_publish_cb().

563 {
564  ao2_lock(pub->state);
565  ao2_replace(pub->state->msg, msg);
566  ao2_unlock(pub->state);
567 
568  stasis_publish(pub->state->topic, msg);
569 }
struct stasis_topic * topic
Definition: stasis_state.c:61
struct stasis_message * msg
Definition: stasis_state.c:63
#define ao2_unlock(a)
Definition: astobj2.h:730
#define ao2_lock(a)
Definition: astobj2.h:718
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_replace(dst, src)
Definition: astobj2.h:517
struct stasis_state * state
Definition: stasis_state.c:521

◆ stasis_state_publish_by_id()

void stasis_state_publish_by_id ( struct stasis_state_manager manager,
const char *  id,
const struct ast_eid eid,
struct stasis_message msg 
)

Publish to a managed named by id topic, and add an implicit subscriber.

Note
It is recommended when adding new publisher functionality within a module to create and use an explicit publisher instead of using this method.

This creates an implicit publisher keyed off the eid. This ability was mainly implemented in order to maintain compatibility with already established code. Allowing the creation of an implicit publisher made is so less changes were required when stasis state module was initially added.

There should only ever be one publisher for a specifically named managed topic within the system. This being the case we can use the eid to implicitly track the publisher. However once publishing is no longer needed for a topic a call to stasis_state_remove_publish_by_id is required in order to remove the implicit publisher. Thus allowing for its eventual destruction. Without the call to remove a memory leak will occur.

Parameters
managerThe state manager
idA state's unique id
eidThe unique system id
msgThe message to publish
Since
13.28.0
16.5.0

Definition at line 638 of file stasis_state.c.

References ao2_lock, ao2_ref, ao2_replace, ao2_unlock, stasis_state::msg, NULL, stasis_publish(), state, state_find_or_add, state_find_or_add_eid(), and stasis_state::topic.

Referenced by ast_mwi_publish_by_mailbox(), and implicit_publish_cb().

640 {
641  struct stasis_state *state;
642 
643  state = state_find_or_add(manager, NULL, id);
644  if (!state) {
645  return;
646  }
647 
648  ao2_lock(state);
649  state_find_or_add_eid(state, eid);
650  ao2_replace(state->msg, msg);
651  ao2_unlock(state);
652 
653  stasis_publish(state->topic, msg);
654 
655  ao2_ref(state, -1);
656 }
enum sip_cc_notify_state state
Definition: chan_sip.c:959
#define state_find_or_add(mgr, top, id)
Definition: stasis_state.c:270
struct stasis_topic * topic
Definition: stasis_state.c:61
struct stasis_message * msg
Definition: stasis_state.c:63
#define ao2_unlock(a)
Definition: astobj2.h:730
#define NULL
Definition: resample.c:96
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_lock(a)
Definition: astobj2.h:718
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_replace(dst, src)
Definition: astobj2.h:517
static void state_find_or_add_eid(struct stasis_state *state, const struct ast_eid *eid)
Definition: stasis_state.c:586

◆ stasis_state_publisher_id()

const char* stasis_state_publisher_id ( const struct stasis_state_publisher pub)

Retrieve the publisher's underlying state's unique id.

Parameters
pubA stasis state publisher
Return values
Themanaged state's id
Since
13.28.0
16.5.0

Definition at line 552 of file stasis_state.c.

References stasis_state_publisher::state.

Referenced by ast_mwi_publish(), and explicit_publish_cb().

553 {
554  return pub->state->id;
555 }
struct stasis_state * state
Definition: stasis_state.c:521

◆ stasis_state_publisher_topic()

struct stasis_topic* stasis_state_publisher_topic ( struct stasis_state_publisher pub)

Retrieve the publisher's topic.

Note
Returned topic's reference count is NOT incremented. However, the topic is guaranteed to live for the lifetime of the publisher.
Parameters
pubA stasis state publisher
Return values
Thepublisher's topic
Since
13.28.0
16.5.0

Definition at line 557 of file stasis_state.c.

References stasis_state_publisher::state, and stasis_state::topic.

558 {
559  return pub->state->topic;
560 }
struct stasis_topic * topic
Definition: stasis_state.c:61
struct stasis_state * state
Definition: stasis_state.c:521

◆ stasis_state_remove_observer()

void stasis_state_remove_observer ( struct stasis_state_manager manager,
struct stasis_state_observer observer 
)

Remove an observer (will no longer receive managed state related events).

Parameters
managerThe state manager
observerThe observer being removed
Since
13.28.0
16.5.0

Definition at line 700 of file stasis_state.c.

References AST_VECTOR_ELEM_CLEANUP_NOOP, AST_VECTOR_REMOVE_ELEM_UNORDERED, AST_VECTOR_RW_UNLOCK, and AST_VECTOR_RW_WRLOCK.

Referenced by ast_mwi_remove_observer(), and subscriptions_destroy().

702 {
703  AST_VECTOR_RW_WRLOCK(&manager->observers);
704  AST_VECTOR_REMOVE_ELEM_UNORDERED(&manager->observers, observer, AST_VECTOR_ELEM_CLEANUP_NOOP);
705  AST_VECTOR_RW_UNLOCK(&manager->observers);
706 }
#define AST_VECTOR_REMOVE_ELEM_UNORDERED(vec, elem, cleanup)
Remove an element from a vector.
Definition: vector.h:585
#define AST_VECTOR_RW_UNLOCK(vec)
Unlock vector.
Definition: vector.h:900
#define AST_VECTOR_ELEM_CLEANUP_NOOP(elem)
Vector element cleanup that does nothing.
Definition: vector.h:573
#define AST_VECTOR_RW_WRLOCK(vec)
Obtain write lock on vector.
Definition: vector.h:890

◆ stasis_state_remove_publish_by_id()

void stasis_state_remove_publish_by_id ( struct stasis_state_manager manager,
const char *  id,
const struct ast_eid eid,
struct stasis_message msg 
)

Publish to a managed named by id topic, and remove an implicit publisher.

This function should be called after calling stasis_state_publish_by_id at least once for the same manager, id, and eid. If the given stasis message is NULL then the implicit publisher is removed, but no last message is published.

See note and description on stasis_state_publish_by_id for more details about if, and when this function should be used.

Parameters
managerThe state manager
idA state's unique id
eidThe unique system id
msgThe message to publish (can be NULL)
Since
13.28.0
16.5.0

Definition at line 658 of file stasis_state.c.

References ao2_lock, ao2_ref, ao2_unlock, ao2_weakproxy_find, ast_debug, OBJ_SEARCH_KEY, stasis_publish(), state_find_and_remove_eid(), stasis_state_manager::states, and stasis_state::topic.

Referenced by ast_delete_mwi_state_full(), and publishers_destroy().

660 {
661  struct stasis_state *state = ao2_weakproxy_find(manager->states, id, OBJ_SEARCH_KEY, "");
662 
663  if (!state) {
664  /*
665  * In most circumstances state should already exist here. However, if there is no
666  * state then it can mean one of a few things:
667  *
668  * 1. This function was called prior to an implicit publish for the same given
669  * manager, and id.
670  * 2. This function was called more than once for the same manager, and id.
671  * 3. There is ref count problem with the explicit subscribers, and publishers.
672  */
673  ast_debug(5, "Attempted to remove state for id '%s', but state not found\n", id);
674  return;
675  }
676 
677  if (msg) {
678  stasis_publish(state->topic, msg);
679  }
680 
681  ao2_lock(state);
682  state_find_and_remove_eid(state, eid);
683  ao2_unlock(state);
684 
685  ao2_ref(state, -1);
686 }
struct stasis_topic * topic
Definition: stasis_state.c:61
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
#define ao2_unlock(a)
Definition: astobj2.h:730
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_lock(a)
Definition: astobj2.h:718
#define ao2_weakproxy_find(c, arg, flags, tag)
Perform an ao2_find on a container with ao2_weakproxy objects, returning the real object...
Definition: astobj2.h:1768
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic&#39;s subscribers.
Definition: stasis.c:1511
static void state_find_and_remove_eid(struct stasis_state *state, const struct ast_eid *eid)
Definition: stasis_state.c:620
struct ao2_container * states
Definition: stasis_state.c:81

◆ stasis_state_subscribe_pool()

struct stasis_state_subscriber* stasis_state_subscribe_pool ( struct stasis_state_manager manager,
const char *  id,
stasis_subscription_cb  callback,
void *  data 
)

Add a subscriber, and subscribe to its underlying stasis topic.

Adds a subscriber to a managed state based on id. If managed state does not already exists for the given id then new managed state is created. Otherwise the existing state is subscribed to. If the state is successfully subscribed to then a stasis subscription is subsequently created as well.

Parameters
managerThe manager object
idThe unique id of a managed state
callbackThe stasis subscription callback
dataA user data object passed to the stasis subscription
Return values
Astasis state subscriber
NULLif an error occurred
Since
13.28.0
16.5.0

Definition at line 446 of file stasis_state.c.

References ao2_ref, ast_debug, NULL, stasis_state_add_subscriber(), stasis_state_subscriber::stasis_sub, stasis_subscribe_pool, stasis_topic_name(), stasis_state_subscriber::state, sub, and stasis_state::topic.

Referenced by ast_mwi_subscribe_pool(), and subscriptions_create().

448 {
449  struct stasis_topic *topic;
451 
452  if (!sub) {
453  return NULL;
454  }
455 
456  topic = sub->state->topic;
457  ast_debug(3, "Creating stasis state subscription to id '%s'. Topic: '%s':%p %d\n",
458  id, stasis_topic_name(topic), topic, (int)ao2_ref(topic, 0));
459 
460  sub->stasis_sub = stasis_subscribe_pool(topic, callback, data);
461 
462  if (!sub->stasis_sub) {
463  ao2_ref(sub, -1);
464  return NULL;
465  }
466 
467  return sub;
468 }
struct stasis_state * state
Definition: stasis_state.c:386
struct stasis_topic * topic
Definition: stasis_state.c:61
#define NULL
Definition: resample.c:96
struct stasis_state_subscriber * stasis_state_add_subscriber(struct stasis_state_manager *manager, const char *id)
Add a subscriber to the managed stasis state for the given id.
Definition: stasis_state.c:412
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ao2_ref(o, delta)
Definition: astobj2.h:464
const char * stasis_topic_name(const struct stasis_topic *topic)
Return the name of a topic.
Definition: stasis.c:628
#define stasis_subscribe_pool(topic, callback, data)
Definition: stasis.h:682
struct stasis_subscription * stasis_sub
Definition: stasis_state.c:388
struct stasis_forward * sub
Definition: res_corosync.c:240

◆ stasis_state_subscriber_data()

void* stasis_state_subscriber_data ( struct stasis_state_subscriber sub)

Retrieve the last known state stasis message payload for the subscriber.

If a stasis message has been published to this state, this function returns that message's payload object. If no stasis message has been published on the state, or the message's payload does not exist then NULL is returned.

Note
Returned data's reference count is incremented
Parameters
subA stasis state subscriber
Return values
Thesubscriber's state message data
NULLif no data has been published yet
Since
13.28.0
16.5.0

Definition at line 497 of file stasis_state.c.

References ao2_bump, ao2_lock, ao2_unlock, stasis_state::msg, stasis_message_data(), and stasis_state_subscriber::state.

Referenced by ast_mwi_subscriber_data(), and handle_validate().

498 {
499  void *res;
500 
501  /*
502  * The data's reference needs to be bumped before returning so it doesn't disappear
503  * for the caller. Lock state, so the underlying message data is not replaced while
504  * retrieving.
505  */
506  ao2_lock(sub->state);
507  res = ao2_bump(stasis_message_data(sub->state->msg));
508  ao2_unlock(sub->state);
509 
510  return res;
511 }
struct stasis_state * state
Definition: stasis_state.c:386
struct stasis_message * msg
Definition: stasis_state.c:63
#define ao2_unlock(a)
Definition: astobj2.h:730
#define ao2_bump(obj)
Definition: astobj2.h:491
#define ao2_lock(a)
Definition: astobj2.h:718
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.

◆ stasis_state_subscriber_id()

const char* stasis_state_subscriber_id ( const struct stasis_state_subscriber sub)

Retrieve the underlying subscribed to state's unique id.

Parameters
subA stasis state subscriber
Return values
Themanaged state's id
Since
13.28.0
16.5.0

Definition at line 487 of file stasis_state.c.

References stasis_state_subscriber::state.

Referenced by ast_mwi_subscriber_data().

488 {
489  return sub->state->id;
490 }
struct stasis_state * state
Definition: stasis_state.c:386

◆ stasis_state_subscriber_subscription()

struct stasis_subscription* stasis_state_subscriber_subscription ( struct stasis_state_subscriber sub)

Retrieve the stasis topic subscription if available.

Parameters
subA stasis state subscriber
Return values
Thesubscriber's stasis subscription
NULLif no subscription available
Since
13.28.0
16.5.0

Definition at line 513 of file stasis_state.c.

References stasis_state_subscriber::stasis_sub.

Referenced by ast_mwi_subscriber_subscription().

515 {
516  return sub->stasis_sub;
517 }
struct stasis_subscription * stasis_sub
Definition: stasis_state.c:388

◆ stasis_state_subscriber_topic()

struct stasis_topic* stasis_state_subscriber_topic ( struct stasis_state_subscriber sub)

Retrieve the subscriber's topic.

Note
Returned topic's reference count is NOT incremented. However, the topic is guaranteed to live for the lifetime of the subscriber.
Parameters
subA stasis state subscriber
Return values
Thesubscriber's topic
Since
13.28.0
16.5.0

Definition at line 492 of file stasis_state.c.

References stasis_state_subscriber::state, and stasis_state::topic.

Referenced by ast_mwi_subscriber_topic().

493 {
494  return sub->state->topic;
495 }
struct stasis_state * state
Definition: stasis_state.c:386
struct stasis_topic * topic
Definition: stasis_state.c:61

◆ stasis_state_topic()

struct stasis_topic* stasis_state_topic ( struct stasis_state_manager manager,
const char *  id 
)

Retrieve a managed topic creating one if not currently managed.

WARNING This function should not be called before adding a publisher or subscriber or it will cause a memory leak within the stasis state manager. This function is here in order to allow for compatibility with how things used to work.

Also much like the similar functionality from before it returns the stasis topic, but does not bump its reference.

Parameters
managerThe manager object
idThe unique id of/for the topic
Return values
Amanaged stasis topic.
NULLif an error occurred
Since
13.28.0
16.5.0

Definition at line 369 of file stasis_state.c.

References ao2_ref, NULL, state, state_find_or_add, and stasis_state::topic.

Referenced by ast_mwi_topic().

370 {
371  struct stasis_topic *topic;
372  struct stasis_state *state;
373 
374  state = state_find_or_add(manager, NULL, id);
375  if (!state) {
376  return NULL;
377  }
378 
379  topic = state->topic;
380  ao2_ref(state, -1);
381  return topic;
382 }
enum sip_cc_notify_state state
Definition: chan_sip.c:959
#define state_find_or_add(mgr, top, id)
Definition: stasis_state.c:270
struct stasis_topic * topic
Definition: stasis_state.c:61
#define NULL
Definition: resample.c:96
#define ao2_ref(o, delta)
Definition: astobj2.h:464

◆ stasis_state_unsubscribe()

void* stasis_state_unsubscribe ( struct stasis_state_subscriber sub)

Unsubscribe from the stasis topic and stasis state.

Parameters
subA stasis state subscriber
Return values
NULL
Since
13.28.0
16.5.0

Definition at line 470 of file stasis_state.c.

References ao2_ref, NULL, stasis_state_subscriber::stasis_sub, and stasis_unsubscribe().

Referenced by ast_mwi_unsubscribe().

471 {
473  ao2_ref(sub, -1);
474  return NULL;
475 }
#define NULL
Definition: resample.c:96
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct stasis_subscription * stasis_sub
Definition: stasis_state.c:388
struct stasis_subscription * stasis_unsubscribe(struct stasis_subscription *subscription)
Cancel a subscription.
Definition: stasis.c:973

◆ stasis_state_unsubscribe_and_join()

void* stasis_state_unsubscribe_and_join ( struct stasis_state_subscriber sub)

Unsubscribe from the stasis topic, block until the final message is received, and then unsubscribe from stasis state.

Parameters
subA stasis state subscriber
Return values
NULL
Since
13.28.0
16.5.0

Definition at line 477 of file stasis_state.c.

References ao2_ref, NULL, stasis_state_subscriber::stasis_sub, and stasis_unsubscribe_and_join().

Referenced by ast_mwi_unsubscribe_and_join(), and subscriptions_destroy().

478 {
479  if (sub) {
481  ao2_ref(sub, -1);
482  }
483 
484  return NULL;
485 }
#define NULL
Definition: resample.c:96
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct stasis_subscription * stasis_sub
Definition: stasis_state.c:388
struct stasis_subscription * stasis_unsubscribe_and_join(struct stasis_subscription *subscription)
Cancel a subscription, blocking until the last message is processed.
Definition: stasis.c:1136

◆ state_alloc()

static struct stasis_state* state_alloc ( struct stasis_state_manager manager,
struct stasis_topic state_topic,
const char *  id,
const char *  file,
int  line,
const char *  func 
)
static

Definition at line 167 of file stasis_state.c.

References __ao2_alloc(), stasis_state_manager::all_topic, AO2_ALLOC_OPT_LOCK_MUTEX, ao2_bump, ao2_cleanup, ao2_link_flags, ao2_ref, ao2_t_weakproxy_alloc, ao2_t_weakproxy_set_object, ao2_weakproxy_subscribe(), ast_asprintf, ast_assert, ast_free, ast_log, AST_VECTOR_INIT, stasis_state::forward, stasis_state_proxy::id, LOG_ERROR, stasis_state_proxy::manager, stasis_state::manager, name, NULL, OBJ_NOLOCK, stasis_forward_all(), stasis_topic_create(), stasis_topic_name(), state, state_dtor(), state_id_by_topic(), state_proxy_dtor(), state_proxy_sub_cb(), stasis_state_manager::states, and stasis_state::topic.

Referenced by __state_find_or_add().

170 {
171  struct stasis_state_proxy *proxy = NULL;
172  struct stasis_state *state = NULL;
173 
174  if (!id) {
175  /* If not given an id, then a state topic is required */
176  ast_assert(state_topic != NULL);
177 
178  /* Get the id we'll key off of from the state topic */
179  id = state_id_by_topic(manager->all_topic, state_topic);
180  }
181 
182  state = __ao2_alloc(sizeof(*state), state_dtor, AO2_ALLOC_OPT_LOCK_MUTEX, id, file, line, func);
183  if (!state) {
184  goto error_return;
185  }
186 
187  if (!state_topic) {
188  char *name;
189 
190  /*
191  * To provide further detail and to ensure that the topic is unique within the
192  * scope of the system we prefix it with the manager's topic name, which should
193  * itself already be unique.
194  */
195  if (ast_asprintf(&name, "%s/%s", stasis_topic_name(manager->all_topic), id) < 0) {
196  goto error_return;
197  }
198 
199  state->topic = stasis_topic_create(name);
200 
201  ast_free(name);
202  if (!state->topic) {
203  goto error_return;
204  }
205  } else {
206  /*
207  * Since the state topic was passed in, go ahead and bump its reference.
208  * By doing this here first, it allows us to consistently decrease the reference on
209  * state allocation error.
210  */
211  ao2_ref(state_topic, +1);
212  state->topic = state_topic;
213  }
214 
215  proxy = ao2_t_weakproxy_alloc(sizeof(*proxy) + strlen(id) + 1, state_proxy_dtor, id);
216  if (!proxy) {
217  goto error_return;
218  }
219 
220  strcpy(proxy->id, id); /* Safe */
221 
222  state->id = proxy->id;
223  proxy->manager = ao2_bump(manager);
224  state->manager = proxy->manager; /* state->manager is owned by the proxy */
225 
226  state->forward = stasis_forward_all(state->topic, manager->all_topic);
227  if (!state->forward) {
228  goto error_return;
229  }
230 
231  if (AST_VECTOR_INIT(&state->eids, 2)) {
232  goto error_return;
233  }
234 
235  if (ao2_t_weakproxy_set_object(proxy, state, OBJ_NOLOCK, "weakproxy link")) {
236  goto error_return;
237  }
238 
240  goto error_return;
241  }
242 
243  if (!ao2_link_flags(manager->states, proxy, OBJ_NOLOCK)) {
244  goto error_return;
245  }
246 
247  ao2_ref(proxy, -1);
248 
249  return state;
250 
251 error_return:
252  ast_log(LOG_ERROR, "Unable to allocate state '%s' in manager '%s'\n",
253  id, stasis_topic_name(manager->all_topic));
254  ao2_cleanup(state);
255  ao2_cleanup(proxy);
256  return NULL;
257 }
struct stasis_forward * forward
Definition: stasis_state.c:59
enum sip_cc_notify_state state
Definition: chan_sip.c:959
static void state_dtor(void *obj)
Definition: stasis_state.c:121
struct stasis_topic * topic
Definition: stasis_state.c:61
static void state_proxy_dtor(void *obj)
Definition: stasis_state.c:136
struct stasis_state_manager * manager
The manager that owns and handles this state.
Definition: stasis_state.c:57
void * __ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, unsigned int options, const char *tag, const char *file, int line, const char *func) attribute_warn_unused_result
Definition: astobj2.c:765
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
#define ast_assert(a)
Definition: utils.h:695
#define ao2_link_flags(container, obj, flags)
Definition: astobj2.h:1572
int ao2_weakproxy_subscribe(void *weakproxy, ao2_weakproxy_notification_cb cb, void *data, int flags)
Request notification when weakproxy points to NULL.
Definition: astobj2.c:931
#define NULL
Definition: resample.c:96
static void state_proxy_sub_cb(void *obj, void *data)
Definition: stasis_state.c:142
static const char * state_id_by_topic(struct stasis_topic *manager_topic, const struct stasis_topic *state_topic)
Definition: stasis_state.c:104
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:269
struct stasis_topic * all_topic
Definition: stasis_state.c:83
#define ao2_bump(obj)
Definition: astobj2.h:491
#define ast_log
Definition: astobj2.c:42
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct stasis_state_manager * manager
Definition: stasis_state.c:34
struct stasis_topic * stasis_topic_create(const char *name)
Create a new topic.
Definition: stasis.c:618
#define LOG_ERROR
Definition: logger.h:285
#define ao2_t_weakproxy_set_object(weakproxy, obj, flags, tag)
Definition: astobj2.h:586
const char * stasis_topic_name(const struct stasis_topic *topic)
Return the name of a topic.
Definition: stasis.c:628
static const char name[]
Definition: cdr_mysql.c:74
#define ast_free(a)
Definition: astmm.h:182
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define ao2_t_weakproxy_alloc(data_size, destructor_fn, tag)
Definition: astobj2.h:557
struct ao2_container * states
Definition: stasis_state.c:81
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

◆ state_dtor()

static void state_dtor ( void *  obj)
static

Definition at line 121 of file stasis_state.c.

References ao2_cleanup, ast_assert, AST_VECTOR_FREE, AST_VECTOR_SIZE, stasis_state::forward, stasis_state::msg, NULL, stasis_forward_cancel(), and stasis_state::topic.

Referenced by state_alloc().

122 {
123  struct stasis_state *state = obj;
124 
125  state->forward = stasis_forward_cancel(state->forward);
126  ao2_cleanup(state->topic);
127  state->topic = NULL;
128  ao2_cleanup(state->msg);
129  state->msg = NULL;
130 
131  /* All eids should have been removed */
132  ast_assert(AST_VECTOR_SIZE(&state->eids) == 0);
133  AST_VECTOR_FREE(&state->eids);
134 }
struct stasis_forward * forward
Definition: stasis_state.c:59
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition: vector.h:174
struct stasis_topic * topic
Definition: stasis_state.c:61
struct stasis_message * msg
Definition: stasis_state.c:63
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
struct stasis_forward * stasis_forward_cancel(struct stasis_forward *forward)
Definition: stasis.c:1548
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611

◆ state_find_and_remove_eid()

static void state_find_and_remove_eid ( struct stasis_state state,
const struct ast_eid eid 
)
static

Definition at line 620 of file stasis_state.c.

References ao2_ref, ast_eid_cmp(), ast_eid_default, AST_VECTOR_GET_ADDR, AST_VECTOR_REMOVE_UNORDERED, and AST_VECTOR_SIZE.

Referenced by stasis_state_remove_publish_by_id().

621 {
622  size_t i;
623 
624  if (!eid) {
625  eid = &ast_eid_default;
626  }
627 
628  for (i = 0; i < AST_VECTOR_SIZE(&state->eids); ++i) {
629  if (!ast_eid_cmp(AST_VECTOR_GET_ADDR(&state->eids, i), eid)) {
630  AST_VECTOR_REMOVE_UNORDERED(&state->eids, i);
631  /* Balance the reference from state_find_or_add_eid */
632  ao2_ref(state, -1);
633  return;
634  }
635  }
636 }
#define AST_VECTOR_REMOVE_UNORDERED(vec, idx)
Remove an element from an unordered vector by index.
Definition: vector.h:438
int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
Compare two EIDs.
Definition: main/utils.c:2842
#define AST_VECTOR_GET_ADDR(vec, idx)
Get an address of element in a vector.
Definition: vector.h:670
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611

◆ state_find_or_add_eid()

static void state_find_or_add_eid ( struct stasis_state state,
const struct ast_eid eid 
)
static

Definition at line 586 of file stasis_state.c.

References ao2_ref, ast_eid_cmp(), ast_eid_default, AST_VECTOR_APPEND, AST_VECTOR_GET_ADDR, and AST_VECTOR_SIZE.

Referenced by stasis_state_publish_by_id().

587 {
588  size_t i;
589 
590  if (!eid) {
591  eid = &ast_eid_default;
592  }
593 
594  for (i = 0; i < AST_VECTOR_SIZE(&state->eids); ++i) {
595  if (!ast_eid_cmp(AST_VECTOR_GET_ADDR(&state->eids, i), eid)) {
596  break;
597  }
598  }
599 
600  if (i == AST_VECTOR_SIZE(&state->eids)) {
601  if (!AST_VECTOR_APPEND(&state->eids, *eid)) {
602  /* This ensures state cannot be freed if it has any eids */
603  ao2_ref(state, +1);
604  }
605  }
606 }
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
Compare two EIDs.
Definition: main/utils.c:2842
#define AST_VECTOR_GET_ADDR(vec, idx)
Get an address of element in a vector.
Definition: vector.h:670
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611

◆ state_id_by_topic()

static const char* state_id_by_topic ( struct stasis_topic manager_topic,
const struct stasis_topic state_topic 
)
static

Definition at line 104 of file stasis_state.c.

References ast_assert, ast_begins_with(), stasis_state_proxy::id, NULL, and stasis_topic_name().

Referenced by __state_find_or_add(), and state_alloc().

106 {
107  const char *id;
108 
109  /* This topic should always belong to the manager */
111  stasis_topic_name(state_topic)));
112 
113  id = strchr(stasis_topic_name(state_topic), '/');
114 
115  /* The state's unique id should always exist */
116  ast_assert(id != NULL && (id + 1) != NULL);
117 
118  return (id + 1);
119 }
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
const char * stasis_topic_name(const struct stasis_topic *topic)
Return the name of a topic.
Definition: stasis.c:628
static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
Definition: strings.h:94
enum queue_result id
Definition: app_queue.c:1507

◆ state_manager_dtor()

static void state_manager_dtor ( void *  obj)
static

Definition at line 292 of file stasis_state.c.

References stasis_state_manager::all_topic, ao2_cleanup, ao2_container_unregister(), ast_alloca, AST_VECTOR_RW_FREE, stasis_state_proxy::manager, NULL, stasis_topic_name(), stasis_state_manager::states, and stasis_state::topic.

Referenced by stasis_state_manager_create().

293 {
294  struct stasis_state_manager *manager = obj;
295 
296 #ifdef AO2_DEBUG
297  {
298  char *container_name =
299  ast_alloca(strlen(stasis_topic_name(manager->all_topic)) + strlen("-manager") + 1);
300  sprintf(container_name, "%s-manager", stasis_topic_name(manager->all_topic));
301  ao2_container_unregister(container_name);
302  }
303 #endif
304 
305  ao2_cleanup(manager->states);
306  manager->states = NULL;
307  ao2_cleanup(manager->all_topic);
308  manager->all_topic = NULL;
309  AST_VECTOR_RW_FREE(&manager->observers);
310 }
#define NULL
Definition: resample.c:96
struct stasis_topic * all_topic
Definition: stasis_state.c:83
void ao2_container_unregister(const char *name)
Unregister a container for CLI stats and integrity check.
#define AST_VECTOR_RW_FREE(vec)
Deallocates this locked vector.
Definition: vector.h:202
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
const char * stasis_topic_name(const struct stasis_topic *topic)
Return the name of a topic.
Definition: stasis.c:628
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct ao2_container * states
Definition: stasis_state.c:81

◆ state_proxy_dtor()

static void state_proxy_dtor ( void *  obj)
static

Definition at line 136 of file stasis_state.c.

References ao2_cleanup, and stasis_state_proxy::manager.

Referenced by state_alloc().

136  {
137  struct stasis_state_proxy *proxy = obj;
138 
139  ao2_cleanup(proxy->manager);
140 }
struct stasis_state_manager * manager
Definition: stasis_state.c:34
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ state_proxy_sub_cb()

static void state_proxy_sub_cb ( void *  obj,
void *  data 
)
static

Definition at line 142 of file stasis_state.c.

References ao2_unlink, stasis_state_proxy::manager, and stasis_state_manager::states.

Referenced by state_alloc().

143 {
144  struct stasis_state_proxy *proxy = obj;
145 
146  ao2_unlink(proxy->manager->states, proxy);
147 }
struct stasis_state_manager * manager
Definition: stasis_state.c:34
#define ao2_unlink(container, obj)
Definition: astobj2.h:1598
struct ao2_container * states
Definition: stasis_state.c:81

◆ subscriber_dtor()

static void subscriber_dtor ( void *  obj)
static

Definition at line 391 of file stasis_state.c.

References ao2_lock, ao2_ref, ao2_unlock, AST_VECTOR_GET, AST_VECTOR_RW_RDLOCK, AST_VECTOR_RW_UNLOCK, AST_VECTOR_SIZE, stasis_state_proxy::manager, stasis_state::manager, stasis_state::num_subscribers, stasis_state_subscriber::state, and sub.

Referenced by stasis_state_add_subscriber().

392 {
393  size_t i;
394  struct stasis_state_subscriber *sub = obj;
395  struct stasis_state_manager *manager = sub->state->manager;
396 
397  AST_VECTOR_RW_RDLOCK(&manager->observers);
398  for (i = 0; i < AST_VECTOR_SIZE(&manager->observers); ++i) {
399  if (AST_VECTOR_GET(&manager->observers, i)->on_unsubscribe) {
400  AST_VECTOR_GET(&manager->observers, i)->on_unsubscribe(sub->state->id, sub);
401  }
402  }
403  AST_VECTOR_RW_UNLOCK(&manager->observers);
404 
405  ao2_lock(sub->state);
406  --sub->state->num_subscribers;
407  ao2_unlock(sub->state);
408 
409  ao2_ref(sub->state, -1);
410 }
struct stasis_state * state
Definition: stasis_state.c:386
#define AST_VECTOR_RW_UNLOCK(vec)
Unlock vector.
Definition: vector.h:900
struct stasis_state_manager * manager
The manager that owns and handles this state.
Definition: stasis_state.c:57
#define AST_VECTOR_RW_RDLOCK(vec)
Obtain read lock on vector.
Definition: vector.h:880
#define ao2_unlock(a)
Definition: astobj2.h:730
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_lock(a)
Definition: astobj2.h:718
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:682
struct stasis_forward * sub
Definition: res_corosync.c:240
unsigned int num_subscribers
Definition: stasis_state.c:52
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611