Source

chut / src / chut_user_monitor.erl

Diff from to

File src/chut_user_monitor.erl

 -behaviour(gen_fsm).
 
 %% API
--export([start_link/3, add_handler/2, delete_handler/2]).
+-export([start_link/2, add_handler/2, delete_handler/2]).
 
 %% gen_fsm callbacks
 -export([init/1, waiting/2, listening/2, handle_event/3,
 %% API
 %%====================================================================
 %%--------------------------------------------------------------------
-%% Function: start_link(UserId, Dispatcher, TimeOut) ->
+%% Function: start_link(UserId, TimeOut) ->
 %%               {ok,Pid} | ignore | {error,Error}
 %% Description:Creates a gen_fsm process which calls Module:init/1 to
 %% initialize. To ensure a synchronized start-up procedure, this function
 %%
 %% In this one, TimeOut is the time to live of the user when there are
 %% no listen_handlers connected to the event manager.
-%% The Dispatcher is yet unused, but will be when monitoring of users
-%% is added in (no YAGNI rethinking needed, I hope!)
 %% The UserId is needed for the fsm to terminate its supervisor when
 %% the time is out.
 %%--------------------------------------------------------------------
-start_link(UserId, Dispatcher, TimeOut) ->
-  gen_fsm:start_link({global, {monitor,UserId}},
-                     ?MODULE,
+start_link(UserId, TimeOut) ->
+  gen_fsm:start_link(?MODULE,
                      orddict:from_list([{id, UserId},
-                                        {dispatcher, Dispatcher},
                                         {timeout, TimeOut}]),
                      []).
 
 %% client listener.
 %%--------------------------------------------------------------------
 add_handler(UserId, HandlerId) ->
-    gen_fsm:sync_send_all_state_event({global, {monitor,UserId}}, {add_handler, HandlerId}).
+    Pid = global:whereis_name({monitor, UserId}),
+    gen_fsm:sync_send_all_state_event(Pid, {add_handler, HandlerId}).
 
 %%--------------------------------------------------------------------
 %% Function: delete_handler(UserId, HandlerId) -> ok
 %% mode and timeout if there are no new handlers.
 %%--------------------------------------------------------------------
 delete_handler(UserId, HandlerId) ->
-    gen_fsm:send_event({global, {monitor,UserId}}, {delete_handler, HandlerId}).
+    Pid = global:whereis_name({monitor, UserId}),
+    gen_fsm:send_event(Pid, {delete_handler, HandlerId}).
 
 %%====================================================================
 %% gen_fsm callbacks
 %% handlers.
 %%--------------------------------------------------------------------
 init(Opts) ->
-    T = orddict:fetch(timeout,Opts),
+    global:register_name({monitor, orddict:fetch(id, Opts)}, self()),
+    T = orddict:fetch(timeout, Opts),
     {ok, waiting, orddict:store(handlers, [], Opts), T}.