Source

chut / src / usr_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(usr_manager).

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

%%--------------------------------------------------------------------
%% Function: start_link() -> {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}}).

%%--------------------------------------------------------------------
%% 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);
add_handler(UserId, Module, Params) when is_atom(Module) ->
    gen_event:add_handler({global, {manager, UserId}}, 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) ->
    gen_event:add_sup_handler({global, {manager, UserId}}, Handler, Params);
add_sup_handler(UserId, Module, Params) when is_atom(Module) ->
    gen_event:add_sup_handler({global, {manager, UserId}}, 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).

%%--------------------------------------------------------------------
%% Function: get_handlers(UserId) -> [term()]
%% Description: returns a list of all the handlers registered
%%--------------------------------------------------------------------
get_handlers(UserId) ->
    gen_event:which_handlers({global, {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).