FreeSWITCH API Documentation  1.7.0
Public Member Functions | Data Fields
Event Class Reference

#include <switch_cpp.h>

+ Collaboration diagram for Event:

Public Member Functions

 Event (const char *type, const char *subclass_name=NULL)
 
 Event (switch_event_t *wrap_me, int free_me=0)
 
virtual ~Event ()
 
int chat_execute (const char *app, const char *data=NULL)
 
int chat_send (const char *dest_proto=NULL)
 
const char * serialize (const char *format=NULL)
 
bool setPriority (switch_priority_t priority=SWITCH_PRIORITY_NORMAL)
 
const char * getHeader (const char *header_name)
 
char * getBody (void)
 
const char * getType (void)
 
bool addBody (const char *value)
 
bool addHeader (const char *header_name, const char *value)
 
bool delHeader (const char *header_name)
 
bool fire (void)
 
 Event (const char *type, const char *subclass_name=NULL)
 
 Event (switch_event_t *wrap_me, int free_me=0)
 
virtual ~Event ()
 
int chat_execute (const char *app, const char *data=NULL)
 
int chat_send (const char *dest_proto=NULL)
 
const char * serialize (const char *format=NULL)
 
bool setPriority (switch_priority_t priority=SWITCH_PRIORITY_NORMAL)
 
const char * getHeader (const char *header_name)
 
char * getBody (void)
 
const char * getType (void)
 
bool addBody (const char *value)
 
bool addHeader (const char *header_name, const char *value)
 
bool delHeader (const char *header_name)
 
bool fire (void)
 

Data Fields

switch_event_tevent
 
char * serialized_string
 
int mine
 

Detailed Description

Definition at line 158 of file switch_cpp.h.

Constructor & Destructor Documentation

Event::Event ( const char *  type,
const char *  subclass_name = NULL 
)

Definition at line 293 of file switch_cpp.cpp.

References event, mine, serialized_string, SWITCH_CHANNEL_LOG, switch_event_create_json(), switch_event_create_subclass, SWITCH_EVENT_CUSTOM, SWITCH_EVENT_MESSAGE, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_LOG_WARNING, switch_name_event(), SWITCH_STATUS_SUCCESS, and zstr.

294 {
295  switch_event_types_t event_id;
296 
297  if (!strcasecmp(type, "json") && !zstr(subclass_name)) {
298  if (switch_event_create_json(&event, subclass_name) != SWITCH_STATUS_SUCCESS) {
299  return;
300  }
301 
302  event_id = event->event_id;
303 
304  } else {
305  if (switch_name_event(type, &event_id) != SWITCH_STATUS_SUCCESS) {
306  event_id = SWITCH_EVENT_MESSAGE;
307  }
308 
309  if (!zstr(subclass_name) && event_id != SWITCH_EVENT_CUSTOM) {
310  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_WARNING, "Changing event type to custom because you specified a subclass name!\n");
311  event_id = SWITCH_EVENT_CUSTOM;
312  }
313 
314  if (switch_event_create_subclass(&event, event_id, subclass_name) != SWITCH_STATUS_SUCCESS) {
315  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Failed to create event!\n");
316  event = NULL;
317  }
318  }
319 
320  serialized_string = NULL;
321  mine = 1;
322 }
int mine
Definition: switch_cpp.h:163
#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
switch_event_t * event
Definition: switch_cpp.h:161
#define switch_event_create_subclass(_e, _eid, _sn)
Definition: switch_event.h:153
#define zstr(x)
Definition: switch_utils.h:281
switch_status_t switch_event_create_json(switch_event_t **event, const char *json)
char * serialized_string
Definition: switch_cpp.h:162
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.
Event::Event ( switch_event_t wrap_me,
int  free_me = 0 
)

Definition at line 324 of file switch_cpp.cpp.

References mine, and serialized_string.

325 {
326  event = wrap_me;
327  mine = free_me;
328  serialized_string = NULL;
329 }
int mine
Definition: switch_cpp.h:163
char * serialized_string
Definition: switch_cpp.h:162
Event::~Event ( )
virtual

