<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Hello,</div><div><br></div><div>you could achieve dynamic behavior through add_handler/add_sup_handler and delete_handler.</div><div>The major point here you are re-using existed OTP function. </div><div><br></div><div>The requirement to accure all of the state of another event handler might be challenging. </div><div>I believe you need to </div><div> a) either execute callback in dedicated process spawn(fun() -> Callback(Payload) end)</div><div> b) re-factor your listener concept where event handlers independent. I would propose to follow-up this way.</div><div><br></div><div>- Dmitry</div><br><div><div>On Oct 25, 2012, at 7:01 PM, Son Tran-Nguyen wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div><div>>Could you please elaborate</div><div>> * why do you have a list of callbacks within an event manager? Event Manager manages a list of event handlers for you?</div><div><br></div><div>I want a dynamic way to define event handlers. Instead of statically defining event handlers in callback modules, I want some way for other processes to listen to when a gen_event manager receives event and act on that. By using a Fun as callback, it call inherits the scope of the process which calls it, adding more flexibility.</div>

<div><br></div><div>> * why do you try to call event handler?</div></div><font color="#3366ff"><font><font face="tahoma,sans-serif"><div><font color="#3366ff"><font><font face="tahoma,sans-serif"><br>Mostly to acquire some or all of the state of another event handler module.</font></font></font></div>

<div><font color="#3366ff"><font><font face="tahoma,sans-serif"><br></font></font></font></div><br clear="all"></font></font></font>Sincerely,<br><br><div><br>Son Tran-Nguyen</div><br>
<br><br><div class="gmail_quote">On Thu, Oct 25, 2012 at 10:56 AM, Dmitry Kolesnikov <span dir="ltr"><<a href="mailto:dmkolesnikov@gmail.com" target="_blank">dmkolesnikov@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div style="word-wrap:break-word">Hello,<div><br></div><div>In the nutshell, you are having deadlock at gen_event:call within event manager.</div><div><br></div><div>Could you please elaborate</div><div> * why do you have a list of callbacks within an event manager? Event Manager manages a list of event handlers for you?</div>

<div> * why do you try to call event handler?</div><div><br></div><div>I think in your case, a gen_server is required so that event_handler catches an event and passes it to gen_server that does a magical job for you.</div>

<div><br></div><div>- Dmitry</div><div><br><div><div><div class="h5"><div>On Oct 25, 2012, at 6:40 PM, Son Tran-Nguyen wrote:</div><br></div></div><blockquote type="cite"><div><div class="h5"><div><font color="#3366ff"><font><font face="tahoma,sans-serif">Hi,</font></font></font></div>

<div><font color="#3366ff"><font><font face="tahoma,sans-serif"><br></font></font></font></div><div><font color="#3366ff"><font><font face="tahoma,sans-serif">I am trying to make a listener tree using gen_event behavior.</font></font></font></div>



<div><font color="#3366ff"><font><font face="tahoma,sans-serif"><br></font></font></font></div><div><font color="#3366ff"><font><font face="tahoma,sans-serif">I have a listener module, which will have an event type and a </font></font></font></div>



<div><font color="#3366ff"><font><font face="tahoma,sans-serif">list of callbacks in its state. </font></font></font><span style="font-family:tahoma,sans-serif;color:rgb(51,102,255)">It will receive event in format </span></div>



<div><span style="font-family:tahoma,sans-serif;color:rgb(51,102,255)">{EventName, Payload}, and call all the callbacks with Payload</span></div><div><span style="font-family:tahoma,sans-serif;color:rgb(51,102,255)">if EventName matches the event type it handles:</span></div>



<div><span style="font-family:tahoma,sans-serif;color:rgb(51,102,255)"><br></span></div><div><span style="font-family:tahoma,sans-serif;color:rgb(51,102,255)"><div>handle_event({EventName, Payload}, {EventName, Callbacks}) -></div>



<div><span style="white-space:pre-wrap">  </span>[Callback(Payload) || Callback <- Callbacks],</div><div><span style="white-space:pre-wrap"> </span>{ok, {EventName, Callbacks}};</div>

</span></div><div><span style="font-family:tahoma,sans-serif;color:rgb(51,102,255)"><br></span></div><div><span style="font-family:tahoma,sans-serif;color:rgb(51,102,255)">In another module, I have a function</span></div>



<div><span style="font-family:tahoma,sans-serif;color:rgb(51,102,255)"><br></span></div><div><span style="font-family:tahoma,sans-serif;color:rgb(51,102,255)">on(EventName, EventMgr, Callback)</span></div><div><span style="font-family:tahoma,sans-serif;color:rgb(51,102,255)"><br>



</span></div><div><font color="#3366ff" face="tahoma, sans-serif">Which allows me to add the listener callback module into a</font></div><div><font color="#3366ff" face="tahoma, sans-serif">gen_event manager EventMgr for EventName.</font></div>



<div><font color="#3366ff" face="tahoma, sans-serif"><br></font></div><div><font color="#3366ff" face="tahoma, sans-serif">The EventMgr also has other callback modules to hold other</font></div><div><font color="#3366ff" face="tahoma, sans-serif">states.</font></div>



<div><font color="#3366ff" face="tahoma, sans-serif"><br></font></div><div><font color="#3366ff" face="tahoma, sans-serif">If my Callback is like this</font></div><div><font color="#3366ff" face="tahoma, sans-serif"><br>


</font></div>
<div><font color="#3366ff" face="tahoma, sans-serif">Callback = fun(Payload) -></font></div><div><font color="#3366ff" face="tahoma, sans-serif">  io:format("About to run callback~n),</font></div><div><font color="#3366ff" face="tahoma, sans-serif">  Prop = gen_event:call(EventMgr, other_module, some_property)</font></div>



<font color="#3366ff"><font><font face="tahoma,sans-serif"><div><font color="#3366ff"><font><font face="tahoma,sans-serif">  io:format("This never runs~n")</font></font></font></div><div><font color="#3366ff"><font><font face="tahoma,sans-serif">end</font></font></font></div>



<div><font color="#3366ff"><font><font face="tahoma,sans-serif"><br></font></font></font></div><div><font color="#3366ff"><font><font face="tahoma,sans-serif">The problem is gen_event:call is not running inside the Callback.</font></font></font></div>



<div><font color="#3366ff"><font><font face="tahoma,sans-serif">I don't event see the second io:format</font></font></font></div><div><font color="#3366ff"><font><font face="tahoma,sans-serif"><br></font></font></font></div>



<div><font color="#3366ff"><font><font face="tahoma,sans-serif">To think about it, the Callback is executed inside a handle_event</font></font></font></div><div>of the EventMgr, so I guess it's not in the right scope of something?</div>



<div><br></div><div>It's also the same if inside a handle_event of some other module,</div><div>I do gen_event:call(self(), a_different_module, some_property), so</div><div>I take that this is not allowed.</div><div>


<br>
</div><div>Is there anyway to do what I want to do?</div><br clear="all"></font></font></font>Sincerely,<br><br><div><br>Son Tran-Nguyen</div></div></div>
_______________________________________________<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br><a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>

</blockquote></div><br></div></div></blockquote></div><br>
</blockquote></div><br></body></html>