let me know the concept of gen_event behaviour

Wiger Ulf <>
Mon Feb 10 21:07:13 CET 2003


Perhaps an example can illustrate gen_event. The behaviour basically works like this:

-module(simple_gen_event).

start_link(Name) ->
  spawn_link(fun() ->
               register(Name, self()),
               event_loop([])
             end).

add_handler(Name, Module, State) ->
   Name ! {add_handler, self(), Module, State},
   receive
      ok -> ok;
   end.

notify(Name, Event) ->
   Name ! {event, Event}.

event_loop(Modules) ->
   receive
      {add_handler, From, M, S} ->
         From ! ok,
         event_loop([{M,S}|Modules]);
      {event, E} ->
         Modules1 = call_modules(Modules, E),
         event_loop(Modules1)
   end.

call_modules([{M,S}|Modules], E) ->
   case catch M:handle_event(E, S) of
      {'EXIT', _} ->
         %% remove handler
         call_modules(Modules, E);
      {ok, NewS} ->
         [{M,NewS}|call_modules(Modules, E)]
   end;
call_modules([], _) ->
   [].

That is, you define a callback module if you want to handle events that are generated. Each event is just a message. Normally, you callback module would have a handle_event(Event, State) function that matches on a few events and simply ignores the rest.

As event handlers are called sequentially, they should do as little as possible in the handle_event/2 function. Sending a message to some server or state machine that then does the real work is probably a good idea.

/Uffe
  ----- Original Message ----- 
  From: Suresh S 
  To:  
  Sent: den 10 februari 2003 17:43
  Subject: let me know the concept of gen_event behaviour 


  Hi,

  There is an Event manager can add Event handlers, This event handlers will handle particular event,

  Is every  Event is a Call back module ? if so  suppose there are 1000 calls processing in the applicaiton at once, then 1000 call events are generated, then if we use gen_event behaviour there will be 1000 call back modules active( generated )

  Please clarify this, let me know the concept of gen_event behaviours  

  Thanking u in advance.

  regards

  Suresh S



    

   Catch all the cricket action. Download Yahoo! Score tracker

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20030210/aec9ca83/attachment.html>


More information about the erlang-questions mailing list