From 506bcd280d746af49ccced5b6f84580e8066cc93 Mon Sep 17 00:00:00 2001 From: lazedo Date: Thu, 31 Oct 2019 12:41:47 +0000 Subject: [PATCH] [core] allow event-channel debugging & single delivery --- src/include/switch_types.h | 4 +++- src/switch_core.c | 14 ++++++++++++++ src/switch_event.c | 20 +++++++++++++++----- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/include/switch_types.h b/src/include/switch_types.h index ff69cbd9e5..9302ad5c3d 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -387,7 +387,9 @@ typedef enum { SCF_DIALPLAN_TIMESTAMPS = (1 << 24), SCF_CPF_SOFT_PREFIX = (1 << 25), SCF_CPF_SOFT_LOOKUP = (1 << 26), - SCF_EVENT_CHANNEL_ENABLE_HIERARCHY_DELIVERY = (1 << 27) + SCF_EVENT_CHANNEL_ENABLE_HIERARCHY_DELIVERY = (1 << 27), + SCF_EVENT_CHANNEL_HIERARCHY_DELIVERY_ONCE = (1 << 28), + SCF_EVENT_CHANNEL_LOG_UNDELIVERABLE_JSON = (1 << 29) } switch_core_flag_enum_t; typedef uint32_t switch_core_flag_t; diff --git a/src/switch_core.c b/src/switch_core.c index 359265c02f..baed1783e6 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -2369,6 +2369,20 @@ static void switch_load_core_config(const char *file) } else { switch_clear_flag((&runtime), SCF_EVENT_CHANNEL_ENABLE_HIERARCHY_DELIVERY); } + } else if (!strcasecmp(var, "event-channel-hierarchy-deliver-once") && !zstr(val)) { + int v = switch_true(val); + if (v) { + switch_set_flag((&runtime), SCF_EVENT_CHANNEL_HIERARCHY_DELIVERY_ONCE); + } else { + switch_clear_flag((&runtime), SCF_EVENT_CHANNEL_HIERARCHY_DELIVERY_ONCE); + } + } else if (!strcasecmp(var, "event-channel-log-undeliverable-json") && !zstr(val)) { + int v = switch_true(val); + if (v) { + switch_set_flag((&runtime), SCF_EVENT_CHANNEL_LOG_UNDELIVERABLE_JSON); + } else { + switch_clear_flag((&runtime), SCF_EVENT_CHANNEL_LOG_UNDELIVERABLE_JSON); + } } } } diff --git a/src/switch_event.c b/src/switch_event.c index 7392e41618..6b1a3bfc42 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -2934,17 +2934,21 @@ static void ecd_deliver(event_channel_data_t **ecdP) const char *sep = switch_core_get_event_channel_key_separator(); char *x_argv[SWITCH_CHANNEL_DISPATCH_MAX_KEY_PARTS] = { 0 }; int x_argc = switch_separate_string_string(key, (char*) sep, x_argv, SWITCH_CHANNEL_DISPATCH_MAX_KEY_PARTS); - char buf[512]; - int i; + char buf[1024]; + int i, r; for(i=x_argc - 1; i > 0; i--) { int z; - memset(buf, 0, 512); + memset(buf, 0, 1024); sprintf(buf, "%s", x_argv[0]); for(z=1; z < i; z++) { strcat(buf, sep); strcat(buf, x_argv[z]); } - t += _switch_event_channel_broadcast(buf, ecd->event_channel, ecd->json, ecd->key, ecd->id); + r = _switch_event_channel_broadcast(buf, ecd->event_channel, ecd->json, ecd->key, ecd->id); + t += r; + if (r && switch_core_test_flag(SCF_EVENT_CHANNEL_HIERARCHY_DELIVERY_ONCE)) { + break; + } } } else { char *p = NULL; @@ -2958,7 +2962,13 @@ static void ecd_deliver(event_channel_data_t **ecdP) t += _switch_event_channel_broadcast(SWITCH_EVENT_CHANNEL_GLOBAL, ecd->event_channel, ecd->json, ecd->key, ecd->id); if(t == 0) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "no subscribers for %s , %s\n", ecd->event_channel, ecd->key); + if (switch_core_test_flag(SCF_EVENT_CHANNEL_LOG_UNDELIVERABLE_JSON)) { + char *json = cJSON_Print(ecd->json); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "no subscribers for %s , %s => %s\n", ecd->event_channel, ecd->key, json); + switch_safe_free(json); + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "no subscribers for %s , %s\n", ecd->event_channel, ecd->key); + } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "delivered to %u subscribers for %s\n", t, ecd->event_channel); }