Definition at line 331 of file switch_cpp.cpp.

References event, mine, serialized_string, and switch_event_destroy().

332 {
333 
334  if (serialized_string) {
335  free(serialized_string);
336  }
337 
338  if (event && mine) {
340  }
341 }
int mine
Definition: switch_cpp.h:163
switch_event_t * event
Definition: switch_cpp.h:161
char * serialized_string
Definition: switch_cpp.h:162
void switch_event_destroy(switch_event_t **event)
Destroy an event.
Event::Event ( const char *  type,
const char *  subclass_name = NULL 
)
Event::Event ( switch_event_t wrap_me,
int  free_me = 0 
)
virtual Event::~Event ( )
virtual

Member Function Documentation

bool Event::addBody ( const char *  value)

Definition at line 483 of file switch_cpp.cpp.

References SWITCH_CHANNEL_LOG, switch_event_add_body(), SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_SUCCESS, and this_check.

484 {
485  this_check(false);
486 
487  if (event) {
488  return switch_event_add_body(event, "%s", value) == SWITCH_STATUS_SUCCESS ? true : false;
489  } else {
490  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to addBody an event that does not exist!\n");
491  }
492 
493  return false;
494 }
#define SWITCH_CHANNEL_LOG
switch_event_t * event
Definition: switch_cpp.h:161
switch_status_t switch_event_add_body(switch_event_t *event, const char *fmt,...) PRINTF_FUNCTION(2
Add a body to an event.
#define this_check(x)
Definition: switch_cpp.h:12
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.
bool Event::addBody ( const char *  value)
bool Event::addHeader ( const char *  header_name,
const char *  value 
)

Definition at line 451 of file switch_cpp.cpp.

References SWITCH_CHANNEL_LOG, switch_event_add_header_string(), SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STACK_BOTTOM, SWITCH_STATUS_SUCCESS, and this_check.

452 {
453  this_check(false);
454 
455  if (event) {
456  return switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header_name, value) == SWITCH_STATUS_SUCCESS ? true : false;
457  } else {
458  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to addHeader an event that does not exist!\n");
459  }
460 
461  return false;
462 }
#define SWITCH_CHANNEL_LOG
switch_event_t * event
Definition: switch_cpp.h:161
#define this_check(x)
Definition: switch_cpp.h:12
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.
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.
bool Event::addHeader ( const char *  header_name,
const char *  value 
)
int Event::chat_execute ( const char *  app,
const char *  data = NULL 
)

Definition at line 343 of file switch_cpp.cpp.

References switch_core_execute_chat_app().

344 {
345  return (int) switch_core_execute_chat_app(event, app, data);
346 }
switch_event_t * event
Definition: switch_cpp.h:161
switch_status_t switch_core_execute_chat_app(switch_event_t *message, const char *app, const char *data)
int Event::chat_execute ( const char *  app,
const char *  data = NULL 
)
int Event::chat_send ( const char *  dest_proto = NULL)

Definition at line 348 of file switch_cpp.cpp.

References switch_core_chat_send(), switch_event_get_header, and zstr.

349 {
350  if (zstr(dest_proto)) {
351  dest_proto = switch_event_get_header(event, "dest_proto");
352  }
353 
354  return (int) switch_core_chat_send(dest_proto, event);
355 }
switch_event_t * event
Definition: switch_cpp.h:161
#define zstr(x)
Definition: switch_utils.h:281
switch_status_t switch_core_chat_send(const char *dest_proto, switch_event_t *message_event)
#define switch_event_get_header(_e, _h)
Definition: switch_event.h:172
int Event::chat_send ( const char *  dest_proto = NULL)
bool Event::delHeader ( const char *  header_name)
bool Event::delHeader ( const char *  header_name)

Definition at line 464 of file switch_cpp.cpp.

References SWITCH_CHANNEL_LOG, switch_event_del_header, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_SUCCESS, this_check, and zstr.

