%% Author: mark
%% Created: Sep 15, 2009
%% Description: TODO: Add description to freeswitch_callback
-module(freeswitch_callback).
-behaviour(gen_server).
-record(st, {fsnode, pbxpid}).
-export([start/2, terminate/2, code_change/3, init/1,
handle_call/3, handle_cast/2, handle_info/2,
xml_fetch/1]).
start(Node, Section) ->
gen_server:start(?MODULE, [Node, Section], []).
init([Node, Section]) ->
io:format( "freeswitch_callback:init( [Node=~w, Section=~w])~n", [Node, Section] ),
{api, Node} ! {bind, Section},
{ok, #st{fsnode=Node}}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
dumpParams( Params ) ->
case Params of
[] ->
ok;
[ H | T ] ->
case H of
undefined ->
io:format( " undefined~n" );
{ Key, Value } ->
io:format( " ~s=~s~n", [Key, Value ] )
end,
dumpParams( T )
end.
%%
%% Configuration handler for mod_sofia.
%%
xml_fetch({fetch, configuration, "configuration", "name", "sofia.conf", Params}) ->
io:format( "freeswitch_callback:handle_call( {fetch, configuration, Tag=\"configuration\", Key=\"name\", Value=\"sofia.conf\", Params=...} )~n"),
dumpParams( Params ),
Xml =
"
",
{ok, Xml };
%%
%% Configuration handler replies that the requested document section, tag, and key are not
%% found.
%%
xml_fetch({fetch, configuration, Tag, Key, Value, Params}) ->
io:format( "freeswitch_callback:handle_call( {fetch, configuration, Tag=~s, Key=~s, Value=~s, Params=...} )~n",
[Tag, Key, Value]),
dumpParams( Params ),
Xml =
"
",
{ok, Xml };
%%
%% Directory handler replies that the requested document section, tag, and key are not
%% found.
%%
xml_fetch({fetch, directory, Tag, Key, Value, Params}) ->
io:format( "freeswitch_callback:xml_fetch( {fetch, directory, Tag=~s, Key=~s, Value=~s, Params=...} )~n",
[Tag, Key, Value]),
dumpParams( Params ),
Xml =
"
",
{ok, Xml };
%%
%% Dialplan handler replies that the requested document section, tag, and key are not
%% found.
%%
xml_fetch({fetch, dialplan, Tag, Key, Value, Params}) ->
io:format( "freeswitch_callback:xml_fetch( {fetch, dialplan, Tag=~s, Key=~s, Value=~s, Params=...} )~n",
[Tag, Key, Value]),
dumpParams( Params ),
Xml =
"
",
{ok, Xml };
%%
%% Default handler replies that the requested document section, tag, and key are not
%% found.
%%
xml_fetch({fetch, Section, Tag, Key, Value, Params}) ->
io:format( "freeswitch_callback:xml_fetch( {fetch, Section=~w, Tag=~s, Key=~s, Value=~s, Params=...} )~n",
[Section, Tag, Key, Value]),
dumpParams( Params ),
Xml =
"
",
{ok, Xml };
%%
%% If the request isn't recognized, just log it.
%%
xml_fetch( Request ) ->
io:format( "freeswitch_callback:xml_fetch( Request=~w ) not recognized~n",
[Request]),
Xml =
"
",
{ok, Xml }.
%%
%% If the request isn't recognized, just log it and do nothing.
%%
handle_call(Request, _From, State) ->
io:format("freeswitch_callback:handle_call( ~w, _From, State) unrecognized request~n",
[Request]),
{reply, {error, unrecognized_request}, State}.
handle_cast(Message, State) ->
error_logger:error_msg("~p received unrecognized cast ~p~n",
[self(), Message]),
{noreply, State}.
handle_info({fetch, Section, Tag, Key, Value, FetchID, Params}, #st{fsnode=Node}=State) ->
{ok, XML} = xml_fetch({fetch, Section, Tag, Key, Value, Params}),
{api, Node} ! {fetch_reply, FetchID, XML},
receive
ok ->
{noreply, State};
{error, Reason} ->
{stop, {error, Reason}, State}
end;
handle_info(Info, State) ->
io:format( "freeswitch_callback:handle_info() Info=~w~n", [Info]),
{noreply, State}.