%%%----------------------------------------------------------------------------- %%% @copyright (C) 2010-2019, 2600Hz %%% @doc Trunk-Store responder waits for Auth and Route requests on the broadcast %%% Exchange, and delivers the requests to the corresponding handler. %%% TS responder also receives responses from the handlers and returns them %%% to the requester. %%% Each request received by TS_RESPONDER should be put into a new spawn() %%% to avoid blocking on each request. %%% %%% @author James Aimonetti %%% @end %%%----------------------------------------------------------------------------- -module(ts_responder). -behaviour(gen_listener). %% API -export([start_link/0]). -export([init/1 ,handle_call/3 ,handle_cast/2 ,handle_info/2 ,handle_event/2 ,terminate/2 ,code_change/3 ]). -include("ts.hrl"). -define(SERVER, ?MODULE). -define(RESPONDERS, [{'ts_route_req', [{<<"dialplan">>, <<"route_req">>}]}]). -define(BINDINGS, [{'route', [{'types', ?RESOURCE_TYPES_HANDLED} ,{'restrict_to', ['account']} ] }]). -define(ROUTE_QUEUE_NAME, <<"trunkstore_listener">>). -define(ROUTE_QUEUE_OPTIONS, [{'exclusive', 'false'}]). -define(ROUTE_CONSUME_OPTIONS, [{'exclusive', 'false'}]). -record(state, {}). -type state() :: #state{}. %%%============================================================================= %%% API %%%============================================================================= %%------------------------------------------------------------------------------ %% @doc Starts the server. %% @end %%------------------------------------------------------------------------------ -spec start_link() -> kz_types:startlink_ret(). start_link() -> gen_listener:start_link(?SERVER, [{'responders', ?RESPONDERS} ,{'bindings', ?BINDINGS} ,{'queue_name', ?ROUTE_QUEUE_NAME} ,{'queue_options', ?ROUTE_QUEUE_OPTIONS} ,{'consume_options', ?ROUTE_CONSUME_OPTIONS} ], []). %%%============================================================================= %%% gen_server callbacks %%%============================================================================= %%------------------------------------------------------------------------------ %% @doc Initializes the server. %% @end %%------------------------------------------------------------------------------ -spec init([]) -> {'ok', state()}. init([]) -> lager:info("started ts_responder"), {'ok', #state{}}. %%------------------------------------------------------------------------------ %% @doc Handling call messages. %% @end %%------------------------------------------------------------------------------ -spec handle_call(any(), kz_term:pid_ref(), state()) -> kz_types:handle_call_ret_state(state()). handle_call(_Request, _From, State) -> {'reply', 'ignored', State}. %%------------------------------------------------------------------------------ %% @doc Handling cast messages. %% @end %%------------------------------------------------------------------------------ -spec handle_cast(any(), state()) -> kz_types:handle_cast_ret_state(state()). handle_cast(_Msg, State) -> {'noreply', State}. %%------------------------------------------------------------------------------ %% @doc Handling all non call/cast messages. %% @end %%------------------------------------------------------------------------------ -spec handle_info(any(), state()) -> kz_types:handle_info_ret_state(state()). handle_info(_Unhandled, State) -> lager:info("unknown message: ~p", [_Unhandled]), {'noreply', State}. %%------------------------------------------------------------------------------ %% @doc Allows listener to pass options to handlers. %% @end %%------------------------------------------------------------------------------ -spec handle_event(kz_json:object(), kz_term:proplist()) -> gen_listener:handle_event_return(). handle_event(_JObj, _State) -> {'reply', []}. %%------------------------------------------------------------------------------ %% @doc This function is called by a `gen_server' when it is about to %% terminate. It should be the opposite of `Module:init/1' and do any %% necessary cleaning up. When it returns, the `gen_server' terminates %% with Reason. The return value is ignored. %% %% @end %%------------------------------------------------------------------------------ -spec terminate(any(), state()) -> 'ok'. terminate(_Reason, _) -> lager:info("ts_responder terminating: ~p", [_Reason]). %%------------------------------------------------------------------------------ %% @doc Convert process state when code is changed. %% @end %%------------------------------------------------------------------------------ -spec code_change(any(), state(), any()) -> {'ok', state()}. code_change(_OldVsn, State, _Extra) -> {'ok', State}. %%%============================================================================= %%% Internal functions %%%=============================================================================