From 698fa0455b253bb667e010cdb1ea92c35f42eb91 Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Tue, 3 Aug 2010 18:05:19 -0400 Subject: [PATCH] Add 3 new commands; session_event, session_noevents, session_nixevent --- .../mod_erlang_event/handle_msg.c | 127 ++++++++++++++++++ .../mod_erlang_event/mod_erlang_event.c | 30 +++-- 2 files changed, 149 insertions(+), 8 deletions(-) diff --git a/src/mod/event_handlers/mod_erlang_event/handle_msg.c b/src/mod/event_handlers/mod_erlang_event/handle_msg.c index 18d4aa791d..4bceaaf4e8 100644 --- a/src/mod/event_handlers/mod_erlang_event/handle_msg.c +++ b/src/mod/event_handlers/mod_erlang_event/handle_msg.c @@ -308,6 +308,57 @@ static switch_status_t handle_msg_event(listener_t *listener, int arity, ei_x_bu return SWITCH_STATUS_SUCCESS; } +static switch_status_t handle_msg_session_event(listener_t *listener, erlang_msg *msg, int arity, ei_x_buff * buf, ei_x_buff * rbuf) +{ + char atom[MAXATOMLEN]; + + if (arity == 1) { + ei_x_encode_tuple_header(rbuf, 2); + ei_x_encode_atom(rbuf, "error"); + ei_x_encode_atom(rbuf, "badarg"); + } else { + session_elem_t *session; + if ((session = find_session_elem_by_pid(listener, &msg->from))) { + + int custom = 0; + switch_event_types_t type; + int i = 0; + + for (i = 1; i < arity; i++) { + if (!ei_decode_atom(buf->buff, &buf->index, atom)) { + + if (custom) { + switch_core_hash_insert(session->event_hash, atom, MARKER); + } else if (switch_name_event(atom, &type) == SWITCH_STATUS_SUCCESS) { + if (type == SWITCH_EVENT_ALL) { + uint32_t x = 0; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "ALL events enabled for %s\n", session->uuid_str); + for (x = 0; x < SWITCH_EVENT_ALL; x++) { + session->event_list[x] = 1; + } + } + if (type <= SWITCH_EVENT_ALL) { + session->event_list[type] = 1; + } + if (type == SWITCH_EVENT_CUSTOM) { + custom++; + } + + } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "enable event %s for session %s\n", atom, session->uuid_str); + } + } + ei_x_encode_atom(rbuf, "ok"); + } else { + ei_x_encode_tuple_header(rbuf, 2); + ei_x_encode_atom(rbuf, "error"); + ei_x_encode_atom(rbuf, "notlistening"); + } + } + return SWITCH_STATUS_SUCCESS; +} + static switch_status_t handle_msg_nixevent(listener_t *listener, int arity, ei_x_buff * buf, ei_x_buff * rbuf) { char atom[MAXATOMLEN]; @@ -352,6 +403,58 @@ static switch_status_t handle_msg_nixevent(listener_t *listener, int arity, ei_x return SWITCH_STATUS_SUCCESS; } +static switch_status_t handle_msg_session_nixevent(listener_t *listener, erlang_msg *msg, int arity, ei_x_buff * buf, ei_x_buff * rbuf) +{ + char atom[MAXATOMLEN]; + + if (arity == 1) { + ei_x_encode_tuple_header(rbuf, 2); + ei_x_encode_atom(rbuf, "error"); + ei_x_encode_atom(rbuf, "badarg"); + } else { + session_elem_t *session; + if ((session = find_session_elem_by_pid(listener, &msg->from))) { + int custom = 0; + int i = 0; + switch_event_types_t type; + + for (i = 1; i < arity; i++) { + if (!ei_decode_atom(buf->buff, &buf->index, atom)) { + + if (custom) { + switch_core_hash_delete(session->event_hash, atom); + } else if (switch_name_event(atom, &type) == SWITCH_STATUS_SUCCESS) { + uint32_t x = 0; + + if (type == SWITCH_EVENT_CUSTOM) { + custom++; + } else if (type == SWITCH_EVENT_ALL) { + for (x = 0; x <= SWITCH_EVENT_ALL; x++) { + session->event_list[x] = 0; + } + } else { + if (session->event_list[SWITCH_EVENT_ALL]) { + session->event_list[SWITCH_EVENT_ALL] = 0; + for (x = 0; x < SWITCH_EVENT_ALL; x++) { + session->event_list[x] = 1; + } + } + session->event_list[type] = 0; + } + } + } + } + ei_x_encode_atom(rbuf, "ok"); + } else { /* no session for this pid */ + ei_x_encode_tuple_header(rbuf, 2); + ei_x_encode_atom(rbuf, "error"); + ei_x_encode_atom(rbuf, "notlistening"); + } + } + return SWITCH_STATUS_SUCCESS; +} + + static switch_status_t handle_msg_api(listener_t *listener, erlang_msg * msg, int arity, ei_x_buff * buf, ei_x_buff * rbuf) { char api_cmd[MAXATOMLEN]; @@ -649,8 +752,12 @@ static switch_status_t handle_msg_tuple(listener_t *listener, erlang_msg * msg, ret = handle_msg_set_log_level(listener, arity, buf, rbuf); } else if (!strncmp(tupletag, "event", MAXATOMLEN)) { ret = handle_msg_event(listener, arity, buf, rbuf); + } else if (!strncmp(tupletag, "session_event", MAXATOMLEN)) { + ret = handle_msg_session_event(listener, msg, arity, buf, rbuf); } else if (!strncmp(tupletag, "nixevent", MAXATOMLEN)) { ret = handle_msg_nixevent(listener, arity, buf, rbuf); + } else if (!strncmp(tupletag, "session_nixevent", MAXATOMLEN)) { + ret = handle_msg_session_nixevent(listener, msg, arity, buf, rbuf); } else if (!strncmp(tupletag, "api", MAXATOMLEN)) { ret = handle_msg_api(listener, msg, arity, buf, rbuf); } else if (!strncmp(tupletag, "bgapi", MAXATOMLEN)) { @@ -726,6 +833,26 @@ static switch_status_t handle_msg_atom(listener_t *listener, erlang_msg * msg, e ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "notlistening"); } + } else if (!strncmp(atom, "session_noevents", MAXATOMLEN)) { + session_elem_t *session; + if ((session = find_session_elem_by_pid(listener, &msg->from))) { + void *pop; + uint8_t x = 0; + + /*purge the event queue */ + while (switch_queue_trypop(session->event_queue, &pop) == SWITCH_STATUS_SUCCESS); + for (x = 0; x <= SWITCH_EVENT_ALL; x++) { + session->event_list[x] = 0; + } + /* wipe the hash */ + switch_core_hash_destroy(&session->event_hash); + switch_core_hash_init(&session->event_hash, session->pool); + ei_x_encode_atom(rbuf, "ok"); + } else { + ei_x_encode_tuple_header(rbuf, 2); + ei_x_encode_atom(rbuf, "error"); + ei_x_encode_atom(rbuf, "notlistening"); + } } else if (!strncmp(atom, "exit", MAXATOMLEN)) { ei_x_encode_atom(rbuf, "ok"); ret = SWITCH_STATUS_TERM; diff --git a/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.c b/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.c index 526d8b6d6c..48068bd1dd 100644 --- a/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.c +++ b/src/mod/event_handlers/mod_erlang_event/mod_erlang_event.c @@ -149,16 +149,30 @@ static void send_event_to_attached_sessions(listener_t *listener, switch_event_t } if ((s = (session_elem_t*)switch_core_hash_find(listener->sessions, uuid))) { - switch_log_printf(SWITCH_CHANNEL_UUID_LOG(s->uuid_str), SWITCH_LOG_DEBUG, "Sending event %s to attached session %s\n", - switch_event_name(event->event_id), s->uuid_str); - if (switch_event_dup(&clone, event) == SWITCH_STATUS_SUCCESS) { - /* add the event to the queue for this session */ - if (switch_queue_trypush(s->event_queue, clone) != SWITCH_STATUS_SUCCESS) { - switch_log_printf(SWITCH_CHANNEL_UUID_LOG(s->uuid_str), SWITCH_LOG_ERROR, "Lost event!\n"); - switch_event_destroy(&clone); + int send = 0; + if (s->event_list[SWITCH_EVENT_ALL]) { + send = 1; + } else if ((s->event_list[event->event_id])) { + if (event->event_id != SWITCH_EVENT_CUSTOM || !event->subclass_name || (switch_core_hash_find(s->event_hash, event->subclass_name))) { + send = 1; + } + } + + if (send) { + switch_log_printf(SWITCH_CHANNEL_UUID_LOG(s->uuid_str), SWITCH_LOG_DEBUG, "Sending event %s to attached session %s\n", + switch_event_name(event->event_id), s->uuid_str); + if (switch_event_dup(&clone, event) == SWITCH_STATUS_SUCCESS) { + /* add the event to the queue for this session */ + if (switch_queue_trypush(s->event_queue, clone) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_UUID_LOG(s->uuid_str), SWITCH_LOG_ERROR, "Lost event!\n"); + switch_event_destroy(&clone); + } + } else { + switch_log_printf(SWITCH_CHANNEL_UUID_LOG(s->uuid_str), SWITCH_LOG_ERROR, "Memory Error!\n"); } } else { - switch_log_printf(SWITCH_CHANNEL_UUID_LOG(s->uuid_str), SWITCH_LOG_ERROR, "Memory Error!\n"); + switch_log_printf(SWITCH_CHANNEL_UUID_LOG(s->uuid_str), SWITCH_LOG_DEBUG, "Ignoring event %s for attached session %s\n", + switch_event_name(event->event_id), s->uuid_str); } } }