Skip to content

Commit

Permalink
use freeswitch async api in originate (#6196)
Browse files Browse the repository at this point in the history
  • Loading branch information
lazedo authored and mark2600 committed Dec 12, 2019
1 parent 9a1fd9d commit 435831d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
20 changes: 9 additions & 11 deletions applications/ecallmgr/src/ecallmgr_originate.erl
Original file line number Diff line number Diff line change
Expand Up @@ -546,22 +546,20 @@ add_loopback('false') ->
-spec originate_execute(atom(), kz_term:ne_binary(), pos_integer()) ->
{'ok', kz_term:ne_binary()} |
{'error', kz_term:ne_binary() | 'timeout' | 'crash'}.
originate_execute(Node, Dialstrings, Timeout) ->
originate_execute(Node, Dialstrings, _Timeout) ->
lager:debug("executing originate on ~s: ~s", [Node, Dialstrings]),
case freeswitch:api(Node
,'originate'
,kz_term:to_list(Dialstrings)
,Timeout * ?MILLISECONDS_IN_SECOND + 2000
)
case freeswitch:async_api(Node
,'originate'
,kz_term:to_list(Dialstrings)
)
of
{'ok', <<"+OK ", ID/binary>>} ->
UUID = kz_binary:strip(binary:replace(ID, <<"\n">>, <<>>)),
{'ok', UUID} ->
Media = get('hold_media'),
_Pid = kz_util:spawn(fun set_music_on_hold/3, [Node, UUID, Media]),
{'ok', UUID};
{'ok', Other} ->
lager:debug("recv other 'ok': ~s", [Other]),
{'error', kz_binary:strip(binary:replace(Other, <<"\n">>, <<>>))};
'ok' ->
lager:debug("recv other 'ok'"),
{'error', 'empty_response'};
{'error', Error} when is_binary(Error) ->
lager:debug("error originating: ~s", [Error]),
{'error', kz_binary:strip(binary:replace(Error, <<"\n">>, <<>>))};
Expand Down
6 changes: 6 additions & 0 deletions applications/ecallmgr/src/freeswitch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
,bgapi4/5
]).

-export([async_api/3]).

-include("ecallmgr.hrl").

-define(TIMEOUT, 5 * ?MILLISECONDS_IN_SECOND).
Expand Down Expand Up @@ -140,3 +142,7 @@ config(Node) -> ?FS_MODULE:config(Node).
{'ok', binary()} |
{'error', 'timeout' | 'exception' | binary()}.
bgapi4(Node, Cmd, Args, Fun, CallBackParams) -> ?FS_MODULE:bgapi4(Node, Cmd, Args, Fun, CallBackParams).

-spec async_api(atom(), atom(), string() | binary()) -> fs_api_return().
async_api(Node, Cmd, Args) -> ?FS_MODULE:async_api(Node, Cmd, Args).

41 changes: 41 additions & 0 deletions applications/ecallmgr/src/mod_kazoo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
,bgapi4/5
]).

-export([async_api/3]).

-include("ecallmgr.hrl").

-define(TIMEOUT, 5 * ?MILLISECONDS_IN_SECOND).
Expand Down Expand Up @@ -133,6 +135,27 @@ fetch_reply(Node, FetchID, Section, Reply, Timeout) ->
{'error', 'exception'}
end.

api_result(Result, 'undefined') -> Result;
api_result(Result, Bin) ->
case kz_binary:strip_left(kz_binary:strip_right(Bin, <<"\n">>), $\s) of
<<>> when Result =:= 'error' -> {error, 'failed'};
<<"true">> -> {Result, true};
<<"false">> -> {Result, false};
<<>> -> ok;
Msg -> {Result, maybe_number(Msg, byte_size(Msg))}
end.

maybe_number(Msg, Size)
when Size < 10 ->
Float = (catch erlang:binary_to_float(Msg)),
Int = (catch erlang:binary_to_integer(Msg)),
case {is_number(Float), is_number(Int)} of
{'true', _} -> Float;
{_, 'true'} -> Int;
_ -> Msg
end;
maybe_number(Msg, _Size) -> Msg.

-spec api(atom(), kz_term:text()) -> fs_api_return().
api(Node, Cmd) ->
api(Node, Cmd, "").
Expand Down Expand Up @@ -387,3 +410,21 @@ bgapi4(Node, Cmd, Args, Fun, CallBackParams) ->
internal_fs_error(Reason) ->
Error = kz_binary:strip(binary:replace(Reason, <<"\n">>, <<>>)),
{'error', Error}.

%%------------------------------------------------------------------------------
%% @doc Make a background API call to FreeSWITCH and wait for reply.
%% @end
%%------------------------------------------------------------------------------
-spec async_api(atom(), atom(), string() | binary()) -> freeswitch:fs_api_return().
async_api(Node, Cmd, Args) ->
case bgapi(Node, Cmd, Args) of
{'error', _} = Error -> Error;
{'ok', JobId} ->
receive
{'bgok', JobId, <<"-ERR", Reason/binary>>} -> api_result('error', Reason);
{'bgok', JobId, <<"+OK", Result/binary>>} -> api_result('ok', Result);
{'bgok', JobId, Result} -> api_result('ok', Result);
{'bgerror', JobId, Error} -> api_result('error', Error)
end
end.

0 comments on commit 435831d

Please sign in to comment.