Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert to using gproc instead of gen_event
Signed-off-by: Gregory Haskins <ghaskins@novell.com>
- Loading branch information
Gregory Haskins
committed
Apr 16, 2011
1 parent
798f356
commit e8c080d
Showing
11 changed files
with
144 additions
and
32 deletions.
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,28 @@ | ||
| -module(event_logger). | ||
| -export([init/1, handle_event/2, terminate/2]). | ||
| -behavor(gen_server). | ||
|
|
||
| -export([start_link/0]). | ||
| -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). | ||
|
|
||
| start_link() -> | ||
| gen_server:start_link(?MODULE, [], []). | ||
|
|
||
| init(_Args) -> | ||
| edist_event_bus:subscribe([]), | ||
| {ok, []}. | ||
|
|
||
| handle_event(Event, State) -> | ||
| handle_call(_Request, _From, State) -> | ||
| {reply, {error, enotsup}, State}. | ||
|
|
||
| handle_cast(_Request, State) -> | ||
| {noreply, State}. | ||
|
|
||
| handle_info({edist_event_bus, Header, Data}=Event, State) -> | ||
| error_logger:info_msg("Event: ~p~n", [Event]), | ||
| {ok, State}. | ||
| {noreply, State}. | ||
|
|
||
| terminate(_Args, _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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| -module(edist_event_bus). | ||
| -export([subscribe/1, subscribe/2, notify/2, notify/3]). | ||
|
|
||
| -include_lib("stdlib/include/qlc.hrl"). | ||
|
|
||
| -record(subscriber, {cookie}). | ||
| -record(options, {scope=global, cookie}). | ||
|
|
||
| subscribe(Options) -> | ||
| subscribe(all, Options). | ||
|
|
||
| subscribe(EventType, RawOptions) -> | ||
| Options = parse_options(RawOptions), | ||
| Scope = case Options#options.scope of | ||
| local -> l; | ||
| global -> g | ||
| end, | ||
| gproc:reg({p, Scope, {?MODULE, EventType}}, | ||
| #subscriber{cookie=Options#options.cookie}). | ||
|
|
||
| notify(EventType, Msg) -> | ||
| notify(EventType, Msg, []). | ||
|
|
||
| notify(EventType, Msg, RawOptions) -> | ||
| Options = parse_options(RawOptions), | ||
| Scope = Options#options.scope, | ||
|
|
||
| Q = qlc:q([{P, S} | ||
| || {{p, '_', {?MODULE, E}}, P, S} <- gproc:table({Scope, props}), | ||
| E =:= EventType orelse E =:= all]), | ||
| lists:foreach(fun({P, S}) -> | ||
| Header = [ | ||
| {headerver, 1}, | ||
| {from, self()}, | ||
| {type, EventType}, | ||
| {cookie, S#subscriber.cookie} | ||
| ], | ||
|
|
||
| P ! {?MODULE, Header, Msg} | ||
| end, | ||
| qlc:e(Q)). | ||
|
|
||
| parse_options(List) -> | ||
| parse_options(List, #options{}). | ||
|
|
||
| parse_options([{scope, RawScope} | T], Options) -> | ||
| Scope = case RawScope of | ||
| local -> local; | ||
| _ -> global | ||
| end, | ||
| parse_options(T, Options#options{scope=Scope}); | ||
| parse_options([{cookie, Cookie} | T], Options) -> | ||
| parse_options(T, Options#options{cookie=Cookie}); | ||
| parse_options([H | _T], _Options) -> | ||
| throw({earg, H}); | ||
| parse_options([], Options) -> | ||
| Options. | ||
|
|
||
|
|
||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| -module(edist_event_bus_test). | ||
| -include_lib("eunit/include/eunit.hrl"). | ||
|
|
||
| init_test() -> | ||
| ok = application:start(gen_leader), | ||
| ok = application:start(gproc). | ||
|
|
||
| subscribe_test() -> | ||
| true = edist_event_bus:subscribe(foo, [{cookie, bar}, {scope, local}]), | ||
| edist_event_bus:notify(foo, baz, [{scope, local}]), | ||
|
|
||
| receive | ||
| {edist_event_bus, _Header, baz} -> | ||
| ok; | ||
| Msg -> throw({"Unexpected msg", Msg}) | ||
| after | ||
| 0 -> throw(timeout) | ||
| end. | ||
|
|
||
| badoptions_test() -> | ||
| try | ||
| edist_event_bus:subscribe(bar, [{bar, baz}]), | ||
| throw("unexpected success") | ||
| catch | ||
| throw:{earg, {bar, baz}} -> ok | ||
| end. | ||
|
|
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