Source

chut / src / chut_user_sup.erl

%%%===================================================================
%%% Supervises a given user and acts as an entry point for the internal
%%% FSM to kill the user.
%%%===================================================================
-module(chut_user_sup).
-behaviour(supervisor).

%% supervisor callback
-export([init/1]).
%% API
-export([start_link/2]).

start_link(UserId, TimeOut) ->
    supervisor:start_link({global, UserId}, ?MODULE, [UserId, TimeOut]).


%%--------------------------------------------------------------------
%% Function: init([UserId, Timeout]) -> [ChildSpec]
%% Description: Creates the children specification for the event
%% manager and the fsm constituting a user. A one for all restart
%% strategy is used because only if the FSM and the event handler can
%% a user work properly.
%%--------------------------------------------------------------------
init([UserId, TimeOut]) ->
    MonitorId = {monitor, UserId},
    ManagerId = {manager, UserId},
    MonitorSpec = {MonitorId,
                   {chut_user_monitor, start_link, [UserId, ManagerId, TimeOut]},
                   transient,
                   TimeOut,
                   worker,
                   [chut_user_monitor]},
    ManagerSpec = {ManagerId,
                   {chut_user_manager, start_link, [UserId]},
                   transient,
                   TimeOut,
                   worker,
                   dynamic},
    {ok, {{one_for_all, 1, TimeOut},
         [MonitorSpec, ManagerSpec]}}.