Make handlecall return more correct errors; sync freeswitch.erl with git version
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12592 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
parent
7e3b26eaae
commit
5e1fcbd1bf
|
@ -14,13 +14,13 @@
|
||||||
|
|
||||||
-module(freeswitch).
|
-module(freeswitch).
|
||||||
|
|
||||||
-export([send/2, api/3, bgapi/3, event/2,
|
-export([send/2, api/3, api/2, bgapi/3, bgapi/4, event/2,
|
||||||
nixevent/2, noevents/1, close/1,
|
nixevent/2, noevents/1, close/1,
|
||||||
get_event_header/2, get_event_body/1,
|
get_event_header/2, get_event_body/1,
|
||||||
get_event_name/1, getpid/1, sendmsg/3,
|
get_event_name/1, getpid/1, sendmsg/3,
|
||||||
sendevent/3, handlecall/2, start_fetch_handler/4,
|
sendevent/3, handlecall/2, handlecall/3, start_fetch_handler/4,
|
||||||
start_log_handler/3, start_event_handler/3]).
|
start_log_handler/3, start_event_handler/3]).
|
||||||
-define(TIMEOUT, 10000).
|
-define(TIMEOUT, 5000).
|
||||||
|
|
||||||
%% @doc Return the value for a specific header in an event or `{error,notfound}'.
|
%% @doc Return the value for a specific header in an event or `{error,notfound}'.
|
||||||
get_event_header([], _Needle) ->
|
get_event_header([], _Needle) ->
|
||||||
|
@ -63,11 +63,18 @@ send(Node, Term) ->
|
||||||
api(Node, Cmd, Args) ->
|
api(Node, Cmd, Args) ->
|
||||||
{api, Node} ! {api, Cmd, Args},
|
{api, Node} ! {api, Cmd, Args},
|
||||||
receive
|
receive
|
||||||
X -> X
|
{ok, X} ->
|
||||||
|
{ok, X};
|
||||||
|
{error, X} ->
|
||||||
|
{error, X}
|
||||||
after ?TIMEOUT ->
|
after ?TIMEOUT ->
|
||||||
timeout
|
timeout
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%% @doc Same as @link{api/3} except there's no additional arguments.
|
||||||
|
api(Node, Cmd) ->
|
||||||
|
api(Node, Cmd, "").
|
||||||
|
|
||||||
%% @doc Make a backgrounded API call to FreeSWITCH. The asynchronous reply is
|
%% @doc Make a backgrounded API call to FreeSWITCH. The asynchronous reply is
|
||||||
%% sent to calling process after it is received. This function
|
%% sent to calling process after it is received. This function
|
||||||
%% returns the result of the initial bgapi call or `timeout' if FreeSWITCH fails
|
%% returns the result of the initial bgapi call or `timeout' if FreeSWITCH fails
|
||||||
|
@ -104,11 +111,48 @@ bgapi(Node, Cmd, Args) ->
|
||||||
{api, X} -> X
|
{api, X} -> X
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%% @doc Make a backgrounded API call to FreeSWITCH. The asynchronous reply is
|
||||||
|
%% passed as the argument to `Fun' after it is received. This function
|
||||||
|
%% returns the result of the initial bgapi call or `timeout' if FreeSWITCH fails
|
||||||
|
%% to respond.
|
||||||
|
bgapi(Node, Cmd, Args, Fun) ->
|
||||||
|
Self = self(),
|
||||||
|
% spawn a new process so that both responses go here instead of directly to
|
||||||
|
% the calling process.
|
||||||
|
spawn(fun() ->
|
||||||
|
{bgapi, Node} ! {bgapi, Cmd, Args},
|
||||||
|
receive
|
||||||
|
{error, Reason} ->
|
||||||
|
% send the error condition to the calling process
|
||||||
|
Self ! {api, {error, Reason}};
|
||||||
|
{ok, JobID} ->
|
||||||
|
% send the reply to the calling process
|
||||||
|
Self ! {api, ok},
|
||||||
|
receive % wait for the job's reply
|
||||||
|
{bgok, JobID, Reply} ->
|
||||||
|
% Call the function with the reply
|
||||||
|
Fun(ok, Reply);
|
||||||
|
{bgerror, JobID, Reply} ->
|
||||||
|
Fun(error, Reply)
|
||||||
|
end
|
||||||
|
after ?TIMEOUT ->
|
||||||
|
% send a timeout to the calling process
|
||||||
|
Self ! {api, timeout}
|
||||||
|
end
|
||||||
|
end),
|
||||||
|
|
||||||
|
% get the initial result of the command, NOT the asynchronous response, and
|
||||||
|
% return it
|
||||||
|
receive
|
||||||
|
{api, X} -> X
|
||||||
|
end.
|
||||||
|
|
||||||
%% @doc Request to receive any events in the list `List'.
|
%% @doc Request to receive any events in the list `List'.
|
||||||
event(Node, Events) when is_list(Events) ->
|
event(Node, Events) when is_list(Events) ->
|
||||||
{event, Node} ! list_to_tuple(lists:append([event], Events)),
|
{event, Node} ! list_to_tuple(lists:append([event], Events)),
|
||||||
receive
|
receive
|
||||||
X -> X
|
ok -> ok;
|
||||||
|
{error, Reason} -> {error, Reason}
|
||||||
after ?TIMEOUT ->
|
after ?TIMEOUT ->
|
||||||
timeout
|
timeout
|
||||||
end;
|
end;
|
||||||
|
@ -130,7 +174,8 @@ nixevent(Node, Event) when is_atom(Event) ->
|
||||||
noevents(Node) ->
|
noevents(Node) ->
|
||||||
{noevents, Node} ! noevents,
|
{noevents, Node} ! noevents,
|
||||||
receive
|
receive
|
||||||
X -> X
|
ok -> ok;
|
||||||
|
{error, Reason} -> {error, Reason}
|
||||||
after ?TIMEOUT ->
|
after ?TIMEOUT ->
|
||||||
timeout
|
timeout
|
||||||
end.
|
end.
|
||||||
|
@ -139,7 +184,7 @@ noevents(Node) ->
|
||||||
close(Node) ->
|
close(Node) ->
|
||||||
{close, Node} ! exit,
|
{close, Node} ! exit,
|
||||||
receive
|
receive
|
||||||
X -> X
|
ok -> ok
|
||||||
after ?TIMEOUT ->
|
after ?TIMEOUT ->
|
||||||
timeout
|
timeout
|
||||||
end.
|
end.
|
||||||
|
@ -150,7 +195,8 @@ close(Node) ->
|
||||||
sendevent(Node, EventName, Headers) ->
|
sendevent(Node, EventName, Headers) ->
|
||||||
{sendevent, Node} ! {sendevent, EventName, Headers},
|
{sendevent, Node} ! {sendevent, EventName, Headers},
|
||||||
receive
|
receive
|
||||||
X -> X
|
ok -> ok;
|
||||||
|
{error, Reason} -> {error, Reason}
|
||||||
after ?TIMEOUT ->
|
after ?TIMEOUT ->
|
||||||
timeout
|
timeout
|
||||||
end.
|
end.
|
||||||
|
@ -160,7 +206,8 @@ sendevent(Node, EventName, Headers) ->
|
||||||
sendmsg(Node, UUID, Headers) ->
|
sendmsg(Node, UUID, Headers) ->
|
||||||
{sendmsg, Node} ! {sendmsg, UUID, Headers},
|
{sendmsg, Node} ! {sendmsg, UUID, Headers},
|
||||||
receive
|
receive
|
||||||
X -> X
|
ok -> ok;
|
||||||
|
{error, Reason} -> {error, Reason}
|
||||||
after ?TIMEOUT ->
|
after ?TIMEOUT ->
|
||||||
timeout
|
timeout
|
||||||
end.
|
end.
|
||||||
|
@ -171,17 +218,29 @@ sendmsg(Node, UUID, Headers) ->
|
||||||
getpid(Node) ->
|
getpid(Node) ->
|
||||||
{getpid, Node} ! getpid,
|
{getpid, Node} ! getpid,
|
||||||
receive
|
receive
|
||||||
X -> X
|
{ok, Pid} when is_pid(Pid) -> {ok, Pid}
|
||||||
after ?TIMEOUT ->
|
after ?TIMEOUT ->
|
||||||
timeout
|
timeout
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% @doc Request that FreeSWITCH send any events pertaining to call `UUID' to
|
%% @doc Request that FreeSWITCH send any events pertaining to call `UUID' to
|
||||||
%% `Process' where process is a registered process name.
|
%% `Process' where process is a registered process name.
|
||||||
handlecall(Node, Process) ->
|
handlecall(Node, UUID, Process) ->
|
||||||
{handlecall, Node} ! {handlecall, Process},
|
{handlecall, Node} ! {handlecall, UUID, Process},
|
||||||
receive
|
receive
|
||||||
X -> X
|
ok -> ok;
|
||||||
|
{error, Reason} -> {error, Reason}
|
||||||
|
after ?TIMEOUT ->
|
||||||
|
timeout
|
||||||
|
end.
|
||||||
|
|
||||||
|
%% @doc Request that FreeSWITCH send any events pertaining to call `UUID' to
|
||||||
|
%% the calling process.
|
||||||
|
handlecall(Node, UUID) ->
|
||||||
|
{handlecall, Node} ! {handlecall, UUID},
|
||||||
|
receive
|
||||||
|
ok -> ok;
|
||||||
|
{error, Reason} -> {error, Reason}
|
||||||
after ?TIMEOUT ->
|
after ?TIMEOUT ->
|
||||||
timeout
|
timeout
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -568,11 +568,17 @@ static switch_status_t handle_msg_handlecall(listener_t *listener, erlang_msg *m
|
||||||
ei_x_encode_atom(rbuf, "badarg");
|
ei_x_encode_atom(rbuf, "badarg");
|
||||||
} else {
|
} else {
|
||||||
switch_core_session_t *session;
|
switch_core_session_t *session;
|
||||||
if (!switch_strlen_zero_buf(uuid_str) && (session = switch_core_session_locate(uuid_str))) {
|
if (!switch_strlen_zero_buf(uuid_str) && SWITCH_UUID_FORMATTED_LENGTH == strlen(uuid_str)) {
|
||||||
/* create a new session list element and attach it to this listener */
|
if ((session = switch_core_session_locate(uuid_str))) {
|
||||||
if ((arity==2 && attach_call_to_pid(listener, &msg->from, session)) ||
|
/* create a new session list element and attach it to this listener */
|
||||||
(arity==3 && attach_call_to_registered_process(listener, reg_name, session))) {
|
if ((arity==2 && attach_call_to_pid(listener, &msg->from, session)) ||
|
||||||
ei_x_encode_atom(rbuf, "ok");
|
(arity==3 && attach_call_to_registered_process(listener, reg_name, session))) {
|
||||||
|
ei_x_encode_atom(rbuf, "ok");
|
||||||
|
} else {
|
||||||
|
ei_x_encode_tuple_header(rbuf, 2);
|
||||||
|
ei_x_encode_atom(rbuf, "error");
|
||||||
|
ei_x_encode_atom(rbuf, "session_attach_failed");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ei_x_encode_tuple_header(rbuf, 2);
|
ei_x_encode_tuple_header(rbuf, 2);
|
||||||
ei_x_encode_atom(rbuf, "error");
|
ei_x_encode_atom(rbuf, "error");
|
||||||
|
|
Loading…
Reference in New Issue