[Freeswitch-svn] [commit] r10089 - in freeswitch/trunk/src: . include include/private mod/applications/mod_commands

Freeswitch SVN anthm at freeswitch.org
Tue Oct 21 12:22:39 EDT 2008


Author: anthm
Date: Tue Oct 21 12:22:38 2008
New Revision: 10089

Modified:
   freeswitch/trunk/src/include/private/switch_core_pvt.h
   freeswitch/trunk/src/include/switch_core.h
   freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c
   freeswitch/trunk/src/switch_core_io.c
   freeswitch/trunk/src/switch_core_session.c

Log:
add scheduler support for heartbeat

Modified: freeswitch/trunk/src/include/private/switch_core_pvt.h
==============================================================================
--- freeswitch/trunk/src/include/private/switch_core_pvt.h	(original)
+++ freeswitch/trunk/src/include/private/switch_core_pvt.h	Tue Oct 21 12:22:38 2008
@@ -153,6 +153,7 @@
 	switch_codec_t bug_codec;
 	uint32_t read_frame_count;
 	uint32_t track_duration;
+	uint32_t track_id;
 };
 
 struct switch_media_bug {

Modified: freeswitch/trunk/src/include/switch_core.h
==============================================================================
--- freeswitch/trunk/src/include/switch_core.h	(original)
+++ freeswitch/trunk/src/include/switch_core.h	Tue Oct 21 12:22:38 2008
@@ -119,6 +119,9 @@
 ///\{
 
 
+SWITCH_DECLARE(void) switch_core_session_sched_heartbeat(switch_core_session_t *session, uint32_t seconds);
+SWITCH_DECLARE(void) switch_core_session_unsched_heartbeat(switch_core_session_t *session);
+
 SWITCH_DECLARE(void) switch_core_session_enable_heartbeat(switch_core_session_t *session, uint32_t seconds);
 SWITCH_DECLARE(void) switch_core_session_disable_heartbeat(switch_core_session_t *session);
 

Modified: freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c	Tue Oct 21 12:22:38 2008
@@ -2352,13 +2352,14 @@
 	return SWITCH_STATUS_SUCCESS;
 }
 
