sdlan/src/sdlan_stun_sup.erl
2026-04-02 14:26:48 +08:00

65 lines
2.1 KiB
Erlang

%%%-------------------------------------------------------------------
%% @doc sdlan top level supervisor.
%% @end
%%%-------------------------------------------------------------------
-module(sdlan_stun_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
-define(SERVER, ?MODULE).
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
%% sup_flags() = #{strategy => strategy(), % optional
%% intensity => non_neg_integer(), % optional
%% period => pos_integer()} % optional
%% child_spec() = #{id => child_id(), % mandatory
%% start => mfargs(), % mandatory
%% restart => restart(), % optional
%% shutdown => shutdown(), % optional
%% type => worker(), % optional
%% modules => modules()} % optional
init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
Specs = [
#{
id => sdlan_stun_port_assist,
start => {sdlan_stun_port_assist, start_link, []},
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['sdlan_stun_port_assist']
},
#{
id => sdlan_stun_peer_assist,
start => {sdlan_stun_peer_assist, start_link, []},
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['sdlan_stun_peer_assist']
}
],
{ok, {SupFlags, Specs ++ stun_acceptors()}}.
stun_acceptors() ->
{ok, StunServers} = application:get_env(sdlan, stun_servers),
Port = proplists:get_value(port, StunServers),
AcceptorNums = proplists:get_value(acceptor_nums, StunServers),
lists:map(fun(Id) ->
Name = sdlan_stun:get_name(Id),
#{
id => Name,
start => {sdlan_stun, start_link, [Name, Port]},
restart => permanent,
shutdown => 2000,
type => worker,
modules => ['sdlan_stun']
}
end, lists:seq(1, AcceptorNums)).