gen_event help

Alexander Kuleshov kuleshovmail@REDACTED
Thu Jan 27 19:08:50 CET 2011


Hello,

I try to write simple gen_event applcation in erlang.

My code:

-module(test).
-behavior(gen_event).

-export([notify/0]).

%% API
-export([start_link/0, add_handler/0, stop/0]).

%% gen_event callbacks
-export([init/1, handle_event/2, handle_call/2,
     handle_info/2, terminate/2, code_change/3]).

-define(SERVER, ?MODULE).

start_link() ->
    gen_event:start_link({local, ?SERVER}).

add_handler() ->
    gen_event:add_handler(?SERVER, ?MODULE, []).

stop() ->
    gen_event:stop(?MODULE).

init([]) ->
    %add_handler(),
    {ok, null}.

handle_event(_Event, State) ->
    {ok, State};
handle_event({test}, State) ->
    io:format("Test"),
    {ok, State}.

handle_call(_Request, State) ->
    Reply = ok,
    {ok, Reply, State}.

handle_info(_Info, State) ->
    {ok, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

notify() ->
    gen_event:notify(?SERVER, {test}).
When i try notify i expect that function handle_event({test}, State)
execute but nothing occurs. Why? What's wrong?

And where i can see simple example of gen_event?

Thank you.


More information about the erlang-questions mailing list