[Freeswitch-svn] [commit] r11296 - in freeswitch/branches/ctrix/mod_airpe: . config

FreeSWITCH SVN ctrix at freeswitch.org
Mon Jan 19 15:30:29 PST 2009


Author: ctrix
Date: Mon Jan 19 17:30:28 2009
New Revision: 11296

Log:
tweaks...

(add some locking, add option "restart-clients-on-reloadxml", reworking of reloadxml, 
reworking the shutdown of the module, remove noisy debug messages, airpe_uuid api command)



Modified:
   freeswitch/branches/ctrix/mod_airpe/Makefile
   freeswitch/branches/ctrix/mod_airpe/airpe_api.c
   freeswitch/branches/ctrix/mod_airpe/airpe_if_common.c
   freeswitch/branches/ctrix/mod_airpe/airpe_if_x11.c
   freeswitch/branches/ctrix/mod_airpe/config/airpe.conf.xml
   freeswitch/branches/ctrix/mod_airpe/mod_airpe.c
   freeswitch/branches/ctrix/mod_airpe/mod_airpe.h

Modified: freeswitch/branches/ctrix/mod_airpe/Makefile
==============================================================================
--- freeswitch/branches/ctrix/mod_airpe/Makefile	(original)
+++ freeswitch/branches/ctrix/mod_airpe/Makefile	Mon Jan 19 17:30:28 2009
@@ -2,6 +2,7 @@
 
 MODNAME=mod_airpe
 
+LOCAL_CFLAGS=
 LOCAL_LDFLAGS=-lX11
 LOCAL_OBJS=airpe_if_common.o airpe_api.o airpe_apps.o airpe_if_x11.o airpe_if_osx.o airpe_if_win32.o
 LOCAL_SOURCES=airpe_if_common.c airpe_api.c airpe_apps.c airpe_if_x11.c airpe_if_osx.c airpe_if_win32.c

Modified: freeswitch/branches/ctrix/mod_airpe/airpe_api.c
==============================================================================
--- freeswitch/branches/ctrix/mod_airpe/airpe_api.c	(original)
+++ freeswitch/branches/ctrix/mod_airpe/airpe_api.c	Mon Jan 19 17:30:28 2009
@@ -37,6 +37,45 @@
 */
 
 
+#define AIRPE_UUID_SYNTAX "airpe_uuid_command <clientname>"
+SWITCH_STANDARD_API(airpe_uuid_command)
+{
+    int argc = 0;
+    char *argv[2] = { 0 };
+    char *mycmd  = NULL;
+    char *client = NULL;
+    airpe_interface_t *airpe = NULL;
+
+    if (!switch_strlen_zero(cmd) && (mycmd = strdup(cmd))) {
+	argc = switch_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
+	switch_assert(argv[0]);
+    }
+
+    if ( argc != 1 ) {
+	stream->write_function(stream, "-USAGE: %s\n", AIRPE_UUID_SYNTAX);
+	goto done;
+    }
+
+    client = argv[0];
+    airpe = airpe_find_interface(client);
+
+    if ( !airpe ) {
+	stream->write_function(stream, "-ERR interface not found for client ");
+	stream->write_function(stream, client);
+	goto done;
+    } 
+
+    if ( !airpe->active_session_uuid ) {
+	goto done;
+    }
+
+    stream->write_function(stream, airpe->active_session_uuid);
+    stream->write_function(stream, "\n");
+
+ done:
+    return SWITCH_STATUS_SUCCESS;
+}
+
 #define AIRPE_RAW_SYNTAX "airpe_raw_command <clientname> <command string>"
 SWITCH_STANDARD_API(airpe_raw_command)
 {
@@ -255,7 +294,6 @@
 		stream->write_function(stream, buf);
 	    }
 	}
-
     }
 
  done:
@@ -350,6 +388,9 @@
 
     switch_api_interface_t *api;
 
+    switch_console_set_complete("add airpe_uuid ");
+    SWITCH_ADD_API(api, "airpe_uuid", "gets the uuid of the (eventually) attached session", airpe_uuid_command, AIRPE_UUID_SYNTAX);
+
     switch_console_set_complete("add airpe_debug ");
     SWITCH_ADD_API(api, "airpe_debug", "gets/sets debug status of an airpe client", airpe_debug_command, AIRPE_DEBUG_SYNTAX);
 

Modified: freeswitch/branches/ctrix/mod_airpe/airpe_if_common.c
==============================================================================
--- freeswitch/branches/ctrix/mod_airpe/airpe_if_common.c	(original)
+++ freeswitch/branches/ctrix/mod_airpe/airpe_if_common.c	Mon Jan 19 17:30:28 2009
@@ -174,7 +174,7 @@
 
     assert(airpe);
 
