Source

chut / src / chut_user_manager.erl

Full commit
%%%===================================================================
%%% This event manager is part of the 3 processes making a user up.
%%% It is responsible of holding all the handlers having to do with
%%% message handling.
%%%===================================================================
-module(chut_user_manager).

%% API
-export([start_link/0, add_handler/3, add_sup_handler/3, delete_handler/3,
         notify/2, get_handlers/1]).

%%--------------------------------------------------------------------
%% 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() ->
    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) ->
    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) ->
    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()
%% Description: Adds an event handler that watches for the calling process'
%% exit signals. This avoids having zombie listeners left over. The actual
%% handling of errors is done in the callback module's terminate/2 function
%%--------------------------------------------------------------------
add_sup_handler(UserId, Handler = {_Module, _HandlerId}, 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) ->
    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) ->
    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:whereis_name({manager, UserId})).

%%--------------------------------------------------------------------
%% Function: notify(Event) -> ok | {error, Reason}
%% Description: Sends the Event through the event manager.
%%--------------------------------------------------------------------
notify(UserId, Message) ->
    case global:whereis_name({manager, UserId}) of
        undefined -> ok; % fail silently like with {global, Name}.
        Pid -> gen_event:notify(Pid, Message)
    end.