[core] Add switch_core_session_set_external_id() which lets you define an additional identifier that you can use in switch_core_session_locate().

This commit is contained in:
Chris Rienzo
2020-06-23 17:50:18 -04:00
committed by Andrey Volk
parent 6da4f147de
commit c2e240d6dc
7 changed files with 184 additions and 3 deletions

View File

@@ -2,7 +2,7 @@ include $(top_srcdir)/build/modmake.rulesam
noinst_PROGRAMS = switch_event switch_hash switch_ivr_originate switch_utils switch_core switch_console switch_vpx switch_core_file \
switch_ivr_play_say switch_core_codec switch_rtp switch_xml
noinst_PROGRAMS += switch_core_video switch_core_db switch_vad switch_packetizer test_sofia switch_core_asr
noinst_PROGRAMS += switch_core_video switch_core_db switch_vad switch_packetizer test_sofia switch_core_asr switch_core_session
AM_LDFLAGS += -avoid-version -no-undefined $(SWITCH_AM_LDFLAGS) $(openssl_LIBS)
AM_LDFLAGS += $(FREESWITCH_LIBS) $(switch_builddir)/libfreeswitch.la $(CORE_LIBS) $(APR_LIBS)

View File

@@ -0,0 +1,75 @@
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2020, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthm@freeswitch.org>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Chris Rienzo <chris@signalwire.com>
*
*
* switch_core_session.c -- tests sessions
*
*/
#include <switch.h>
#include <test/switch_test.h>
FST_CORE_BEGIN("./conf")
{
FST_SUITE_BEGIN(switch_core_session)
{
FST_SETUP_BEGIN()
{
}
FST_SETUP_END()
FST_TEARDOWN_BEGIN()
{
}
FST_TEARDOWN_END()
FST_SESSION_BEGIN(session_external_id)
{
fst_check(switch_core_session_set_external_id(fst_session, switch_core_session_get_uuid(fst_session)) != SWITCH_STATUS_SUCCESS);
fst_check(switch_core_session_set_external_id(fst_session, "foo") == SWITCH_STATUS_SUCCESS);
switch_core_session_t *session = switch_core_session_locate("foo");
fst_requires(session);
fst_check_string_equals(switch_core_session_get_uuid(session), switch_core_session_get_uuid(fst_session));
fst_check_string_equals(switch_core_session_get_external_id(session), "foo");
fst_check(switch_core_session_set_external_id(fst_session, "bar") == SWITCH_STATUS_SUCCESS);
fst_check_string_equals(switch_core_session_get_external_id(session), "bar");
fst_requires(switch_core_session_locate("foo") == NULL);
switch_core_session_rwunlock(session);
session = switch_core_session_locate("bar");
fst_requires(session);
switch_core_session_rwunlock(session);
session = switch_core_session_locate(switch_core_session_get_uuid(fst_session));
fst_requires(session);
switch_core_session_rwunlock(session);
switch_channel_hangup(fst_channel, SWITCH_CAUSE_NORMAL_CLEARING);
session = switch_core_session_locate("bar");
fst_check(session == NULL);
}
FST_SESSION_END()
}
FST_SUITE_END()
}
FST_CORE_END()

View File

@@ -55,6 +55,20 @@ static switch_status_t my_on_destroy(switch_core_session_t *session)
return SWITCH_STATUS_SUCCESS;
}
const char *external_id_to_match = NULL;
static int got_external_id_in_event = 0;
static void on_hangup_event(switch_event_t *event)
{
if (external_id_to_match) {
const char *external_id = switch_event_get_header(event, "Session-External-ID");
if (external_id && !strcmp(external_id_to_match, external_id)) {
got_external_id_in_event = 1;
}
}
}
static switch_state_handler_table_t state_handlers = {
/*.on_init */ NULL,
/*.on_routing */ NULL,
@@ -89,6 +103,8 @@ FST_CORE_BEGIN("./conf")
{
fst_requires_module("mod_loopback");
application_hit = 0;
external_id_to_match = NULL;
got_external_id_in_event = 0;
}
FST_SETUP_END()
@@ -97,6 +113,37 @@ FST_CORE_BEGIN("./conf")
}
FST_TEARDOWN_END()
FST_TEST_BEGIN(originate_test_external_id)
{
switch_core_session_t *session = NULL;
switch_channel_t *channel = NULL;
switch_status_t status;
switch_call_cause_t cause;
switch_event_t *ovars = NULL;
switch_event_create(&ovars, SWITCH_EVENT_CLONE);
switch_event_add_header_string(ovars, SWITCH_STACK_BOTTOM, "origination_external_id", "zzzz");
status = switch_ivr_originate(NULL, &session, &cause, "null/+15553334444", 2, NULL, NULL, NULL, NULL, ovars, SOF_NONE, NULL, NULL);
fst_requires(session);
fst_check(status == SWITCH_STATUS_SUCCESS);
switch_event_destroy(&ovars);
switch_core_session_rwunlock(session);
session = switch_core_session_locate("zzzz");
fst_requires(session);
channel = switch_core_session_get_channel(session);
fst_requires(channel);
external_id_to_match = "zzzz";
switch_event_bind("originate_test_external_id", SWITCH_EVENT_CHANNEL_HANGUP, SWITCH_EVENT_SUBCLASS_ANY, on_hangup_event, NULL);
switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
switch_core_session_rwunlock(session);
switch_yield(1000 * 1000);
fst_check(got_external_id_in_event == 1);
switch_event_unbind_callback(on_hangup_event);
}
FST_TEST_END()
FST_TEST_BEGIN(originate_test_early_state_handler)
{
switch_core_session_t *session = NULL;