465 {
466  this_check(false);
467 
468  if (zstr(header_name)) {
469  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Trying to delHeader an invalid header!\n");
470  return false;
471  }
472 
473  if (event) {
474  return switch_event_del_header(event, header_name) == SWITCH_STATUS_SUCCESS ? true : false;
475  } else {
476  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to delHeader an event that does not exist!\n");
477  }
478 
479  return false;
480 }
#define SWITCH_CHANNEL_LOG
#define switch_event_del_header(_e, _h)
Definition: switch_event.h:211
switch_event_t * event
Definition: switch_cpp.h:161
#define this_check(x)
Definition: switch_cpp.h:12
#define zstr(x)
Definition: switch_utils.h:281
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.
bool Event::fire ( void  )
bool Event::fire ( void  )

Definition at line 393 of file switch_cpp.cpp.

References SWITCH_CHANNEL_LOG, switch_event_destroy(), switch_event_dup(), switch_event_fire, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_STATUS_SUCCESS, and this_check.

394 {
395 
396  this_check(false);
397 
398  if (!mine) {
400  return false;
401  }
402 
403  if (event) {
404  switch_event_t *new_event;
405  if (switch_event_dup(&new_event, event) == SWITCH_STATUS_SUCCESS) {
406  if (switch_event_fire(&new_event) != SWITCH_STATUS_SUCCESS) {
407  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Failed to fire the event!\n");
408  switch_event_destroy(&new_event);
409  return false;
410  }
411  return true;
412  } else {
413  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Failed to dup the event!\n");
414  }
415  } else {
416  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to fire an event that does not exist!\n");
417  }
418  return false;
419 }
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:412
int mine
Definition: switch_cpp.h:163
#define SWITCH_CHANNEL_LOG
switch_event_t * event
Definition: switch_cpp.h:161
Representation of an event.
Definition: switch_event.h:80
#define this_check(x)
Definition: switch_cpp.h:12
switch_status_t switch_event_dup(switch_event_t **event, switch_event_t *todup)
Duplicate an event.
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.
void switch_event_destroy(switch_event_t **event)
Destroy an event.
char* Event::getBody ( void  )
char * Event::getBody ( void  )

Definition at line 496 of file switch_cpp.cpp.

References SWITCH_CHANNEL_LOG, switch_event_get_body(), SWITCH_LOG_ERROR, switch_log_printf(), and this_check.

497 {
498 
499  this_check((char *)"");
500 
501  if (event) {
503  } else {
504  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to getBody an event that does not exist!\n");
505  }
506 
507  return NULL;
508 }
#define SWITCH_CHANNEL_LOG
switch_event_t * event
Definition: switch_cpp.h:161
#define this_check(x)
Definition: switch_cpp.h:12
char * switch_event_get_body(switch_event_t *event)
Retrieve the body value from an event.
Definition: switch_event.c:838
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.
const char * Event::getHeader ( const char *  header_name)

Definition at line 434 of file switch_cpp.cpp.

References SWITCH_CHANNEL_LOG, switch_event_get_header, SWITCH_LOG_ERROR, switch_log_printf(), this_check, and zstr.

435 {
436  this_check("");
437 
438  if (zstr(header_name)) {
439  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Trying to getHeader an invalid header!\n");
440  return NULL;
441  }
442 
443  if (event) {
444  return switch_event_get_header(event, header_name);
445  } else {
446  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to getHeader an event that does not exist!\n");
447  }
448  return NULL;
449 }
#define SWITCH_CHANNEL_LOG
switch_event_t * event
Definition: switch_cpp.h:161
#define this_check(x)
Definition: switch_cpp.h:12
#define zstr(x)
Definition: switch_utils.h:281
#define switch_event_get_header(_e, _h)
Definition: switch_event.h:172
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.
const char* Event::getHeader ( const char *  header_name)
const char * Event::getType ( void  )

Definition at line 510 of file switch_cpp.cpp.

References SWITCH_CHANNEL_LOG, switch_event_name(), SWITCH_LOG_ERROR, switch_log_printf(), and this_check.

