FreeSWITCH API Documentation  1.7.0
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 ()
 
 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 182 of file switch_cpp.h.

Constructor & Destructor Documentation

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's master pool.
Definition: switch_core.h:631
#define zstr(x)
Definition: switch_utils.h:281
switch_memory_pool_t * pool
Definition: switch_cpp.h:184
switch_queue_t * events
Definition: switch_cpp.h:187
uint32_t node_index
Definition: switch_cpp.h:193
switch_status_t switch_queue_create(switch_queue_t **queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
Definition: switch_apr.c:1109
int bind(const char *event_name, const char *subclass_name="")
Definition: switch_cpp.cpp:67
EventConsumer::~EventConsumer ( )

Definition at line 156 of file switch_cpp.cpp.

References cleanup().

157 {
158  cleanup();
159 }
EventConsumer::EventConsumer ( const char *  event_name = NULL,
const char *  subclass_name = "",
int  len = 5000 
)
EventConsumer::~EventConsumer ( )

Member Function Documentation

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

Definition at line 67 of file switch_cpp.cpp.

References event_handler(), SWITCH_CHANNEL_LOG, SWITCH_EVENT_ALL, switch_event_bind_removable(), SWITCH_EVENT_CUSTOM, SWITCH_LOG_ERROR, SWITCH_LOG_NOTICE, 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 
84  if (node_index <= SWITCH_EVENT_ALL &&
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_NOTICE, "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_event_node_t * enodes[SWITCH_EVENT_ALL+1]
Definition: switch_cpp.h:192
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:430
#define zstr(x)
Definition: switch_utils.h:281
uint32_t node_index
Definition: switch_cpp.h:193
#define switch_str_nil(s)
Make a null string a blank string instead.
Definition: switch_utils.h:903
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.
int EventConsumer::bind ( const char *  event_name,
const char *  subclass_name = "" 
)
void EventConsumer::cleanup ( )
void EventConsumer::cleanup ( )

Definition at line 123 of file switch_cpp.cpp.

References pool, switch_core_destroy_memory_pool, switch_event_destroy(), switch_event_unbind(), switch_queue_interrupt_all(), switch_queue_trypop(), and SWITCH_STATUS_SUCCESS.

Referenced by ~EventConsumer().

124 {
125 
126  uint32_t i;
127  void *pop;
128 
129  if (!ready) {
130  return;
131  }
132 
133  ready = 0;
134 
135  for (i = 0; i < node_index; i++) {
137  }
138 
139  node_index = 0;
140 
141  if (events) {
143  }
144 
146  switch_event_t *event = (switch_event_t *) pop;
147  switch_event_destroy(&event);
148  }
149 
150 
152 
153 }
switch_event_node_t * enodes[SWITCH_EVENT_ALL+1]
Definition: switch_cpp.h:192
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:640
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:1140
switch_memory_pool_t * pool
Definition: switch_cpp.h:184
switch_queue_t * events
Definition: switch_cpp.h:187
Event * pop(int block=0, int timeout=0)
Definition: switch_cpp.cpp:96
uint32_t node_index
Definition: switch_cpp.h:193
switch_status_t switch_queue_interrupt_all(switch_queue_t *queue)
Definition: switch_apr.c:1145
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.
Event * EventConsumer::pop ( int  block = 0,
int  timeout = 0 
)

Definition at line 96 of file switch_cpp.cpp.

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

97 {
98  void *pop = NULL;
99  Event *ret = NULL;
100  switch_event_t *event;
101 
102  if (!ready) {
103  return NULL;
104  }
105 
106  if (block) {
107  if (timeout > 0) {
108  switch_queue_pop_timeout(events, &pop, (switch_interval_time_t) timeout * 1000); // millisec rather than microsec
109  } else {
110  switch_queue_pop(events, &pop);
111  }
112  } else {
114  }
115 
116  if ((event = (switch_event_t *) pop)) {
117  ret = new Event(event, 1);
118  }
119 
120  return ret;
121 }
switch_status_t switch_queue_pop_timeout(switch_queue_t *queue, void **data, switch_interval_time_t timeout)
Definition: switch_apr.c:1124
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:1140
switch_status_t switch_queue_pop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1119
switch_queue_t * events
Definition: switch_cpp.h:187
Event * pop(int block=0, int timeout=0)
Definition: switch_cpp.cpp:96
int64_t switch_interval_time_t
Definition: switch_apr.h:191
Event* EventConsumer::pop ( int  block = 0,
int  timeout = 0 
)

Field Documentation

char * EventConsumer::e_callback

Definition at line 189 of file switch_cpp.h.

char * EventConsumer::e_cb_arg

Definition at line 191 of file switch_cpp.h.

switch_event_types_t EventConsumer::e_event_id

Definition at line 188 of file switch_cpp.h.

char * EventConsumer::e_subclass_name

Definition at line 190 of file switch_cpp.h.

switch_event_node_t * EventConsumer::enodes

Definition at line 192 of file switch_cpp.h.

switch_queue_t * EventConsumer::events

Definition at line 187 of file switch_cpp.h.

Referenced by event_handler(), and EventConsumer().

uint32_t EventConsumer::node_index

Definition at line 193 of file switch_cpp.h.

Referenced by EventConsumer().

switch_memory_pool_t * EventConsumer::pool
protected

Definition at line 184 of file switch_cpp.h.

Referenced by EventConsumer().

int EventConsumer::ready
protected

Definition at line 185 of file switch_cpp.h.

Referenced by EventConsumer().


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