]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
change mod_sofia to use new core based recovery engine
authorAnthony Minessale <anthm@freeswitch.org>
Wed, 22 Aug 2012 21:27:02 +0000 (16:27 -0500)
committerAnthony Minessale <anthm@freeswitch.org>
Wed, 22 Aug 2012 21:27:07 +0000 (16:27 -0500)
src/mod/endpoints/mod_sofia/mod_sofia.c
src/mod/endpoints/mod_sofia/mod_sofia.h
src/mod/endpoints/mod_sofia/sofia.c
src/mod/endpoints/mod_sofia/sofia_glue.c

index 5cb910859774c092443d21e744af695510cec8ea..54670fd802b0238b9a554ffc4263d7ecb4f91f6e 100644 (file)
@@ -87,11 +87,11 @@ static switch_status_t sofia_on_init(switch_core_session_t *session)
                sofia_glue_tech_absorb_sdp(tech_pvt);
        }
 
-       if (sofia_test_flag(tech_pvt, TFLAG_RECOVERING) || sofia_test_flag(tech_pvt, TFLAG_RECOVERING_BRIDGE)) {
+       if (switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING) || switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING_BRIDGE)) {
                sofia_set_flag(tech_pvt, TFLAG_RECOVERED);
        }
 
-       if (sofia_test_flag(tech_pvt, TFLAG_OUTBOUND) || sofia_test_flag(tech_pvt, TFLAG_RECOVERING)) {
+       if (sofia_test_flag(tech_pvt, TFLAG_OUTBOUND) || switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING)) {
                const char *var;
 
                if ((var = switch_channel_get_variable(channel, SOFIA_SECURE_MEDIA_VARIABLE)) && !zstr(var)) {
@@ -115,10 +115,10 @@ static switch_status_t sofia_on_init(switch_core_session_t *session)
 
 
 
-       if (sofia_test_flag(tech_pvt, TFLAG_RECOVERING_BRIDGE)) {
+       if (switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING_BRIDGE)) {
                switch_channel_set_state(channel, CS_RESET);
        } else {
-               if (sofia_test_flag(tech_pvt, TFLAG_RECOVERING)) {
+               if (switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING)) {
                        switch_channel_set_state(channel, CS_EXECUTE);
                } else {
                        /* Move channel's state machine to ROUTING */
@@ -167,7 +167,7 @@ static switch_status_t sofia_on_reset(switch_core_session_t *session)
                                          switch_channel_get_name(switch_core_session_get_channel(session)));
 
 
-       if (sofia_test_flag(tech_pvt, TFLAG_RECOVERING_BRIDGE)) {
+       if (switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING_BRIDGE)) {
                switch_core_session_t *other_session = NULL;
                const char *uuid = switch_core_session_get_uuid(session);
 
@@ -200,7 +200,7 @@ static switch_status_t sofia_on_reset(switch_core_session_t *session)
                        }
                }
 
-               sofia_clear_flag(tech_pvt, TFLAG_RECOVERING_BRIDGE);
+               switch_channel_clear_flag(tech_pvt->channel, CF_RECOVERING_BRIDGE);
        }
 
 
@@ -232,7 +232,7 @@ static switch_status_t sofia_on_execute(switch_core_session_t *session)
        switch_channel_t *channel = switch_core_session_get_channel(session);
        switch_assert(tech_pvt != NULL);
 
-       sofia_clear_flag(tech_pvt, TFLAG_RECOVERING);
+       switch_channel_clear_flag(tech_pvt->channel, CF_RECOVERING);
 
        if (!sofia_test_flag(tech_pvt, TFLAG_HOLD_LOCK)) {
                sofia_clear_flag_locked(tech_pvt, TFLAG_SIP_HOLD);
@@ -423,7 +423,6 @@ switch_status_t sofia_on_hangup(switch_core_session_t *session)
        const char *ps_cause = NULL, *use_my_cause;
        const char *gateway_name = NULL;
        sofia_gateway_t *gateway_ptr = NULL;
-       int rec;
 
        if ((gateway_name = switch_channel_get_variable(channel, "sip_gateway_name"))) {
                gateway_ptr = sofia_reg_find_gateway(gateway_name);
@@ -435,12 +434,6 @@ switch_status_t sofia_on_hangup(switch_core_session_t *session)
 
        switch_mutex_lock(tech_pvt->sofia_mutex);
 
-       rec = sofia_test_flag(tech_pvt, TFLAG_RECOVERING);
-       sofia_clear_flag(tech_pvt, TFLAG_RECOVERING);
-
-       if (!rec) {
-               sofia_glue_tech_untrack(tech_pvt->profile, session, SWITCH_TRUE);
-       }
 
        if (!switch_channel_test_flag(channel, CF_ANSWERED)) {
                if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) {
@@ -1541,9 +1534,6 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi
        switch (msg->message_id) {
        case SWITCH_MESSAGE_INDICATE_RECOVERY_REFRESH:
        case SWITCH_MESSAGE_INDICATE_APPLICATION_EXEC:
-               {
-                       sofia_glue_tech_track(tech_pvt->profile, session);
-               }
                break;
        case SWITCH_MESSAGE_INDICATE_PROXY_MEDIA:
                {
@@ -1666,7 +1656,6 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi
                {
 
                        switch_channel_set_variable(channel, SOFIA_REPLACES_HEADER, NULL);
-                       sofia_glue_tech_track(tech_pvt->profile, session);
 
                        sofia_set_flag(tech_pvt, TFLAG_SIMPLIFY);
 
@@ -1753,8 +1742,6 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi
        case SWITCH_MESSAGE_INDICATE_UNBRIDGE:
                if (switch_rtp_ready(tech_pvt->rtp_session)) {
                        
-                       sofia_glue_tech_track(tech_pvt->profile, session);
-
                        if (sofia_test_flag(tech_pvt, TFLAG_JB_PAUSED)) {
                                sofia_clear_flag(tech_pvt, TFLAG_JB_PAUSED);
                                if (switch_channel_test_flag(tech_pvt->channel, CF_JITTERBUFFER)) {
@@ -1787,8 +1774,6 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi
                                rtp_flush_read_buffer(tech_pvt->rtp_session, SWITCH_RTP_FLUSH_ONCE);
                        }
 
-                       //sofia_glue_tech_untrack(tech_pvt->profile, session);
-
                }
                goto end;
        case SWITCH_MESSAGE_INDICATE_AUDIO_SYNC:
@@ -3620,13 +3605,14 @@ static switch_status_t cmd_profile(char **argv, int argc, switch_stream_handle_t
        if (!strcasecmp(argv[1], "recover")) {
                if (argv[2] && !strcasecmp(argv[2], "flush")) {
                        sofia_glue_profile_recover(profile, SWITCH_TRUE);
+
                        stream->write_function(stream, "Flushing recovery database.\n");
                } else {
                        int x = sofia_glue_profile_recover(profile, SWITCH_FALSE);
                        if (x) {
-                               stream->write_function(stream, "Recovered %d call(s)\n", x);
+                               stream->write_function(stream, "Recovered %d session(s)\n", x);
                        } else {
-                               stream->write_function(stream, "No calls to recover.\n");
+                               stream->write_function(stream, "No sessions to recover.\n");
                        }
                }
 
@@ -5609,12 +5595,6 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load)
                return SWITCH_STATUS_GENERR;
        }
 
-       if (switch_event_bind(modname, SWITCH_EVENT_CUSTOM, MY_EVENT_RECOVERY, sofia_glue_track_event_handler, NULL) != SWITCH_STATUS_SUCCESS) {
-
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n");
-               return SWITCH_STATUS_GENERR;
-       }
-
        if (switch_event_bind(modname, SWITCH_EVENT_TRAP, SWITCH_EVENT_SUBCLASS_ANY, general_event_handler, NULL) != SWITCH_STATUS_SUCCESS) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n");
                return SWITCH_STATUS_GENERR;
@@ -5641,6 +5621,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load)
        sofia_endpoint_interface->interface_name = "sofia";
        sofia_endpoint_interface->io_routines = &sofia_io_routines;
        sofia_endpoint_interface->state_handler = &sofia_event_handlers;
+       sofia_endpoint_interface->recover_callback = sofia_recover_callback;
 
        management_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_MANAGEMENT_INTERFACE);
        management_interface->relative_oid = "1001";
@@ -5732,7 +5713,7 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_sofia_shutdown)
 
        switch_event_unbind_callback(sofia_presence_event_handler);
        switch_event_unbind_callback(sofia_presence_mwi_event_handler);
-       switch_event_unbind_callback(sofia_glue_track_event_handler);
+
        switch_event_unbind_callback(general_event_handler);
        switch_event_unbind_callback(event_handler);
 
index a9a0160fce526b65916c95bb74002fff0e3d9a9e..97d397255d014a4c658b792b94485f0122536a5b 100644 (file)
@@ -37,6 +37,7 @@
 
 /*Defines etc..*/
 /*************************************************************************************************************************************************************/
+#define SOFIA_RECOVER "sofia"
 #define MANUAL_BYE 1
 #define SQL_CACHE_TIMEOUT 300
 #define DEFAULT_NONCE_TTL 60
@@ -246,7 +247,6 @@ typedef enum {
        PFLAG_LOG_AUTH_FAIL,
        PFLAG_FORWARD_MWI_NOTIFY,
        PFLAG_TRACK_CALLS,
-       PFLAG_TRACK_CALLS_EVENTS,
        PFLAG_DESTROY,
        PFLAG_EXTENDED_INFO_PARSING,
        PFLAG_T38_PASSTHRU,
@@ -327,9 +327,6 @@ typedef enum {
        TFLAG_PASS_RFC2833,
        TFLAG_UPDATING_DISPLAY,
        TFLAG_ENABLE_SOA,
-       TFLAG_TRACKED,
-       TFLAG_RECOVERING,
-       TFLAG_RECOVERING_BRIDGE,
        TFLAG_T38_PASSTHRU,
        TFLAG_RECOVERED,
        TFLAG_AUTOFLUSH_DURING_BRIDGE,
@@ -957,7 +954,7 @@ char *sofia_reg_find_reg_url(sofia_profile_t *profile, const char *user, const c
 void event_handler(switch_event_t *event);
 void sofia_presence_event_handler(switch_event_t *event);
 void sofia_presence_mwi_event_handler(switch_event_t *event);
-void sofia_glue_track_event_handler(switch_event_t *event);
+
 void sofia_presence_cancel(void);
 switch_status_t config_sofia(int reload, char *profile_name);
 void sofia_reg_auth_challenge(sofia_profile_t *profile, nua_handle_t *nh, sofia_dispatch_event_t *de,
@@ -1161,8 +1158,6 @@ void sofia_info_send_sipfrag(switch_core_session_t *aleg, switch_core_session_t
 void sofia_update_callee_id(switch_core_session_t *session, sofia_profile_t *profile, sip_t const *sip, switch_bool_t send);
 void sofia_send_callee_id(switch_core_session_t *session, const char *name, const char *number);
 int sofia_sla_supported(sip_t const *sip);
-void sofia_glue_tech_untrack(sofia_profile_t *profile, switch_core_session_t *session, switch_bool_t force);
-void sofia_glue_tech_track(sofia_profile_t *profile, switch_core_session_t *session);
 int sofia_glue_recover(switch_bool_t flush);
 int sofia_glue_profile_recover(sofia_profile_t *profile, switch_bool_t flush);
 void sofia_profile_destroy(sofia_profile_t *profile);
@@ -1192,7 +1187,7 @@ char *sofia_glue_get_host(const char *str, switch_memory_pool_t *pool);
 void sofia_presence_check_subscriptions(sofia_profile_t *profile, time_t now);
 void sofia_msg_thread_start(int idx);
 void crtp_init(switch_loadable_module_interface_t *module_interface);
-
+int sofia_recover_callback(switch_core_session_t *session);
 
 /* For Emacs:
  * Local Variables:
index 317ec28f55cf6f8bb8bfbc591d9a8b5a295c5def..b3e0f4c0f88b3dc0166fa1de7a58cb4c0a5e3df1 100644 (file)
@@ -1123,7 +1123,7 @@ static void our_sofia_event_callback(nua_event_t event,
                                }
 
                                extract_header_vars(profile, sip, session, nh);
-                               sofia_glue_tech_track(tech_pvt->profile, session);
+                               switch_core_recovery_track(session);
                                sofia_set_flag(tech_pvt, TFLAG_GOT_ACK);
                        }
                }
@@ -4479,9 +4479,6 @@ switch_status_t config_sofia(int reload, char *profile_name)
                                        } else if (!strcasecmp(var, "track-calls")) {
                                                if (switch_true(val)) {
                                                        sofia_set_pflag(profile, PFLAG_TRACK_CALLS);
-                                               } else if (!strcasecmp(val, "events")) {
-                                                       sofia_set_pflag(profile, PFLAG_TRACK_CALLS);
-                                                       sofia_set_pflag(profile, PFLAG_TRACK_CALLS_EVENTS);
                                                }
                                        } else if (!strcasecmp(var, "NDLB-received-in-nat-reg-contact") && switch_true(val)) {
                                                sofia_set_pflag(profile, PFLAG_RECIEVED_IN_NAT_REG_CONTACT);
@@ -5780,7 +5777,7 @@ static void sofia_handle_sip_r_invite(switch_core_session_t *session, int status
                        }
 
                        if ((!switch_channel_test_flag(channel, CF_EARLY_MEDIA) && !switch_channel_test_flag(channel, CF_ANSWERED) &&
-                                !switch_channel_test_flag(channel, CF_RING_READY)) || sofia_test_flag(tech_pvt, TFLAG_RECOVERING)) {
+                                !switch_channel_test_flag(channel, CF_RING_READY)) || switch_channel_test_flag(channel, CF_RECOVERING)) {
                                const char *from_user = "", *from_host = "", *to_user = "", *to_host = "", *contact_user = "", *contact_host = "";
                                const char *user_agent = "", *call_id = "";
                                const char *to_tag = "";
@@ -5860,8 +5857,8 @@ static void sofia_handle_sip_r_invite(switch_core_session_t *session, int status
 
                        extract_header_vars(profile, sip, session, nh);
                        extract_vars(profile, sip, session);
-                       sofia_glue_tech_track(tech_pvt->profile, session);
-                       sofia_clear_flag(tech_pvt, TFLAG_RECOVERING);
+                       switch_core_recovery_track(session);
+                       switch_channel_clear_flag(tech_pvt->channel, CF_RECOVERING);
                }
 
        }
@@ -7984,7 +7981,7 @@ void sofia_handle_sip_i_reinvite(switch_core_session_t *session,
                switch_channel_set_variable_printf(channel, "sip_recieved_port", "%d", network_port);
                switch_channel_set_variable_printf(channel, "sip_via_rport", "%d", network_port);
                
-               sofia_glue_tech_track(tech_pvt->profile, session);
+               switch_core_recovery_track(session);
        }
 
        if (sofia_test_pflag(profile, PFLAG_MANAGE_SHARED_APPEARANCE)) {
@@ -8351,6 +8348,7 @@ void sofia_handle_sip_i_invite(switch_core_session_t *session, nua_t *nua, sofia
                }
 
                switch_channel_set_variable(channel, "sofia_profile_name", profile->name);
+               switch_channel_set_variable(channel, "recovery_profile_name", profile->name);
                switch_channel_set_variable(channel, "sofia_profile_domain_name", profile->domain_name);
 
                if (!zstr(sip->sip_from->a_display)) {
index dbf1dc5bb7d56b227ab9dfc5e9105f8e5ba7ec2e..ceb4343fb908f0f7b95c90880a076c0b3a79817c 100644 (file)
@@ -689,7 +689,7 @@ void sofia_glue_set_local_sdp(private_object_t *tech_pvt, const char *ip, switch
                                switch_snprintf(buf + strlen(buf), SDPBUFLEN - strlen(buf), "a=rtpmap:%d %s/%ld\n", tech_pvt->video_pt, tech_pvt->video_rm_encoding,
                                                                tech_pvt->video_rm_rate);
 
-                               if (sofia_test_flag(tech_pvt, TFLAG_RECOVERING)) {
+                               if (switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING)) {
                                        pass_fmtp = tech_pvt->video_rm_fmtp;
                                } else {
 
@@ -933,6 +933,11 @@ void sofia_glue_attach_private(switch_core_session_t *session, sofia_profile_t *
 
        tech_pvt->session = session;
        tech_pvt->channel = switch_core_session_get_channel(session);
+
+       if (sofia_test_pflag(profile, PFLAG_TRACK_CALLS)) {
+               switch_channel_set_flag(tech_pvt->channel, CF_TRACKABLE);
+       }
+
        sofia_glue_check_dtmf_type(tech_pvt);
        switch_channel_set_cap(tech_pvt->channel, CC_MEDIA_ACK);
        switch_channel_set_cap(tech_pvt->channel, CC_BYPASS_MEDIA);
@@ -2085,7 +2090,7 @@ switch_status_t sofia_glue_do_invite(switch_core_session_t *session)
        int require_timer = 1;
 
 
-       if (sofia_test_flag(tech_pvt, TFLAG_RECOVERING)) {
+       if (switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING)) {
                const char *recover_contact = switch_channel_get_variable(tech_pvt->channel, "sip_recover_contact");
                recover_via = switch_channel_get_variable(tech_pvt->channel, "sip_recover_via");
 
@@ -2412,7 +2417,7 @@ switch_status_t sofia_glue_do_invite(switch_core_session_t *session)
                        cid_type = sofia_cid_name2type(val);
                }
 
-               if (sofia_test_flag(tech_pvt, TFLAG_RECOVERING) && switch_channel_direction(tech_pvt->channel) == SWITCH_CALL_DIRECTION_INBOUND) {
+               if (switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING) && switch_channel_direction(tech_pvt->channel) == SWITCH_CALL_DIRECTION_INBOUND) {
                        if (zstr((use_name = switch_channel_get_variable(tech_pvt->channel, "effective_callee_id_name"))) &&
                                zstr((use_name = switch_channel_get_variable(tech_pvt->channel, "sip_callee_id_name")))) {
                                if (!(use_name = switch_channel_get_variable(tech_pvt->channel, "sip_to_display"))) {
@@ -2506,7 +2511,7 @@ switch_status_t sofia_glue_do_invite(switch_core_session_t *session)
                sofia_private->is_call++;
                sofia_private->is_static++;
 
-               if (sofia_test_flag(tech_pvt, TFLAG_RECOVERING)) {
+               if (switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING)) {
                        sofia_private->is_call++;
                }
 
@@ -2543,6 +2548,7 @@ switch_status_t sofia_glue_do_invite(switch_core_session_t *session)
 
        if (!switch_channel_get_variable(channel, "sofia_profile_name")) {
                switch_channel_set_variable(channel, "sofia_profile_name", tech_pvt->profile->name);
+               switch_channel_set_variable(channel, "recovery_profile_name", tech_pvt->profile->name);
        }
 
        extra_headers = sofia_glue_get_extra_headers(channel, SOFIA_SIP_HEADER_PREFIX);
@@ -3818,8 +3824,7 @@ switch_status_t sofia_glue_activate_rtp(private_object_t *tech_pvt, switch_rtp_f
  end:
 
        sofia_clear_flag_locked(tech_pvt, TFLAG_REINVITE);
-       sofia_glue_tech_track(tech_pvt->profile, tech_pvt->session);
-
+       switch_core_recovery_track(tech_pvt->session);
 
        switch_mutex_unlock(tech_pvt->sofia_mutex);
 
@@ -5375,7 +5380,7 @@ void sofia_glue_pass_sdp(private_object_t *tech_pvt, char *sdp)
                other_channel = switch_core_session_get_channel(other_session);
                switch_channel_set_variable(other_channel, SWITCH_B_SDP_VARIABLE, sdp);
 
-               if (!sofia_test_flag(tech_pvt, TFLAG_CHANGE_MEDIA) && !sofia_test_flag(tech_pvt, TFLAG_RECOVERING) &&
+               if (!sofia_test_flag(tech_pvt, TFLAG_CHANGE_MEDIA) && !switch_channel_test_flag(tech_pvt->channel, CF_RECOVERING) &&
                        (switch_channel_direction(other_channel) == SWITCH_CALL_DIRECTION_OUTBOUND &&
  switch_channel_direction(tech_pvt->channel) == SWITCH_CALL_DIRECTION_OUTBOUND && switch_channel_test_flag(tech_pvt->channel, CF_PROXY_MODE))) {
                        switch_ivr_nomedia(val, SMF_FORCE);
@@ -5715,39 +5720,36 @@ void sofia_glue_del_profile(sofia_profile_t *profile)
        switch_mutex_unlock(mod_sofia_globals.hash_mutex);
 }
 
-struct recover_helper {
-       sofia_profile_t *profile;
-       int total;
-};
-
-
-static int recover_callback(void *pArg, int argc, char **argv, char **columnNames)
+int sofia_recover_callback(switch_core_session_t *session) 
 {
-       struct recover_helper *h = (struct recover_helper *) pArg;
-       switch_xml_t xml;
-       switch_core_session_t *session;
-       switch_channel_t *channel;
+
+       switch_channel_t *channel = switch_core_session_get_channel(session);
        private_object_t *tech_pvt = NULL;
+       sofia_profile_t *profile = NULL;
        const char *tmp;
        const char *rr;
+       int r = 0;
+       const char *profile_name = switch_channel_get_variable_dup(channel, "recovery_profile_name", SWITCH_FALSE, -1);
+       
 
-       xml = switch_xml_parse_str_dynamic(argv[3], SWITCH_TRUE);
-
-       if (!xml)
+       if (zstr(profile_name)) {
+               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Missing profile\n");
                return 0;
+       }
 
-       if (!(session = switch_core_session_request_xml(sofia_endpoint_interface, NULL, xml))) {
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Invalid cdr data, call not recovered\n");
+       if (!(profile = sofia_glue_find_profile(profile_name))) {
+               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Invalid profile %s\n", profile_name);
                return 0;
        }
 
+
        if (!(tech_pvt = (private_object_t *) switch_core_session_alloc(session, sizeof(private_object_t)))) {
                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Hey where is my memory pool?\n");
                switch_core_session_destroy(&session);
-               return 0;
+               goto end;
        }
 
-       channel = tech_pvt->channel = switch_core_session_get_channel(session);
+       tech_pvt->channel = channel;
 
        switch_mutex_init(&tech_pvt->flag_mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
        switch_mutex_init(&tech_pvt->sofia_mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
@@ -5804,7 +5806,7 @@ static int recover_callback(void *pArg, int argc, char **argv, char **columnName
 
        tech_pvt->dest_to = tech_pvt->dest;
 
-       sofia_glue_attach_private(session, h->profile, tech_pvt, NULL);
+       sofia_glue_attach_private(session, profile, tech_pvt, NULL);
        switch_channel_set_name(tech_pvt->channel, switch_channel_get_variable(channel, "channel_name"));
 
 
@@ -5826,7 +5828,6 @@ static int recover_callback(void *pArg, int argc, char **argv, char **columnName
        }
 
        if (session) {
-               switch_caller_extension_t *extension = NULL;
                const char *ip = switch_channel_get_variable(channel, SWITCH_LOCAL_MEDIA_IP_VARIABLE);
                const char *a_ip = switch_channel_get_variable(channel, SWITCH_ADVERTISED_MEDIA_IP_VARIABLE);
                const char *port = switch_channel_get_variable(channel, SWITCH_LOCAL_MEDIA_PORT_VARIABLE);
@@ -5834,7 +5835,7 @@ static int recover_callback(void *pArg, int argc, char **argv, char **columnName
                const char *r_port = switch_channel_get_variable(channel, SWITCH_REMOTE_MEDIA_PORT_VARIABLE);
                const char *use_uuid;
 
-               sofia_set_flag(tech_pvt, TFLAG_RECOVERING);
+               switch_channel_set_flag(channel, CF_RECOVERING);
 
                if ((use_uuid = switch_channel_get_variable(channel, "origination_uuid"))) {
                        if (switch_core_session_set_uuid(session, use_uuid) == SWITCH_STATUS_SUCCESS) {
@@ -5918,8 +5919,7 @@ static int recover_callback(void *pArg, int argc, char **argv, char **columnName
                        sofia_glue_set_local_sdp(tech_pvt, NULL, 0, NULL, 1);
 
                        if (sofia_glue_activate_rtp(tech_pvt, 0) != SWITCH_STATUS_SUCCESS) {
-                               switch_xml_free(xml);
-                               return 0;
+                               goto end;
                        }
                        
                        if (switch_rtp_ready(tech_pvt->rtp_session)) {
@@ -5943,39 +5943,13 @@ static int recover_callback(void *pArg, int argc, char **argv, char **columnName
                        }
 
                }
-
-               if (switch_channel_get_partner_uuid(channel)) {
-                       sofia_set_flag(tech_pvt, TFLAG_RECOVERING_BRIDGE);
-               } else {
-                       switch_xml_t callflow, param, x_extension;
-                       if ((extension = switch_caller_extension_new(session, "recovery", "recovery")) == 0) {
-                               abort();
-                       }
-
-                       if ((callflow = switch_xml_child(xml, "callflow")) && (x_extension = switch_xml_child(callflow, "extension"))) {
-                               for (param = switch_xml_child(x_extension, "application"); param; param = param->next) {
-                                       const char *var = switch_xml_attr_soft(param, "app_name");
-                                       const char *val = switch_xml_attr_soft(param, "app_data");
-                                       /* skip announcement type apps */
-                                       if (strcasecmp(var, "speak") && strcasecmp(var, "playback") && strcasecmp(var, "gentones") && strcasecmp(var, "say")) {
-                                               switch_caller_extension_add_application(session, extension, var, val);
-                                       }
-                               }
-                       }
-
-                       switch_channel_set_caller_extension(channel, extension);
-               }
-
-               switch_channel_set_state(channel, CS_INIT);
-               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "Resurrecting fallen channel %s\n", switch_channel_get_name(channel));
-               switch_core_session_thread_launch(session);
        }
 
-       switch_xml_free(xml);
+       r++;
 
-       h->total++;
+ end:
 
-       return 0;
+       return r;
 
 }
 
@@ -6001,140 +5975,21 @@ int sofia_glue_recover(switch_bool_t flush)
 
 int sofia_glue_profile_recover(sofia_profile_t *profile, switch_bool_t flush)
 {
-       char *sql;                              
        int r = 0;
 
        if (profile) {
-               struct recover_helper h = { 0 };
-               h.profile = profile;
-               h.total = 0;
-
                sofia_clear_pflag_locked(profile, PFLAG_STANDBY);
 
                if (flush) {
-                       sql = switch_mprintf("delete from sip_recovery where profile_name='%q'", profile->name);
-                       sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
+                       switch_core_recovery_flush(SOFIA_RECOVER, profile->name);
                } else {
-
-                       sql = switch_mprintf("select profile_name, hostname, uuid, metadata "
-                                                                "from sip_recovery where runtime_uuid!='%q' and profile_name='%q'", switch_core_get_uuid(), profile->name);
-
-                       sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, recover_callback, &h);
-                       r += h.total;
-                       free(sql);
-                       sql = NULL;
-
-                       sql = switch_mprintf("delete "
-                                                                "from sip_recovery where runtime_uuid!='%q' and profile_name='%q'", switch_core_get_uuid(), profile->name);
-
-                       sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
+                       r = switch_core_recovery_recover(SOFIA_RECOVER, profile->name);
                }
        }
 
        return r;
 }
 
-void sofia_glue_track_event_handler(switch_event_t *event)
-{
-       char *sql, *buf = NULL;
-       char *profile_name = NULL;
-
-       switch_assert(event);           // Just a sanity check
-
-       if ((buf = switch_event_get_header_nil(event, "sql")) && (profile_name = switch_event_get_header_nil(event, "profile_name"))) {
-               sofia_profile_t *profile;
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s\n", switch_event_get_header_nil(event, "Event-Calling-Function"));
-               if ((profile = sofia_glue_find_profile(profile_name))) {
-                       sql = switch_mprintf("%s", buf);
-                       sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
-                       sofia_glue_release_profile(profile);
-               }
-       }
-
-       return;
-}
-void sofia_glue_tech_untrack(sofia_profile_t *profile, switch_core_session_t *session, switch_bool_t force)
-{
-       char *sql = NULL;
-       private_object_t *tech_pvt = (private_object_t *) switch_core_session_get_private(session);
-
-       if (!sofia_test_pflag(profile, PFLAG_TRACK_CALLS) || (sofia_test_flag(tech_pvt, TFLAG_RECOVERING))) {
-               return;
-       }
-
-       if (sofia_test_pflag(profile, PFLAG_TRACK_CALLS) && (sofia_test_flag(tech_pvt, TFLAG_TRACKED) || force)) {
-               switch_event_t *event = NULL;
-
-               if (force) {
-                       sql = switch_mprintf("delete from sip_recovery where uuid='%q'", switch_core_session_get_uuid(session));
-                       
-               } else {
-                       sql = switch_mprintf("delete from sip_recovery where runtime_uuid='%q' and uuid='%q'",
-                                                                switch_core_get_uuid(), switch_core_session_get_uuid(session));
-               }
-
-               if (sofia_test_pflag(profile, PFLAG_TRACK_CALLS_EVENTS)) {
-                       if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, MY_EVENT_RECOVERY_SEND) == SWITCH_STATUS_SUCCESS) {
-                               switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "profile_name", profile->name);
-                               switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "sql", sql);
-                               switch_event_fire(&event);
-                       }
-               }
-               
-               sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
-               sofia_clear_flag(tech_pvt, TFLAG_TRACKED);
-               
-               switch_safe_free(sql);
-       }
-       
-
-}
-
-void sofia_glue_tech_track(sofia_profile_t *profile, switch_core_session_t *session)
-{
-       private_object_t *tech_pvt = (private_object_t *) switch_core_session_get_private(session);
-       switch_xml_t cdr = NULL;
-       char *xml_cdr_text = NULL;
-       char *sql = NULL;
-
-       if (!sofia_test_pflag(profile, PFLAG_TRACK_CALLS) || sofia_test_flag(tech_pvt, TFLAG_RECOVERING)) {
-               return;
-       }
-
-       if (switch_ivr_generate_xml_cdr(session, &cdr) == SWITCH_STATUS_SUCCESS) {
-               xml_cdr_text = switch_xml_toxml_nolock(cdr, SWITCH_FALSE);
-               switch_xml_free(cdr);
-       }
-
-       if (xml_cdr_text) {
-               if (sofia_test_flag(tech_pvt, TFLAG_TRACKED)) {
-                       sql = switch_mprintf("update sip_recovery set metadata='%q' where uuid='%q'",  xml_cdr_text, switch_core_session_get_uuid(session));
-               } else {
-                       
-                       sql = switch_mprintf("insert into sip_recovery (runtime_uuid, profile_name, hostname, uuid, metadata) values ('%q','%q','%q','%q','%q')",
-                                                                switch_core_get_uuid(), profile->name, mod_sofia_globals.hostname, switch_core_session_get_uuid(session), xml_cdr_text);
-               }
-
-               if (sofia_test_pflag(profile, PFLAG_TRACK_CALLS_EVENTS)) {
-                       switch_event_t *event = NULL;
-                       if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, MY_EVENT_RECOVERY_SEND) == SWITCH_STATUS_SUCCESS) {
-                               switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "profile_name", profile->name);
-                               switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "sql", sql);
-                               switch_event_fire(&event);
-                       }
-               }
-
-               sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
-               
-               free(xml_cdr_text);
-               sofia_set_flag(tech_pvt, TFLAG_TRACKED);
-               
-       }
-       
-       
-       switch_safe_free(sql);
-
-}
 
 int sofia_glue_init_sql(sofia_profile_t *profile)
 {
@@ -6166,15 +6021,6 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
                "   sub_host         VARCHAR(255)\n"
                ");\n";
 
-       char recovery_sql[] =
-               "CREATE TABLE sip_recovery (\n"
-               "   runtime_uuid    VARCHAR(255),\n"
-               "   profile_name    VARCHAR(255),\n"
-               "   hostname        VARCHAR(255),\n"
-               "   uuid            VARCHAR(255),\n"
-               "   metadata        text\n"
-               ");\n";
-
        char pres_sql[] =
                "CREATE TABLE sip_presence (\n"
                "   sip_user        VARCHAR(255),\n"
@@ -6346,10 +6192,6 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
                "create index ssd_contact_str on sip_shared_appearance_dialogs (contact_str)",
                "create index ssd_call_id on sip_shared_appearance_dialogs (call_id)",
                "create index ssd_expires on sip_shared_appearance_dialogs (expires)",
-               "create index sr_1 on sip_recovery (runtime_uuid)",
-               "create index sr_2 on sip_recovery (profile_name)",
-               "create index sr_3 on sip_recovery (hostname)",
-               "create index sr_4 on sip_recovery (uuid)",
                NULL
        };
                
@@ -6417,11 +6259,6 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
 
        switch_cache_db_test_reactive(dbh, test_sql, "DROP TABLE sip_shared_appearance_dialogs", shared_appearance_dialogs_sql);
                
-       free(test_sql);
-       test_sql = switch_mprintf("select count(profile_name) from sip_recovery where hostname='%q'", mod_sofia_globals.hostname);
-
-
-       switch_cache_db_test_reactive(dbh, test_sql, "DROP TABLE sip_recovery", recovery_sql);
        free(test_sql);
 
        for (x = 0; indexes[x]; x++) {
@@ -6904,7 +6741,7 @@ void sofia_glue_tech_simplify(private_object_t *tech_pvt)
 
                                        did_simplify = 1;
 
-                                       sofia_glue_tech_track(tech_pvt->profile, inbound_session);
+                                       switch_core_recovery_track(inbound_session);
 
                                        switch_channel_set_flag(inbound_channel, CF_SIMPLIFY);