Guest User

Untitled

a guest
Oct 20th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.93 KB | None | 0 0
  1. -module(simple_tcp_sup).
  2.  
  3. -behaviour(supervisor).
  4.  
  5. -export([start_link/0,
  6.          start_child/1,
  7.          kill_child/1
  8.         ]).
  9.  
  10. -export([init/1]).
  11.  
  12. -define(SERVER, ?MODULE).
  13.  
  14. start_link() ->
  15.     supervisor:start_link({local, ?SERVER}, ?MODULE, []).
  16.  
  17. start_child({socket, Socket}) ->
  18.     io:fwrite("Spawning child with socket...~n"),
  19.     supervisor:start_child(?SERVER, [{socket, Socket}]);
  20.  
  21. start_child({port, Port}) ->
  22.     io:fwrite("Spawning child with port...~n"),
  23.     supervisor:start_child(?SERVER, [{port, Port}]).
  24.  
  25. kill_child(Pid) ->
  26.     supervisor:terminate_child(?SERVER, Pid),
  27.     io:fwrite("Child is dead: ~p, ~p ~n", [Pid, erlang:is_process_alive(Pid)]).
  28.  
  29. init([]) ->
  30.     Element = {simple_tcp, {simple_tcp, start_link, []},
  31.                temporary, brutal_kill, worker, [simple_tcp]},
  32.     Children = [Element],
  33.     RestartStrategy = {simple_one_for_one, 0, 1},
  34.     {ok, {RestartStrategy, Children}}.
Add Comment
Please, Sign In to add comment