RTS API Documentation  1.10.11
Public Member Functions | Data Fields | Protected Attributes
EventConsumer Class Reference

#include <switch_cpp.h>

+ Collaboration diagram for EventConsumer:

Public Member Functions

 EventConsumer (const char *event_name=NULL, const char *subclass_name="", int len=5000)
 
 ~ EventConsumer ()
 
int bind (const char *event_name, const char *subclass_name="")
 
Eventpop (int block=0, int timeout=0)
 
void cleanup ()
 

Data Fields

switch_queue_tevents
 
switch_event_types_t e_event_id
 
char * e_callback
 
char * e_subclass_name
 
char * e_cb_arg
 
switch_event_node_tenodes [SWITCH_EVENT_ALL+1]
 
uint32_t node_index
 

Protected Attributes

switch_memory_pool_tpool
 
int ready
 

Detailed Description

Definition at line 183 of file switch_cpp.h.

Constructor & Destructor Documentation

◆ EventConsumer()

EventConsumer::EventConsumer ( const char *  event_name = NULL,
const char *  subclass_name = "",
int  len = 5000 
)

Definition at line 54 of file switch_cpp.cpp.

References bind(), events, node_index, pool, ready, switch_core_new_memory_pool, switch_queue_create(), and zstr.

55 {
56 
59  node_index = 0;
60  ready = 1;
61 
62  if (!zstr(event_name)) {
63  bind(event_name, subclass_name);
64  }
65 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core&#39;s master pool.
Definition: switch_core.h:633
#define zstr(x)
Definition: switch_utils.h:314
uint32_t node_index
Definition: switch_cpp.h:194
switch_queue_t * events
Definition: switch_cpp.h:188
switch_memory_pool_t * pool
Definition: switch_cpp.h:185
switch_status_t switch_queue_create(switch_queue_t **queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
Definition: switch_apr.c:1233
int bind(const char *event_name, const char *subclass_name="")
Definition: switch_cpp.cpp:67

◆ ~ EventConsumer()

EventConsumer::~ EventConsumer ( )

Member Function Documentation

◆ bind()

int EventConsumer::bind ( const char *  event_name,
const char *  subclass_name = "" 
)

Definition at line 67 of file switch_cpp.cpp.

References enodes, event_handler(), node_index, ready, SWITCH_CHANNEL_LOG, SWITCH_EVENT_ALL, switch_event_bind_removable(), SWITCH_EVENT_CUSTOM, SWITCH_LOG_DEBUG, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_LOG_WARNING, switch_name_event(), SWITCH_STATUS_SUCCESS, switch_str_nil, and zstr.

Referenced by EventConsumer().

68 {
70 
71  if (!ready) {
72  return 0;
73  }
74 
75  if (switch_name_event(event_name, &event_id) != SWITCH_STATUS_SUCCESS) {
76  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't bind to %s, event not found\n", event_name);
77  return 0;
78  }
79 
80  if (zstr(subclass_name)) {
81  subclass_name = NULL;
82  }
83 
85  switch_event_bind_removable(__FILE__, event_id, subclass_name, event_handler, this, &enodes[node_index]) == SWITCH_STATUS_SUCCESS) {
86  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "bound to %s %s\n", event_name, switch_str_nil(subclass_name));
87  node_index++;
88  return 1;
89  }
90 
91  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot bind to %s %s\n", event_name, switch_str_nil(subclass_name));
92  return 0;
93 }
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.
#define SWITCH_CHANNEL_LOG
switch_event_types_t
Built-in Events.
switch_status_t switch_name_event(const char *name, switch_event_types_t *type)
return the event id that matches a given event name
Definition: switch_event.c:438
#define zstr(x)
Definition: switch_utils.h:314
switch_event_node_t * enodes[SWITCH_EVENT_ALL+1]
Definition: switch_cpp.h:193
uint32_t node_index
Definition: switch_cpp.h:194
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:993
static void event_handler(switch_event_t *event)
Definition: switch_cpp.cpp:40
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.

◆ cleanup()

void EventConsumer::cleanup ( )

Definition at line 126 of file switch_cpp.cpp.

References enodes, events, node_index, pool, pop(), ready, switch_core_destroy_memory_pool, SWITCH_DECLARE_CONSTRUCTOR, switch_event_destroy(), switch_event_unbind(), switch_queue_interrupt_all(), switch_queue_trypop(), and SWITCH_STATUS_SUCCESS.