511 {
512  this_check("");
513 
514  if (event) {
516  } else {
517  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to getType an event that does not exist!\n");
518  }
519 
520  return (char *) "invalid";
521 }
switch_event_types_t event_id
Definition: switch_event.h:82
#define SWITCH_CHANNEL_LOG
switch_event_t * event
Definition: switch_cpp.h:161
#define this_check(x)
Definition: switch_cpp.h:12
const char * switch_event_name(switch_event_types_t event)
Render the name of an event id enumeration.
Definition: switch_event.c:422
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.
const char* Event::getType ( void  )
const char * Event::serialize ( const char *  format = NULL)

Definition at line 357 of file switch_cpp.cpp.

References switch_event_serialize(), switch_event_serialize_json(), switch_event_xmlize(), SWITCH_FALSE, switch_mprintf(), switch_safe_free, SWITCH_STATUS_SUCCESS, SWITCH_TRUE, SWITCH_VA_NONE, switch_xml_free(), switch_xml_toxml(), and this_check.

358 {
359  this_check("");
360 
361 
363 
364  if (!event) {
365  return "";
366  }
367 
368  if (format && !strcasecmp(format, "xml")) {
369  switch_xml_t xml;
370  if ((xml = switch_event_xmlize(event, SWITCH_VA_NONE))) {
372  switch_xml_free(xml);
373  return serialized_string;
374  } else {
375  return "";
376  }
377  } else if (format && !strcasecmp(format, "json")) {
379  return serialized_string;
380  } else {
382  char *new_serialized_string = switch_mprintf("'%s'", serialized_string);
383  free(serialized_string);
384  serialized_string = new_serialized_string;
385  return serialized_string;
386  }
387  }
388 
389  return "";
390 
391 }
switch_status_t switch_event_serialize_json(switch_event_t *event, char **str)
void switch_xml_free(_In_opt_ switch_xml_t xml)
frees the memory allocated for an switch_xml structure
char * switch_xml_toxml(_In_ switch_xml_t xml, _In_ switch_bool_t prn_header)
Converts an switch_xml structure back to xml in html format. Returns a string of html data that \ mus...
switch_event_t * event
Definition: switch_cpp.h:161
#define this_check(x)
Definition: switch_cpp.h:12
A representation of an XML tree.
Definition: switch_xml.h:76
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
char * serialized_string
Definition: switch_cpp.h:162
#define SWITCH_VA_NONE
switch_status_t switch_event_serialize(switch_event_t *event, char **str, switch_bool_t encode)
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
switch_xml_t switch_event_xmlize(switch_event_t *event, const char *fmt,...) PRINTF_FUNCTION(2
Render a XML representation of an event suitable for printing or network transport.
const char* Event::serialize ( const char *  format = NULL)
bool Event::setPriority ( switch_priority_t  priority = SWITCH_PRIORITY_NORMAL)

Definition at line 421 of file switch_cpp.cpp.

References SWITCH_CHANNEL_LOG, switch_event_set_priority(), SWITCH_LOG_ERROR, switch_log_printf(), and this_check.

422 {
423  this_check(false);
424 
425  if (event) {
426  switch_event_set_priority(event, priority);
427  return true;
428  } else {
429  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to setPriority an event that does not exist!\n");
430  }
431  return false;
432 }
#define SWITCH_CHANNEL_LOG
switch_event_t * event
Definition: switch_cpp.h:161
switch_status_t switch_event_set_priority(switch_event_t *event, switch_priority_t priority)
Set the priority of an event.
Definition: switch_event.c:760
#define this_check(x)
Definition: switch_cpp.h:12
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.
bool Event::setPriority ( switch_priority_t  priority = SWITCH_PRIORITY_NORMAL)

Field Documentation

switch_event_t * Event::event

Definition at line 161 of file switch_cpp.h.

Referenced by Event(), and ~Event().

int Event::mine

Definition at line 163 of file switch_cpp.h.

Referenced by Event(), and ~Event().

char * Event::serialized_string

Definition at line 162 of file switch_cpp.h.

Referenced by Event(), and ~Event().


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