make event_serialize dynamic

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@3314 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale
2006-11-11 19:32:45 +00:00
parent eb1bafe567
commit 68bab16e37
8 changed files with 71 additions and 70 deletions
@@ -158,17 +158,20 @@ static void event_handler(switch_event_t *event)
if (send) {
char *packet;
memcpy(buf, &globals.host_hash, sizeof(globals.host_hash));
packet = buf + sizeof(globals.host_hash);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Multicast-Sender", globals.hostname);
switch (event->event_id) {
case SWITCH_EVENT_LOG:
return;
default:
switch_event_serialize(event, packet, sizeof(buf) - sizeof(globals.host_hash), NULL);
len = strlen(packet) + sizeof(globals.host_hash);;
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\nEVENT\n--------------------------------\n%s\n", buf);
switch_socket_sendto(globals.udp_socket, globals.addr, 0, buf, &len);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Multicast-Sender", globals.hostname);
if (switch_event_serialize(event, &packet) == SWITCH_STATUS_SUCCESS) {
memcpy(buf, &globals.host_hash, sizeof(globals.host_hash));
switch_copy_string(buf + sizeof(globals.host_hash), packet, sizeof(buf) - sizeof(globals.host_hash));
len = strlen(packet) + sizeof(globals.host_hash);;
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\nEVENT\n--------------------------------\n%s\n", buf);
switch_socket_sendto(globals.udp_socket, globals.addr, 0, buf, &len);
switch_safe_free(packet);
}
break;
}
}
@@ -345,24 +345,18 @@ static switch_status_t read_packet(listener_t *listener, switch_event_t **event,
if (switch_queue_trypop(listener->event_queue, &pop) == SWITCH_STATUS_SUCCESS) {
char hbuf[512];
switch_event_t *event = (switch_event_t *) pop;
char *etype, *packet, *xmlstr = NULL;
if (!listener->ebuf) {
listener->ebuf = switch_core_alloc(listener->pool, CMD_BUFLEN);
}
char *etype;
do_sleep = 0;
if (listener->format == EVENT_FORMAT_PLAIN) {
etype = "plain";
switch_event_serialize(event, listener->ebuf, CMD_BUFLEN, NULL);
packet = listener->ebuf;
switch_event_serialize(event, &listener->ebuf);
} else {
switch_xml_t xml;
etype = "xml";
if ((xml = switch_event_xmlize(event, NULL))) {
xmlstr = switch_xml_toxml(xml);
packet = xmlstr;
listener->ebuf = switch_xml_toxml(xml);
switch_xml_free(xml);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "XML ERROR!\n");
@@ -370,7 +364,7 @@ static switch_status_t read_packet(listener_t *listener, switch_event_t **event,
}
}
len = strlen(packet);
len = strlen(listener->ebuf);
snprintf(hbuf, sizeof(hbuf), "Content-Length: %"APR_SSIZE_T_FMT"\n"
"Content-Type: text/event-%s\n"
@@ -379,12 +373,10 @@ static switch_status_t read_packet(listener_t *listener, switch_event_t **event,
len = strlen(hbuf);
switch_socket_send(listener->sock, hbuf, &len);
len = strlen(packet);
switch_socket_send(listener->sock, packet, &len);
len = strlen(listener->ebuf);
switch_socket_send(listener->sock, listener->ebuf, &len);
if (xmlstr) {
free(xmlstr);
}
switch_safe_free(listener->ebuf);
}
}
}
@@ -392,7 +384,9 @@ static switch_status_t read_packet(listener_t *listener, switch_event_t **event,
switch_yield(1000);
}
}
return status;
}
@@ -38,7 +38,7 @@ static const char modname[] = "mod_event_test";
static void event_handler(switch_event_t *event)
{
char buf[1024];
char *buf;
switch_xml_t xml;
char *xmlstr = "N/A";
uint8_t dofree = 0;
@@ -47,7 +47,7 @@ static void event_handler(switch_event_t *event)
case SWITCH_EVENT_LOG:
return;
default:
switch_event_serialize(event, buf, sizeof(buf), NULL);
switch_event_serialize(event, &buf);
if ((xml = switch_event_xmlize(event, NULL))) {
xmlstr = switch_xml_toxml(xml);
dofree++;
@@ -58,6 +58,8 @@ static void event_handler(switch_event_t *event)
break;
}
switch_safe_free(buf);
if (dofree) {
if (xml) {
switch_xml_free(xml);
@@ -60,7 +60,7 @@ static struct {
static void event_handler(switch_event_t *event)
{
char buf[1024];
char *buf;
iks *msg;
int loops = 0;
@@ -78,12 +78,13 @@ static void event_handler(switch_event_t *event)
switch (event->event_id) {
default:
switch_event_serialize(event, buf, sizeof(buf), NULL);
switch_event_serialize(event, &buf);
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "\nEVENT\n--------------------------------\n%s\n", buf);
msg = iks_make_msg(IKS_TYPE_NONE, globals.target_jid, buf);
iks_insert_attrib(msg, "subject", "Event");
iks_send(globals.session.parser, msg);
iks_delete(msg);
switch_safe_free(buf);
break;
}
}
@@ -264,7 +264,7 @@ static JSBool event_get_type(JSContext *cx, JSObject *obj, uintN argc, jsval *ar
static JSBool event_serialize(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
struct event_obj *eo = JS_GetPrivate(cx, obj);
char buf[1024];
char *buf;
uint8_t isxml = 0;
if (!eo) {
@@ -291,8 +291,10 @@ static JSBool event_serialize(JSContext *cx, JSObject *obj, uintN argc, jsval *a
*rval = BOOLEAN_TO_JSVAL( JS_FALSE );
}
} else {
switch_event_serialize(eo->event, buf, sizeof(buf), NULL);
*rval = STRING_TO_JSVAL (JS_NewStringCopyZ(cx, buf));
if (switch_event_serialize(eo->event, &buf) == SWITCH_STATUS_SUCCESS) {
*rval = STRING_TO_JSVAL (JS_NewStringCopyZ(cx, buf));
switch_safe_free(buf);
}
}
return JS_TRUE;