-    if ( airpe->debug )
+    if ( airpe->debug && strncmp(msg, "PING", 4) )
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "msg to airpe client %s: > %s\n", airpe->name, msg);
 
     status = airpe_skype_send_message(airpe->skype_window, msg);
@@ -204,13 +204,14 @@
 }
 
 switch_status_t airpe_explicit_hangup(airpe_interface_t *airpe) {
+    switch_status_t status = SWITCH_STATUS_FALSE;
     char buf[SKYPE_MSG_LEN];
 
-    if ( airpe->active_call_id > 0 ) {
+    if ( airpe && airpe->active_call_id > 0 ) {
 	snprintf(buf, sizeof(buf), "SET CALL %d STATUS FINISHED", airpe->active_call_id);
-	return airpe_cmd_write(airpe, buf);
+	status = airpe_cmd_write(airpe, buf);
     }
-    return SWITCH_STATUS_FALSE;
+    return status;
 }
 
 /*****************************************************************************
@@ -536,7 +537,7 @@
     assert(airpe);
     assert(msg);
 
-    if ( airpe->debug )
+    if ( airpe->debug && strncmp(msg, "PONG", 4) )
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "airpe client %s: < %s\n", airpe->name, msg);
 
     if ( airpe->active_session_uuid ) {

Modified: freeswitch/branches/ctrix/mod_airpe/airpe_if_x11.c
==============================================================================
--- freeswitch/branches/ctrix/mod_airpe/airpe_if_x11.c	(original)
+++ freeswitch/branches/ctrix/mod_airpe/airpe_if_x11.c	Mon Jan 19 17:30:28 2009
@@ -129,6 +129,8 @@
     disp 	= window->disp;
     handle_P 	= window->win;
 
+    trap_errors();
+
     atom1 = XInternAtom(disp, "SKYPECONTROLAPI_MESSAGE_BEGIN", False);
     atom2 = XInternAtom(disp, "SKYPECONTROLAPI_MESSAGE", False);
     pos = 0;
@@ -141,7 +143,6 @@
     xev.xclient.window 		= handle_P;
     xev.xclient.format 		= 8;
 
-    trap_errors();
     do {
 	unsigned int i;
 	for (i = 0; i < 20 && i + pos <= len; ++i)
@@ -158,6 +159,7 @@
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "airpe client X11 error.\n");
 	airpe_skype_avalaible(window);
     }
+
     return SWITCH_STATUS_SUCCESS;
 }
 
@@ -275,6 +277,7 @@
 	    struct timeval timeout;
 	    int ret;
 
+	    memset(&timeout, 0, sizeof(struct timeval) );
 	    timeout.tv_usec = 10 * 1000;
 	    ret = select(x11_fd+1, &rset, 0, 0, &timeout);
 
@@ -328,14 +331,14 @@
 
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "airpe client %s: exiting thread\n", airpe->name);
 
-	/* Hangup our call, if we have one */
-	airpe_explicit_hangup(airpe);
-
+	XCloseDisplay(disp);
     } else {
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "airpe client %s: skype not running on display %s\n", airpe->name, airpe->X11_display);
     }
 
-  return NULL;
+    airpe->airpe_thread_skype = NULL;
+
+    return NULL;
 }
 
 #endif

Modified: freeswitch/branches/ctrix/mod_airpe/config/airpe.conf.xml
==============================================================================
--- freeswitch/branches/ctrix/mod_airpe/config/airpe.conf.xml	(original)
+++ freeswitch/branches/ctrix/mod_airpe/config/airpe.conf.xml	Mon Jan 19 17:30:28 2009
@@ -2,6 +2,7 @@
 
   <globals>
     <param name="audio-tcp-port_start" value="21000"/>
+    <param name="restart-clients-on-reloadxml" value="true"/>
 
     <!-- Those params are used for all clients if not explicitly set for each one -->
     <param name="context" value="public"/>

Modified: freeswitch/branches/ctrix/mod_airpe/mod_airpe.c
==============================================================================
--- freeswitch/branches/ctrix/mod_airpe/mod_airpe.c	(original)
+++ freeswitch/branches/ctrix/mod_airpe/mod_airpe.c	Mon Jan 19 17:30:28 2009
@@ -41,6 +41,7 @@
     char *context;
     char *destination;
     int  audio_tcp_port_start;
