Current section
Files
Jump to
Current section
Files
src/denrei_sup.erl
%%%-------------------------------------------------------------------
%%% @private
%%% @doc
%%% Denrei's top level supervisor.
%%% @end
%%%-------------------------------------------------------------------
-module(denrei_sup).
-behaviour(supervisor).
%% Includes
-include("denrei.hrl").
%% API
-export([start_link/0, start_link/1]).
%% supervisor callbacks
-export([init/1]).
%%%===================================================================
%%% API functions
%%%===================================================================
-spec(start_link() -> {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() ->
start_link([]).
-spec(start_link(Args :: term()) -> {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link(Args) ->
Name = denrei_utils:get_opt("NAME", name, Args, ?DENREI_NAME),
Ref = list_to_atom(Name ++ "_supervisor"),
supervisor:start_link({local, Ref}, ?MODULE, Args).
%%%===================================================================
%%% Supervisor callbacks
%%%===================================================================
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Whenever a supervisor is started using supervisor:start_link/[2,3],
%% this function is called by the new process to find out about
%% restart strategy, maximum restart frequency and child
%% specifications.
%%
%% @end
%%--------------------------------------------------------------------
-spec(init(Args :: term()) -> {ok, {SupFlags :: supervisor:sup_flags(), [ChildSpec :: supervisor:child_spec()]}} | ignore).
init(Args) ->
Name = denrei_utils:get_opt("NAME", name, Args, ?DENREI_NAME),
Transport = denrei_utils:get_opt("TRANSPORT", transport, Args, ?DENREI_TRANSPORT),
Address = denrei_utils:to_ip(denrei_utils:get_opt("IP", ip, Args, ?DENREI_IP)),
Port = denrei_utils:to_integer(denrei_utils:get_opt("PORT", port, Args, ?DENREI_PORT)),
Acceptors = denrei_utils:to_integer(denrei_utils:get_opt("ACCEPTORS", acceptors, Args, ?DENREI_ACCEPTORS)),
{ok, TreePid} = case proplists:get_value(tree, Args) of
Pid when is_pid(Pid) ->
{ok, Pid};
_ ->
denrei_tree_server:start_link()
end,
Ref = list_to_atom(Name ++ "_listener"),
TransOpts = [{ip, Address}, {port, Port}],
ProtoOpts = [{tree, TreePid}],
lager:info("~p transport options: ~p", [Ref, TransOpts]),
lager:info("~p protocol options: ~p", [Ref, ProtoOpts]),
ListenerSpec = ranch:child_spec(Ref, Acceptors,
Transport, TransOpts,
denrei_protocol, ProtoOpts),
{ok, {{one_for_one, 10, 10}, [ListenerSpec]}}.
%%%===================================================================
%%% Internal functions
%%%===================================================================