<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2722.900" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>Perhaps an example can illustrate gen_event. The behaviour
basically works like this:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>-module(simple_gen_event).</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>start_link(Name) -></FONT></DIV>
<DIV><FONT face="Courier New" size=2> spawn_link(fun() -></FONT></DIV>
<DIV><FONT face="Courier New"
size=2>
register(Name, self()),</FONT></DIV>
<DIV><FONT face="Courier New"
size=2>
event_loop([])</FONT></DIV>
<DIV><FONT face="Courier New"
size=2> end).</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>add_handler(Name, Module, State)
-></FONT></DIV>
<DIV><FONT face="Courier New" size=2> Name ! {add_handler, self(),
Module, State},</FONT></DIV>
<DIV><FONT face="Courier New" size=2> receive</FONT></DIV>
<DIV><FONT face="Courier New" size=2> ok ->
ok;</FONT></DIV>
<DIV><FONT face="Courier New" size=2> end.</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>notify(Name, Event) -></FONT></DIV>
<DIV><FONT face="Courier New" size=2> Name ! {event,
Event}.</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>event_loop(Modules) -></FONT></DIV>
<DIV><FONT face="Courier New" size=2> receive</FONT></DIV>
<DIV><FONT face="Courier New" size=2>
{add_handler, From, M, S} -></FONT></DIV>
<DIV><FONT face="Courier New"
size=2> From ! ok,</FONT></DIV>
<DIV><FONT face="Courier New"
size=2>
event_loop([{M,S}|Modules]);</FONT></DIV>
<DIV><FONT face="Courier New" size=2> {event, E}
-></FONT></DIV>
<DIV><FONT face="Courier New"
size=2> Modules1 =
call_modules(Modules, E),</FONT></DIV>
<DIV><FONT face="Courier New"
size=2>
event_loop(Modules1)</FONT></DIV>
<DIV><FONT face="Courier New" size=2> end.</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>call_modules([{M,S}|Modules], E)
-></FONT></DIV>
<DIV><FONT face="Courier New" size=2> case catch M:handle_event(E,
S) of</FONT></DIV>
<DIV><FONT face="Courier New" size=2> {'EXIT', _}
-></FONT></DIV>
<DIV><FONT face="Courier New"
size=2> %% remove
handler</FONT></DIV>
<DIV><FONT face="Courier New"
size=2> call_modules(Modules,
E);</FONT></DIV>
<DIV><FONT face="Courier New" size=2> {ok, NewS}
-></FONT></DIV>
<DIV><FONT face="Courier New"
size=2>
[{M,NewS}|call_modules(Modules, E)]</FONT></DIV>
<DIV><FONT face="Courier New" size=2> end;</FONT></DIV>
<DIV><FONT face="Courier New" size=2>call_modules([], _) -></FONT></DIV>
<DIV><FONT face="Courier New" size=2> [].</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT size=2>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.</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>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.</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>/Uffe</FONT></DIV>
<BLOCKQUOTE dir=ltr
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV
style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B>
<A title=sureshsaragadam@yahoo.com
href="mailto:sureshsaragadam@yahoo.com">Suresh S</A> </DIV>
<DIV style="FONT: 10pt arial"><B>To:</B> <A title=erlang-questions@erlang.org
href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</A>
</DIV>
<DIV style="FONT: 10pt arial"><B>Sent:</B> den 10 februari 2003 17:43</DIV>
<DIV style="FONT: 10pt arial"><B>Subject:</B> let me know the concept of
gen_event behaviour </DIV>
<DIV><BR></DIV>
<P>Hi,</P>
<P>There is an Event manager can add Event handlers, This event handlers will
handle particular event,</P>
<P>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 )</P>
<P>Please clarify this, let me know the concept of gen_event
behaviours </P>
<P>Thanking u in advance.</P>
<P>regards</P>
<P>Suresh S</P>
<P> </P>
<P> </P>
<P><IMG height=28 src="http://sg.yimg.com/i/aa/icons/28/cricket.gif" width=28>
Catch all the cricket action. Download <A
href="http://in.sports.yahoo.com/cricket/tracker.html" target=_blank>Yahoo!
Score tracker</A></P></BLOCKQUOTE></BODY></HTML>