Revision
17514
Author
mrene
Date
2010-05-10 11:33:47 -0500 (Mon, 10 May 2010)

Log Message

mod_sofia: fire an event for gateway ping

Modified Paths

Diff

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h (17513 => 17514)


--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h	2010-05-10 16:19:58 UTC (rev 17513)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h	2010-05-10 16:33:47 UTC (rev 17514)
@@ -332,7 +332,9 @@
 
 typedef enum {
 	SOFIA_GATEWAY_DOWN,
-	SOFIA_GATEWAY_UP
+	SOFIA_GATEWAY_UP,
+
+	SOFIA_GATEWAY_INVALID
 } sofia_gateway_status_t;
 
 typedef enum {
@@ -981,3 +983,7 @@
 int sofia_glue_recover(switch_bool_t flush);
 void sofia_profile_destroy(sofia_profile_t *profile);
 switch_status_t sip_dig_function(_In_opt_z_ const char *cmd, _In_opt_ switch_core_session_t *session, _In_ switch_stream_handle_t *stream);
+const char *sofia_gateway_status_name(sofia_gateway_status_t status);
+void sofia_reg_fire_custom_gateway_state_event(sofia_gateway_t *gateway, int status, const char *phrase);
+
+

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c (17513 => 17514)


--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	2010-05-10 16:19:58 UTC (rev 17513)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	2010-05-10 16:33:47 UTC (rev 17514)
@@ -3589,13 +3589,23 @@
 	return status;
 }
 
+const char *sofia_gateway_status_name(sofia_gateway_status_t status)
+{
+	static const char *status_names[] = { "DOWN", "UP", NULL };
+
+	if (status < SOFIA_GATEWAY_INVALID) {
+		return status_names[status];
+	} else {
+		return "INVALID";
+	}
+}
+
 static void sofia_handle_sip_r_options(switch_core_session_t *session, 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_gateway_t *gateway = NULL;
-	static const char *status_names[] = { "DOWN", "UP", NULL };
 
 	if (sofia_private && !zstr(sofia_private->gateway_name)) {
 		gateway = sofia_reg_find_gateway(sofia_private->gateway_name);
@@ -3611,12 +3621,14 @@
 			if (gateway->ping_count < gateway->ping_max) {
 				gateway->ping_count++;
 
-				if (gateway->ping_count >= 0)
+				if (gateway->ping_count >= 0 && gateway->status != SOFIA_GATEWAY_UP) {
 					gateway->status = SOFIA_GATEWAY_UP;
+					sofia_reg_fire_custom_gateway_state_event(gateway, status, phrase);
+				}
 
 				switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING,
 								  "Ping succeeded %s with code %d - count %d/%d/%d, state %s\n",
-								  gateway->name, status, gateway->ping_min, gateway->ping_count, gateway->ping_max, status_names[gateway->status]);
+								  gateway->name, status, gateway->ping_min, gateway->ping_count, gateway->ping_max, sofia_gateway_status_name(gateway->status));
 			}
 		} else {
 			if (gateway->state == REG_STATE_REGED) {
@@ -3627,13 +3639,15 @@
 			if (gateway->ping_count > gateway->ping_min) {
 				gateway->ping_count--;
 
-				if (gateway->ping_count <= 0)
+				if (gateway->ping_count <= 0 && gateway->status != SOFIA_GATEWAY_DOWN) {
 					gateway->status = SOFIA_GATEWAY_DOWN;
+					sofia_reg_fire_custom_gateway_state_event(gateway, status, phrase);
+				}
 			}
 
 			switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING,
 							  "Ping failed %s with code %d - count %d/%d/%d, state %s\n",
-							  gateway->name, status, gateway->ping_min, gateway->ping_count, gateway->ping_max, status_names[gateway->status]);
+							  gateway->name, status, gateway->ping_min, gateway->ping_count, gateway->ping_max, sofia_gateway_status_name(gateway->status));
 		}
 
 		gateway->ping = switch_epoch_time_now(NULL) + gateway->ping_freq;

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c (17513 => 17514)


--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c	2010-05-10 16:19:58 UTC (rev 17513)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c	2010-05-10 16:33:47 UTC (rev 17514)
@@ -90,12 +90,13 @@
 
 }
 
-static void sofia_reg_fire_custom_gateway_state_event(sofia_gateway_t *gateway, int status, const char *phrase)
+void sofia_reg_fire_custom_gateway_state_event(sofia_gateway_t *gateway, int status, const char *phrase)
 {
 	switch_event_t *s_event;
 	if (switch_event_create_subclass(&s_event, SWITCH_EVENT_CUSTOM, MY_EVENT_GATEWAY_STATE) == SWITCH_STATUS_SUCCESS) {
 		switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "Gateway", gateway->name);
 		switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "State", sofia_state_string(gateway->state));
+		switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "Ping-Status", sofia_gateway_status_name(gateway->status));
 		if (!zstr(phrase)) {
 			switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "Phrase", phrase);
 		}