提交 d9de1807 authored 作者: Anthony Minessale's avatar Anthony Minessale

Add state change i/o hook to the core and change some spidermonkey behaviour.

The most important thing to check is you now must create a new session with a blank constructor:
s = new Session();
then call s.originate() with all the former args that were documented to be for the constructor
this will will return true or false to indicate if the call worked.

See below this sample code demonstrates all of the changes:

////////////////////////////////////////////////////////////////////////////////
function on_hangup(hup_session)
{
    console_log("debug", "HANGUP!!!! name: " + hup_session.name + " cause: " + hup_session.cause + "\n");
	//exit here would end the script so you could cleanup and just be done
	//exit();
}

//set the on_hangup function to be called when this session is hungup
session.setHangupHook(on_hangup);

//allocate a new b_session
var b_session = new Session();

//make a call with b_session.  If this fails, all we will be able to access is the b_session.cause attr
if (b_session.originate(session, "sofia/mydomain.com/888@conference.freeswitch.org")) {
	//Inform the scripting engine to automaticly hang this session up when the script ends
    b_session.setAutoHangup(true);
	//set the on_hangup function to be called when this session is hungup
    b_session.setHangupHook(on_hangup);	
	//bridge session with b_session
    bridge(session, b_session);
} else {
    console_log("debug", "Originate Failed.. cause: " + b_session.cause + "\n");
}
////////////////////////////////////////////////////////////////////////////////






