Created
October 30, 2009 22:13
-
-
Save leandrosilva/222769 to your computer and use it in GitHub Desktop.
My custom OTP behaviour for event management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Do you want to try it? | |
--- | |
1st. Compile | |
$ erlc gen_event_manager.erl my_event_manager.erl my_event.erl | |
2nd. Test | |
$ erl | |
Erlang R14A (erts-5.8) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false] | |
Eshell V5.8 (abort with ^G) | |
1> my_event_manager:start_link(). | |
{ok,[]} | |
2> my_event_manager:add_handler(my_event, []). | |
ok | |
3> my_event_manager:which_handlers(). | |
[my_event] | |
4> my_event_manager:notify_event("java"). | |
--> event: [106,97,118,97] | |
ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(gen_event_manager). | |
-export([start_link/3]). | |
-export([behaviour_info/1]). | |
start_link(ServerName, CallbackModule, Args) -> | |
gen_event:start_link(ServerName), | |
CallbackModule:init(Args). | |
behaviour_info(callbacks) -> | |
[{init, 1}, | |
{add_handler, 2}, | |
{delete_handler, 2}, | |
{swap_handler, 2}, | |
{which_handlers, 0}, | |
{notify_event, 1}]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(my_event). | |
-behaviour(gen_event). | |
-export([init/1, handle_event/2, terminate/2, handle_call/2, handle_info/2, code_change/3]). | |
init(_Args) -> | |
{ok, []}. | |
handle_event(Event, State) -> | |
io:format("--> event: ~w~n", [Event]), | |
{ok, State}. | |
terminate(_Args, _State) -> | |
ok. | |
handle_call(_Request, _State) -> ok. | |
handle_info(_Info, _State) -> ok. | |
code_change(_OldVsn, State, _Extra) -> {ok, State}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(my_event_manager). | |
-behaviour(gen_event_manager). | |
-export([start_link/0]). | |
-export([init/1, add_handler/2, delete_handler/2, swap_handler/2, which_handlers/0, notify_event/1]). | |
%% api | |
start_link() -> | |
gen_event_manager:start_link({local, ?MODULE}, ?MODULE, []). | |
%% callbacks | |
init(_Args) -> | |
{ok, []}. | |
add_handler(HandlerModule, InitArgs) -> | |
gen_event:add_handler(?MODULE, HandlerModule, InitArgs). | |
delete_handler(HandlerModule, State) -> | |
gen_event:delete_handler(?MODULE, HandlerModule, State). | |
swap_handler({_OldHandlerModule, _TerminateArgs} = Old, {_NewHandleModule, _InitHandler} = New) -> | |
gen_event:swap_handler(?MODULE, Old, New). | |
which_handlers() -> | |
gen_event:which_handlers(?MODULE). | |
notify_event(Event) -> | |
gen_event:notify(?MODULE, Event). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment