FreeSWITCH API Documentation  1.7.0
Data Structures | Macros | Typedefs | Functions | Variables
switch_event.c File Reference
#include <switch.h>
#include <switch_event.h>
#include "tpl.h"
#include "private/switch_core_pvt.h"
+ Include dependency graph for switch_event.c:

Go to the source code of this file.

Data Structures

struct  switch_event_node
 A node to store binded events. More...
 
struct  switch_event_subclass
 A registered custom event subclass. More...
 
struct  switch_event_channel_sub_node_s
 
struct  switch_event_channel_sub_node_head_s
 
struct  event_channel_data_t
 
struct  alias_node_s
 
struct  la_node_s
 
struct  switch_live_array_s
 

Macros

#define DISPATCH_QUEUE_LEN   10000
 
#define MAX_DISPATCH_VAL   64
 
#define ALLOC(size)   malloc(size)
 
#define DUP(str)   my_dup(str)
 
#define FREE(ptr)   switch_safe_free(ptr)
 
#define SWITCH_SERIALIZED_EVENT_MAP   "S(iiisss)A(S(ss))"
 
#define resize(l)
 

Typedefs

typedef struct
switch_event_channel_sub_node_s 
switch_event_channel_sub_node_t
 
typedef struct
switch_event_channel_sub_node_head_s 
switch_event_channel_sub_node_head_t
 
typedef struct alias_node_s alias_node_t
 
typedef struct la_node_s la_node_t
 

Functions

static void unsub_all_switch_event_channel (void)
 
static char * my_dup (const char *s)
 
static int switch_events_match (switch_event_t *event, switch_event_node_t *node)
 
static void *SWITCH_THREAD_FUNC switch_event_deliver_thread (switch_thread_t *thread, void *obj)
 
static void switch_event_deliver_thread_pool (switch_event_t **event)
 
static void *SWITCH_THREAD_FUNC switch_event_dispatch_thread (switch_thread_t *thread, void *obj)
 
static switch_status_t switch_event_queue_dispatch_event (switch_event_t **eventp)
 
void switch_event_deliver (switch_event_t **event)
 Deliver an event to all of the registered event listeners. More...
 
switch_status_t switch_event_running (void)
 Determine if the event system has been initialized. More...
 
const char * switch_event_name (switch_event_types_t event)
 Render the name of an event id enumeration. More...
 
switch_status_t switch_name_event (const char *name, switch_event_types_t *type)
 return the event id that matches a given event name More...
 
switch_status_t switch_event_free_subclass_detailed (const char *owner, const char *subclass_name)
 
switch_status_t switch_event_reserve_subclass_detailed (const char *owner, const char *subclass_name)
 Reserve a subclass name for private use with a custom event. More...
 
void switch_core_memory_reclaim_events (void)
 
switch_status_t switch_event_shutdown (void)
 Stop the eventing system. More...
 
static void check_dispatch (void)
 
void switch_event_launch_dispatch_threads (uint32_t max)
 
switch_status_t switch_event_init (switch_memory_pool_t *pool)
 Start the eventing system. More...
 
switch_status_t switch_event_create_subclass_detailed (const char *file, const char *func, int line, switch_event_t **event, switch_event_types_t event_id, const char *subclass_name)
 Create an event. More...
 
switch_status_t switch_event_set_priority (switch_event_t *event, switch_priority_t priority)
 Set the priority of an event. More...
 
switch_status_t switch_event_rename_header (switch_event_t *event, const char *header_name, const char *new_header_name)
 
switch_event_header_tswitch_event_get_header_ptr (switch_event_t *event, const char *header_name)
 Retrieve a header value from an event. More...
 
char * switch_event_get_header_idx (switch_event_t *event, const char *header_name, int idx)
 
char * switch_event_get_body (switch_event_t *event)
 Retrieve the body value from an event. More...
 
switch_status_t switch_event_del_header_val (switch_event_t *event, const char *header_name, const char *val)
 
static switch_event_header_tnew_header (const char *header_name)
 
int switch_event_add_array (switch_event_t *event, const char *var, const char *val)
 
static switch_status_t switch_event_base_add_header (switch_event_t *event, switch_stack_t stack, const char *header_name, char *data)
 
switch_status_t switch_event_add_header (switch_event_t *event, switch_stack_t stack, const char *header_name, const char *fmt,...)
 
switch_status_t switch_event_set_subclass_name (switch_event_t *event, const char *subclass_name)
 
switch_status_t switch_event_add_header_string (switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
 Add a string header to an event. More...
 
switch_status_t switch_event_set_body (switch_event_t *event, const char *body)
 
switch_status_t switch_event_add_body (switch_event_t *event, const char *fmt,...)
 
void switch_event_destroy (switch_event_t **event)
 Destroy an event. More...
 
void switch_event_merge (switch_event_t *event, switch_event_t *tomerge)
 
switch_status_t switch_event_dup (switch_event_t **event, switch_event_t *todup)
 Duplicate an event. More...
 
switch_status_t switch_event_dup_reply (switch_event_t **event, switch_event_t *todup)
 
switch_status_t switch_event_binary_deserialize (switch_event_t **eventp, void **data, switch_size_t len, switch_bool_t destroy)
 Render a string representation of an event suitable for printing or network transport. More...
 
switch_status_t switch_event_binary_serialize (switch_event_t *event, void **data, switch_size_t *len)
 
switch_status_t switch_event_serialize (switch_event_t *event, char **str, switch_bool_t encode)
 
switch_status_t switch_event_create_array_pair (switch_event_t **event, char **names, char **vals, int len)
 
switch_status_t switch_event_create_brackets (char *data, char a, char b, char c, switch_event_t **event, char **new_data, switch_bool_t dup)
 
switch_status_t switch_event_create_json (switch_event_t **event, const char *json)
 
switch_status_t switch_event_serialize_json_obj (switch_event_t *event, cJSON **json)
 
switch_status_t switch_event_serialize_json (switch_event_t *event, char **str)
 
static switch_xml_t add_xml_header (switch_xml_t xml, char *name, char *value, int offset)
 
switch_xml_t switch_event_xmlize (switch_event_t *event, const char *fmt,...)
 
void switch_event_prep_for_delivery_detailed (const char *file, const char *func, int line, switch_event_t *event)
 
switch_status_t switch_event_fire_detailed (const char *file, const char *func, int line, switch_event_t **event, void *user_data)
 Fire an event with full arguement list. More...
 
switch_status_t switch_event_get_custom_events (switch_console_callback_match_t **matches)
 
switch_status_t switch_event_bind_removable (const char *id, switch_event_types_t event, const char *subclass_name, switch_event_callback_t callback, void *user_data, switch_event_node_t **node)
 Bind an event callback to a specific event. More...
 
switch_status_t switch_event_bind (const char *id, switch_event_types_t event, const char *subclass_name, switch_event_callback_t callback, void *user_data)
 Bind an event callback to a specific event. More...
 
switch_status_t switch_event_unbind_callback (switch_event_callback_t callback)
 
switch_status_t switch_event_unbind (switch_event_node_t **node)
 Unbind a bound event consumer. More...
 
switch_status_t switch_event_create_pres_in_detailed (char *file, char *func, int line, const char *proto, const char *login, const char *from, const char *from_domain, const char *status, const char *event_type, const char *alt_event_type, int event_count, const char *unique_id, const char *channel_state, const char *answer_state, const char *call_direction)
 
char * switch_event_expand_headers_check (switch_event_t *event, const char *in, switch_event_t *var_list, switch_event_t *api_list, uint32_t recur)
 
char * switch_event_build_param_string (switch_event_t *event, const char *prefix, switch_hash_t *vars_map)
 
int switch_event_check_permission_list (switch_event_t *list, const char *name)
 
void switch_json_add_presence_data_cols (switch_event_t *event, cJSON *json, const char *prefix)
 
void switch_event_add_presence_data_cols (switch_channel_t *channel, switch_event_t *event, const char *prefix)
 
static uint32_t switch_event_channel_unsub_head (switch_event_channel_func_t func, switch_event_channel_sub_node_head_t *head)
 
static uint32_t switch_event_channel_unsub_channel (switch_event_channel_func_t func, const char *event_channel)
 
static switch_status_t switch_event_channel_sub_channel (const char *event_channel, switch_event_channel_func_t func, switch_event_channel_id_t id)
 
static uint32_t _switch_event_channel_broadcast (const char *event_channel, const char *broadcast_channel, cJSON *json, const char *key, switch_event_channel_id_t id)
 
static void destroy_ecd (event_channel_data_t **ecdP)
 
static void ecd_deliver (event_channel_data_t **ecdP)
 
static void *SWITCH_THREAD_FUNC switch_event_channel_deliver_thread (switch_thread_t *thread, void *obj)
 
switch_status_t switch_event_channel_broadcast (const char *event_channel, cJSON **json, const char *key, switch_event_channel_id_t id)
 
uint32_t switch_event_channel_unbind (const char *event_channel, switch_event_channel_func_t func)
 
switch_status_t switch_event_channel_bind (const char *event_channel, switch_event_channel_func_t func, switch_event_channel_id_t *id)
 
switch_bool_t switch_event_channel_permission_verify (const char *cookie, const char *event_channel)
 
void switch_event_channel_permission_modify (const char *cookie, const char *event_channel, switch_bool_t set)
 
void switch_event_channel_permission_clear (const char *cookie)
 
static switch_status_t la_broadcast (switch_live_array_t *la, cJSON **json)
 
switch_status_t switch_live_array_visible (switch_live_array_t *la, switch_bool_t visible, switch_bool_t force)
 
switch_status_t switch_live_array_clear (switch_live_array_t *la)
 
switch_status_t switch_live_array_bootstrap (switch_live_array_t *la, const char *sessid, switch_event_channel_id_t channel_id)
 
switch_status_t switch_live_array_destroy (switch_live_array_t **live_arrayP)
 
switch_bool_t switch_live_array_isnew (switch_live_array_t *la)
 
switch_bool_t switch_live_array_clear_alias (switch_live_array_t *la, const char *event_channel, const char *name)
 
switch_bool_t switch_live_array_add_alias (switch_live_array_t *la, const char *event_channel, const char *name)
 
switch_status_t switch_live_array_create (const char *event_channel, const char *name, switch_event_channel_id_t channel_id, switch_live_array_t **live_arrayP)
 
cJSONswitch_live_array_get (switch_live_array_t *la, const char *name)
 
cJSONswitch_live_array_get_idx (switch_live_array_t *la, int idx)
 
void switch_live_array_lock (switch_live_array_t *la)
 
void switch_live_array_unlock (switch_live_array_t *la)
 
switch_status_t switch_live_array_del (switch_live_array_t *la, const char *name)
 
switch_status_t switch_live_array_add (switch_live_array_t *la, const char *name, int index, cJSON **obj, switch_bool_t duplicate)
 
void switch_live_array_set_user_data (switch_live_array_t *la, void *user_data)
 
void switch_live_array_set_command_handler (switch_live_array_t *la, switch_live_array_command_handler_t command_handler)
 
void switch_live_array_parse_json (cJSON *json, switch_event_channel_id_t channel_id)
 

Variables

struct {
   switch_event_channel_id_t   ID
 
   switch_thread_rwlock_t *   rwlock
 
   switch_hash_t *   hash
 
   switch_hash_t *   perm_hash
 
   switch_hash_t *   lahash
 
   switch_mutex_t *   lamutex
 
event_channel_manager
 
static unsigned int MAX_DISPATCH = MAX_DISPATCH_VAL
 
static unsigned int SOFT_MAX_DISPATCH = 0
 
static char guess_ip_v4 [80] = ""
 
static char guess_ip_v6 [80] = ""
 
static switch_event_node_tEVENT_NODES [SWITCH_EVENT_ALL+1] = { NULL }
 
static switch_thread_rwlock_tRWLOCK = NULL
 
static switch_mutex_tBLOCK = NULL
 
static switch_mutex_tPOOL_LOCK = NULL
 
static switch_memory_pool_tRUNTIME_POOL = NULL
 
static switch_memory_pool_tTHRUNTIME_POOL = NULL
 
static switch_thread_tEVENT_DISPATCH_QUEUE_THREADS [MAX_DISPATCH_VAL] = { 0 }
 
static uint8_t EVENT_DISPATCH_QUEUE_RUNNING [MAX_DISPATCH_VAL] = { 0 }
 
static switch_queue_tEVENT_DISPATCH_QUEUE = NULL
 
static switch_queue_tEVENT_CHANNEL_DISPATCH_QUEUE = NULL
 
static switch_mutex_tEVENT_QUEUE_MUTEX = NULL
 
static switch_hash_tCUSTOM_HASH = NULL
 
static int THREAD_COUNT = 0
 
static int DISPATCH_THREAD_COUNT = 0
 
static int EVENT_CHANNEL_DISPATCH_THREAD_COUNT = 0
 
static int EVENT_CHANNEL_DISPATCH_THREAD_STARTING = 0
 
static int SYSTEM_RUNNING = 0
 
static uint64_t EVENT_SEQUENCE_NR = 0
 
static char * EVENT_NAMES []
 
static int PENDING = 0
 

Macro Definition Documentation

#define ALLOC (   size)    malloc(size)

Definition at line 120 of file switch_event.c.

Referenced by new_header(), and switch_event_create_subclass_detailed().

#define DISPATCH_QUEUE_LEN   10000
#define DUP (   str)    my_dup(str)
#define FREE (   ptr)    switch_safe_free(ptr)
#define MAX_DISPATCH_VAL   64

Definition at line 80 of file switch_event.c.

Referenced by switch_event_dispatch_thread().

#define resize (   l)
Value:
{\
char *dp;\
olen += (len + l + block);\
cpos = c - data;\
if ((dp = realloc(data, olen))) {\
data = dp;\
c = data + cpos;\
memset(c, 0, olen - cpos);\
}} \
if((uint32_t)(unpack->cur-unpack->buf) > unpack->buflen)
memset(buf, 0, buflen)

Definition at line 2185 of file switch_event.c.

Referenced by switch_event_expand_headers_check().

#define SWITCH_SERIALIZED_EVENT_MAP   "S(iiisss)A(S(ss))"

Typedef Documentation

typedef struct alias_node_s alias_node_t
typedef struct la_node_s la_node_t

Function Documentation

static uint32_t _switch_event_channel_broadcast ( const char *  event_channel,
const char *  broadcast_channel,
cJSON json,
const char *  key,
switch_event_channel_id_t  id 
)
static

Definition at line 2841 of file switch_event.c.

References event_channel_manager, switch_event_channel_sub_node_s::func, switch_event_channel_sub_node_s::id, switch_event_channel_sub_node_s::next, switch_event_channel_sub_node_head_s::node, switch_core_hash_find(), switch_thread_rwlock_rdlock(), and switch_thread_rwlock_unlock().

Referenced by ecd_deliver().

2843 {
2846  uint32_t x = 0;
2847 
2849  if ((head = switch_core_hash_find(event_channel_manager.hash, event_channel))) {
2850  for (np = head->node; np; np = np->next) {
2851  if (np->id == id) {
2852  continue;
2853  }
2854 
2855  np->func(broadcast_channel, json, key, id);
2856  x++;
2857  }
2858  }
2860 
2861  return x;
2862 }
switch_status_t switch_thread_rwlock_unlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:263
void * switch_core_hash_find(_In_ switch_hash_t *hash, _In_z_ const char *key)
Retrieve data from a given hash.
switch_event_channel_id_t id
static struct @4 event_channel_manager
switch_status_t switch_thread_rwlock_rdlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:227
switch_event_channel_sub_node_t * node
switch_event_channel_func_t func
struct switch_event_channel_sub_node_s * next
static switch_xml_t add_xml_header ( switch_xml_t  xml,
char *  name,
char *  value,
int  offset 
)
static

Definition at line 1833 of file switch_event.c.

References memset(), switch_assert, switch_url_encode(), switch_xml_add_child_d, and switch_xml_set_txt_d.

Referenced by switch_event_xmlize().

1834 {
1835  switch_xml_t header = switch_xml_add_child_d(xml, name, offset);
1836 
1837  if (header) {
1838  switch_size_t encode_len = (strlen(value) * 3) + 1;
1839  char *encode_buf = malloc(encode_len);
1840 
1841  switch_assert(encode_buf);
1842 
1843  memset(encode_buf, 0, encode_len);
1844  switch_url_encode((char *) value, encode_buf, encode_len);
1845  switch_xml_set_txt_d(header, encode_buf);
1846  free(encode_buf);
1847  }
1848 
1849  return header;
1850 }
#define switch_xml_add_child_d(xml, name, off)
wrapper for switch_xml_add_child() that strdup()s name
Definition: switch_xml.h:269
A representation of an XML tree.
Definition: switch_xml.h:76
char * switch_url_encode(const char *url, char *buf, size_t len)
uintptr_t switch_size_t
#define switch_xml_set_txt_d(xml, txt)
wrapper for switch_xml_set_txt() that strdup()s txt \ sets the character content for the given tag an...
Definition: switch_xml.h:283
#define switch_assert(expr)
memset(buf, 0, buflen)
static void check_dispatch ( void  )
static

Definition at line 604 of file switch_event.c.

References BLOCK, DISPATCH_QUEUE_LEN, EVENT_DISPATCH_QUEUE, MAX_DISPATCH, switch_cond_next(), switch_event_launch_dispatch_threads(), switch_mutex_lock(), switch_mutex_unlock(), switch_queue_create(), THREAD_COUNT, and THRUNTIME_POOL.

Referenced by switch_event_fire_detailed(), switch_event_init(), and switch_event_launch_dispatch_threads().

605 {
606  if (!EVENT_DISPATCH_QUEUE) {
608 
609  if (!EVENT_DISPATCH_QUEUE) {
612 
613  while (!THREAD_COUNT) {
615  }
616  }
618  }
619 }
static unsigned int MAX_DISPATCH
Definition: switch_event.c:81
#define DISPATCH_QUEUE_LEN
Definition: switch_event.c:42
void switch_event_launch_dispatch_threads(uint32_t max)
Definition: switch_event.c:623
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
static switch_queue_t * EVENT_DISPATCH_QUEUE
Definition: switch_event.c:93
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static int THREAD_COUNT
Definition: switch_event.c:97
static switch_mutex_t * BLOCK
Definition: switch_event.c:87
void switch_cond_next(void)
Definition: switch_time.c:638
static switch_memory_pool_t * THRUNTIME_POOL
Definition: switch_event.c:90
switch_status_t switch_queue_create(switch_queue_t **queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
Definition: switch_apr.c:1109
static void destroy_ecd ( event_channel_data_t **  ecdP)
static

Definition at line 2864 of file switch_event.c.

References cJSON_Delete(), event_channel_data_t::event_channel, event_channel_data_t::json, event_channel_data_t::key, and switch_safe_free.

Referenced by ecd_deliver(), switch_event_channel_broadcast(), and switch_event_channel_deliver_thread().

2865 {
2866  event_channel_data_t *ecd = *ecdP;
2867  *ecdP = NULL;
2868 
2870  switch_safe_free(ecd->key);
2871  if (ecd->json) {
2872  cJSON_Delete(ecd->json);
2873  ecd->json = NULL;
2874  }
2875 
2876  free(ecd);
2877 }
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
void cJSON_Delete(cJSON *c)
Definition: switch_json.c:93
static void ecd_deliver ( event_channel_data_t **  ecdP)
static

Definition at line 2879 of file switch_event.c.

References _switch_event_channel_broadcast(), destroy_ecd(), event_channel_data_t::event_channel, event_channel_data_t::id, event_channel_data_t::json, event_channel_data_t::key, and SWITCH_EVENT_CHANNEL_GLOBAL.

Referenced by switch_event_channel_deliver_thread().

2880 {
2881  event_channel_data_t *ecd = *ecdP;
2882  char *p;
2883 
2884  *ecdP = NULL;
2885 
2886  _switch_event_channel_broadcast(ecd->event_channel, ecd->event_channel, ecd->json, ecd->key, ecd->id);
2887 
2888  if ((p = strchr(ecd->event_channel, '.'))) {
2889  char *main_channel = strdup(ecd->event_channel);
2890  p = strchr(main_channel, '.');
2891  *p = '\0';
2892  _switch_event_channel_broadcast(main_channel, ecd->event_channel, ecd->json, ecd->key, ecd->id);
2893  free(main_channel);
2894  }
2896 
2897  destroy_ecd(&ecd);
2898 }
static uint32_t _switch_event_channel_broadcast(const char *event_channel, const char *broadcast_channel, cJSON *json, const char *key, switch_event_channel_id_t id)
switch_event_channel_id_t id
static void destroy_ecd(event_channel_data_t **ecdP)
#define SWITCH_EVENT_CHANNEL_GLOBAL
Definition: switch_event.h:438
static switch_status_t la_broadcast ( switch_live_array_t la,
cJSON **  json 
)
static

Definition at line 3107 of file switch_event.c.

References switch_live_array_s::aliases, switch_live_array_s::channel_id, cJSON_CreateString(), cJSON_Duplicate(), cJSON_GetObjectItem(), cJSON_ReplaceItemInObject(), alias_node_s::event_channel, switch_live_array_s::event_channel, switch_live_array_s::mutex, alias_node_s::name, alias_node_s::next, switch_event_channel_broadcast(), switch_mutex_lock(), and switch_mutex_unlock().

Referenced by switch_live_array_add(), switch_live_array_clear(), switch_live_array_del(), and switch_live_array_visible().

3108 {
3109  alias_node_t *np;
3110 
3111  if (la->aliases) {
3112  switch_mutex_lock(la->mutex);
3113  for (np = la->aliases; np; np = np->next) {
3114  cJSON *dup = cJSON_Duplicate(*json, 1);
3115  cJSON *data = cJSON_GetObjectItem(dup, "data");
3116 
3117  cJSON_ReplaceItemInObject(dup, "eventChannel", cJSON_CreateString(np->event_channel));
3119 
3120  switch_event_channel_broadcast(np->event_channel, &dup, __FILE__, la->channel_id);
3121  }
3123  }
3124 
3125  return switch_event_channel_broadcast(la->event_channel, json, __FILE__, la->channel_id);
3126 
3127 }
struct alias_node_s * next
char * event_channel
void cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem)
Definition: switch_json.c:534
switch_status_t switch_event_channel_broadcast(const char *event_channel, cJSON **json, const char *key, switch_event_channel_id_t id)
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
alias_node_t * aliases
cJSON * cJSON_GetObjectItem(const cJSON *object, const char *string)
Definition: switch_json.c:498
cJSON * cJSON_CreateString(const char *string)
Definition: switch_json.c:542
switch_event_channel_id_t channel_id
cJSON * cJSON_Duplicate(cJSON *item, int recurse)
Definition: switch_json.c:553
switch_mutex_t * mutex
static char* my_dup ( const char *  s)
static

Definition at line 110 of file switch_event.c.

References switch_assert.

111 {
112  size_t len = strlen(s) + 1;
113  void *new = malloc(len);
114  switch_assert(new);
115 
116  return (char *) memcpy(new, s, len);
117 }
#define switch_assert(expr)
static switch_event_header_t* new_header ( const char *  header_name)
static

Definition at line 899 of file switch_event.c.

References ALLOC, DUP, memset(), switch_event_header::name, switch_assert, switch_queue_trypop(), and SWITCH_STATUS_SUCCESS.

Referenced by switch_event_base_add_header().

900 {
901  switch_event_header_t *header;
902 
903 #ifdef SWITCH_EVENT_RECYCLE
904  void *pop;
905  if (EVENT_HEADER_RECYCLE_QUEUE && switch_queue_trypop(EVENT_HEADER_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
906  header = (switch_event_header_t *) pop;
907  } else {
908 #endif
909  header = ALLOC(sizeof(*header));
910  switch_assert(header);
911 #ifdef SWITCH_EVENT_RECYCLE
912  }
913 #endif
914 
915  memset(header, 0, sizeof(*header));
916  header->name = DUP(header_name);
917 
918  return header;
919 
920 }
An event Header.
Definition: switch_event.h:65
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1140
#define ALLOC(size)
Definition: switch_event.c:120
#define DUP(str)
Definition: switch_event.c:123
#define switch_assert(expr)
memset(buf, 0, buflen)
switch_status_t switch_event_add_body ( switch_event_t event,
const char *  fmt,
  ... 
)

Definition at line 1208 of file switch_event.c.

References switch_safe_free, SWITCH_STATUS_GENERR, SWITCH_STATUS_SUCCESS, and switch_vasprintf().

Referenced by switch_event_create_json().

1209 {
1210  int ret = 0;
1211  char *data;
1212 
1213  va_list ap;
1214  if (fmt) {
1215  va_start(ap, fmt);
1216  ret = switch_vasprintf(&data, fmt, ap);
1217  va_end(ap);
1218 
1219  if (ret == -1) {
1220  return SWITCH_STATUS_GENERR;
1221  } else {
1222  switch_safe_free(event->body);
1223  event->body = data;
1224  return SWITCH_STATUS_SUCCESS;
1225  }
1226  } else {
1227  return SWITCH_STATUS_GENERR;
1228  }
1229 }
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
int switch_vasprintf(_Out_opt_ char **buf, _In_z_ _Printf_format_string_ const char *format, _In_ va_list ap)
switch_status_t switch_event_add_header ( switch_event_t event,
switch_stack_t  stack,
const char *  header_name,
const char *  fmt,
  ... 
)

Definition at line 1160 of file switch_event.c.

References switch_event_base_add_header(), SWITCH_STATUS_MEMERR, and switch_vasprintf().

Referenced by switch_event_create_pres_in_detailed(), and switch_event_prep_for_delivery_detailed().

1161 {
1162  int ret = 0;
1163  char *data;
1164  va_list ap;
1165 
1166  va_start(ap, fmt);
1167  ret = switch_vasprintf(&data, fmt, ap);
1168  va_end(ap);
1169 
1170  if (ret == -1) {
1171  return SWITCH_STATUS_MEMERR;
1172  }
1173 
1174  return switch_event_base_add_header(event, stack, header_name, data);
1175 }
int switch_vasprintf(_Out_opt_ char **buf, _In_z_ _Printf_format_string_ const char *format, _In_ va_list ap)
static switch_status_t switch_event_base_add_header(switch_event_t *event, switch_stack_t stack, const char *header_name, char *data)
Definition: switch_event.c:968
static switch_status_t switch_event_base_add_header ( switch_event_t event,
switch_stack_t  stack,
const char *  header_name,
char *  data 
)
static

Definition at line 968 of file switch_event.c.

References switch_event_header::array, DUP, EF_UNIQ_HEADERS, FREE, switch_event_header::hash, switch_event_header::idx, switch_event::last_header, switch_event_header::name, new_header(), switch_event_header::next, switch_assert, switch_ci_hashfunc_default(), switch_event_add_array(), switch_event_del_header, switch_event_get_header_ptr(), switch_event_set_body(), switch_safe_free, switch_snprintf(), SWITCH_STACK_BOTTOM, SWITCH_STACK_PUSH, SWITCH_STACK_TOP, SWITCH_STACK_UNSHIFT, SWITCH_STATUS_SUCCESS, switch_test_flag, switch_event_header::value, and zstr.

Referenced by switch_event_add_header(), and switch_event_add_header_string().

969 {
970  switch_event_header_t *header = NULL;
971  switch_ssize_t hlen = -1;
972  int exists = 0, fly = 0;
973  char *index_ptr;
974  int index = 0;
975  char *real_header_name = NULL;
976 
977 
978  if (!strcmp(header_name, "_body")) {
979  switch_event_set_body(event, data);
980  }
981 
982  if ((index_ptr = strchr(header_name, '['))) {
983  index_ptr++;
984  index = atoi(index_ptr);
985  real_header_name = DUP(header_name);
986  if ((index_ptr = strchr(real_header_name, '['))) {
987  *index_ptr++ = '\0';
988  }
989  header_name = real_header_name;
990  }
991 
992  if (index_ptr || (stack & SWITCH_STACK_PUSH) || (stack & SWITCH_STACK_UNSHIFT)) {
993 
994  if (!(header = switch_event_get_header_ptr(event, header_name)) && index_ptr) {
995 
996  header = new_header(header_name);
997 
998  if (switch_test_flag(event, EF_UNIQ_HEADERS)) {
999  switch_event_del_header(event, header_name);
1000  }
1001 
1002  fly++;
1003  }
1004 
1005  if (header || (header = switch_event_get_header_ptr(event, header_name))) {
1006 
1007  if (index_ptr) {
1008  if (index > -1 && index <= 4000) {
1009  if (index < header->idx) {
1010  FREE(header->array[index]);
1011  header->array[index] = DUP(data);
1012  } else {
1013  int i;
1014  char **m;
1015 
1016  m = realloc(header->array, sizeof(char *) * (index + 1));
1017  switch_assert(m);
1018  header->array = m;
1019  for (i = header->idx; i < index; i++) {
1020  m[i] = DUP("");
1021  }
1022  m[index] = DUP(data);
1023  header->idx = index + 1;
1024  if (!fly) {
1025  exists = 1;
1026  }
1027 
1028  goto redraw;
1029  }
1030  }
1031  goto end;
1032  } else {
1033  if ((stack & SWITCH_STACK_PUSH) || (stack & SWITCH_STACK_UNSHIFT)) {
1034  exists++;
1035  stack &= ~(SWITCH_STACK_TOP | SWITCH_STACK_BOTTOM);
1036  } else {
1037  header = NULL;
1038  }
1039  }
1040  }
1041  }
1042 
1043 
1044  if (!header) {
1045 
1046  if (zstr(data)) {
1047  switch_event_del_header(event, header_name);
1048  FREE(data);
1049  goto end;
1050  }
1051 
1052  if (switch_test_flag(event, EF_UNIQ_HEADERS)) {
1053  switch_event_del_header(event, header_name);
1054  }
1055 
1056  if (!strncmp(data, "ARRAY::", 7)) {
1057  switch_event_add_array(event, header_name, data);
1058  FREE(data);
1059  goto end;
1060  }
1061 
1062 
1063  header = new_header(header_name);
1064  }
1065 
1066  if ((stack & SWITCH_STACK_PUSH) || (stack & SWITCH_STACK_UNSHIFT)) {
1067  char **m = NULL;
1068  switch_size_t len = 0;
1069  char *hv;
1070  int i = 0, j = 0;
1071 
1072  if (header->value && !header->idx) {
1073  m = malloc(sizeof(char *));
1074  switch_assert(m);
1075  m[0] = header->value;
1076  header->value = NULL;
1077  header->array = m;
1078  header->idx++;
1079  m = NULL;
1080  }
1081 
1082  i = header->idx + 1;
1083  m = realloc(header->array, sizeof(char *) * i);
1084  switch_assert(m);
1085 
1086  if ((stack & SWITCH_STACK_PUSH)) {
1087  m[header->idx] = data;
1088  } else if ((stack & SWITCH_STACK_UNSHIFT)) {
1089  for (j = header->idx; j > 0; j--) {
1090  m[j] = m[j-1];
1091  }
1092  m[0] = data;
1093  }
1094 
1095  header->idx++;
1096  header->array = m;
1097 
1098  redraw:
1099  len = 0;
1100  for(j = 0; j < header->idx; j++) {
1101  len += strlen(header->array[j]) + 2;
1102  }
1103 
1104  if (len) {
1105  len += 8;
1106  hv = realloc(header->value, len);
1107  switch_assert(hv);
1108  header->value = hv;
1109 
1110  if (header->idx > 1) {
1111  switch_snprintf(header->value, len, "ARRAY::");
1112  } else {
1113  *header->value = '\0';
1114  }
1115 
1116  hv += strlen(header->value);
1117  for(j = 0; j < header->idx; j++) {
1118  if (j > 0) {
1119  memcpy(hv, "|:", 2);
1120  hv += 2;
1121  }
1122  memcpy(hv, header->array[j], strlen(header->array[j]));
1123  hv += strlen(header->array[j]);
1124  }
1125  *hv = '\0';
1126  }
1127 
1128  } else {
1129  switch_safe_free(header->value);
1130  header->value = data;
1131  }
1132 
1133  if (!exists) {
1134  header->hash = switch_ci_hashfunc_default(header->name, &hlen);
1135 
1136  if ((stack & SWITCH_STACK_TOP)) {
1137  header->next = event->headers;
1138  event->headers = header;
1139  if (!event->last_header) {
1140  event->last_header = header;
1141  }
1142  } else {
1143  if (event->last_header) {
1144  event->last_header->next = header;
1145  } else {
1146  event->headers = header;
1147  header->next = NULL;
1148  }
1149  event->last_header = header;
1150  }
1151  }
1152 
1153  end:
1154 
1155  switch_safe_free(real_header_name);
1156 
1157  return SWITCH_STATUS_SUCCESS;
1158 }
#define switch_event_del_header(_e, _h)
Definition: switch_event.h:211
int switch_event_add_array(switch_event_t *event, const char *var, const char *val)
Definition: switch_event.c:922
An event Header.
Definition: switch_event.h:65
unsigned int switch_ci_hashfunc_default(const char *char_key, switch_ssize_t *klen)
Definition: switch_apr.c:98
switch_event_header_t * last_header
Definition: switch_event.h:92
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
#define zstr(x)
Definition: switch_utils.h:281
#define FREE(ptr)
Definition: switch_event.c:126
intptr_t switch_ssize_t
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
static switch_event_header_t * new_header(const char *header_name)
Definition: switch_event.c:899
uintptr_t switch_size_t
struct switch_event_header * next
Definition: switch_event.h:76
#define DUP(str)
Definition: switch_event.c:123
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
switch_status_t switch_event_set_body(switch_event_t *event, const char *body)
#define switch_assert(expr)
switch_event_header_t * switch_event_get_header_ptr(switch_event_t *event, const char *header_name)
Retrieve a header value from an event.
Definition: switch_event.c:796
unsigned long hash
Definition: switch_event.h:75
static void* SWITCH_THREAD_FUNC switch_event_channel_deliver_thread ( switch_thread_t thread,
void *  obj 
)
static

Definition at line 2900 of file switch_event.c.

References destroy_ecd(), ecd_deliver(), EVENT_CHANNEL_DISPATCH_THREAD_COUNT, EVENT_CHANNEL_DISPATCH_THREAD_STARTING, EVENT_QUEUE_MUTEX, SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_os_yield(), switch_queue_pop(), switch_queue_trypop(), SWITCH_STATUS_SUCCESS, SYSTEM_RUNNING, and THREAD_COUNT.

Referenced by switch_event_channel_broadcast().

2901 {
2902  switch_queue_t *queue = (switch_queue_t *) obj;
2903  void *pop = NULL;
2904  event_channel_data_t *ecd = NULL;
2905 
2907  THREAD_COUNT++;
2911 
2912  while(SYSTEM_RUNNING) {
2913 
2914  if (switch_queue_pop(queue, &pop) != SWITCH_STATUS_SUCCESS) {
2915  continue;
2916  }
2917 
2918  if (!pop) {
2919  break;
2920  }
2921 
2922  ecd = (event_channel_data_t *) pop;
2923  ecd_deliver(&ecd);
2924  switch_os_yield();
2925  }
2926 
2927  while (switch_queue_trypop(queue, &pop) == SWITCH_STATUS_SUCCESS) {
2928  ecd = (event_channel_data_t *) pop;
2929  destroy_ecd(&ecd);
2930  }
2931 
2933  THREAD_COUNT--;
2936 
2937  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Event Channel Dispatch Thread Ended.\n");
2938  return NULL;
2939 }
struct apr_queue_t switch_queue_t
Definition: switch_apr.h:590
#define SWITCH_CHANNEL_LOG
static int SYSTEM_RUNNING
Definition: switch_event.c:101
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1140
switch_status_t switch_queue_pop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1119
static int EVENT_CHANNEL_DISPATCH_THREAD_STARTING
Definition: switch_event.c:100
static void destroy_ecd(event_channel_data_t **ecdP)
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
void switch_os_yield(void)
Definition: switch_time.c:141
static void ecd_deliver(event_channel_data_t **ecdP)
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static int THREAD_COUNT
Definition: switch_event.c:97
static switch_mutex_t * EVENT_QUEUE_MUTEX
Definition: switch_event.c:95
static int EVENT_CHANNEL_DISPATCH_THREAD_COUNT
Definition: switch_event.c:99
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static switch_status_t switch_event_channel_sub_channel ( const char *  event_channel,
switch_event_channel_func_t  func,
switch_event_channel_id_t  id 
)
static

Definition at line 2776 of file switch_event.c.

References switch_event_channel_sub_node_head_s::event_channel, event_channel_manager, switch_event_channel_sub_node_s::func, switch_event_channel_sub_node_s::head, switch_event_channel_sub_node_s::id, switch_event_channel_sub_node_s::next, switch_event_channel_sub_node_head_s::node, switch_core_hash_find(), switch_core_hash_insert, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_thread_rwlock_unlock(), switch_thread_rwlock_wrlock(), switch_zmalloc, and switch_event_channel_sub_node_head_s::tail.

Referenced by switch_event_channel_bind().

2778 {
2782 
2784 
2785  if (!(head = switch_core_hash_find(event_channel_manager.hash, event_channel))) {
2786  switch_zmalloc(head, sizeof(*head));
2787  head->event_channel = strdup(event_channel);
2788  switch_core_hash_insert(event_channel_manager.hash, event_channel, head);
2789 
2790  switch_zmalloc(node, sizeof(*node));
2791  node->func = func;
2792  node->id = id;
2793 
2794  node->head = head;
2795  head->node = node;
2796  head->tail = node;
2797  status = SWITCH_STATUS_SUCCESS;
2798  } else {
2799  int exist = 0;
2800 
2801  for (np = head->node; np; np = np->next) {
2802  if (np->func == func) {
2803  exist = 1;
2804  break;
2805  }
2806  }
2807 
2808  if (!exist) {
2809  switch_zmalloc(node, sizeof(*node));
2810 
2811  node->func = func;
2812  node->id = id;
2813  node->head = head;
2814 
2815 
2816  if (!head->node) {
2817  head->node = node;
2818  head->tail = node;
2819  } else {
2820  head->tail->next = node;
2821  head->tail = head->tail->next;
2822  }
2823  status = SWITCH_STATUS_SUCCESS;
2824  }
2825  }
2826 
2828 
2829  return status;
2830 }
switch_status_t switch_thread_rwlock_unlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:263
void * switch_core_hash_find(_In_ switch_hash_t *hash, _In_z_ const char *key)
Retrieve data from a given hash.
switch_event_channel_id_t id
static struct @4 event_channel_manager
struct switch_event_channel_sub_node_head_s * head
switch_status_t switch_thread_rwlock_wrlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:237
switch_event_channel_sub_node_t * node
switch_event_channel_func_t func
#define switch_zmalloc(ptr, len)
switch_status_t
Common return values.
switch_event_channel_sub_node_t * tail
#define switch_core_hash_insert(_h, _k, _d)
Definition: switch_core.h:1410
struct switch_event_channel_sub_node_s * next
static uint32_t switch_event_channel_unsub_channel ( switch_event_channel_func_t  func,
const char *  event_channel 
)
static

Definition at line 2745 of file switch_event.c.

References event_channel_manager, switch_core_hash_find(), switch_core_hash_first, switch_core_hash_next(), switch_core_hash_this(), switch_event_channel_unsub_head(), switch_thread_rwlock_unlock(), and switch_thread_rwlock_wrlock().

Referenced by switch_event_channel_unbind().

2746 {
2748  uint32_t x = 0;
2749 
2751 
2752  if (!event_channel) {
2753  switch_hash_index_t *hi;
2754  void *val;
2755 
2756  for (hi = switch_core_hash_first(event_channel_manager.hash); hi; hi = switch_core_hash_next(&hi)) {
2757  switch_core_hash_this(hi, NULL, NULL, &val);
2758 
2759  if (val) {
2760  head = (switch_event_channel_sub_node_head_t *) val;
2761  x += switch_event_channel_unsub_head(func, head);
2762  }
2763  }
2764 
2765  } else {
2766  if ((head = switch_core_hash_find(event_channel_manager.hash, event_channel))) {
2767  x += switch_event_channel_unsub_head(func, head);
2768  }
2769  }
2770 
2772 
2773  return x;
2774 }
switch_status_t switch_thread_rwlock_unlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:263
void * switch_core_hash_find(_In_ switch_hash_t *hash, _In_z_ const char *key)
Retrieve data from a given hash.
static struct @4 event_channel_manager
switch_status_t switch_thread_rwlock_wrlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:237
void switch_core_hash_this(_In_ switch_hash_index_t *hi, _Out_opt_ptrdiff_cap_(klen) const void **key, _Out_opt_ switch_ssize_t *klen, _Out_ void **val)
Gets the key and value of the current hash element.
switch_hash_index_t * switch_core_hash_next(_In_ switch_hash_index_t **hi)
Gets the next element of a hashtable.
static uint32_t switch_event_channel_unsub_head(switch_event_channel_func_t func, switch_event_channel_sub_node_head_t *head)
#define switch_core_hash_first(_h)
Definition: switch_core.h:1501
static uint32_t switch_event_channel_unsub_head ( switch_event_channel_func_t  func,
switch_event_channel_sub_node_head_t head 
)
static

Definition at line 2679 of file switch_event.c.

References switch_event_channel_sub_node_head_s::event_channel, switch_event_channel_sub_node_s::func, switch_event_channel_sub_node_s::head, switch_event_channel_sub_node_s::next, switch_event_channel_sub_node_head_s::node, SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, switch_log_printf(), and switch_event_channel_sub_node_head_s::tail.

Referenced by switch_event_channel_unsub_channel(), and unsub_all_switch_event_channel().

2680 {
2681  uint32_t x = 0;
2682 
2683  switch_event_channel_sub_node_t *thisnp = NULL, *np, *last = NULL;
2684 
2685  np = head->tail = head->node;
2686 
2687  while (np) {
2688 
2689  thisnp = np;
2690  np = np->next;
2691 
2692  if (!func || thisnp->func == func) {
2693  x++;
2694 
2695  if (last) {
2696  last->next = np;
2697  } else {
2698  head->node = np;
2699  }
2700 
2701 
2702  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "UNSUBBING %p [%s]\n", (void *)(intptr_t)thisnp->func, thisnp->head->event_channel);
2703 
2704 
2705  thisnp->func = NULL;
2706  free(thisnp);
2707  } else {
2708  last = thisnp;
2709  head->tail = last;
2710  }
2711  }
2712 
2713  return x;
2714 }
#define SWITCH_CHANNEL_LOG
struct switch_event_channel_sub_node_head_s * head
switch_event_channel_sub_node_t * node
switch_event_channel_func_t func
switch_event_channel_sub_node_t * tail
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
struct switch_event_channel_sub_node_s * next
switch_status_t switch_event_create_pres_in_detailed ( char *  file,
char *  func,
int  line,
const char *  proto,
const char *  login,
const char *  from,
const char *  from_domain,
const char *  status,
const char *  event_type,
const char *  alt_event_type,
int  event_count,
const char *  unique_id,
const char *  channel_state,
const char *  answer_state,
const char *  call_direction 
)

Definition at line 2157 of file switch_event.c.

References switch_event_add_header(), switch_event_add_header_string(), switch_event_create_subclass, switch_event_fire_detailed(), SWITCH_EVENT_PRESENCE_IN, SWITCH_EVENT_SUBCLASS_ANY, SWITCH_STACK_TOP, SWITCH_STATUS_MEMERR, and SWITCH_STATUS_SUCCESS.

2164 {
2165  switch_event_t *pres_event;
2166 
2168  switch_event_add_header_string(pres_event, SWITCH_STACK_TOP, "proto", proto);
2169  switch_event_add_header_string(pres_event, SWITCH_STACK_TOP, "login", login);
2170  switch_event_add_header(pres_event, SWITCH_STACK_TOP, "from", "%s@%s", from, from_domain);
2171  switch_event_add_header_string(pres_event, SWITCH_STACK_TOP, "status", status);
2172  switch_event_add_header_string(pres_event, SWITCH_STACK_TOP, "event_type", event_type);
2173  switch_event_add_header_string(pres_event, SWITCH_STACK_TOP, "alt_event_type", alt_event_type);
2174  switch_event_add_header(pres_event, SWITCH_STACK_TOP, "event_count", "%d", event_count);
2175  switch_event_add_header_string(pres_event, SWITCH_STACK_TOP, "unique-id", alt_event_type);
2176  switch_event_add_header_string(pres_event, SWITCH_STACK_TOP, "channel-state", channel_state);
2177  switch_event_add_header_string(pres_event, SWITCH_STACK_TOP, "answer-state", answer_state);
2178  switch_event_add_header_string(pres_event, SWITCH_STACK_TOP, "presence-call-direction", call_direction);
2179  switch_event_fire_detailed(file, func, line, &pres_event, NULL);
2180  return SWITCH_STATUS_SUCCESS;
2181  }
2182  return SWITCH_STATUS_MEMERR;
2183 }
#define SWITCH_EVENT_SUBCLASS_ANY
Definition: switch_event.h:128
Representation of an event.
Definition: switch_event.h:80
#define switch_event_create_subclass(_e, _eid, _sn)
Definition: switch_event.h:153
switch_status_t switch_event_fire_detailed(const char *file, const char *func, int line, switch_event_t **event, void *user_data)
Fire an event with full arguement list.
switch_status_t switch_event_add_header(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *fmt,...)
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
static void* SWITCH_THREAD_FUNC switch_event_deliver_thread ( switch_thread_t thread,
void *  obj 
)
static

Definition at line 264 of file switch_event.c.

References switch_event_deliver().

Referenced by switch_event_deliver_thread_pool().

265 {
266  switch_event_t *event = (switch_event_t *) obj;
267 
268  switch_event_deliver(&event);
269 
270  return NULL;
271 }
Representation of an event.
Definition: switch_event.h:80
void switch_event_deliver(switch_event_t **event)
Deliver an event to all of the registered event listeners.
Definition: switch_event.c:392
static void switch_event_deliver_thread_pool ( switch_event_t **  event)
static

Definition at line 273 of file switch_event.c.

References switch_thread_data_s::alloc, switch_thread_data_s::func, switch_thread_data_s::obj, switch_thread_data_s::pool, switch_assert, switch_event_deliver_thread(), and switch_thread_pool_launch_thread().

Referenced by switch_event_fire_detailed().

274 {
276 
277  td = malloc(sizeof(*td));
278  switch_assert(td);
279 
280  td->alloc = 1;
282  td->obj = *event;
283  td->pool = NULL;
284 
285  *event = NULL;
286 
288 
289 }
switch_memory_pool_t * pool
Definition: switch_core.h:69
switch_thread_start_t func
Definition: switch_core.h:66
#define switch_assert(expr)
static void *SWITCH_THREAD_FUNC switch_event_deliver_thread(switch_thread_t *thread, void *obj)
Definition: switch_event.c:264
switch_status_t switch_thread_pool_launch_thread(switch_thread_data_t **tdp)
static void* SWITCH_THREAD_FUNC switch_event_dispatch_thread ( switch_thread_t thread,
void *  obj 
)
static

Definition at line 291 of file switch_event.c.

References DISPATCH_THREAD_COUNT, EVENT_DISPATCH_QUEUE_RUNNING, EVENT_DISPATCH_QUEUE_THREADS, EVENT_QUEUE_MUTEX, MAX_DISPATCH_VAL, SWITCH_CHANNEL_LOG, switch_event_deliver(), SWITCH_LOG_CONSOLE, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_os_yield(), switch_queue_pop(), SWITCH_STATUS_SUCCESS, SYSTEM_RUNNING, and THREAD_COUNT.

Referenced by switch_event_launch_dispatch_threads().

292 {
293  switch_queue_t *queue = (switch_queue_t *) obj;
294  int my_id = 0;
295 
297  THREAD_COUNT++;
299 
300  for (my_id = 0; my_id < MAX_DISPATCH_VAL; my_id++) {
301  if (EVENT_DISPATCH_QUEUE_THREADS[my_id] == thread) {
302  break;
303  }
304  }
305 
306  if ( my_id >= MAX_DISPATCH_VAL ) {
308  return NULL;
309  }
310 
311  EVENT_DISPATCH_QUEUE_RUNNING[my_id] = 1;
313 
314 
315  for (;;) {
316  void *pop = NULL;
317  switch_event_t *event = NULL;
318 
319  if (!SYSTEM_RUNNING) {
320  break;
321  }
322 
323  if (switch_queue_pop(queue, &pop) != SWITCH_STATUS_SUCCESS) {
324  continue;
325  }
326 
327  if (!pop) {
328  break;
329  }
330 
331  event = (switch_event_t *) pop;
332  switch_event_deliver(&event);
333  switch_os_yield();
334  }
335 
336 
338  EVENT_DISPATCH_QUEUE_RUNNING[my_id] = 0;
339  THREAD_COUNT--;
342 
343  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Dispatch Thread %d Ended.\n", my_id);
344  return NULL;
345 
346 }
struct apr_queue_t switch_queue_t
Definition: switch_apr.h:590
#define SWITCH_CHANNEL_LOG
static uint8_t EVENT_DISPATCH_QUEUE_RUNNING[MAX_DISPATCH_VAL]
Definition: switch_event.c:92
static int SYSTEM_RUNNING
Definition: switch_event.c:101
#define MAX_DISPATCH_VAL
Definition: switch_event.c:80
Representation of an event.
Definition: switch_event.h:80
switch_status_t switch_queue_pop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1119
static switch_thread_t * thread
Definition: switch_log.c:279
static switch_thread_t * EVENT_DISPATCH_QUEUE_THREADS[MAX_DISPATCH_VAL]
Definition: switch_event.c:91
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
void switch_os_yield(void)
Definition: switch_time.c:141
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static int THREAD_COUNT
Definition: switch_event.c:97
void switch_event_deliver(switch_event_t **event)
Deliver an event to all of the registered event listeners.
Definition: switch_event.c:392
static switch_mutex_t * EVENT_QUEUE_MUTEX
Definition: switch_event.c:95
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static int DISPATCH_THREAD_COUNT
Definition: switch_event.c:98
static switch_status_t switch_event_queue_dispatch_event ( switch_event_t **  eventp)
static

Definition at line 350 of file switch_event.c.

References DISPATCH_QUEUE_LEN, DISPATCH_THREAD_COUNT, EVENT_DISPATCH_QUEUE, EVENT_QUEUE_MUTEX, MAX_DISPATCH, PENDING, SOFT_MAX_DISPATCH, switch_event_launch_dispatch_threads(), switch_mutex_lock(), switch_mutex_unlock(), switch_queue_push(), switch_queue_size(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and SYSTEM_RUNNING.

Referenced by switch_event_fire_detailed().

351 {
352 
353  switch_event_t *event = *eventp;
354 
355  if (!SYSTEM_RUNNING) {
356  return SWITCH_STATUS_FALSE;
357  }
358 
359  while (event) {
360  int launch = 0;
361 
363 
365  if (SOFT_MAX_DISPATCH + 1 > MAX_DISPATCH) {
366  launch++;
367  PENDING++;
368  }
369  }
370 
372 
373  if (launch) {
374  if (SOFT_MAX_DISPATCH + 1 < MAX_DISPATCH) {
376  }
377 
379  PENDING--;
381  }
382 
383  *eventp = NULL;
385  event = NULL;
386 
387  }
388 
389  return SWITCH_STATUS_SUCCESS;
390 }
unsigned int switch_queue_size(switch_queue_t *queue)
Definition: switch_apr.c:1114
static unsigned int MAX_DISPATCH
Definition: switch_event.c:81
static int SYSTEM_RUNNING
Definition: switch_event.c:101
#define DISPATCH_QUEUE_LEN
Definition: switch_event.c:42
static unsigned int SOFT_MAX_DISPATCH
Definition: switch_event.c:82
Representation of an event.
Definition: switch_event.h:80
void switch_event_launch_dispatch_threads(uint32_t max)
Definition: switch_event.c:623
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
static switch_queue_t * EVENT_DISPATCH_QUEUE
Definition: switch_event.c:93
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
static int PENDING
Definition: switch_event.c:348
static switch_mutex_t * EVENT_QUEUE_MUTEX
Definition: switch_event.c:95
switch_status_t switch_queue_push(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1129
static int DISPATCH_THREAD_COUNT
Definition: switch_event.c:98
switch_xml_t switch_event_xmlize ( switch_event_t event,
const char *  fmt,
  ... 
)

Definition at line 1852 of file switch_event.c.

References add_xml_header(), switch_event_header::array, switch_event_header::idx, switch_event_header::name, switch_event_header::next, switch_snprintf(), switch_xml_add_child_d, switch_xml_new(), switch_xml_set_txt_d, switch_event_header::value, and zstr.

1853 {
1855  char *data = NULL, *body = NULL;
1856  int ret = 0;
1857  switch_xml_t xml = NULL;
1858  uint32_t off = 0;
1859  va_list ap;
1860  switch_xml_t xheaders = NULL;
1861 
1862  if (!(xml = switch_xml_new("event"))) {
1863  return xml;
1864  }
1865 
1866  if (!zstr(fmt)) {
1867  va_start(ap, fmt);
1868 #ifdef HAVE_VASPRINTF
1869  ret = vasprintf(&data, fmt, ap);
1870 #else
1871  data = (char *) malloc(2048);
1872  if (!data) {
1873  va_end(ap);
1874  return NULL;
1875  }
1876  ret = vsnprintf(data, 2048, fmt, ap);
1877 #endif
1878  va_end(ap);
1879  if (ret == -1) {
1880 #ifndef HAVE_VASPRINTF
1881  free(data);
1882 #endif
1883  return NULL;
1884  }
1885  }
1886 
1887  if ((xheaders = switch_xml_add_child_d(xml, "headers", off++))) {
1888  int hoff = 0;
1889  for (hp = event->headers; hp; hp = hp->next) {
1890 
1891  if (hp->idx) {
1892  int i;
1893  for (i = 0; i < hp->idx; i++) {
1894  add_xml_header(xheaders, hp->name, hp->array[i], hoff++);
1895  }
1896  } else {
1897  add_xml_header(xheaders, hp->name, hp->value, hoff++);
1898  }
1899  }
1900  }
1901 
1902  if (!zstr(data)) {
1903  body = data;
1904  } else if (event->body) {
1905  body = event->body;
1906  }
1907 
1908  if (body) {
1909  int blen = (int) strlen(body);
1910  char blena[25];
1911  switch_snprintf(blena, sizeof(blena), "%d", blen);
1912  if (blen) {
1913  switch_xml_t xbody = NULL;
1914 
1915  add_xml_header(xml, "Content-Length", blena, off++);
1916  if ((xbody = switch_xml_add_child_d(xml, "body", off++))) {
1917  switch_xml_set_txt_d(xbody, body);
1918  }
1919  }
1920  }
1921 
1922  if (data) {
1923  free(data);
1924  }
1925 
1926  return xml;
1927 }
static switch_xml_t add_xml_header(switch_xml_t xml, char *name, char *value, int offset)
#define switch_xml_add_child_d(xml, name, off)
wrapper for switch_xml_add_child() that strdup()s name
Definition: switch_xml.h:269
An event Header.
Definition: switch_event.h:65
A representation of an XML tree.
Definition: switch_xml.h:76
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
#define zstr(x)
Definition: switch_utils.h:281
switch_xml_t switch_xml_new(_In_opt_z_ const char *name)
returns a new empty switch_xml structure with the given root tag name
struct switch_event_header * next
Definition: switch_event.h:76
#define switch_xml_set_txt_d(xml, txt)
wrapper for switch_xml_set_txt() that strdup()s txt \ sets the character content for the given tag an...
Definition: switch_xml.h:283
switch_event_header_t * headers
Definition: switch_event.h:90
static int switch_events_match ( switch_event_t event,
switch_event_node_t node 
)
static

Definition at line 225 of file switch_event.c.

References switch_event_node::event_id, switch_event::event_id, switch_event_node::subclass_name, switch_event::subclass_name, SWITCH_EVENT_ALL, and switch_event_get_header.

Referenced by switch_event_deliver().

226 {
227  int match = 0;
228 
229  if (node->event_id == SWITCH_EVENT_ALL) {
230  match++;
231 
232  if (!node->subclass_name) {
233  return match;
234  }
235  }
236 
237  if (match || event->event_id == node->event_id) {
238 
239  if (event->subclass_name && node->subclass_name) {
240  if (!strncasecmp(node->subclass_name, "file:", 5)) {
241  char *file_header;
242  if ((file_header = switch_event_get_header(event, "file")) != 0) {
243  match = !strcmp(node->subclass_name + 5, file_header) ? 1 : 0;
244  }
245  } else if (!strncasecmp(node->subclass_name, "func:", 5)) {
246  char *func_header;
247  if ((func_header = switch_event_get_header(event, "function")) != 0) {
248  match = !strcmp(node->subclass_name + 5, func_header) ? 1 : 0;
249  }
250  } else if (event->subclass_name && node->subclass_name) {
251  match = !strcmp(event->subclass_name, node->subclass_name) ? 1 : 0;
252  }
253  } else if ((event->subclass_name && !node->subclass_name) || (!event->subclass_name && !node->subclass_name)) {
254  match = 1;
255  } else {
256  match = 0;
257  }
258  }
259 
260  return match;
261 }
switch_event_types_t event_id
Definition: switch_event.h:82
switch_event_types_t event_id
Definition: switch_event.c:50
#define switch_event_get_header(_e, _h)
Definition: switch_event.h:172
char * subclass_name
Definition: switch_event.h:88
static void unsub_all_switch_event_channel ( void  )
static

Definition at line 2716 of file switch_event.c.

References switch_event_channel_sub_node_head_s::event_channel, event_channel_manager, switch_core_hash_delete(), switch_core_hash_first_iter(), switch_core_hash_this(), switch_event_channel_unsub_head(), switch_event_destroy(), switch_thread_rwlock_unlock(), and switch_thread_rwlock_wrlock().

Referenced by switch_event_shutdown().

2717 {
2718  switch_hash_index_t *hi = NULL;
2719  const void *var;
2720  void *val;
2722 
2724 
2725  while ((hi = switch_core_hash_first_iter( event_channel_manager.perm_hash, hi))) {
2726  switch_event_t *vals = NULL;
2727  switch_core_hash_this(hi, &var, NULL, &val);
2728  vals = (switch_event_t *) val;
2730  switch_event_destroy(&vals);
2731  }
2732 
2733  while ((hi = switch_core_hash_first_iter( event_channel_manager.hash, hi))) {
2734  switch_core_hash_this(hi, NULL, NULL, &val);
2735  head = (switch_event_channel_sub_node_head_t *) val;
2736  switch_event_channel_unsub_head(NULL, head);
2738  free(head->event_channel);
2739  free(head);
2740  }
2741 
2743 }
switch_status_t switch_thread_rwlock_unlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:263
Representation of an event.
Definition: switch_event.h:80
static struct @4 event_channel_manager
switch_status_t switch_thread_rwlock_wrlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:237
void switch_core_hash_this(_In_ switch_hash_index_t *hi, _Out_opt_ptrdiff_cap_(klen) const void **key, _Out_opt_ switch_ssize_t *klen, _Out_ void **val)
Gets the key and value of the current hash element.
void * switch_core_hash_delete(_In_ switch_hash_t *hash, _In_z_ const char *key)
Delete data from a hash based on desired key.
void switch_event_destroy(switch_event_t **event)
Destroy an event.
static uint32_t switch_event_channel_unsub_head(switch_event_channel_func_t func, switch_event_channel_sub_node_head_t *head)
switch_hash_index_t * switch_core_hash_first_iter(_In_ switch_hash_t *hash, switch_hash_index_t *hi)
Gets the first element of a hashtable.

Variable Documentation

switch_mutex_t* BLOCK = NULL
static
switch_hash_t* CUSTOM_HASH = NULL
static

Definition at line 96 of file switch_event.c.

int DISPATCH_THREAD_COUNT = 0
static
switch_queue_t* EVENT_CHANNEL_DISPATCH_QUEUE = NULL
static

Definition at line 94 of file switch_event.c.

Referenced by switch_event_channel_broadcast(), and switch_event_shutdown().

int EVENT_CHANNEL_DISPATCH_THREAD_COUNT = 0
static
int EVENT_CHANNEL_DISPATCH_THREAD_STARTING = 0
static
struct { ... } event_channel_manager
switch_queue_t* EVENT_DISPATCH_QUEUE = NULL
static
uint8_t EVENT_DISPATCH_QUEUE_RUNNING[MAX_DISPATCH_VAL] = { 0 }
static
switch_thread_t* EVENT_DISPATCH_QUEUE_THREADS[MAX_DISPATCH_VAL] = { 0 }
static
char* EVENT_NAMES[]
static

Definition at line 132 of file switch_event.c.

Referenced by switch_event_name(), and switch_name_event().

switch_event_node_t* EVENT_NODES[SWITCH_EVENT_ALL+1] = { NULL }
static

Definition at line 85 of file switch_event.c.

switch_mutex_t* EVENT_QUEUE_MUTEX = NULL
static
uint64_t EVENT_SEQUENCE_NR = 0
static

Definition at line 102 of file switch_event.c.

Referenced by switch_event_prep_for_delivery_detailed().

char guess_ip_v4[80] = ""
static

Definition at line 83 of file switch_event.c.

Referenced by switch_event_init(), and switch_event_prep_for_delivery_detailed().

char guess_ip_v6[80] = ""
static

Definition at line 84 of file switch_event.c.

Referenced by switch_event_init(), and switch_event_prep_for_delivery_detailed().

Definition at line 72 of file switch_event.c.

switch_hash_t* lahash

Definition at line 76 of file switch_event.c.

switch_mutex_t* lamutex

Definition at line 77 of file switch_event.c.

unsigned int MAX_DISPATCH = MAX_DISPATCH_VAL
static
int PENDING = 0
static

Definition at line 348 of file switch_event.c.

Referenced by switch_event_queue_dispatch_event().

switch_hash_t* perm_hash

Definition at line 75 of file switch_event.c.

switch_mutex_t* POOL_LOCK = NULL
static

Definition at line 88 of file switch_event.c.

Referenced by switch_event_init().

switch_memory_pool_t* RUNTIME_POOL = NULL
static

Definition at line 73 of file switch_event.c.

switch_thread_rwlock_t* RWLOCK = NULL
static
unsigned int SOFT_MAX_DISPATCH = 0
static
int SYSTEM_RUNNING = 0
static
int THREAD_COUNT = 0
static
switch_memory_pool_t* THRUNTIME_POOL = NULL
static