PageRenderTime 195ms CodeModel.GetById 35ms RepoModel.GetById 10ms app.codeStats 1ms

/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. -behaviour(supervisor).
  3. %% API
  4. -export([start_link/0, add_server/3, cast_all/1]).
  5. %% Supervisor callbacks
  6. -export([init/1]).
  7. -define(SERVER, ?MODULE).
  8. %%====================================================================
  9. %% API functions
  10. %%====================================================================
  11. start_link() ->
  12. supervisor:start_link({local, ?SERVER}, ?MODULE, []).
  13. add_server(Interval, Host, Port) when is_list(Host) ->
  14. case inet:getaddr(Host, inet6) of
  15. {ok, Addr} -> add_server(Interval, Addr, Port);
  16. {error, _} -> case inet:getaddr(Host, inet) of
  17. {ok, Addr} -> add_server(Interval, Addr, Port);
  18. {error, Reason} -> {error, Reason}
  19. end
  20. end;
  21. add_server(Interval, Host, Port) ->
  22. supervisor:start_child(?SERVER,
  23. {{server, Host, Port},
  24. {collectd_server, start_link, [Interval, Host, Port]},
  25. permanent, 1000000, worker, [collectd_server]}).
  26. cast_all(Msg) ->
  27. Children = supervisor:which_children(?SERVER),
  28. lists:foreach(fun({_Id, Pid, _Type, _Modules}) ->
  29. gen_server:cast(Pid, Msg)
  30. end, Children).
  31. %%====================================================================
  32. %% Supervisor callbacks
  33. %%====================================================================
  34. init([]) ->
  35. {ok,{{one_for_one,1,1}, []}}.
  36. %%====================================================================
  37. %% Internal functions
  38. %%====================================================================