127 {
128 
129  uint32_t i;
130  void *pop;
131 
132  if (!ready) {
133  return;
134  }
135 
136  ready = 0;
137 
138  for (i = 0; i < node_index; i++) {
140  }
141 
142  node_index = 0;
143 
145 
147  switch_event_t *event = (switch_event_t *) pop;
148  switch_event_destroy(&event);
149  }
150 
151 
153 
154 }
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:642
Representation of an event.
Definition: switch_event.h:80
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1264
switch_event_node_t * enodes[SWITCH_EVENT_ALL+1]
Definition: switch_cpp.h:193
Event * pop(int block=0, int timeout=0)
Definition: switch_cpp.cpp:96
uint32_t node_index
Definition: switch_cpp.h:194
switch_queue_t * events
Definition: switch_cpp.h:188
switch_status_t switch_queue_interrupt_all(switch_queue_t *queue)
Definition: switch_apr.c:1269
switch_memory_pool_t * pool
Definition: switch_cpp.h:185
void switch_event_destroy(switch_event_t **event)
Destroy an event.
switch_status_t switch_event_unbind(switch_event_node_t **node)
Unbind a bound event consumer.

◆ pop()

Event * EventConsumer::pop ( int  block = 0,
int  timeout = 0 
)

Definition at line 96 of file switch_cpp.cpp.

References events, ready, switch_queue_pop(), switch_queue_pop_timeout(), and switch_queue_trypop().

Referenced by cleanup().

97 {
98  void *pop = NULL;
99  Event *ret = NULL;
100  switch_event_t *event;
101  switch_status_t res;
102 
103  if (!ready) {
104  return NULL;
105  }
106 
107  if (block) {
108  if (timeout > 0) {
109  res = switch_queue_pop_timeout(events, &pop, (switch_interval_time_t) timeout * 1000); // millisec rather than microsec
110  } else {
111  res = switch_queue_pop(events, &pop);
112  }
113  } else {
114  res = switch_queue_trypop(events, &pop);
115  }
116 
117  (void)res;
118 
119  if ((event = (switch_event_t *) pop)) {
120  ret = new Event(event, 1);
121  }
122 
123  return ret;
124 }
switch_status_t switch_queue_pop_timeout(switch_queue_t *queue, void **data, switch_interval_time_t timeout)
Definition: switch_apr.c:1248
Representation of an event.
Definition: switch_event.h:80
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1264
switch_status_t switch_queue_pop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1243
Event * pop(int block=0, int timeout=0)
Definition: switch_cpp.cpp:96
int64_t switch_interval_time_t
Definition: switch_apr.h:191
switch_status_t
Common return values.
switch_queue_t * events
Definition: switch_cpp.h:188

Field Documentation

◆ e_callback

char* EventConsumer::e_callback

Definition at line 190 of file switch_cpp.h.

◆ e_cb_arg

char* EventConsumer::e_cb_arg

Definition at line 192 of file switch_cpp.h.

◆ e_event_id

switch_event_types_t EventConsumer::e_event_id

Definition at line 189 of file switch_cpp.h.

◆ e_subclass_name

char* EventConsumer::e_subclass_name

Definition at line 191 of file switch_cpp.h.

◆ enodes

switch_event_node_t* EventConsumer::enodes[SWITCH_EVENT_ALL+1]

Definition at line 193 of file switch_cpp.h.

Referenced by bind(), and cleanup().

◆ events

switch_queue_t* EventConsumer::events

Definition at line 188 of file switch_cpp.h.

Referenced by cleanup(), event_handler(), EventConsumer(), and pop().

◆ node_index

uint32_t EventConsumer::node_index

Definition at line 194 of file switch_cpp.h.

Referenced by bind(), cleanup(), and EventConsumer().

◆ pool

switch_memory_pool_t* EventConsumer::pool
protected

Definition at line 185 of file switch_cpp.h.

Referenced by cleanup(), EventConsumer(), and IVRMenu::IVRMenu().

◆ ready

int EventConsumer::ready
protected

Definition at line 186 of file switch_cpp.h.

Referenced by bind(), cleanup(), EventConsumer(), and pop().


The documentation for this class was generated from the following files: