<html><head><meta http-equiv="Content-Type" content="text/html charset=iso-8859-1"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>Le 2 mars 2013 à 15:33, Laurens Van Houtven &lt;_@lvh.cc&gt; a écrit :</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr">I'm guessing that this is another question that will be solved as soon as I see the code (perhaps you should put all your code up somewhere); but all I do know is that all Deferreds buy you is an abstraction for organizing callbacks; it's not a dispatch mechanism (and if you're using it as one now, you do have dispatch logic, it just lives somewhere else).<br></div></blockquote><div><br></div><div>The dispatch is done in the&nbsp;pcapDataReceived method (in the ServerFactory) thanks to the dictionary of deferred (using the line id as key).</div><div>And each line is associated to a server on a specific port.</div><div>It's maybe a bit strange, but does what I want.</div><div><br></div><div>Here is the code if you are interested:&nbsp;<a href="http://pastebin.com/VeQgQ85z">http://pastebin.com/VeQgQ85z</a></div><div>Unfortunately, I can't give any data to test it (I only have live data that I don't think I can share)...</div><div><br></div><br><blockquote type="cite"><div dir="ltr">

</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Mar 2, 2013 at 3:25 PM, Benjamin BERTRAND <span dir="ltr">&lt;<a href="mailto:beenje@gmail.com" target="_blank">beenje@gmail.com</a>&gt;</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"><br><div><div>Le 2 mars 2013 à 14:53, Laurens Van Houtven &lt;_@lvh.cc&gt; a écrit :</div>

<div class="im"><br><blockquote type="cite"><div dir="ltr">Ah, but that too appears to be missing in the original code ;-)<br><br><div>The stuff you're doing with deferreds there seems a bit strange. In your example, why not just call sendMessage when you get the packet?<br>

</div></div></blockquote><div><br></div></div><div>If I had only one server yes.</div><div>But the thing is I have to send the message to a different server depending on the line id.</div><div>That was my initial problem.</div>

<div>I don't see how I could call the sendMessage method corresponding to a specific server directly.</div><div>Or?</div><div><div class="h5"><div><br></div><blockquote type="cite"><div dir="ltr">

</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Mar 2, 2013 at 2:28 PM, Benjamin BERTRAND <span dir="ltr">&lt;<a href="mailto:beenje@gmail.com" target="_blank">beenje@gmail.com</a>&gt;</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"><br><div><div>Le 2 mars 2013 à 10:08, Laurens Van Houtven &lt;_@lvh.cc&gt; a écrit :</div>



<div><br><blockquote type="cite"><p>Yes, that looks okay, but that wasn't in your original sample ;-)</p><div><br></div></blockquote><div><br></div></div><div>Yep, sorry about that.</div><div>I was more focused on the ServerFactory and Protocol.</div>



<div>The pcap in a thread comes from the link I mentioned in my first post:&nbsp;<a href="http://dound.com/2009/09/integrating-twisted-with-a-pcap-based-python-packet-sniffer/" target="_blank">http://dound.com/2009/09/integrating-twisted-with-a-pcap-based-python-packet-sniffer/</a></div>



<div>But I know, it's better to put everything in one post. People shouldn't have to click links.</div><div><br><blockquote type="cite"><p>I'm on my phone at the moment which isn't great for code review, but it looks like you only fire one deferred per line?</p>



<div><br></div></blockquote><div><br></div></div><div>There is a specific deferred by line.</div><div>I re-arm it in the&nbsp;messageToSend method (that wasn't in the sample either).</div><div>In the Oldimon class, I have:</div>



<div><br></div><div><div>&nbsp; &nbsp; def messageToSend(self, message):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; self.sendMessage(message)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; # Re-arm the deferred</div><div><div>&nbsp; &nbsp; &nbsp; &nbsp; self.factory.deferred[self.line] = defer.Deferred()</div>



<div>&nbsp; &nbsp; &nbsp; &nbsp; self.factory.deferred[self.line].addCallback(self.messageToSend)</div></div></div><div><div><br></div><br><blockquote type="cite">
<div class="gmail_quote">On Mar 2, 2013 9:50 AM, "Benjamin BERTRAND" &lt;<a href="mailto:beenje@gmail.com" target="_blank">beenje@gmail.com</a>&gt; wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">




<br>
Le 2 mars 2013 à 04:34, <a href="mailto:exarkun@twistedmatrix.com" target="_blank">exarkun@twistedmatrix.com</a> a écrit :<br>
<br>
&gt; On 1 Mar, 09:52 pm, <a href="mailto:beenje@gmail.com" target="_blank">beenje@gmail.com</a> wrote:<br>
&gt;&gt; Thanks for the answer!<br>
&gt;&gt;<br>
&gt;&gt; I was hoping to avoid having to put something like AMP in place,<br>
&gt;&gt; because it looked a bit overkill for my case.<br>
&gt;&gt; I think I actually found a way :-)<br>
&gt;<br>
&gt; Unfortunately, it looks like the code that you shared will only work<br>
&gt; accidentally (if at all). &nbsp;You cannot use Twisted APIs except in the<br>
&gt; reactor thread. &nbsp;You will at least need to add in some code to send data<br>
&gt; back to the reactor thread before you use Twisted APIs (such as<br>
&gt; `Deferred.callback`).<br>
<br>
<br>
In run_pcap, I call reactor.callFromThread(callback, x25_data, line_id). See below.<br>
That seems to work with the tests I did.<br>
Am I missing something?<br>
<br>
/Benjamin<br>
<br>
def run_pcap(device, pcap_filter, callback):<br>
<br>
&nbsp; &nbsp; def analyse_packet(hdr, data):<br>
&nbsp; &nbsp; &nbsp; &nbsp; # check the data<br>
&nbsp; &nbsp; &nbsp; &nbsp; reactor.callFromThread(callback, x25_data, line_id)<br>
<br>
&nbsp; &nbsp; p = pcapy.open_live(device, 65535, 1, 100)<br>
&nbsp; &nbsp; p.setfilter(pcap_filter)<br>
&nbsp; &nbsp; p.loop(-1, analyse_packet)<br>
<br>
<br>
class Oldimon(Protocol):<br>
<br>
&nbsp; &nbsp; def __init__(self, factory):<br>
&nbsp; &nbsp; &nbsp; &nbsp; self.factory = factory<br>
&nbsp; &nbsp; &nbsp; &nbsp; self.line = None<br>
<br>
&nbsp; &nbsp; def connectionMade(self):<br>
&nbsp; &nbsp; &nbsp; &nbsp; # Check the server port to get the line<br>
&nbsp; &nbsp; &nbsp; &nbsp; # associated to this protocol<br>
&nbsp; &nbsp; &nbsp; &nbsp; port = self.transport.getHost().port<br>
&nbsp; &nbsp; &nbsp; &nbsp; self.line = LINES_PORT[port]<br>
&nbsp; &nbsp; &nbsp; &nbsp; # Add the callback for this line<br>
&nbsp; &nbsp; &nbsp; &nbsp; self.factory.deferred[self.line] = defer.Deferred()<br>
&nbsp; &nbsp; &nbsp; &nbsp; self.factory.deferred[self.line].addCallback(self.messageToSend)<br>
<br>
<br>
class OldimonFactory(ServerFactory):<br>
<br>
&nbsp; &nbsp; def __init__(self, device, pcap_filter):<br>
&nbsp; &nbsp; &nbsp; &nbsp; # pcapDataReceived callback is called everytime a message<br>
&nbsp; &nbsp; &nbsp; &nbsp; # is received<br>
&nbsp; &nbsp; &nbsp; &nbsp; reactor.callInThread(run_pcap, device, pcap_filter, self.pcapDataReceived)<br>
&nbsp; &nbsp; &nbsp; &nbsp; # Dict with a deferred for each line<br>
&nbsp; &nbsp; &nbsp; &nbsp; self.deferred = dict(zip(LINES_PORT.values(), [None] * len(LINES_PORT)))<br>
<br>
&nbsp; &nbsp; def buildProtocol(self, addr):<br>
&nbsp; &nbsp; &nbsp; &nbsp; return Oldimon(self)<br>
<br>
&nbsp; &nbsp; def pcapDataReceived(self, data, line):<br>
&nbsp; &nbsp; &nbsp; &nbsp; if self.deferred[line] is not None:<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Fire the callback for line<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d, self.deferred[line] = self.deferred[line], None<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d.callback(data)<br>
<br>
oldimon_factory = OldimonFactory(device, pcap_filter)<br>
for port in LINES_PORT.keys():<br>
&nbsp; &nbsp; reactor.listenTCP(port, oldimon_factory)<br>
reactor.run()<br>
<br>
<br>
&gt;<br>
&gt; Jean-Paul<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; Twisted-Python mailing list<br>
&gt; <a href="mailto:Twisted-Python@twistedmatrix.com" target="_blank">Twisted-Python@twistedmatrix.com</a><br>
&gt; <a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
<br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com" target="_blank">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</blockquote></div>
_______________________________________________<br>Twisted-Python mailing list<br><a href="mailto:Twisted-Python@twistedmatrix.com" target="_blank">Twisted-Python@twistedmatrix.com</a><br><a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>



</blockquote></div></div><br></div><br>_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com" target="_blank">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br><div dir="ltr">cheers<div>lvh</div></div>
</div>
_______________________________________________<br>Twisted-Python mailing list<br><a href="mailto:Twisted-Python@twistedmatrix.com" target="_blank">Twisted-Python@twistedmatrix.com</a><br><a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>

</blockquote></div></div></div><br></div><br>_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br><div dir="ltr">cheers<div>lvh</div></div>
</div>
_______________________________________________<br>Twisted-Python mailing list<br><a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python<br></blockquote></div><br></body></html>