RTS API Documentation  1.10.11
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)
 
bool merge (Event *to_merge)
 

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() [1/2]

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

Definition at line 294 of file switch_cpp.cpp.

References 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.

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

Event::Event ( switch_event_t wrap_me,
int  free_me = 0 
)

Definition at line 325 of file switch_cpp.cpp.

References SWITCH_DECLARE_CONSTRUCTOR, and switch_event_destroy().

326 {
327  event = wrap_me;
328  mine = free_me;
329  serialized_string = NULL;
330 }
int mine
Definition: switch_cpp.h:163
char * serialized_string
Definition: switch_cpp.h:162

◆ ~ Event()

virtual Event::~ Event ( )
virtual

Member Function Documentation

◆ addBody()

bool Event::addBody ( const char *  value)

Definition at line 484 of file switch_cpp.cpp.

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

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

◆ addHeader()

bool Event::addHeader ( const char *  header_name,
const char *  value 
)

Definition at line 452 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, this_check, and true.

453 {
454  this_check(false);
455 
456  if (event) {
458  } else {
459  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to addHeader an event that does not exist!\n");
460  }
461 
462  return false;
463 }
#define SWITCH_CHANNEL_LOG
const char *const const char *const const cJSON *const value
#define this_check(x)
Definition: switch_cpp.h:12
switch_event_t * event
Definition: switch_cpp.h:161
#define true
Definition: cJSON.c:61
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.

◆ chat_execute()

int Event::chat_execute ( const char *  app,
const char *  data = NULL 
)

Definition at line 344 of file switch_cpp.cpp.

References switch_core_execute_chat_app().

345 {
346  return (int) switch_core_execute_chat_app(event, app, data);
347 }
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)

◆ chat_send()

int Event::chat_send ( const char *  dest_proto = NULL)

Definition at line 349 of file switch_cpp.cpp.

References switch_core_chat_send(), switch_event_get_header, and zstr.

350 {
351  if (zstr(dest_proto)) {
352  dest_proto = switch_event_get_header(event, "dest_proto");
353  }
354 
355  return (int) switch_core_chat_send(dest_proto, event);
356 }
#define zstr(x)
Definition: switch_utils.h:314
switch_event_t * event
Definition: switch_cpp.h:161
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

◆ delHeader()

bool Event::delHeader ( const char *  header_name)

Definition at line 465 of file switch_cpp.cpp.

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

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

◆ fire()

bool Event::fire ( void  )

Definition at line 394 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.

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

◆ getBody()

char * Event::getBody ( void  )

Definition at line 497 of file switch_cpp.cpp.

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

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

◆ getHeader()

const char * Event::getHeader ( const char *  header_name)

Definition at line 435 of file switch_cpp.cpp.

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

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

◆ getType()

const char * Event::getType ( void  )

Definition at line 511 of file switch_cpp.cpp.

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

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

◆ merge()

bool Event::merge ( Event to_merge)

Definition at line 524 of file switch_cpp.cpp.

References event, SWITCH_CHANNEL_LOG, switch_event_merge(), SWITCH_LOG_ERROR, switch_log_printf(), and this_check.

525 {
526  this_check(false);
527 
528  if (!event) {
529  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to merge to an event that does not exist!\n");
530  return false;
531  }
532 
533  if (!to_merge || !to_merge->event) {
534  switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to merge from an event that does not exist!\n");
535  return false;
536  }
537 
538  switch_event_merge(event, to_merge->event);
539 
540  return true;
541 }
#define SWITCH_CHANNEL_LOG
#define this_check(x)
Definition: switch_cpp.h:12
switch_event_t * event
Definition: switch_cpp.h:161
void switch_event_merge(switch_event_t *event, switch_event_t *tomerge)
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.

◆ serialize()

const char * Event::serialize ( const char *  format = NULL)

Definition at line 358 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.

359 {
360  this_check("");
361 
362 
364 
365  if (!event) {
366  return "";
367  }
368 
369  if (format && !strcasecmp(format, "xml")) {
370  switch_xml_t xml;
371  if ((xml = switch_event_xmlize(event, SWITCH_VA_NONE))) {
373  switch_xml_free(xml);
374  return serialized_string;
375  } else {
376  return "";
377  }
378  } else if (format && !strcasecmp(format, "json")) {
380  return serialized_string;
381  } else {
383  char *new_serialized_string = switch_mprintf("%s", serialized_string);
384  free(serialized_string);
385  serialized_string = new_serialized_string;
386  return serialized_string;
387  }
388  }
389 
390  return "";
391 
392 }
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
#define this_check(x)
Definition: switch_cpp.h:12
A representation of an XML tree.
Definition: switch_xml.h:79
char const int const cJSON_bool format
Definition: switch_cJSON.h:153
switch_event_t * event
Definition: switch_cpp.h:161
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
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,...)
#define switch_xml_toxml(xml, prn_header)
Converts an switch_xml structure back to xml in html format. Returns a string of html data that \ mus...
Definition: switch_xml.h:224
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.

◆ setPriority()

bool Event::setPriority ( switch_priority_t  priority = SWITCH_PRIORITY_NORMAL)

Definition at line 422 of file switch_cpp.cpp.

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

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

Field Documentation

◆ event

switch_event_t* Event::event

Definition at line 161 of file switch_cpp.h.

Referenced by merge(), CoreSession::sendEvent(), and CoreSession::setEventData().

◆ mine

int Event::mine

Definition at line 163 of file switch_cpp.h.

◆ serialized_string

char* Event::serialized_string

Definition at line 162 of file switch_cpp.h.


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