]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
improvement towards stable mod_sofia that can start/stop profiles (see i told ya...
authorAnthony Minessale <anthony.minessale@gmail.com>
Thu, 3 May 2007 01:55:25 +0000 (01:55 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Thu, 3 May 2007 01:55:25 +0000 (01:55 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@5074 d0543943-73ff-0310-b7d9-9358b9ac24b2

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
src/mod/endpoints/mod_sofia/sofia_presence.c
src/mod/endpoints/mod_sofia/sofia_reg.c

index 3a85ddd9adb8a3d2d742c36dc008e881f4246162..e27a73deed475553c4235759c83eb1656a285b23 100644 (file)
@@ -1022,7 +1022,7 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t
 
                                stream->write_function(stream, "%s\n", line);
 
-                               switch_thread_rwlock_unlock(profile->rwlock);
+                               sofia_glue_release_profile(profile);
                        } else {
                                stream->write_function(stream, "Invalid Profile!\n");
                        }
@@ -1047,8 +1047,8 @@ static switch_status_t cmd_status(char **argv, int argc, switch_stream_handle_t
                                ac++;
                                stream->write_function(stream, "%25s\t%s\t  %32s\t%s\n", vvar, "  alias", profile->name, "ALIASED");
                        } else {
-                               stream->write_function(stream, "%25s\t%s\t  %32s\t%s\n", profile->name, "profile", profile->url,
-                                                                          sofia_test_pflag(profile, PFLAG_RUNNING) ? "RUNNING" : "DOWN");
+                               stream->write_function(stream, "%25s\t%s\t  %32s\t%s (%u)\n", profile->name, "profile", profile->url,
+                                                                          sofia_test_pflag(profile, PFLAG_RUNNING) ? "RUNNING" : "DOWN", profile->inuse);
                                c++;
 
                                for (gp = profile->gateways; gp; gp = gp->next) {
@@ -1072,24 +1072,21 @@ static switch_status_t cmd_profile(char **argv, int argc, switch_stream_handle_t
 {
        sofia_profile_t *profile = NULL;
        char *profile_name = argv[0];
+       const char *err;
+       switch_xml_t xml_root;
 
        if (argc < 2) {
                stream->write_function(stream, "Invalid Args!\n");
                return SWITCH_STATUS_SUCCESS;
        }
 
-       if (argc > 2 && !strcasecmp(argv[2], "reloadxml")) {
-               const char *err;
-               switch_xml_t xml_root;
-
-               if ((xml_root = switch_xml_open_root(1, &err))) {
-                       switch_xml_free(xml_root);
-               }
-
-               stream->write_function(stream, "Reload XML [%s]\n", err);
-       }
-
        if (!strcasecmp(argv[1], "start")) {
+               if (argc > 2 && !strcasecmp(argv[2], "reloadxml")) {
+                       if ((xml_root = switch_xml_open_root(1, &err))) {
+                               switch_xml_free(xml_root);
+                       }
+                       stream->write_function(stream, "Reload XML [%s]\n", err);
+               }
                if (config_sofia(1, argv[0]) == SWITCH_STATUS_SUCCESS) {
                        stream->write_function(stream, "%s started successfully\n", argv[0]);
                } else {
@@ -1103,23 +1100,35 @@ static switch_status_t cmd_profile(char **argv, int argc, switch_stream_handle_t
                return SWITCH_STATUS_SUCCESS;
        }
 
-       if (!strcasecmp(argv[1], "stop")) {
-               sofia_clear_pflag_locked(profile, PFLAG_RUNNING);
-               stream->write_function(stream, "stopping: %s", profile->name);
-       } else if (!strcasecmp(argv[1], "restart")) {
+       if (!strcasecmp(argv[1], "stop") || !strcasecmp(argv[1], "restart")) {
                int rsec = 30;
-
-               if (time(NULL) - profile->started < rsec) {
-                       stream->write_function(stream, "Profile %s must be up for at least %d seconds to restart\n", rsec, profile->name);
+               int diff = (int) (time(NULL) - profile->started);
+               int remain = rsec - diff;
+               if (diff < rsec) {
+                       stream->write_function(stream, "Profile %s must be up for at least %d seconds to stop/restart.\nPlease wait %d second%s\n", 
+                                                                  profile->name, rsec, remain, remain == 1 ? "" : "s");
                } else {
-                       sofia_set_pflag_locked(profile, PFLAG_RESPAWN);
-                       sofia_clear_pflag_locked(profile, PFLAG_RUNNING);
-                       stream->write_function(stream, "restarting: %s", profile->name);
+
+                       if (argc > 2 && !strcasecmp(argv[2], "reloadxml")) {
+                               if ((xml_root = switch_xml_open_root(1, &err))) {
+                                       switch_xml_free(xml_root);
+                               }
+                               stream->write_function(stream, "Reload XML [%s]\n", err);
+                       }
+
+                       if (!strcasecmp(argv[1], "stop")) {
+                               sofia_clear_pflag_locked(profile, PFLAG_RUNNING);
+                               stream->write_function(stream, "stopping: %s", profile->name);
+                       } else {
+                               sofia_set_pflag_locked(profile, PFLAG_RESPAWN);
+                               sofia_clear_pflag_locked(profile, PFLAG_RUNNING);
+                               stream->write_function(stream, "restarting: %s", profile->name);
+                       }
                }
        }
 
        if (profile) {
-               switch_thread_rwlock_unlock(profile->rwlock);
+               sofia_glue_release_profile(profile);
        }
 
        return SWITCH_STATUS_SUCCESS;
@@ -1328,7 +1337,6 @@ static switch_call_cause_t sofia_outgoing_channel(switch_core_session_t *session
                        tech_pvt->dest = switch_core_session_sprintf(nsession, "sip:%s", dest);
                }
                tech_pvt->invite_contact = switch_core_session_strdup(nsession, gateway_ptr->register_contact);
-               sofia_reg_release_gateway(gateway_ptr);
        } else {
                if (!(dest = strchr(profile_name, '/'))) {
                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid URL\n");
@@ -1416,7 +1424,7 @@ static switch_call_cause_t sofia_outgoing_channel(switch_core_session_t *session
 
   done:
        if (profile) {
-               switch_thread_rwlock_unlock(profile->rwlock);
+               sofia_glue_release_profile(profile);
        }
 
        return cause;
index a965957807446ec09b182fac3758c53d6a06278a..854dddaeedbecbf150765c9b17ec606446ee2b6d 100644 (file)
@@ -439,15 +439,13 @@ void sofia_presence_handle_sip_i_subscribe(int status,
                                         char const *phrase,
                                         nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[]);
 
-sofia_profile_t *sofia_glue_find_profile(char *key);
-switch_status_t sofia_glue_add_profile(char *key, sofia_profile_t *profile);
+
 void sofia_glue_execute_sql(sofia_profile_t *profile, switch_bool_t master, char *sql, switch_mutex_t *mutex);
 void sofia_reg_check_expire(sofia_profile_t *profile, time_t now);
 void sofia_reg_check_gateway(sofia_profile_t *profile, time_t now);
 void sofia_reg_unregister(sofia_profile_t *profile);
 switch_status_t sofia_glue_ext_address_lookup(char **ip, switch_port_t *port, char *sourceip, switch_memory_pool_t *pool);
-sofia_gateway_t *sofia_reg_find_gateway(char *key);
-switch_status_t sofia_reg_add_gateway(char *key, sofia_gateway_t *gateway);
+
 void sofia_glue_pass_sdp(private_object_t *tech_pvt, char *sdp);
 int sofia_glue_get_user_host(char *in, char **user, char **host);
 switch_call_cause_t sofia_glue_sip_cause_to_freeswitch(int status);
@@ -469,5 +467,20 @@ switch_bool_t sofia_glue_execute_sql_callback(sofia_profile_t *profile,
                                                                                          void *pdata);
 char *sofia_glue_execute_sql2str(sofia_profile_t *profile, switch_mutex_t *mutex, char *sql, char *resbuf, size_t len);
 void sofia_glue_check_video_codecs(private_object_t *tech_pvt);
-void sofia_reg_release_gateway(sofia_gateway_t *gateway);
 void sofia_glue_del_profile(sofia_profile_t *profile);
+
+switch_status_t sofia_glue_add_profile(char *key, sofia_profile_t *profile);
+void sofia_glue_release_profile__(const char *file, const char *func, int line, sofia_profile_t *profile);
+#define sofia_glue_release_profile(x) sofia_glue_release_profile__(__FILE__, __SWITCH_FUNC__, __LINE__,  x)
+
+sofia_profile_t *sofia_glue_find_profile__(const char *file, const char *func, int line, char *key);
+#define sofia_glue_find_profile(x) sofia_glue_find_profile__(__FILE__, __SWITCH_FUNC__, __LINE__,  x)
+
+
+switch_status_t sofia_reg_add_gateway(char *key, sofia_gateway_t *gateway);
+sofia_gateway_t *sofia_reg_find_gateway__(const char *file, const char *func, int line, char *key);
+#define sofia_reg_find_gateway(x) sofia_reg_find_gateway__(__FILE__, __SWITCH_FUNC__, __LINE__,  x)
+
+
+void sofia_reg_release_gateway__(const char *file, const char *func, int line, sofia_gateway_t *gateway);
+#define sofia_reg_release_gateway(x) sofia_reg_release_gateway__(__FILE__, __SWITCH_FUNC__, __LINE__, x);
index 69c8d11eabc292114649d9a7fd57adacd9507466..cee1bc603740da9db4babd57ff6b89b2c259c3ac 100644 (file)
@@ -237,7 +237,7 @@ void event_handler(switch_event_t *event)
                }
 
                if (profile) {
-                       switch_thread_rwlock_unlock(profile->rwlock);
+                       sofia_glue_release_profile(profile);
                }
        }
 }
@@ -615,7 +615,7 @@ switch_status_t config_sofia(int reload, char *profile_name)
        if (!switch_strlen_zero(profile_name) && (profile = sofia_glue_find_profile(profile_name))) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile [%s] Already exists.\n", switch_str_nil(profile_name));
                status = SWITCH_STATUS_FALSE;
-               switch_thread_rwlock_unlock(profile->rwlock);
+               sofia_glue_release_profile(profile);
                return status;
        }
 
index d475ba5eb285c93a866a1354ea8344b67c44182a..4f4b6da374724205a4767003a76062331dcb2658 100644 (file)
@@ -1418,29 +1418,44 @@ char *sofia_glue_get_url_from_contact(char *buf, uint8_t to_dup)
        return url;
 }
 
-
-sofia_profile_t *sofia_glue_find_profile(char *key)
+sofia_profile_t *sofia_glue_find_profile__(const char *file, const char *func, int line, char *key)
 {
        sofia_profile_t *profile;
 
        switch_mutex_lock(mod_sofia_globals.hash_mutex);
        if ((profile = (sofia_profile_t *) switch_core_hash_find(mod_sofia_globals.profile_hash, key))) {
-               if (!sofia_test_pflag(profile, PFLAG_RUNNING)) {
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is not running\n", profile->name);
-                       profile = NULL;
-               } else if (switch_thread_rwlock_tryrdlock(profile->rwlock) != SWITCH_STATUS_SUCCESS) {
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is locked\n", profile->name);
+               if (switch_thread_rwlock_tryrdlock(profile->rwlock) != SWITCH_STATUS_SUCCESS) {
+#ifdef SOFIA_DEBUG_RWLOCKS
+                       switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "Profile %s is locked\n", profile->name);
+#endif
                        profile = NULL;
-               }
+               }               
        } else {
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is not in the hash\n", profile->name);
+#ifdef SOFIA_DEBUG_RWLOCKS
+               switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "Profile %s is not in the hash\n", key);
+#endif
        }
-
+#ifdef SOFIA_DEBUG_RWLOCKS
+       if (profile) {
+               switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX LOCK %s\n", profile->name);
+       }
+#endif
        switch_mutex_unlock(mod_sofia_globals.hash_mutex);
 
        return profile;
 }
 
+
+void sofia_glue_release_profile__(const char *file, const char *func, int line, sofia_profile_t *profile)
+{
+       if (profile) {
+#ifdef SOFIA_DEBUG_RWLOCKS
+               switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX UNLOCK %s\n", profile->name);
+#endif
+               switch_thread_rwlock_unlock(profile->rwlock);
+       }
+}
+
 switch_status_t sofia_glue_add_profile(char *key, sofia_profile_t *profile)
 {
        switch_status_t status = SWITCH_STATUS_FALSE;
index 2c2113e7216cfc978d634da41dbe7eae1b10ff67..13f3eb6bd2550462b248aa16c97d7381b8d1af9c 100644 (file)
@@ -234,11 +234,9 @@ void sofia_presence_mwi_event_handler(switch_event_t *event)
 
        switch_safe_free(sql);
        switch_safe_free(dup_account);
-
        if (profile) {
-               switch_thread_rwlock_unlock(profile->rwlock);
+               sofia_glue_release_profile(profile);
        }
-
 }
 
 void sofia_presence_event_handler(switch_event_t *event)
@@ -374,7 +372,7 @@ void sofia_presence_event_handler(switch_event_t *event)
                                                                                                profile);
 
                                
-                               switch_thread_rwlock_unlock(profile->rwlock);
+                               sofia_glue_release_profile(profile);
                                        
                                switch_safe_free(sql);
                        }
@@ -619,9 +617,8 @@ static int sofia_presence_mwi_callback(void *pArg, int argc, char **argv, char *
        switch_safe_free(id);
        switch_safe_free(exp);
 
-       if (profile) {
-               switch_thread_rwlock_unlock(profile->rwlock);
-       }
+       sofia_glue_release_profile(profile);
+
 
        return 0;
 }
index b3e77a4e75a92af136db26cd8060777086dd5950..c3d719429a33a11d78a5c233ac7a8ea906ebbc0a 100644 (file)
@@ -553,7 +553,7 @@ void sofia_reg_handle_sip_r_challenge(int status,
        }
 
        if (profile) {
-               sofia_gateway_t *gateway_ptr;
+               sofia_gateway_t *gateway_ptr = NULL;
 
                if ((duprealm = strdup(realm))) {
                        qrealm = duprealm;
@@ -585,28 +585,37 @@ void sofia_reg_handle_sip_r_challenge(int status,
                                                && !strcasecmp(gateway_ptr->register_realm, qrealm)) {
                                                gateway = gateway_ptr;
 
-                                               if (!switch_test_flag(gateway->profile, PFLAG_RUNNING)) {
-                                                       gateway = NULL;
-                                               } else if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) {
+                                               if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) {
+#ifdef SOFIA_DEBUG_RWLOCKS
+                                                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is locked\n", gateway->profile->name);
+#endif
                                                        gateway = NULL;
-                                               }                                               
+                                               }
+#ifdef SOFIA_DEBUG_RWLOCKS
+                                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX GW LOCK %s\n", gateway->profile->name);
+#endif
                                                break;
                                        }
                                }
                                switch_mutex_unlock(mod_sofia_globals.hash_mutex);
                        }
-
+                       
                        if (!gateway) {
                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No Match for Scheme [%s] Realm [%s]\n", scheme, qrealm);
-                               goto cancel;
                        }
+                       
                        switch_safe_free(duprealm);
+
+                       if (!gateway) {
+                               goto cancel;
+                       }
+
                } else {
                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n");
                        goto cancel;
                }
        }
