Modified: freeswitch/branches/cseket/src/mod/event_handlers/mod_json_cdr/mod_json_cdr.c (16807 => 16808)
--- freeswitch/branches/cseket/src/mod/event_handlers/mod_json_cdr/mod_json_cdr.c 2010-02-25 08:13:42 UTC (rev 16807)
+++ freeswitch/branches/cseket/src/mod/event_handlers/mod_json_cdr/mod_json_cdr.c 2010-02-25 10:04:27 UTC (rev 16808)
@@ -27,7 +27,7 @@
* Bret McDanel <trixter AT 0xdecafbad.com>
* Justin Cassidy <xachenant@hotmail.com>
*
- * mod_xml_cdr.c -- XML CDR Module to files or curl
+ * mod_json_cdr.c -- JSON CDR Module to files or curl
*
*/
#include <sys/stat.h>
@@ -40,7 +40,6 @@
#define ENCODING_NONE 0
#define ENCODING_DEFAULT 1
#define ENCODING_BASE64 2
-#define ENCODING_TEXTXML 2
static struct {
char *cred;
@@ -69,13 +68,12 @@
int disable100continue;
int rotate;
int auth_scheme;
- int json;
switch_memory_pool_t *pool;
} globals;
-SWITCH_MODULE_LOAD_FUNCTION(mod_xml_cdr_load);
-SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_cdr_shutdown);
-SWITCH_MODULE_DEFINITION(mod_xml_cdr, mod_xml_cdr_load, mod_xml_cdr_shutdown, NULL);
+SWITCH_MODULE_LOAD_FUNCTION(mod_json_cdr_load);
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_json_cdr_shutdown);
+SWITCH_MODULE_DEFINITION(mod_json_cdr, mod_json_cdr_load, mod_json_cdr_shutdown, NULL);
/* this function would have access to the HTML returned by the webserver, we don't need it
* and the default curl activity is to print to stdout, something not as desirable
@@ -86,7 +84,7 @@
return size * nitems;
}
-static switch_status_t set_xml_cdr_log_dirs()
+static switch_status_t set_json_cdr_log_dirs()
{
switch_time_exp_t tm;
char *path = NULL;
@@ -115,12 +113,12 @@
globals.log_dir = path;
switch_thread_rwlock_unlock(globals.log_path_lock);
} else {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create new mod_xml_cdr log_dir path\n");
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create new mod_json_cdr log_dir path\n");
switch_safe_free(path);
status = SWITCH_STATUS_FALSE;
}
} else {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to generate new mod_xml_cdr log_dir path\n");
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to generate new mod_json_cdr log_dir path\n");
status = SWITCH_STATUS_FALSE;
}
} else {
@@ -153,12 +151,12 @@
globals.err_log_dir = path;
switch_thread_rwlock_unlock(globals.log_path_lock);
} else {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create new mod_xml_cdr err_log_dir path\n");
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create new mod_json_cdr err_log_dir path\n");
switch_safe_free(path);
status = SWITCH_STATUS_FALSE;
}
} else {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to generate new mod_xml_cdr err_log_dir path\n");
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to generate new mod_json_cdr err_log_dir path\n");
status = SWITCH_STATUS_FALSE;
}
} else {
@@ -598,12 +596,12 @@
static switch_status_t my_on_reporting(switch_core_session_t *session)
{
- switch_xml_t cdr = NULL;
- char *xml_text = NULL;
+ struct json_object *json_cdr = NULL;
+ char *json_text = NULL;
char *path = NULL;
- char *curl_xml_text = NULL;
+ char *curl_json_text = NULL;
const char *logdir = NULL;
- char *xml_text_escaped = NULL;
+ char *json_text_escaped = NULL;
int fd = -1;
uint32_t cur_try;
long httpRes;
@@ -614,7 +612,6 @@
switch_status_t status = SWITCH_STATUS_FALSE;
int is_b;
const char *a_prefix = "";
- struct json_object *json_cdr = NULL;
if (globals.shutdown) {
return SWITCH_STATUS_SUCCESS;
@@ -627,36 +624,27 @@
if (!is_b && globals.prefix_a)
a_prefix = "a_";
- if (globals.json) {
- if (generate_json_cdr(session, &json_cdr) != SWITCH_STATUS_SUCCESS) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Generating Data!\n");
- return SWITCH_STATUS_FALSE;
- }
- xml_text = json_object_to_json_string(json_cdr);
+ if (generate_json_cdr(session, &json_cdr) != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Generating Data!\n");
+ return SWITCH_STATUS_FALSE;
}
- else {
- if (switch_ivr_generate_xml_cdr(session, &cdr) != SWITCH_STATUS_SUCCESS) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Generating Data!\n");
- return SWITCH_STATUS_FALSE;
- }
+
+ json_text = json_object_to_json_string(json_cdr);
- /* build the XML */
- xml_text = switch_xml_toxml(cdr, SWITCH_TRUE);
- }
- if (!xml_text) {
+ if (!json_text) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
goto error;
}
switch_thread_rwlock_rdlock(globals.log_path_lock);
- if (!(logdir = switch_channel_get_variable(channel, "xml_cdr_base"))) {
+ if (!(logdir = switch_channel_get_variable(channel, "json_cdr_base"))) {
logdir = globals.log_dir;
}
if (!zstr(logdir) && (globals.log_http_and_disk || !globals.url_count)) {
- path = switch_mprintf("%s%s%s%s.cdr.xml", logdir, SWITCH_PATH_SEPARATOR, a_prefix, switch_core_session_get_uuid(session));
+ path = switch_mprintf("%s%s%s%s.cdr.json", logdir, SWITCH_PATH_SEPARATOR, a_prefix, switch_core_session_get_uuid(session));
switch_thread_rwlock_unlock(globals.log_path_lock);
if (path) {
#ifdef _MSC_VER
@@ -665,7 +653,7 @@
if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) > -1) {
#endif
int wrote;
- wrote = write(fd, xml_text, (unsigned) strlen(xml_text));
+ wrote = write(fd, json_text, (unsigned) strlen(json_text));
close(fd);
fd = -1;
} else {
@@ -688,36 +676,32 @@
char *destUrl = NULL;
curl_handle = curl_easy_init();
- if (globals.encode == ENCODING_TEXTXML) {
- headers = curl_slist_append(headers, "Content-Type: text/xml");
- } else if (globals.encode) {
- switch_size_t need_bytes = strlen(xml_text) * 3;
+ if (globals.encode) {
+ switch_size_t need_bytes = strlen(json_text) * 3;
- xml_text_escaped = malloc(need_bytes);
- switch_assert(xml_text_escaped);
- memset(xml_text_escaped, 0, need_bytes);
+ json_text_escaped = malloc(need_bytes);
+ switch_assert(json_text_escaped);
+ memset(json_text_escaped, 0, need_bytes);
if (globals.encode == ENCODING_DEFAULT) {
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
- switch_url_encode(xml_text, xml_text_escaped, need_bytes);
+ switch_url_encode(json_text, json_text_escaped, need_bytes);
} else {
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-base64-encoded");
- switch_b64_encode((unsigned char *) xml_text, need_bytes / 3, (unsigned char *) xml_text_escaped, need_bytes);
+ switch_b64_encode((unsigned char *) json_text, need_bytes / 3, (unsigned char *) json_text_escaped, need_bytes);
}
- if (!json_cdr) {
- switch_safe_free(xml_text);
+
+ json_text = json_text_escaped;
+
+ if (!(curl_json_text = switch_mprintf("cdr=%s", json_text))) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
+ goto error;
}
- xml_text = xml_text_escaped;
} else {
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-plaintext");
+ curl_json_text = json_text;
}
- if (globals.encode == ENCODING_TEXTXML) {
- curl_xml_text = xml_text;
- } else if (!(curl_xml_text = switch_mprintf("cdr=%s", xml_text))) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
- goto error;
- }
if (!zstr(globals.cred)) {
curl_easy_setopt(curl_handle, CURLOPT_HTTPAUTH, globals.auth_scheme);
@@ -726,8 +710,8 @@
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl_handle, CURLOPT_POST, 1);
- curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, curl_xml_text);
- curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "freeswitch-xml/1.0");
+ curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, curl_json_text);
+ curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "freeswitch-json/1.0");
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, httpCallBack);
if (globals.disable100continue) {
@@ -812,7 +796,7 @@
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to post to web server, writing to file\n");
switch_thread_rwlock_rdlock(globals.log_path_lock);
- path = switch_mprintf("%s%s%s%s.cdr.xml", globals.err_log_dir, SWITCH_PATH_SEPARATOR, a_prefix, switch_core_session_get_uuid(session));
+ path = switch_mprintf("%s%s%s%s.cdr.json", globals.err_log_dir, SWITCH_PATH_SEPARATOR, a_prefix, switch_core_session_get_uuid(session));
switch_thread_rwlock_unlock(globals.log_path_lock);
if (path) {
#ifdef _MSC_VER
@@ -821,7 +805,7 @@
if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) > -1) {
#endif
int wrote;
- wrote = write(fd, xml_text, (unsigned) strlen(xml_text));
+ wrote = write(fd, json_text, (unsigned) strlen(json_text));
close(fd);
fd = -1;
} else {
@@ -849,19 +833,14 @@
if (slist) {
curl_slist_free_all(slist);
}
- if (curl_xml_text != xml_text) {
- switch_safe_free(curl_xml_text);
+ if (curl_json_text != json_text) {
+ switch_safe_free(curl_json_text);
}
- if (json_cdr) {
- json_object_put(json_cdr);
- switch_safe_free(xml_text_escaped);
- } else {
- switch_safe_free(xml_text);
- }
+ json_object_put(json_cdr);
+ switch_safe_free(json_text_escaped);
switch_safe_free(path);
- switch_xml_free(cdr);
return status;
}
@@ -872,7 +851,7 @@
if (sig && !strcmp(sig, "HUP")) {
if (globals.rotate) {
- set_xml_cdr_log_dirs();
+ set_json_cdr_log_dirs();
}
}
}
@@ -891,9 +870,9 @@
/*.on_reporting */ my_on_reporting
};
-SWITCH_MODULE_LOAD_FUNCTION(mod_xml_cdr_load)
+SWITCH_MODULE_LOAD_FUNCTION(mod_json_cdr_load)
{
- char *cf = "xml_cdr.conf";
+ char *cf = "json_cdr.conf";
switch_xml_t cfg, xml, settings, param;
switch_status_t status = SWITCH_STATUS_SUCCESS;
@@ -948,8 +927,6 @@
} else if (!strcasecmp(var, "encode") && !zstr(val)) {
if (!strcasecmp(val, "base64")) {
globals.encode = ENCODING_BASE64;
- } else if (!strcasecmp(val, "textxml")) {
- globals.encode = ENCODING_TEXTXML;
} else {
globals.encode = switch_true(val) ? ENCODING_DEFAULT : ENCODING_NONE;
}
@@ -959,7 +936,7 @@
globals.rotate = switch_true(val);
} else if (!strcasecmp(var, "log-dir")) {
if (zstr(val)) {
- globals.base_log_dir = switch_mprintf("%s%sxml_cdr", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
+ globals.base_log_dir = switch_mprintf("%s%sjson_cdr", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
} else {
if (switch_is_file_path(val)) {
globals.base_log_dir = switch_core_strdup(globals.pool, val);
@@ -969,7 +946,7 @@
}
} else if (!strcasecmp(var, "err-log-dir")) {
if (zstr(val)) {
- globals.base_err_log_dir = switch_mprintf("%s%sxml_cdr", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
+ globals.base_err_log_dir = switch_mprintf("%s%sjson_cdr", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
} else {
if (switch_is_file_path(val)) {
globals.base_err_log_dir = switch_core_strdup(globals.pool, val);
@@ -1008,19 +985,18 @@
} else if (!strcasecmp(val, "any")) {
globals.auth_scheme = CURLAUTH_ANY;
}
- } else if (!strcasecmp(var, "json")) {
- globals.json = switch_true(val);
}
if (zstr(globals.base_err_log_dir)) {
if (!zstr(globals.base_log_dir)) {
globals.base_err_log_dir = switch_core_strdup(globals.pool, globals.base_log_dir);
} else {
- globals.base_err_log_dir = switch_core_sprintf(globals.pool, "%s%sxml_cdr", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
+ globals.base_err_log_dir = switch_core_sprintf(globals.pool, "%s%sjson_cdr", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
}
}
}
}
+
if (globals.retries < 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Retries is negative, setting to 0\n");
globals.retries = 0;
@@ -1033,13 +1009,13 @@
globals.retries++;
- set_xml_cdr_log_dirs();
+ set_json_cdr_log_dirs();
switch_xml_free(xml);
return status;
}
-SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_cdr_shutdown)
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_json_cdr_shutdown)
{
globals.shutdown = 1;