Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial version

  • Loading branch information...
commit aaac5856111baac31ecce9bae68b053091db6791 0 parents
@yrashk authored
2  .gitignore
@@ -0,0 +1,2 @@
+ebin
+.eunit
11 src/gen_event_forwarder.app.src
@@ -0,0 +1,11 @@
+{application, gen_event_forwarder,
+ [
+ {description, "gen_event forwarder"},
+ {vsn, "1.0"},
+ {registered, []},
+ {applications, [
+ kernel,
+ stdlib
+ ]},
+ {env, []}
+ ]}.
109 src/gen_event_forwarder.erl
@@ -0,0 +1,109 @@
+-module(gen_event_forwarder).
+
+-behaviour(gen_event).
+
+%% gen_event callbacks
+-export([init/1, handle_event/2, handle_call/2,
+ handle_info/2, terminate/2, code_change/3]).
+
+-define(SERVER, ?MODULE).
+
+-record(state, {
+ relay_to
+ }).
+
+%%%===================================================================
+%%% gen_event callbacks
+%%%===================================================================
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Whenever a new event handler is added to an event manager,
+%% this function is called to initialize the event handler.
+%%
+%% @spec init(Args) -> {ok, State}
+%% @end
+%%--------------------------------------------------------------------
+init([RelayTo]) ->
+ {ok, #state{
+ relay_to = RelayTo
+ }}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Whenever an event manager receives an event sent using
+%% gen_event:notify/2 or gen_event:sync_notify/2, this function is
+%% called for each installed event handler to handle the event.
+%%
+%% @spec handle_event(Event, State) ->
+%% {ok, State} |
+%% {swap_handler, Args1, State1, Mod2, Args2} |
+%% remove_handler
+%% @end
+%%--------------------------------------------------------------------
+handle_event(Event, #state{ relay_to = RelayTo } = State) ->
+ RelayTo ! {gen_event, self(), event, Event},
+ {ok, State}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Whenever an event manager receives a request sent using
+%% gen_event:call/3,4, this function is called for the specified
+%% event handler to handle the request.
+%%
+%% @spec handle_call(Request, State) ->
+%% {ok, Reply, State} |
+%% {swap_handler, Reply, Args1, State1, Mod2, Args2} |
+%% {remove_handler, Reply}
+%% @end
+%%--------------------------------------------------------------------
+handle_call(_Request, State) ->
+ {ok, ok, State}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% This function is called for each installed event handler when
+%% an event manager receives any other message than an event or a
+%% synchronous request (or a system message).
+%%
+%% @spec handle_info(Info, State) ->
+%% {ok, State} |
+%% {swap_handler, Args1, State1, Mod2, Args2} |
+%% remove_handler
+%% @end
+%%--------------------------------------------------------------------
+handle_info(Info, #state{ relay_to = RelayTo } = State) ->
+ RelayTo ! {gen_event, self(), info, Info},
+ {ok, State}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Whenever an event handler is deleted from an event manager, this
+%% function is called. It should be the opposite of Module:init/1 and
+%% do any necessary cleaning up.
+%%
+%% @spec terminate(Reason, State) -> void()
+%% @end
+%%--------------------------------------------------------------------
+terminate(_Reason, _State) ->
+ ok.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Convert process state when code is changed
+%%
+%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
+%% @end
+%%--------------------------------------------------------------------
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+%%%===================================================================
+%%% Internal functions
+%%%===================================================================
22 test/gen_event_forwarder_tests.erl
@@ -0,0 +1,22 @@
+-module(gen_event_forwarder_tests).
+-include_lib("eunit/include/eunit.hrl").
+
+event_relay_test() ->
+ {ok, EventMgr} = gen_event:start_link(),
+ gen_event:add_handler(EventMgr, gen_event_forwarder, [self()]),
+ gen_event:notify(EventMgr, test_event),
+ receive
+ X ->
+ ?assertEqual({gen_event, EventMgr, event, test_event}, X)
+ end.
+
+info_relay_test() ->
+ {ok, EventMgr} = gen_event:start_link(),
+ gen_event:add_handler(EventMgr, gen_event_forwarder, [self()]),
+ EventMgr ! test_msg,
+ receive
+ X ->
+ ?assertEqual({gen_event, EventMgr, info, test_msg}, X)
+ end.
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.