-
+       
        snprintf(authentication, sizeof(authentication), "%s:%s:%s:%s", scheme, realm, gateway->register_username, gateway->register_password);
 
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Authenticating '%s' with '%s'.\n", profile->username, authentication);
@@ -617,10 +626,16 @@ void sofia_reg_handle_sip_r_challenge(int status,
        tl_gets(tags, NUTAG_CALLSTATE_REF(ss_state), SIPTAG_WWW_AUTHENTICATE_REF(authenticate), TAG_END());
 
        nua_authenticate(nh, SIPTAG_EXPIRES_STR(gateway->expires_str), NUTAG_AUTH(authentication), TAG_END());
-       sofia_reg_release_gateway(gateway);
+       if (gateway) {
+               sofia_reg_release_gateway(gateway);
+               gateway = NULL;
+       }
        return;
 
  cancel:
+       if (gateway) {
+               sofia_reg_release_gateway(gateway);
+       }
        if (session) {
                switch_channel_t *channel = switch_core_session_get_channel(session);
                switch_channel_hangup(channel, SWITCH_CAUSE_MANDATORY_IE_MISSING);
@@ -816,26 +831,32 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile, sip_authorization_t co
 }
 
 
-sofia_gateway_t *sofia_reg_find_gateway(char *key)
+sofia_gateway_t *sofia_reg_find_gateway__(const char *file, const char *func, int line, char *key)
 {
        sofia_gateway_t *gateway = NULL;
 
        switch_mutex_lock(mod_sofia_globals.hash_mutex);
        if ((gateway = (sofia_gateway_t *) switch_core_hash_find(mod_sofia_globals.gateway_hash, key))) {
-               if (!sofia_test_pflag(gateway->profile, PFLAG_RUNNING)) {
-                       gateway = NULL;
-               } else if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) {
+               if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) {
+                       switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "Profile %s is locked\n", gateway->profile->name);
                        gateway = NULL;
                }
        }
-
+       if (gateway) {
+#ifdef SOFIA_DEBUG_RWLOCKS
+               switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX GW LOCK %s\n", gateway->profile->name);
+#endif
+       }
        switch_mutex_unlock(mod_sofia_globals.hash_mutex);
        return gateway;
 }
 
-void sofia_reg_release_gateway(sofia_gateway_t *gateway)
+void sofia_reg_release_gateway__(const char *file, const char *func, int line, sofia_gateway_t *gateway)
 {
        switch_thread_rwlock_unlock(gateway->profile->rwlock);
+#ifdef SOFIA_DEBUG_RWLOCKS
+       switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX GW UNLOCK %s\n", gateway->profile->name);
+#endif
 }
 
 switch_status_t sofia_reg_add_gateway(char *key, sofia_gateway_t *gateway)