add funcs to binary seralize/deserialize switch_events into a contiguous binary frame

This commit is contained in:
Anthony Minessale 2012-09-13 17:46:13 -05:00
parent c28c99c394
commit d5e5fb032a
2 changed files with 108 additions and 1 deletions

View File

@ -102,6 +102,20 @@ struct switch_event {
int flags; int flags;
}; };
typedef struct switch_serial_event_s {
int event_id;
int priority;
int flags;
char *owner;
char *subclass_name;
char *body;
} switch_serial_event_t;
typedef struct switch_serial_event_header_s {
char *name;
char *value;
} switch_serial_event_header_t;
typedef enum { typedef enum {
EF_UNIQ_HEADERS = (1 << 0), EF_UNIQ_HEADERS = (1 << 0),
EF_NO_CHAT_EXEC = (1 << 1), EF_NO_CHAT_EXEC = (1 << 1),
@ -295,6 +309,8 @@ SWITCH_DECLARE(switch_status_t) switch_event_free_subclass_detailed(const char *
\return SWITCH_STATUS_SUCCESS if the operation was successful \return SWITCH_STATUS_SUCCESS if the operation was successful
\note you must free the resulting string when you are finished with it \note you must free the resulting string when you are finished with it
*/ */
SWITCH_DECLARE(switch_status_t) switch_event_binary_deserialize(switch_event_t **eventp, void **data, switch_size_t len, switch_bool_t destroy);
SWITCH_DECLARE(switch_status_t) switch_event_binary_serialize(switch_event_t *event, void **data, switch_size_t *len);
SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, char **str, switch_bool_t encode); SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, char **str, switch_bool_t encode);
SWITCH_DECLARE(switch_status_t) switch_event_serialize_json(switch_event_t *event, char **str); SWITCH_DECLARE(switch_status_t) switch_event_serialize_json(switch_event_t *event, char **str);
SWITCH_DECLARE(switch_status_t) switch_event_create_json(switch_event_t **event, const char *json); SWITCH_DECLARE(switch_status_t) switch_event_create_json(switch_event_t **event, const char *json);

View File

@ -34,7 +34,7 @@
#include <switch.h> #include <switch.h>
#include <switch_event.h> #include <switch_event.h>
#include "tpl.h"
//#define SWITCH_EVENT_RECYCLE //#define SWITCH_EVENT_RECYCLE
#define DISPATCH_QUEUE_LEN 100 #define DISPATCH_QUEUE_LEN 100
@ -1284,6 +1284,97 @@ SWITCH_DECLARE(switch_status_t) switch_event_dup_reply(switch_event_t **event, s
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
#define SWITCH_SERIALIZED_EVENT_MAP "S(iiisss)A(S(ss))"
SWITCH_DECLARE(switch_status_t) switch_event_binary_deserialize(switch_event_t **eventp, void **data, switch_size_t len, switch_bool_t destroy)
{
switch_event_t *event;
tpl_node *tn;
switch_serial_event_t e;
switch_serial_event_header_t sh;
int how = TPL_MEM;
switch_event_create(&event, SWITCH_EVENT_CLONE);
switch_assert(event);
tn = tpl_map(SWITCH_SERIALIZED_EVENT_MAP, &e, &sh);
if (!destroy) {
how |= TPL_EXCESS_OK;
}
tpl_load(tn, how, data, len);
tpl_unpack(tn, 0);
event->event_id = e.event_id;
event->priority = e.priority;
event->flags = e.flags;
event->owner = e.owner;
event->subclass_name = e.subclass_name;
event->body = e.body;
while(tpl_unpack(tn, 1)) {
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, sh.name, sh.value);
}
*eventp = event;
tpl_free(tn);
if (destroy) {
free(*data);
}
*data = NULL;
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_status_t) switch_event_binary_serialize(switch_event_t *event, void **data, switch_size_t *len)
{
tpl_node *tn;
switch_serial_event_t e;
switch_serial_event_header_t sh;
switch_event_header_t *eh;
int how = TPL_MEM;
e.event_id = event->event_id;
e.priority = event->priority;
e.flags = event->flags;
e.owner = event->owner;
e.subclass_name = event->subclass_name;
e.body = event->body;
tn = tpl_map(SWITCH_SERIALIZED_EVENT_MAP, &e, &sh);
tpl_pack(tn, 0);
for (eh = event->headers; eh; eh = eh->next) {
if (eh->idx) continue; // no arrays yet
sh.name = eh->name;
sh.value = eh->value;
tpl_pack(tn, 1);
}
if (*len > 0) {
how |= TPL_PREALLOCD;
}
tpl_dump(tn, how, data, len);
tpl_free(tn);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, char **str, switch_bool_t encode) SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, char **str, switch_bool_t encode)
{ {
switch_size_t len = 0; switch_size_t len = 0;