[Freeswitch-users] ERLang configuration callbacks

Mark Sobkow m.sobkow at marketelsystems.com
Mon Sep 14 12:54:31 PDT 2009


I seem to be missing "something" in implementing the ERLang callbacks 
for Freeswitch.  Our Freeswitch server is starting and getting 
registered with ERLang, we're invoking the bind for configuration, but 
I'm not seeing any of my callbacks fire.  What am I missing?

Sample code follows:

-module(freeswitch_bind).

-behaviour(gen_server).

-record(st, {fsnode, pbxpid}).

-export([start/3, terminate/2, code_change/3, init/1,
     handle_call/3, handle_cast/2, handle_info/2]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% gen_server methods
start(Node, Section, Pid) ->
    gen_server:start(?MODULE, [Node, Section, Pid], []).

init([Node, Section, Pid]) ->
    io:format( "freeswitch_bind:init( [Node=~w, Section=~w, Pid=~w])~n", 
[Node, Section, Pid] ),
    {api, Node} ! {bind, Section},
    receive
    ok ->
        {ok, #st{fsnode=Node, pbxpid=Pid}};
    {error, Reason} ->
        {stop, {error, {freeswitch_error, Reason}}}
    after 5000 ->
        {stop, {error, freeswitch_timeout}}
    end.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%
%%    Configuration handler replies that the requested document section, 
tag, and key are not
%%    found.
%%
handle_call({fetch, configuration, Tag, Key, Value, Params}, _From, 
State) ->
    io:format( "freeswitch_fetch:handle_call( {fetch, configuration, 
Tag=~w, Key=~w, Value=~w, Params=~w}, _From, State=~w)~n",
        [Tag, Key, Value, Params, State]),
    Xml =
"<document type=\"freeswitch/xml\">
    <section name=\"result\">
        <result status=\"not found\" />
    </section>
</document>",
    { reply, {ok, Xml }, State };

%%
%%    Directory handler replies that the requested document section, 
tag, and key are not
%%    found.
%%
handle_call({fetch, directory, Tag, Key, Value, Params}, _From, State) ->
    io:format( "freeswitch_fetch:handle_call( {fetch, directory, Tag=~w, 
Key=~w, Value=~w, Params=~w}, _From, State=~w)~n",
        [Tag, Key, Value, Params, State]),
    Xml =
"<document type=\"freeswitch/xml\">
    <section name=\"result\">
        <result status=\"not found\" />
    </section>
</document>",
    { reply, {ok, Xml }, State };

%%
%%    Dialplan handler replies that the requested document section, tag, 
and key are not
%%    found.
%%
handle_call({fetch, dialplan, Tag, Key, Value, Params}, _From, State) ->
    io:format( "freeswitch_fetch:handle_call( {fetch, dialplan, Tag=~w, 
Key=~w, Value=~w, Params=~w}, _From, State=~w)~n",
        [Tag, Key, Value, Params, State]),
    Xml =
"<document type=\"freeswitch/xml\">
    <section name=\"result\">
        <result status=\"not found\" />
    </section>
</document>",
    { reply, {ok, Xml }, State };

%%
%%    Default handler replies that the requested document section, tag, 
and key are not
%%    found.
%%
handle_call({fetch, Section, Tag, Key, Value, Params}, _From, State) ->
    io:format( "freeswitch_fetch:handle_call( {fetch, Section=~w, 
Tag=~w, Key=~w, Value=~w, Params=~w}, _From, State=~w)~n",
        [Section, Tag, Key, Value, Params, State]),
    Xml =
"<document type=\"freeswitch/xml\">
    <section name=\"result\">
        <result status=\"not found\" />
    </section>
</document>",
    { reply, {ok, Xml }, State };

%%
%%    If the request isn't recognized, just log it and do nothing.
%%
handle_call(Request, _From, State) ->
    io:format("freeswitch_bind: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, pbxpid=Pid}=State) ->
    {ok, XML} = gen_server:call(Pid, {fetch, Section, Tag, Key, Value, 
Params}),
    {api, Node} ! {fetch_reply, FetchID, XML},
    receive
    ok ->
        {noreply, State};
    {error, Reason} ->
        {stop, {error, Reason}, State}
    end.





More information about the FreeSWITCH-users mailing list