mod_v8: Fixed line endings, and removed old code.

This commit is contained in:
Peter Olsson 2014-02-01 18:54:39 +01:00
parent 04005dfa68
commit 4f9402c09d
1 changed files with 264 additions and 276 deletions

View File

@ -37,13 +37,13 @@
using namespace std; using namespace std;
using namespace v8; using namespace v8;
typedef struct { typedef struct {
char *cmd; char *cmd;
char *arg; char *arg;
char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
int ack; int ack;
switch_memory_pool_t *pool; switch_memory_pool_t *pool;
} api_command_struct_t; } api_command_struct_t;
static const char js_class_name[] = "EventHandler"; static const char js_class_name[] = "EventHandler";
@ -98,87 +98,77 @@ void FSEventHandler::QueueEvent(switch_event_t *event)
switch_mutex_lock(_mutex); switch_mutex_lock(_mutex);
if (_event_list[SWITCH_EVENT_ALL]) { if (_event_list[SWITCH_EVENT_ALL]) {
send = 1; send = 1;
} else if ((_event_list[event->event_id])) { } else if ((_event_list[event->event_id])) {
if (event->event_id != SWITCH_EVENT_CUSTOM || !event->subclass_name || (switch_core_hash_find(_event_hash, event->subclass_name))) { if (event->event_id != SWITCH_EVENT_CUSTOM || !event->subclass_name || (switch_core_hash_find(_event_hash, event->subclass_name))) {
send = 1; send = 1;
} }
} }
if (send) { if (send) {
if (_filters && _filters->headers) { if (_filters && _filters->headers) {
switch_event_header_t *hp; switch_event_header_t *hp;
const char *hval; const char *hval;
send = 0; send = 0;
for (hp = _filters->headers; hp; hp = hp->next) { for (hp = _filters->headers; hp; hp = hp->next) {
if ((hval = switch_event_get_header(event, hp->name))) { if ((hval = switch_event_get_header(event, hp->name))) {
const char *comp_to = hp->value; const char *comp_to = hp->value;
int pos = 1, cmp = 0; int pos = 1, cmp = 0;
while (comp_to && *comp_to) { while (comp_to && *comp_to) {
if (*comp_to == '+') { if (*comp_to == '+') {
pos = 1; pos = 1;
} else if (*comp_to == '-') { } else if (*comp_to == '-') {
pos = 0; pos = 0;
} else if (*comp_to != ' ') { } else if (*comp_to != ' ') {
break; break;
} }
comp_to++; comp_to++;
} }
if (send && pos) { if (send && pos) {
continue; continue;
} }
if (!comp_to) { if (!comp_to) {
continue; continue;
} }
if (*hp->value == '/') { if (*hp->value == '/') {
switch_regex_t *re = NULL; switch_regex_t *re = NULL;
int ovector[30]; int ovector[30];
cmp = !!switch_regex_perform(hval, comp_to, &re, ovector, sizeof(ovector) / sizeof(ovector[0])); cmp = !!switch_regex_perform(hval, comp_to, &re, ovector, sizeof(ovector) / sizeof(ovector[0]));
switch_regex_safe_free(re); switch_regex_safe_free(re);
} else { } else {
cmp = !strcasecmp(hval, comp_to); cmp = !strcasecmp(hval, comp_to);
} }
if (cmp) { if (cmp) {
if (pos) { if (pos) {
send = 1; send = 1;
} else { } else {
send = 0; send = 0;
break; break;
} }
} }
} }
} }
} }
} }
switch_mutex_unlock(_mutex); switch_mutex_unlock(_mutex);
if (send) { if (send) {
if (switch_event_dup(&clone, event) == SWITCH_STATUS_SUCCESS) { if (switch_event_dup(&clone, event) == SWITCH_STATUS_SUCCESS) {
if (switch_queue_trypush(_event_queue, clone) != SWITCH_STATUS_SUCCESS) {
if (switch_queue_trypush(_event_queue, clone) == SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Lost event to JS EventHandler, you must read the events faster!\n");
// TODO
/*if (l->lost_events) {
int le = l->lost_events;
l->lost_events = 0;
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(l->session), SWITCH_LOG_CRIT, "Lost %d events!\n", le);
}*/
} else {
/*if (++l->lost_events > MAX_MISSED) {
kill_listener(l, NULL);
}*/
switch_event_destroy(&clone); switch_event_destroy(&clone);
} }
} else { } else {
////switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(l->session), SWITCH_LOG_ERROR, "Memory Error!\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n");
} }
} }
} }
@ -194,28 +184,28 @@ void FSEventHandler::DoSubscribe(const v8::FunctionCallbackInfo<v8::Value>& info
String::Utf8Value str(info[i]); String::Utf8Value str(info[i]);
switch_event_types_t etype; switch_event_types_t etype;
if (custom) { if (custom) {
switch_mutex_lock(_mutex); switch_mutex_lock(_mutex);
switch_core_hash_insert(_event_hash, js_safe_str(*str), MARKER); switch_core_hash_insert(_event_hash, js_safe_str(*str), MARKER);
switch_mutex_unlock(_mutex); switch_mutex_unlock(_mutex);
} else if (switch_name_event(js_safe_str(*str), &etype) == SWITCH_STATUS_SUCCESS) { } else if (switch_name_event(js_safe_str(*str), &etype) == SWITCH_STATUS_SUCCESS) {
ret = true; ret = true;
if (etype == SWITCH_EVENT_ALL) { if (etype == SWITCH_EVENT_ALL) {
uint32_t x = 0; uint32_t x = 0;
for (x = 0; x < SWITCH_EVENT_ALL; x++) { for (x = 0; x < SWITCH_EVENT_ALL; x++) {
_event_list[x] = 1; _event_list[x] = 1;
} }
} }
if (etype <= SWITCH_EVENT_ALL) { if (etype <= SWITCH_EVENT_ALL) {
_event_list[etype] = 1; _event_list[etype] = 1;
} }
if (etype == SWITCH_EVENT_CUSTOM) { if (etype == SWITCH_EVENT_CUSTOM) {
custom++; custom++;
} }
} }
} }
info.GetReturnValue().Set(ret); info.GetReturnValue().Set(ret);
@ -242,30 +232,30 @@ JS_EVENTHANDLER_FUNCTION_IMPL(UnSubscribe)
String::Utf8Value str(info[i]); String::Utf8Value str(info[i]);
switch_event_types_t etype; switch_event_types_t etype;
if (custom) { if (custom) {
switch_mutex_lock(_mutex); switch_mutex_lock(_mutex);
switch_core_hash_delete(_event_hash, js_safe_str(*str)); switch_core_hash_delete(_event_hash, js_safe_str(*str));
switch_mutex_unlock(_mutex); switch_mutex_unlock(_mutex);
} else if (switch_name_event(js_safe_str(*str), &etype) == SWITCH_STATUS_SUCCESS) { } else if (switch_name_event(js_safe_str(*str), &etype) == SWITCH_STATUS_SUCCESS) {
uint32_t x = 0; uint32_t x = 0;
ret = true; ret = true;
if (etype == SWITCH_EVENT_CUSTOM) { if (etype == SWITCH_EVENT_CUSTOM) {
custom++; custom++;
} else if (etype == SWITCH_EVENT_ALL) { } else if (etype == SWITCH_EVENT_ALL) {
for (x = 0; x <= SWITCH_EVENT_ALL; x++) { for (x = 0; x <= SWITCH_EVENT_ALL; x++) {
_event_list[x] = 0; _event_list[x] = 0;
} }
} else { } else {
if (_event_list[SWITCH_EVENT_ALL]) { if (_event_list[SWITCH_EVENT_ALL]) {
_event_list[SWITCH_EVENT_ALL] = 0; _event_list[SWITCH_EVENT_ALL] = 0;
for (x = 0; x < SWITCH_EVENT_ALL; x++) { for (x = 0; x < SWITCH_EVENT_ALL; x++) {
_event_list[x] = 1; _event_list[x] = 1;
} }
} }
_event_list[etype] = 0; _event_list[etype] = 0;
} }
} }
} }
info.GetReturnValue().Set(ret); info.GetReturnValue().Set(ret);
@ -279,27 +269,27 @@ JS_EVENTHANDLER_FUNCTION_IMPL(DeleteFilter)
String::Utf8Value str(info[0]); String::Utf8Value str(info[0]);
const char *headerName = js_safe_str(*str); const char *headerName = js_safe_str(*str);
if (zstr(headerName)) { if (zstr(headerName)) {
info.GetReturnValue().Set(false); info.GetReturnValue().Set(false);
return; return;
} }
switch_mutex_lock(_mutex); switch_mutex_lock(_mutex);
if (!_filters) { if (!_filters) {
switch_event_create_plain(&_filters, SWITCH_EVENT_CLONE); switch_event_create_plain(&_filters, SWITCH_EVENT_CLONE);
} }
if (!strcasecmp(headerName, "all")) { if (!strcasecmp(headerName, "all")) {
switch_event_destroy(&_filters); switch_event_destroy(&_filters);
switch_event_create_plain(&_filters, SWITCH_EVENT_CLONE); switch_event_create_plain(&_filters, SWITCH_EVENT_CLONE);
} else { } else {
switch_event_del_header(_filters, headerName); switch_event_del_header(_filters, headerName);
} }
info.GetReturnValue().Set(true); info.GetReturnValue().Set(true);
switch_mutex_unlock(_mutex); switch_mutex_unlock(_mutex);
} }
} }
@ -313,22 +303,22 @@ JS_EVENTHANDLER_FUNCTION_IMPL(AddFilter)
const char *headerName = js_safe_str(*str1); const char *headerName = js_safe_str(*str1);
const char *headerVal = js_safe_str(*str2); const char *headerVal = js_safe_str(*str2);
if (zstr(headerName) || zstr(headerVal)) { if (zstr(headerName) || zstr(headerVal)) {
info.GetReturnValue().Set(false); info.GetReturnValue().Set(false);
return; return;
} }
switch_mutex_lock(_mutex); switch_mutex_lock(_mutex);
if (!_filters) { if (!_filters) {
switch_event_create_plain(&_filters, SWITCH_EVENT_CLONE); switch_event_create_plain(&_filters, SWITCH_EVENT_CLONE);
} }
switch_event_add_header_string(_filters, SWITCH_STACK_BOTTOM, headerName, headerVal); switch_event_add_header_string(_filters, SWITCH_STACK_BOTTOM, headerName, headerVal);
info.GetReturnValue().Set(true); info.GetReturnValue().Set(true);
switch_mutex_unlock(_mutex); switch_mutex_unlock(_mutex);
} }
} }
@ -397,21 +387,19 @@ JS_EVENTHANDLER_FUNCTION_IMPL(SendEvent)
switch_core_session_t *session; switch_core_session_t *session;
switch_status_t status = SWITCH_STATUS_FALSE; switch_status_t status = SWITCH_STATUS_FALSE;
if ((session = switch_core_session_locate(session_uuid.c_str()))) { if ((session = switch_core_session_locate(session_uuid.c_str()))) {
if ((status = switch_core_session_queue_private_event(session, event, SWITCH_FALSE)) == SWITCH_STATUS_SUCCESS) { if ((status = switch_core_session_queue_private_event(session, event, SWITCH_FALSE)) == SWITCH_STATUS_SUCCESS) {
info.GetReturnValue().Set(true); info.GetReturnValue().Set(true);
} else { } else {
info.GetReturnValue().Set(false); info.GetReturnValue().Set(false);
} }
switch_core_session_rwunlock(session); switch_core_session_rwunlock(session);
} else { } else {
info.GetReturnValue().Set(false); info.GetReturnValue().Set(false);
// TODO LOGGING switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid session id [%s]\n", switch_str_nil(session_uuid.c_str()));
//switch_snprintf(reply, reply_len, "-ERR invalid session id [%s]", switch_str_nil(uuid)); }
}
} else { } else {
/* "Normal" event */ /* "Normal" event */
// TODO LOGGING
switch_event_fire(event); switch_event_fire(event);
} }
} }
@ -448,78 +436,78 @@ JS_EVENTHANDLER_FUNCTION_IMPL(ExecuteApi)
} }
} }
static void *SWITCH_THREAD_FUNC api_exec(switch_thread_t *thread, void *obj) static void *SWITCH_THREAD_FUNC api_exec(switch_thread_t *thread, void *obj)
{ {
api_command_struct_t *acs = (api_command_struct_t *) obj; api_command_struct_t *acs = (api_command_struct_t *) obj;
switch_stream_handle_t stream = { 0 }; switch_stream_handle_t stream = { 0 };
char *reply, *freply = NULL; char *reply, *freply = NULL;
switch_status_t status; switch_status_t status;
switch_event_t *event; switch_event_t *event;
if (!acs) { if (!acs) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Internal error.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Internal error.\n");
return NULL; return NULL;
} }
acs->ack = 1; acs->ack = 1;
SWITCH_STANDARD_STREAM(stream); SWITCH_STANDARD_STREAM(stream);
status = switch_api_execute(acs->cmd, acs->arg, NULL, &stream); status = switch_api_execute(acs->cmd, acs->arg, NULL, &stream);
if (status == SWITCH_STATUS_SUCCESS) { if (status == SWITCH_STATUS_SUCCESS) {
reply = (char *)stream.data; reply = (char *)stream.data;
} else { } else {
freply = switch_mprintf("-ERR %s Command not found!\n", acs->cmd); freply = switch_mprintf("-ERR %s Command not found!\n", acs->cmd);
reply = freply; reply = freply;
} }
if (!reply) { if (!reply) {
reply = "Command returned no output!"; reply = "Command returned no output!";
} }
if (switch_event_create(&event, SWITCH_EVENT_BACKGROUND_JOB) == SWITCH_STATUS_SUCCESS) { if (switch_event_create(&event, SWITCH_EVENT_BACKGROUND_JOB) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Job-UUID", acs->uuid_str); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Job-UUID", acs->uuid_str);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Job-Command", acs->cmd); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Job-Command", acs->cmd);
if (acs->arg) { if (acs->arg) {
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Job-Command-Arg", acs->arg); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Job-Command-Arg", acs->arg);
} }
switch_event_add_body(event, "%s", reply); switch_event_add_body(event, "%s", reply);
switch_event_fire(&event); switch_event_fire(&event);
} }
switch_safe_free(stream.data); switch_safe_free(stream.data);
switch_safe_free(freply); switch_safe_free(freply);
switch_memory_pool_t *pool = acs->pool; switch_memory_pool_t *pool = acs->pool;
if (acs->ack == -1) { if (acs->ack == -1) {
int sanity = 2000; int sanity = 2000;
while (acs->ack == -1) { while (acs->ack == -1) {
switch_cond_next(); switch_cond_next();
if (--sanity <= 0) if (--sanity <= 0)
break; break;
} }
} }
acs = NULL; acs = NULL;
switch_core_destroy_memory_pool(&pool); switch_core_destroy_memory_pool(&pool);
pool = NULL; pool = NULL;
return NULL; return NULL;
} }
JS_EVENTHANDLER_FUNCTION_IMPL(ExecuteBgApi) JS_EVENTHANDLER_FUNCTION_IMPL(ExecuteBgApi)
{ {
string cmd; string cmd;
string arg; string arg;
string jobuuid; string jobuuid;
api_command_struct_t *acs = NULL; api_command_struct_t *acs = NULL;
switch_memory_pool_t *pool; switch_memory_pool_t *pool;
switch_thread_t *thread; switch_thread_t *thread;
switch_threadattr_t *thd_attr = NULL; switch_threadattr_t *thd_attr = NULL;
switch_uuid_t uuid; switch_uuid_t uuid;
int sanity = 2000; int sanity = 2000;
if (info.Length() > 0) { if (info.Length() > 0) {
String::Utf8Value str(info[0]); String::Utf8Value str(info[0]);
cmd = js_safe_str(*str); cmd = js_safe_str(*str);
@ -542,42 +530,42 @@ JS_EVENTHANDLER_FUNCTION_IMPL(ExecuteBgApi)
info.GetReturnValue().Set(false); info.GetReturnValue().Set(false);
return; return;
} }
switch_core_new_memory_pool(&pool); switch_core_new_memory_pool(&pool);
acs = (api_command_struct_t *)switch_core_alloc(pool, sizeof(*acs)); acs = (api_command_struct_t *)switch_core_alloc(pool, sizeof(*acs));
switch_assert(acs); switch_assert(acs);
acs->pool = pool; acs->pool = pool;
acs->cmd = switch_core_strdup(acs->pool, cmd.c_str()); acs->cmd = switch_core_strdup(acs->pool, cmd.c_str());
if (arg.c_str()) { if (arg.c_str()) {
acs->arg = switch_core_strdup(acs->pool, arg.c_str()); acs->arg = switch_core_strdup(acs->pool, arg.c_str());
} }
switch_threadattr_create(&thd_attr, acs->pool); switch_threadattr_create(&thd_attr, acs->pool);
switch_threadattr_detach_set(thd_attr, 1); switch_threadattr_detach_set(thd_attr, 1);
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
if (jobuuid.length() > 0) { if (jobuuid.length() > 0) {
switch_copy_string(acs->uuid_str, jobuuid.c_str(), sizeof(acs->uuid_str)); switch_copy_string(acs->uuid_str, jobuuid.c_str(), sizeof(acs->uuid_str));
} else { } else {
switch_uuid_get(&uuid); switch_uuid_get(&uuid);
switch_uuid_format(acs->uuid_str, &uuid); switch_uuid_format(acs->uuid_str, &uuid);
} }
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), acs->uuid_str)); info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), acs->uuid_str));
switch_thread_create(&thread, thd_attr, api_exec, acs, acs->pool); switch_thread_create(&thread, thd_attr, api_exec, acs, acs->pool);
while (!acs->ack) { while (!acs->ack) {
switch_cond_next(); switch_cond_next();
if (--sanity <= 0) if (--sanity <= 0)
break; break;
} }
if (acs->ack == -1) { if (acs->ack == -1) {
acs->ack--; acs->ack--;
} }
} }
JS_EVENTHANDLER_FUNCTION_IMPL_STATIC(Destroy) JS_EVENTHANDLER_FUNCTION_IMPL_STATIC(Destroy)