-#define HEARTBEAT_SYNTAX "<uuid> <on|off|<seconds>>"
+#define HEARTBEAT_SYNTAX "<uuid> [sched] <on|off|<seconds>>"
 SWITCH_STANDARD_API(uuid_session_heartbeat_function)
 {
-	char *mycmd = NULL, *argv[2] = { 0 };
+	char *mycmd = NULL, *argv[3] = { 0 };
 	uint32_t seconds = 60;
 	int argc, tmp;
 	switch_core_session_t *l_session = NULL;
+	int x = 0, sched = 0;
 
 	if (switch_strlen_zero(cmd) || !(mycmd = strdup(cmd))) {
 		goto error;
@@ -2366,26 +2367,38 @@
 
 	argc = switch_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
 
-	if (argc != 2 || !argv[0]) {
+	if (argc < 2 || !argv[0]) {
 		goto error;
 	}
-
+	
 	if (!(l_session = switch_core_session_locate(argv[0]))) {
 		stream->write_function(stream, "-ERR Usage: cannot locate session.\n");
 		return SWITCH_STATUS_SUCCESS;
 	}
+	
+	if (!strcasecmp(argv[1], "sched")) {
+		x = 2;
+		sched++;
+	} else {
+		x = 1;
+	}
 
-	if (switch_is_number(argv[1])) {
-		tmp = atoi(argv[1]);
+	if (switch_is_number(argv[x])) {
+		tmp = atoi(argv[x]);
 		if (tmp > 0) {
 			seconds = tmp;
 		}
-	} else if (!switch_true(argv[1])) {
+	} else if (!switch_true(argv[x])) {
 		seconds = 0;
 	}
 	
 	if (seconds) {
-		switch_core_session_enable_heartbeat(l_session, seconds);
+		if (sched) {
+			switch_core_session_sched_heartbeat(l_session, seconds);
+		} else {
+			switch_core_session_enable_heartbeat(l_session, seconds);
+		}
+
 	} else {
 		switch_core_session_disable_heartbeat(l_session);
 	}

Modified: freeswitch/trunk/src/switch_core_io.c
==============================================================================
--- freeswitch/trunk/src/switch_core_io.c	(original)
+++ freeswitch/trunk/src/switch_core_io.c	Tue Oct 21 12:22:38 2008
@@ -122,7 +122,7 @@
 
 	*frame = NULL;
 
-	if (session->read_codec && session->track_duration) {
+	if (session->read_codec && !session->track_id && session->track_duration) {
 		if (session->read_frame_count == 0) {
 			switch_event_t *event;
 			session->read_frame_count = (session->read_codec->implementation->actual_samples_per_second / 

Modified: freeswitch/trunk/src/switch_core_session.c
==============================================================================
--- freeswitch/trunk/src/switch_core_session.c	(original)
+++ freeswitch/trunk/src/switch_core_session.c	Tue Oct 21 12:22:38 2008
@@ -780,6 +780,39 @@
 
 }
 
+SWITCH_STANDARD_SCHED_FUNC(sch_heartbeat_callback)
+{
+	switch_event_t *event;
+	switch_core_session_t *session;
+	char *uuid = task->cmd_arg;
+	
+	if ((session = switch_core_session_locate(uuid))) {
+		switch_event_create(&event, SWITCH_EVENT_SESSION_HEARTBEAT);
+		switch_channel_event_set_data(session->channel, event);
+		switch_event_fire(&event);
+		switch_core_session_rwunlock(session);
+	}
+
+	/* reschedule this task */
+	task->runtime = switch_timestamp(NULL) + session->track_duration;
+}
+
+SWITCH_DECLARE(void) switch_core_session_unsched_heartbeat(switch_core_session_t *session)
+{
+	if (session->track_id) {
+		switch_scheduler_del_task_id(session->track_id);
+		session->track_id = 0;
+	}
+}
+
+SWITCH_DECLARE(void) switch_core_session_sched_heartbeat(switch_core_session_t *session, uint32_t seconds)
+{
+
+	switch_core_session_unsched_heartbeat(session);
+	session->track_id = switch_scheduler_add_task(switch_timestamp(NULL), sch_heartbeat_callback, (char *) __SWITCH_FUNC__, 
+												  switch_core_session_get_uuid(session), 0, strdup(switch_core_session_get_uuid(session)), SSHF_FREE_ARG);
+}
+
 SWITCH_DECLARE(void) switch_core_session_enable_heartbeat(switch_core_session_t *session, uint32_t seconds)
 {
 	switch_assert(session != NULL);
@@ -788,17 +821,28 @@
 		seconds = 60;
 	}
 
+	if (switch_channel_test_flag(session->channel,  CF_PROXY_MODE)) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s using scheduler due to bypass_media mode\n", switch_channel_get_name(session->channel));
+		switch_core_session_sched_heartbeat(session, seconds);
+		return;
+	}
+
+	switch_core_session_unsched_heartbeat(session);
+	
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s setting session heartbeat to %u second(s).\n", 
 					  switch_channel_get_name(session->channel), seconds);
 	session->track_duration = seconds;
 	session->read_frame_count = 0;
+	
 }
 
 SWITCH_DECLARE(void) switch_core_session_disable_heartbeat(switch_core_session_t *session)
 {
+	switch_core_session_unsched_heartbeat(session);
 	switch_assert(session != NULL);
 	session->read_frame_count = 0;
 	session->track_duration = 0;
+	
 }
 
 static void *SWITCH_THREAD_FUNC switch_core_session_thread(switch_thread_t *thread, void *obj)



More information about the Freeswitch-svn mailing list