+    int  restart_on_reload;
 
     switch_mutex_t *mutex;
     switch_hash_t  *interfaces_hash;
@@ -51,7 +52,12 @@
 
 
 airpe_interface_t *airpe_find_interface( const char *name) {
-    return switch_core_hash_find(globals.interfaces_hash, name);
+    airpe_interface_t *ret = NULL;
+
+    switch_mutex_lock(globals.mutex);
+    ret = switch_core_hash_find(globals.interfaces_hash, name);
+    switch_mutex_unlock(globals.mutex);
+    return ret;
 }
 
 
@@ -65,10 +71,8 @@
     if ( !globals.last_port_used )
 	globals.last_port_used = globals.audio_tcp_port_start;
 
-    switch_mutex_lock(globals.mutex);
     port = globals.last_port_used;
     globals.last_port_used++;
-    switch_mutex_unlock(globals.mutex);
 
     return port;
 }
@@ -207,12 +211,6 @@
 
 static switch_status_t airpe_destroy_client(airpe_interface_t *airpe) {
 
-    if ( airpe->active_call_id ) {
-	char buf[SKYPE_MSG_LEN] = "";
-	snprintf(buf, sizeof(buf), "ALTER CALL %d END HANGUP", airpe->active_call_id );
-	airpe_cmd_write(airpe, buf);
-    }
-
     if ( airpe->audio_in_socket ) {
 	if ( airpe->debug )
 	    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Shutting down socket on port %d for client %s\n", airpe->audio_in_port, airpe->name);
@@ -235,7 +233,9 @@
     airpe_set_partner_handle(airpe, NULL);
     airpe_clear_call_id(airpe);
 
-    switch_core_hash_destroy(&airpe->variables_hash);
+    if ( airpe->variables_hash )
+	switch_core_hash_destroy(&airpe->variables_hash);
+    airpe->variables_hash = NULL;
 
     return SWITCH_STATUS_SUCCESS;
 }
@@ -638,7 +638,7 @@
     CONFIGURATION
  *****************************************************************************/
 
-static switch_status_t load_config(void) {
+static switch_status_t load_config( int reload ) {
     switch_xml_t    	cfg, 
 			xml, 
 			global_settings, 
@@ -669,7 +669,11 @@
 		globals.destination = switch_core_strdup(pool, val);
 	    } else if (!strcmp(var, "audio-tcp-port_start")) {
 		globals.audio_tcp_port_start = atoi(val);
+	    } else if (!strcmp(var, "restart-clients-on-reloadxml")) {
+		globals.restart_on_reload = switch_true(val);
 	    }
+	    else
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Unknown parameter %s\n", var);
 	}
     }
 
@@ -698,7 +702,8 @@
     	    }
 
 	    if ( (newconf = switch_core_hash_find(globals.interfaces_hash, name)) ) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Duplicated client %s. Skipping\n", name);
+		if ( !reload )
+		    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Duplicated client %s. Skipping\n", name);
 		continue;
 	    }
 
@@ -787,13 +792,14 @@
 	    if ( newconf->skype_window ) {
 		status = airpe_create_client(newconf);
 		if ( status == SWITCH_STATUS_SUCCESS ) {
-		    switch_core_hash_insert(globals.interfaces_hash, name, newconf);
+		    switch_mutex_lock(globals.mutex);
+		    switch_core_hash_insert(globals.interfaces_hash, newconf->name, newconf);
+		    switch_mutex_unlock(globals.mutex);
 		}
 		else {
 		    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Airpe: cannot start client %s.\n", newconf->name);
 		}
 	    }
-
 	}
 
     }
