PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/collectd_sup.erl

http://github.com/astro/erlang-collectd
Erlang | 47 lines | 27 code | 9 blank | 11 comment | 0 complexity | 67c1f23b8f7efad4d7c93839167c6fd8 MD5 | raw file
 1-module(collectd_sup).
 2
 3-behaviour(supervisor).
 4
 5%% API
 6-export([start_link/0, add_server/3, cast_all/1]).
 7
 8%% Supervisor callbacks
 9-export([init/1]).
10
11-define(SERVER, ?MODULE).
12
13%%====================================================================
14%% API functions
15%%====================================================================
16start_link() ->
17    supervisor:start_link({local, ?SERVER}, ?MODULE, []).
18
19add_server(Interval, Host, Port) when is_list(Host) ->
20    case inet:getaddr(Host, inet6) of
21	{ok, Addr} -> add_server(Interval, Addr, Port);
22	{error, _} -> case inet:getaddr(Host, inet) of
23			  {ok, Addr} -> add_server(Interval, Addr, Port);
24			  {error, Reason} -> {error, Reason}
25		      end
26    end;
27add_server(Interval, Host, Port) ->
28    supervisor:start_child(?SERVER,
29			   {{server, Host, Port},
30			    {collectd_server, start_link, [Interval, Host, Port]},
31			    permanent, 1000000, worker, [collectd_server]}).
32
33cast_all(Msg) ->
34    Children = supervisor:which_children(?SERVER),
35    lists:foreach(fun({_Id, Pid, _Type, _Modules}) ->
36			  gen_server:cast(Pid, Msg)
37		  end, Children).
38
39%%====================================================================
40%% Supervisor callbacks
41%%====================================================================
42init([]) ->
43    {ok,{{one_for_one,1,1}, []}}.
44
45%%====================================================================
46%% Internal functions
47%%====================================================================