Commits

Fred T-H committed 278fcc2

Adding application supervisors

  • Participants
  • Parent commits b256108

Comments (0)

Files changed (4)

+%%%===================================================================
+%%% Top supervisor for the chut application and its web server.
+%%%===================================================================
+-module(chut_sup).
+-behaviour(supervisor).
+
+%% supervisor callback
+-export([init/1]).
+%% API
+-export([start_link/1]).
+
+start_link(Port) ->
+    supervisor:start_link({local, ?MODULE}, ?MODULE, [Port]).
+
+%%--------------------------------------------------------------------
+%% 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([Port]) ->
+    ChutSpec = {chut_user_supersup,
+                {chut_user_supersup, start_link, []},
+                permanent,
+                infinity,
+                supervisor,
+                [chut_user_supersup]},
+    ServerSpec = {chut_webserver_sup,
+                  {chut_webserver_sup, start_link, [Port]},
+                  permanent,
+                  infinity,
+                  supervisor,
+                  [chut_webserver_sup]},
+    {ok, {{rest_for_one, 10, 60}, [ChutSpec, ServerSpec]}}.
+
+
+

src/chut_user_supersup.erl

 start_link() ->
     supervisor:start_link({local, chut_user_supersup}, ?MODULE, []).
 
+%%--------------------------------------------------------------------
+%% Function: init([]) -> {ok, Restart, [ChildSpec]}
+%% Description: Creates a childless supervisor whose role is to get
+%% children attached via supervisor:start_child/2.
+%%--------------------------------------------------------------------
 init([]) ->
     {ok, {{one_for_one, 1, 15000}, []}}.

src/chut_webserver.erl

 
 %% Internal functions
 handle("/message", Req) ->
+    io:format("message call~n"),
     Params = Req:parse_qs(),
     From = proplists:get_value("from", Params),
     To = proplists:get_value("to", Params),
     reply(Req, mochijson2:encode(ok), Params);
 
 handle("/listen", Req) ->
+    io:format("listen call~n"),
     Params = Req:parse_qs(),
     Nick = proplists:get_value("id", Params),
     {ok,Handler} = chut_client:connect(Nick),
     reply(Req, JSON, Params);
 
 handle("/history", Req) ->
+    io:format("history call~n"),
     Params = Req:parse_qs(),
     Nick = proplists:get_value("id",Params),
     {ok, Handler} = chut_client:connect(Nick),

src/chut_webserver_sup.erl

+%%%===================================================================
+%%% Supervises a given user and acts as an entry point for the internal
+%%% FSM to kill the user.
+%%%===================================================================
+-module(chut_webserver_sup).
+-behaviour(supervisor).
+
+%% supervisor callback
+-export([init/1]).
+%% API
+-export([start_link/1]).
+
+start_link(Port) ->
+    supervisor:start_link({local, chut_webserver_sup}, ?MODULE, [Port]).
+
+%%--------------------------------------------------------------------
+%% 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([Port]) ->
+    {ok, {{one_for_all, 10, 60},
+         [{chut_webserver_sup,
+           {chut_webserver, start_link, [Port]},
+           transient,
+           20000,
+           worker,
+           [chut_webserver]}]}}.
+
+