@@ -804,32 +810,35 @@
 
 static void reloadxml_event_handler(switch_event_t *event)
 {
-    airpe_interface_t *airpe;
-    switch_hash_index_t *hi;
-    void *val;
+    airpe_interface_t *airpe = NULL;
+    switch_hash_index_t *hi = NULL;
+    void *val = NULL;
 
     switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Reloading airpe configuration.\n");
 
-    /* First, mark all of them to be reloaded */
-    for (hi = switch_hash_first(NULL, globals.interfaces_hash); hi; hi = switch_hash_next(hi)) {
-	switch_hash_this(hi, NULL, NULL, &val);
-	airpe = ( airpe_interface_t *) val;
-	airpe->should_reload = 1;
+    if ( globals.restart_on_reload ) {
+	switch_mutex_lock(globals.mutex);
+	hi = switch_hash_first(NULL, globals.interfaces_hash);
+	while ( hi ) {
+	    switch_hash_this(hi, NULL, NULL, &val);
+	    airpe = ( airpe_interface_t *) val;
+	    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Stopping airpe client %s\n", airpe->name);
+	    airpe_explicit_hangup(airpe);
+
+	    airpe->running = 0;
+	    while ( airpe->airpe_thread_skype ) {
+		switch_sleep(100000);
+	    }
+	    hi = switch_hash_next(hi);
+    	    switch_core_hash_delete(globals.interfaces_hash, airpe->name ); 
+	    airpe_destroy_client(airpe);
+	}
+	switch_mutex_unlock(globals.mutex);
     }
 
     /* Update the data of each client */
-//    load_config(1);
+    load_config(1);
 
-    /* Find eventual clients not updated and stop them */
-    for (hi = switch_hash_first(NULL, globals.interfaces_hash); hi; hi = switch_hash_next(hi)) {
-	switch_hash_this(hi, NULL, NULL, &val);
-	airpe = ( airpe_interface_t *) val;
-	if ( airpe->should_reload ) {
-	    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Stopping airpe client %s.\n", airpe->name);
-	    switch_core_hash_delete(globals.interfaces_hash, airpe->name); 
-	    airpe_destroy_client(airpe);
-	}
-    }
 }
 
 
@@ -868,10 +877,10 @@
 
     memset(&globals, 0, sizeof(globals));
 
-    switch_core_hash_init(&globals.interfaces_hash, pool);
     switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, pool);
+    switch_core_hash_init(&globals.interfaces_hash, pool);
 
-    load_config();
+    load_config(0);
 
     *module_interface = switch_loadable_module_create_module_interface(pool, modname);
 
@@ -898,28 +907,33 @@
     airpe_interface_t *airpe;
     switch_hash_index_t *hi;
     void *val;
+    const void *var = NULL;
 
     pool = airpe_module_pool;
 
     switch_event_unbind(&EVENT_NODE);
 
-    for (hi = switch_hash_first(NULL, globals.interfaces_hash); hi; hi = switch_hash_next(hi)) {
-	switch_hash_this(hi, NULL, NULL, &val);
+    switch_mutex_lock(globals.mutex);
+    hi = switch_hash_first(NULL, globals.interfaces_hash);
+    while ( hi ) {
+	switch_hash_this(hi, &var, NULL, &val);
 	airpe = ( airpe_interface_t *) val;
-	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Shutting down airpe client: %s\n", airpe->name);
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Shutting down airpe client: %s\n", (char *)var);
+	airpe_explicit_hangup(airpe);
+
 	airpe->running = 0;
-	/* Wake up the socket */
 	airpe_cmd_write(airpe, "PING");
+    	while ( airpe->airpe_thread_skype ) {
+	    switch_sleep(100000);
+	}
+
+	hi = switch_hash_next(hi);
+	switch_core_hash_delete(globals.interfaces_hash, var); 
 	airpe_destroy_client(airpe);
-	/*
-	we won't delete the hash element. we're going to destroy the whole pool 
-	switch_core_hash_delete(globals.interfaces_hash, airpe->name); 
-	*/
     }
+    switch_mutex_unlock(globals.mutex);
     switch_yield(500000);
 
-    /* TODO... should we check that all interfaces has been switched off ? */
-
     switch_core_hash_destroy(&globals.interfaces_hash);
     switch_core_destroy_memory_pool(&pool);
     return SWITCH_STATUS_UNLOAD;

Modified: freeswitch/branches/ctrix/mod_airpe/mod_airpe.h
==============================================================================
--- freeswitch/branches/ctrix/mod_airpe/mod_airpe.h	(original)
+++ freeswitch/branches/ctrix/mod_airpe/mod_airpe.h	Mon Jan 19 17:30:28 2009
@@ -31,7 +31,7 @@
 #include <switch.h>
 #include <switch_version.h>
 
-#define MODNAME "airpe"
+#define MODNAME "mod_airpe"
 
 #define AIRPE_EVENT_AUTHREQUEST "airpe::authrequest"
 #define AIRPE_EVENT_STATUSCHANGE "airpe::statuschange"
@@ -135,6 +135,7 @@
 struct airpe_interface_s {
     char 			*name;
     int  			debug;
+    int  			detached;
     char 			*dialplan;
     char 			*context;
     char 			*destination;
@@ -160,7 +161,6 @@
     CALL_STATUS			call_status;
     CONN_STATUS			conn_status;
 
-    int				should_reload;
     char			*skype_version;
     char			*mood_text;
     int 			active_call_id;



More information about the Freeswitch-svn mailing list