git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@4773 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 f628a159
...@@ -856,6 +856,15 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_add_event_hook_waitfor_write ...@@ -856,6 +856,15 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_add_event_hook_waitfor_write
\return SWITCH_STATUS_SUCCESS on suceess \return SWITCH_STATUS_SUCCESS on suceess
*/ */
SWITCH_DECLARE(switch_status_t) switch_core_session_add_event_hook_send_dtmf(switch_core_session_t *session, switch_send_dtmf_hook_t send_dtmf); SWITCH_DECLARE(switch_status_t) switch_core_session_add_event_hook_send_dtmf(switch_core_session_t *session, switch_send_dtmf_hook_t send_dtmf);
/*!
\brief Add an event hook to be executed when a session receives a state change signal
\param session session to bind hook to
\param state_change hook to bind
\return SWITCH_STATUS_SUCCESS on suceess
*/
SWITCH_DECLARE(switch_status_t) switch_core_session_add_event_hook_state_change(switch_core_session_t *session,
switch_answer_channel_hook_t state_change);
///\} ///\}
///\defgroup hashf Hash Functions ///\defgroup hashf Hash Functions
......
...@@ -145,6 +145,13 @@ struct switch_io_event_hook_send_dtmf { ...@@ -145,6 +145,13 @@ struct switch_io_event_hook_send_dtmf {
struct switch_io_event_hook_send_dtmf *next; struct switch_io_event_hook_send_dtmf *next;
}; };
/*! \brief Node in which to store state change callback hooks */
struct switch_io_event_hook_state_change {
/*! the send dtmf channel callback hook*/
switch_state_change_hook_t state_change;
struct switch_io_event_hook_state_change *next;
};
/*! \brief A table of lists of io_event_hooks to store the event hooks associated with a session */ /*! \brief A table of lists of io_event_hooks to store the event hooks associated with a session */
struct switch_io_event_hooks { struct switch_io_event_hooks {
/*! a list of outgoing channel hooks */ /*! a list of outgoing channel hooks */
...@@ -167,6 +174,8 @@ struct switch_io_event_hooks { ...@@ -167,6 +174,8 @@ struct switch_io_event_hooks {
switch_io_event_hook_waitfor_write_t *waitfor_write; switch_io_event_hook_waitfor_write_t *waitfor_write;
/*! a list of send dtmf hooks */ /*! a list of send dtmf hooks */
switch_io_event_hook_send_dtmf_t *send_dtmf; switch_io_event_hook_send_dtmf_t *send_dtmf;
/*! a list of state change hooks */
switch_io_event_hook_state_change_t *state_change;
}; };
/*! \brief A table of i/o routines that an endpoint interface can implement */ /*! \brief A table of i/o routines that an endpoint interface can implement */
...@@ -191,6 +200,8 @@ struct switch_io_routines { ...@@ -191,6 +200,8 @@ struct switch_io_routines {
switch_status_t (*receive_message)(switch_core_session_t *, switch_core_session_message_t *); switch_status_t (*receive_message)(switch_core_session_t *, switch_core_session_message_t *);
/*! queue a message for another session*/ /*! queue a message for another session*/
switch_status_t (*receive_event)(switch_core_session_t *, switch_event_t *); switch_status_t (*receive_event)(switch_core_session_t *, switch_event_t *);
/*! change a sessions channel state */
switch_status_t (*state_change)(switch_core_session_t *);
}; };
/*! \brief Abstraction of an module endpoint interface /*! \brief Abstraction of an module endpoint interface
......
...@@ -954,6 +954,7 @@ typedef struct switch_io_event_hook_kill_channel switch_io_event_hook_kill_chann ...@@ -954,6 +954,7 @@ typedef struct switch_io_event_hook_kill_channel switch_io_event_hook_kill_chann
typedef struct switch_io_event_hook_waitfor_read switch_io_event_hook_waitfor_read_t; typedef struct switch_io_event_hook_waitfor_read switch_io_event_hook_waitfor_read_t;
typedef struct switch_io_event_hook_waitfor_write switch_io_event_hook_waitfor_write_t; typedef struct switch_io_event_hook_waitfor_write switch_io_event_hook_waitfor_write_t;
typedef struct switch_io_event_hook_send_dtmf switch_io_event_hook_send_dtmf_t; typedef struct switch_io_event_hook_send_dtmf switch_io_event_hook_send_dtmf_t;
typedef struct switch_io_event_hook_state_change switch_io_event_hook_state_change_t;
typedef struct switch_io_routines switch_io_routines_t; typedef struct switch_io_routines switch_io_routines_t;
typedef struct switch_io_event_hooks switch_io_event_hooks_t; typedef struct switch_io_event_hooks switch_io_event_hooks_t;
typedef struct switch_speech_handle switch_speech_handle_t; typedef struct switch_speech_handle switch_speech_handle_t;
...@@ -989,6 +990,7 @@ typedef switch_status_t (*switch_kill_channel_hook_t)(switch_core_session_t *, i ...@@ -989,6 +990,7 @@ typedef switch_status_t (*switch_kill_channel_hook_t)(switch_core_session_t *, i
typedef switch_status_t (*switch_waitfor_read_hook_t)(switch_core_session_t *, int, int); typedef switch_status_t (*switch_waitfor_read_hook_t)(switch_core_session_t *, int, int);
typedef switch_status_t (*switch_waitfor_write_hook_t)(switch_core_session_t *, int, int); typedef switch_status_t (*switch_waitfor_write_hook_t)(switch_core_session_t *, int, int);
typedef switch_status_t (*switch_send_dtmf_hook_t)(switch_core_session_t *, char *); typedef switch_status_t (*switch_send_dtmf_hook_t)(switch_core_session_t *, char *);
typedef switch_status_t (*switch_state_change_hook_t)(switch_core_session_t *);
typedef struct switch_stream_handle switch_stream_handle_t; typedef struct switch_stream_handle switch_stream_handle_t;
typedef switch_status_t (*switch_stream_handle_write_function_t)(switch_stream_handle_t *handle, const char *fmt, ...); typedef switch_status_t (*switch_stream_handle_write_function_t)(switch_stream_handle_t *handle, const char *fmt, ...);
typedef switch_status_t (*switch_api_function_t)(char *in, switch_core_session_t *session, switch_stream_handle_t *stream); typedef switch_status_t (*switch_api_function_t)(char *in, switch_core_session_t *session, switch_stream_handle_t *stream);
......
...@@ -132,6 +132,8 @@ struct js_session { ...@@ -132,6 +132,8 @@ struct js_session {
JSContext *cx; JSContext *cx;
JSObject *obj; JSObject *obj;
unsigned int flags; unsigned int flags;
switch_call_cause_t cause;
JSFunction *on_hangup;
}; };
......
...@@ -185,7 +185,6 @@ static void switch_core_standard_on_hold(switch_core_session_t *session); ...@@ -185,7 +185,6 @@ static void switch_core_standard_on_hold(switch_core_session_t *session);
/* The main runtime obj we keep this hidden for ourselves */ /* The main runtime obj we keep this hidden for ourselves */
static struct switch_core_runtime runtime; static struct switch_core_runtime runtime;
static void db_pick_path(char *dbname, char *buf, switch_size_t size) static void db_pick_path(char *dbname, char *buf, switch_size_t size)
{ {
...@@ -1835,20 +1834,22 @@ SWITCH_DECLARE(switch_call_cause_t) switch_core_session_outgoing_channel(switch_ ...@@ -1835,20 +1834,22 @@ SWITCH_DECLARE(switch_call_cause_t) switch_core_session_outgoing_channel(switch_
SWITCH_DECLARE(switch_status_t) switch_core_session_answer_channel(switch_core_session_t *session) SWITCH_DECLARE(switch_status_t) switch_core_session_answer_channel(switch_core_session_t *session)
{ {
switch_io_event_hook_answer_channel_t *ptr; switch_io_event_hook_answer_channel_t *ptr;
switch_status_t status = SWITCH_STATUS_FALSE; switch_status_t status = SWITCH_STATUS_SUCCESS;
assert(session != NULL); assert(session != NULL);
if (session->endpoint_interface->io_routines->answer_channel) { if (session->endpoint_interface->io_routines->answer_channel) {
if ((status = session->endpoint_interface->io_routines->answer_channel(session)) == SWITCH_STATUS_SUCCESS) { status = session->endpoint_interface->io_routines->answer_channel(session);
for (ptr = session->event_hooks.answer_channel; ptr; ptr = ptr->next) { }
if ((status = ptr->answer_channel(session)) != SWITCH_STATUS_SUCCESS) {
break; if (status == SWITCH_STATUS_SUCCESS) {
} for (ptr = session->event_hooks.answer_channel; ptr; ptr = ptr->next) {
if ((status = ptr->answer_channel(session)) != SWITCH_STATUS_SUCCESS) {
break;
} }
} }
} else {
status = SWITCH_STATUS_SUCCESS;
} }
return status; return status;
} }
...@@ -1856,19 +1857,23 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_answer_channel(switch_core_s ...@@ -1856,19 +1857,23 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_answer_channel(switch_core_s
SWITCH_DECLARE(switch_status_t) switch_core_session_receive_message(switch_core_session_t *session, switch_core_session_message_t *message) SWITCH_DECLARE(switch_status_t) switch_core_session_receive_message(switch_core_session_t *session, switch_core_session_message_t *message)
{ {
switch_io_event_hook_receive_message_t *ptr; switch_io_event_hook_receive_message_t *ptr;
switch_status_t status = SWITCH_STATUS_FALSE; switch_status_t status = SWITCH_STATUS_SUCCESS;
assert(session != NULL); assert(session != NULL);
if (session->endpoint_interface->io_routines->receive_message) { if (session->endpoint_interface->io_routines->receive_message) {
if ((status = status = session->endpoint_interface->io_routines->receive_message(session, message);
session->endpoint_interface->io_routines->receive_message(session, message)) == SWITCH_STATUS_SUCCESS) {
for (ptr = session->event_hooks.receive_message; ptr; ptr = ptr->next) { }
if ((status = ptr->receive_message(session, message)) != SWITCH_STATUS_SUCCESS) {
break; if (status == SWITCH_STATUS_SUCCESS) {
} for (ptr = session->event_hooks.receive_message; ptr; ptr = ptr->next) {
if ((status = ptr->receive_message(session, message)) != SWITCH_STATUS_SUCCESS) {
break;
} }
} }
} }
switch_core_session_kill_channel(session, SWITCH_SIG_BREAK); switch_core_session_kill_channel(session, SWITCH_SIG_BREAK);
return status; return status;
} }
...@@ -2874,6 +2879,31 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_add_event_hook_answer_channe ...@@ -2874,6 +2879,31 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_add_event_hook_answer_channe
} }
SWITCH_DECLARE(switch_status_t) switch_core_session_add_event_hook_state_change(switch_core_session_t *session,
switch_answer_channel_hook_t state_change)
{
switch_io_event_hook_state_change_t *hook, *ptr;
assert(state_change != NULL);
if ((hook = switch_core_session_alloc(session, sizeof(*hook))) != 0) {
hook->state_change = state_change;
if (!session->event_hooks.state_change) {
session->event_hooks.state_change = hook;
} else {
for (ptr = session->event_hooks.state_change; ptr && ptr->next; ptr = ptr->next);
ptr->next = hook;
}
return SWITCH_STATUS_SUCCESS;
}
return SWITCH_STATUS_MEMERR;
}
SWITCH_DECLARE(switch_status_t) switch_core_session_add_event_hook_read_frame(switch_core_session_t *session, SWITCH_DECLARE(switch_status_t) switch_core_session_add_event_hook_read_frame(switch_core_session_t *session,
switch_read_frame_hook_t read_frame) switch_read_frame_hook_t read_frame)
{ {
...@@ -3216,13 +3246,26 @@ static void switch_core_standard_on_hibernate(switch_core_session_t *session) ...@@ -3216,13 +3246,26 @@ static void switch_core_standard_on_hibernate(switch_core_session_t *session)
SWITCH_DECLARE(void) switch_core_session_signal_state_change(switch_core_session_t *session) SWITCH_DECLARE(void) switch_core_session_signal_state_change(switch_core_session_t *session)
{ {
switch_status_t status = SWITCH_STATUS_SUCCESS;
switch_io_event_hook_state_change_t *ptr;
/* If trylock fails the signal is already awake so we needn't bother */ /* If trylock fails the signal is already awake so we needn't bother */
if (switch_mutex_trylock(session->mutex) == SWITCH_STATUS_SUCCESS) { if (switch_mutex_trylock(session->mutex) == SWITCH_STATUS_SUCCESS) {
switch_thread_cond_signal(session->cond); switch_thread_cond_signal(session->cond);
switch_mutex_unlock(session->mutex); switch_mutex_unlock(session->mutex);
} }
if (session->endpoint_interface->io_routines->state_change) {
status = session->endpoint_interface->io_routines->state_change(session);
}
if (status == SWITCH_STATUS_SUCCESS) {
for (ptr = session->event_hooks.state_change; ptr; ptr = ptr->next) {
if ((status = ptr->state_change(session)) != SWITCH_STATUS_SUCCESS) {
break;
}
}
}
} }
SWITCH_DECLARE(unsigned int) switch_core_session_running(switch_core_session_t *session) SWITCH_DECLARE(unsigned int) switch_core_session_running(switch_core_session_t *session)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论