Fred T-H avatar Fred T-H committed 6796a9e

Moving the registration of the manager process outside of it

Same advantage as for moving then monitor: respect of specs
and being able to use conflict resolving function.

Because a gen_event has no init function, the dispatch event
handler has to do it itself. For now, this is done through
a bit of spaghetti in chut_user:start/3.

Comments (0)

Files changed (4)

src/chut_user.erl

                          supervisor,
                          [chut_user_sup]},
             supervisor:start_child(chut_user_supersup,ChildSpec),
-            chut_user_manager:add_handler(UserId, chut_user_dispatch_handler, UserId),
-            chut_user_manager:add_handler(UserId, chut_user_history_handler, {UserId,HistoryLimit}),
+            Children = supervisor:which_children(global:whereis_name(UserId)),
+            {manager,Pid,_,_} = lists:keyfind(manager, 1, Children),
+            chut_user_manager:add_handler(Pid, chut_user_dispatch_handler, UserId),
+            chut_user_manager:add_handler(Pid, chut_user_history_handler, {UserId,HistoryLimit}),
             io:format("started user process for id ~p~n",[UserId]),
             started;
         _Pid ->

src/chut_user_dispatch_handler.erl

 %% this function is called to initialize the event handler.
 %%--------------------------------------------------------------------
 init(UserId) ->
+    global:register_name({manager, UserId}, self()),
     {ok, UserId}.
 
 %%--------------------------------------------------------------------

src/chut_user_manager.erl

 -module(chut_user_manager).
 
 %% API
--export([start_link/1, add_handler/3, add_sup_handler/3, delete_handler/3,
+-export([start_link/0, add_handler/3, add_sup_handler/3, delete_handler/3,
          notify/2, get_handlers/1]).
 
 %%--------------------------------------------------------------------
-%% Function: start_link() -> {ok,Pid} | {error,Error}
+%% Function: start_link(UserId) -> {ok,Pid} | {error,Error}
 %% Description: Creates an event manager.
 %% The user is global by default as we ought to support a multiple
 %% node system and will need more than a single process
 %%--------------------------------------------------------------------
-start_link(UserId) ->
-    gen_event:start_link({global, {manager, UserId}}).
+start_link() ->
+    gen_event:start_link().
 
 %%--------------------------------------------------------------------
 %% Function: add_handler(Module,Args) -> ok | {'EXIT',Reason} | term()
 %% Description: Adds an event handler
 %%--------------------------------------------------------------------
 add_handler(UserId, Handler = {_Module, _HandlerId}, Params) ->
-    gen_event:add_handler({global, {manager, UserId}}, Handler, Params);
+    Pid = if is_pid(UserId) -> UserId
+           ; true -> global:whereis_name({manager, UserId})
+          end,
+    gen_event:add_handler(Pid, Handler, Params);
 add_handler(UserId, Module, Params) when is_atom(Module) ->
-    gen_event:add_handler({global, {manager, UserId}}, Module, Params).
+    Pid = if is_pid(UserId) -> UserId
+           ; true -> global:whereis_name({manager, UserId})
+          end,
+    gen_event:add_handler(Pid, Module, Params).
 
 %%--------------------------------------------------------------------
 %% Function: add_sup_handler(Module,Args) -> ok | {'EXIT',Reason} | term()
 %% handling of errors is done in the callback module's terminate/2 function
 %%--------------------------------------------------------------------
 add_sup_handler(UserId, Handler = {_Module, _HandlerId}, Params) ->
-    gen_event:add_sup_handler({global, {manager, UserId}}, Handler, Params);
+    Pid = global:whereis_name({manager, UserId}),
+    gen_event:add_sup_handler(Pid, Handler, Params);
 add_sup_handler(UserId, Module, Params) when is_atom(Module) ->
-    gen_event:add_sup_handler({global, {manager, UserId}}, Module, Params).
+    Pid = global:whereis_name({manager, UserId}),
+    gen_event:add_sup_handler(Pid, Module, Params).
 
 %%--------------------------------------------------------------------
 %% Function: delete_handler(UserId, Handler, Params) ->
 %% Description: Removes an event handler
 %%--------------------------------------------------------------------
 delete_handler(UserId, Handler, Params) ->
-    gen_event:delete_handler({global, {manager,UserId}}, Handler, Params).
+    Pid = global:whereis_name({manager, UserId}),
+    gen_event:delete_handler(Pid, Handler, Params).
 
 %%--------------------------------------------------------------------
 %% Function: get_handlers(UserId) -> [term()]
 %% Description: returns a list of all the handlers registered
 %%--------------------------------------------------------------------
 get_handlers(UserId) ->
-    gen_event:which_handlers({global, {manager, UserId}}).
+    gen_event:which_handlers(global:whereis_name({manager, UserId})).
 
 %%--------------------------------------------------------------------
 %% Function: notify(Event) -> ok | {error, Reason}
 %% Description: Sends the Event through the event manager.
 %%--------------------------------------------------------------------
 notify(UserId, Message) ->
-  gen_event:notify({global, {manager,UserId}}, Message).
+    Pid = global:whereis_name({manager, UserId}),
+    gen_event:notify(Pid, Message).

src/chut_user_sup.erl

                    worker,
                    [chut_user_monitor]},
     ManagerSpec = {manager,
-                   {chut_user_manager, start_link, [UserId]},
+                   {chut_user_manager, start_link, []},
                    transient,
                    TimeOut,
                    worker,
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.