提交 7048c16f authored 作者: Anthony Minessale's avatar Anthony Minessale

jsonification into the core and ESL (part 2)

上级 c5086b15
......@@ -169,6 +169,7 @@ library_include_HEADERS = \
src/include/switch_xml.h \
src/include/switch_xml_config.h \
src/include/switch_cpp.h \
src/include/switch_json.h\
libs/libteletone/src/libteletone_detect.h \
libs/libteletone/src/libteletone_generate.h \
libs/libteletone/src/libteletone.h \
......@@ -230,6 +231,7 @@ libfreeswitch_la_SOURCES = \
src/g711.c \
src/switch_pcm.c \
src/switch_profile.c\
src/switch_json.c\
libs/stfu/stfu.c \
libs/libteletone/src/libteletone_detect.c \
libs/libteletone/src/libteletone_generate.c \
......@@ -268,9 +270,9 @@ bin_PROGRAMS = freeswitch fs_cli fs_ivrd
##
## fs_cli ()
##
fs_cli_SOURCES = libs/esl/src/esl.c libs/esl/src/esl_config.c libs/esl/src/esl_event.c libs/esl/src/esl_threadmutex.c libs/esl/fs_cli.c
fs_cli_SOURCES = libs/esl/src/esl.c libs/esl/src/esl_config.c libs/esl/src/esl_event.c libs/esl/src/esl_threadmutex.c libs/esl/fs_cli.c libs/esl/src/esl_json.c
fs_cli_CFLAGS = $(AM_CFLAGS) -I$(switch_srcdir)/libs/esl/src/include
fs_cli_LDFLAGS = $(AM_LDFLAGS) -lpthread $(ESL_LDFLAGS)
fs_cli_LDFLAGS = $(AM_LDFLAGS) -lpthread $(ESL_LDFLAGS) -lm
if HAVE_LIBEDIT
fs_cli_CFLAGS += -DHAVE_EDITLINE -I$(switch_srcdir)/libs/libedit/src
......@@ -280,9 +282,9 @@ endif
##
## fs_ivrd ()
##
fs_ivrd_SOURCES = libs/esl/src/esl.c libs/esl/src/esl_config.c libs/esl/src/esl_event.c libs/esl/src/esl_threadmutex.c libs/esl/ivrd.c
fs_ivrd_SOURCES = libs/esl/src/esl.c libs/esl/src/esl_config.c libs/esl/src/esl_event.c libs/esl/src/esl_threadmutex.c libs/esl/ivrd.c libs/esl/src/esl_json.c
fs_ivrd_CFLAGS = $(AM_CFLAGS) -I$(switch_srcdir)/libs/esl/src/include
fs_ivrd_LDFLAGS = $(AM_LDFLAGS) -lpthread $(ESL_LDFLAGS)
fs_ivrd_LDFLAGS = $(AM_LDFLAGS) -lpthread $(ESL_LDFLAGS) -lm
##
## freeswitch ()
......
......@@ -7,11 +7,11 @@ PICKY=-O2 -ffast-math -Wall -Werror -Wunused-variable -Wwrite-strings -Wstrict-p
CFLAGS=$(BASE_FLAGS) $(PICKY)
CXXFLAGS=$(BASE_FLAGS) -Wall -Werror -Wno-unused-variable
MYLIB=libesl.a
LIBS=-lncurses -lpthread -lesl
LIBS=-lncurses -lpthread -lesl -lm
LDFLAGS=-L.
OBJS=src/esl.o src/esl_event.o src/esl_threadmutex.o src/esl_config.o
SRC=src/esl.c src/esl_event.c src/esl_threadmutex.c src/esl_config.c src/esl_oop.cpp
HEADERS=src/include/esl_config.h src/include/esl_event.h src/include/esl.h src/include/esl_threadmutex.h src/include/esl_oop.h
OBJS=src/esl.o src/esl_event.o src/esl_threadmutex.o src/esl_config.o src/esl_json.o
SRC=src/esl.c src/esl_json.c src/esl_event.c src/esl_threadmutex.c src/esl_config.c src/esl_oop.cpp src/esl_json.c
HEADERS=src/include/esl_config.h src/include/esl_event.h src/include/esl.h src/include/esl_threadmutex.h src/include/esl_oop.h src/include/esl_json.h
SOLINK=-shared -Xlinker -x
# comment the next line to disable c++ (no swig mods for you then)
OBJS += src/esl_oop.o
......
......@@ -527,6 +527,8 @@ ESL_DECLARE(esl_status_t) esl_events(esl_handle_t *handle, esl_event_type_t etyp
if (etype == ESL_EVENT_TYPE_XML) {
type = "xml";
} else if (etype == ESL_EVENT_TYPE_JSON) {
type = "json";
}
snprintf(send_buf, sizeof(send_buf), "event %s %s\n\n", type, value);
......@@ -979,63 +981,67 @@ ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, int check_q, esl_
if (!esl_safe_strcasecmp(hval, "text/disconnect-notice") && revent->body) {
goto fail;
}
if (!esl_safe_strcasecmp(hval, "text/event-plain") && revent->body) {
esl_event_types_t et = ESL_EVENT_CLONE;
char *body = strdup(revent->body);
if (revent->body) {
if (!esl_safe_strcasecmp(hval, "text/event-plain")) {
esl_event_types_t et = ESL_EVENT_CLONE;
char *body = strdup(revent->body);
esl_event_create(&handle->last_ievent, et);
esl_event_create(&handle->last_ievent, et);
beg = body;
beg = body;
while(beg) {
if (!(c = strchr(beg, '\n'))) {
break;
}
while(beg) {
if (!(c = strchr(beg, '\n'))) {
break;
}
hname = beg;
hval = col = NULL;
hname = beg;
hval = col = NULL;
if (hname && (col = strchr(hname, ':'))) {
hval = col + 1;
*col = '\0';
while(*hval == ' ') hval++;
}
if (hname && (col = strchr(hname, ':'))) {
hval = col + 1;
*col = '\0';
while(*hval == ' ') hval++;
}
*c = '\0';
*c = '\0';
if (hname && hval) {
esl_url_decode(hval);
esl_log(ESL_LOG_DEBUG, "RECV INNER HEADER [%s] = [%s]\n", hname, hval);
if (!strcasecmp(hname, "event-name")) {
esl_event_del_header(handle->last_ievent, "event-name");
if (hname && hval) {
esl_url_decode(hval);
esl_log(ESL_LOG_DEBUG, "RECV INNER HEADER [%s] = [%s]\n", hname, hval);
if (!strcasecmp(hname, "event-name")) {
esl_event_del_header(handle->last_ievent, "event-name");
}
esl_event_add_header_string(handle->last_ievent, ESL_STACK_BOTTOM, hname, hval);
esl_name_event(hval, &handle->last_ievent->event_id);
}
esl_event_add_header_string(handle->last_ievent, ESL_STACK_BOTTOM, hname, hval);
esl_name_event(hval, &handle->last_ievent->event_id);
}
beg = c + 1;
beg = c + 1;
if (*beg == '\n') {
beg++;
break;
if (*beg == '\n') {
beg++;
break;
}
}
}
if ((cl = esl_event_get_header(handle->last_ievent, "content-length"))) {
handle->last_ievent->body = strdup(beg);
}
if ((cl = esl_event_get_header(handle->last_ievent, "content-length"))) {
handle->last_ievent->body = strdup(beg);
}
free(body);
free(body);
if (esl_log_level >= 7) {
char *foo;
esl_event_serialize(handle->last_ievent, &foo, ESL_FALSE);
esl_log(ESL_LOG_DEBUG, "RECV EVENT\n%s\n", foo);
free(foo);
if (esl_log_level >= 7) {
char *foo;
esl_event_serialize(handle->last_ievent, &foo, ESL_FALSE);
esl_log(ESL_LOG_DEBUG, "RECV EVENT\n%s\n", foo);
free(foo);
}
} else if (!esl_safe_strcasecmp(hval, "text/event-json")) {
esl_event_create_json(&handle->last_ievent, revent->body);
}
}
if (esl_log_level >= 7) {
char *foo;
esl_event_serialize(revent, &foo, ESL_FALSE);
......
......@@ -437,6 +437,73 @@ ESL_DECLARE(esl_status_t) esl_event_dup(esl_event_t **event, esl_event_t *todup)
return ESL_SUCCESS;
}
ESL_DECLARE(esl_status_t) esl_event_create_json(esl_event_t **event, const char *json)
{
esl_event_t *new_event;
cJSON *cj, *cjp;
if (!(cj = cJSON_Parse(json))) {
return ESL_FAIL;
}
if (esl_event_create(&new_event, ESL_EVENT_CLONE) != ESL_SUCCESS) {
cJSON_Delete(cj);
return ESL_FAIL;
}
for (cjp = cj->child; cjp; cjp = cjp->next) {
char *name = cjp->string;
char *value = cjp->valuestring;
if (name && value) {
if (!strcasecmp(name, "_body")) {
esl_event_add_body(new_event, value);
} else {
if (!strcasecmp(name, "event-name")) {
esl_event_del_header(new_event, "event-name");
}
esl_name_event(value, &new_event->event_id);
esl_event_add_header_string(new_event, ESL_STACK_BOTTOM, name, value);
}
}
}
cJSON_Delete(cj);
*event = new_event;
return ESL_SUCCESS;
}
ESL_DECLARE(esl_status_t) esl_event_serialize_json(esl_event_t *event, char **str)
{
esl_event_header_t *hp;
cJSON *cj;
*str = NULL;
cj = cJSON_CreateObject();
for (hp = event->headers; hp; hp = hp->next) {
cJSON_AddItemToObject(cj, hp->name, cJSON_CreateString(hp->value));
}
if (event->body) {
int blen = (int) strlen(event->body);
char tmp[25];
esl_snprintf(tmp, sizeof(tmp), "%d", blen);
cJSON_AddItemToObject(cj, "Content-Length", cJSON_CreateString(tmp));
cJSON_AddItemToObject(cj, "_body", cJSON_CreateString(event->body));
}
*str = cJSON_Print(cj);
cJSON_Delete(cj);
return ESL_SUCCESS;
}
ESL_DECLARE(esl_status_t) esl_event_serialize(esl_event_t *event, char **str, esl_bool_t encode)
{
size_t len = 0;
......
差异被折叠。
......@@ -249,6 +249,8 @@ int ESLconnection::events(const char *etype, const char *value)
if (!strcmp(etype, "xml")) {
type_id = ESL_EVENT_TYPE_XML;
} else if (!strcmp(etype, "json")) {
type_id = ESL_EVENT_TYPE_JSON;
}
return esl_events(&handle, type_id, value);
......@@ -263,22 +265,32 @@ ESLevent::ESLevent(const char *type, const char *subclass_name)
event_construct_common();
if (esl_name_event(type, &event_id) != ESL_SUCCESS) {
event_id = ESL_EVENT_MESSAGE;
}
if (!strcasecmp(type, "json") && !esl_strlen_zero(subclass_name)) {
if (esl_event_create_json(&event, subclass_name) != ESL_SUCCESS) {
return;
}
event_id = event->event_id;
} else {
if (!esl_strlen_zero(subclass_name) && event_id != ESL_EVENT_CUSTOM) {
esl_log(ESL_LOG_WARNING, "Changing event type to custom because you specified a subclass name!\n");
event_id = ESL_EVENT_CUSTOM;
}
if (esl_name_event(type, &event_id) != ESL_SUCCESS) {
event_id = ESL_EVENT_MESSAGE;
}
if (esl_event_create_subclass(&event, event_id, subclass_name) != ESL_SUCCESS) {
esl_log(ESL_LOG_ERROR, "Failed to create event!\n");
event = NULL;
}
if (!esl_strlen_zero(subclass_name) && event_id != ESL_EVENT_CUSTOM) {
esl_log(ESL_LOG_WARNING, "Changing event type to custom because you specified a subclass name!\n");
event_id = ESL_EVENT_CUSTOM;
}
if (esl_event_create_subclass(&event, event_id, subclass_name) != ESL_SUCCESS) {
esl_log(ESL_LOG_ERROR, "Failed to create event!\n");
event = NULL;
}
}
serialized_string = NULL;
mine = 1;
}
ESLevent::ESLevent(esl_event_t *wrap_me, int free_me)
......@@ -289,7 +301,6 @@ ESLevent::ESLevent(esl_event_t *wrap_me, int free_me)
serialized_string = NULL;
}
ESLevent::ESLevent(ESLevent *me)
{
/* workaround for silly php thing */
......@@ -344,6 +355,11 @@ const char *ESLevent::serialize(const char *format)
return "";
}
if (!strcasecmp(format, "json")) {
esl_event_serialize_json(event, &serialized_string);
return serialized_string;
}
if (esl_event_serialize(event, &serialized_string, ESL_TRUE) == ESL_SUCCESS) {
return serialized_string;
}
......
......@@ -49,7 +49,8 @@ typedef struct esl_event esl_event_t;
typedef enum {
ESL_EVENT_TYPE_PLAIN,
ESL_EVENT_TYPE_XML
ESL_EVENT_TYPE_XML,
ESL_EVENT_TYPE_JSON
} esl_event_type_t;
#ifdef WIN32
......@@ -246,6 +247,9 @@ typedef ssize_t esl_ssize_t;
typedef int esl_filehandle_t;
#endif
#include "math.h"
#include "esl_json.h"
typedef int16_t esl_port_t;
typedef enum {
......
......@@ -273,7 +273,8 @@ ESL_DECLARE(esl_status_t) esl_name_event(const char *name, esl_event_types_t *ty
\note you must free the resulting string when you are finished with it
*/
ESL_DECLARE(esl_status_t) esl_event_serialize(esl_event_t *event, char **str, esl_bool_t encode);
ESL_DECLARE(esl_status_t) esl_event_serialize_json(esl_event_t *event, char **str);
ESL_DECLARE(esl_status_t) esl_event_create_json(esl_event_t **event, const char *json);
/*!
\brief Add a body to an event
\param event the event to add to body to
......
/*
Copyright (c) 2009 Dave Gamble
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "esl.h"
#ifndef cJSON__h
#define cJSON__h
#ifdef __cplusplus
extern "C"
{
#endif
// cJSON Types:
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6
#define cJSON_IsReference 256
// The cJSON structure:
typedef struct cJSON {
struct cJSON *next,*prev; // next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem
struct cJSON *child; // An array or object item will have a child pointer pointing to a chain of the items in the array/object.
int type; // The type of the item, as above.
char *valuestring; // The item's string, if type==cJSON_String
int valueint; // The item's number, if type==cJSON_Number
double valuedouble; // The item's number, if type==cJSON_Number
char *string; // The item's name string, if this item is the child of, or is in the list of subitems of an object.
} cJSON;
typedef struct cJSON_Hooks {
void *(*malloc_fn)(size_t sz);
void (*free_fn)(void *ptr);
} cJSON_Hooks;
// Supply malloc, realloc and free functions to cJSON
extern void cJSON_InitHooks(cJSON_Hooks* hooks);
// Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished.
extern cJSON *cJSON_Parse(const char *value);
// Render a cJSON entity to text for transfer/storage. Free the char* when finished.
extern char *cJSON_Print(cJSON *item);
// Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished.
extern char *cJSON_PrintUnformatted(cJSON *item);
// Delete a cJSON entity and all subentities.
extern void cJSON_Delete(cJSON *c);
// Returns the number of items in an array (or object).
extern int cJSON_GetArraySize(cJSON *array);
// Retrieve item number "item" from array "array". Returns NULL if unsuccessful.
extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
// Get item "string" from object. Case insensitive.
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
// These calls create a cJSON item of the appropriate type.
extern cJSON *cJSON_CreateNull(void);
extern cJSON *cJSON_CreateTrue(void);
extern cJSON *cJSON_CreateFalse(void);
extern cJSON *cJSON_CreateNumber(double num);
extern cJSON *cJSON_CreateString(const char *string);
extern cJSON *cJSON_CreateArray(void);
extern cJSON *cJSON_CreateObject(void);
// These utilities create an Array of count items.
extern cJSON *cJSON_CreateIntArray(int *numbers,int count);
extern cJSON *cJSON_CreateFloatArray(float *numbers,int count);
extern cJSON *cJSON_CreateDoubleArray(double *numbers,int count);
extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
// Append item to the specified array/object.
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
// Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON.
extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
// Remove/Detatch items from Arrays/Objects.
extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);
extern void cJSON_DeleteItemFromArray(cJSON *array,int which);
extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string);
// Update array items.
extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
#ifdef __cplusplus
}
#endif
#endif
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论