<div dir="ltr">I&#39;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&#39;s not a dispatch mechanism (and if you&#39;re using it as one now, you do have dispatch logic, it just lives somewhere else).<br>

</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&#39;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&#39;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></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&#39;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: <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&#39;s better to put everything in one post. People shouldn&#39;t have to click links.</div><div><br><blockquote type="cite"><p>I&#39;m on my phone at the moment which isn&#39;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 messageToSend method (that wasn&#39;t in the sample either).</div><div>In the Oldimon class, I have:</div>



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



<div>        self.factory.deferred[self.line].addCallback(self.messageToSend)</div></div></div></div><div><div><div><br></div><br><blockquote type="cite">
<div class="gmail_quote">On Mar 2, 2013 9:50 AM, &quot;Benjamin BERTRAND&quot; &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).  You cannot use Twisted APIs except in the<br>
&gt; reactor thread.  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>
    def analyse_packet(hdr, data):<br>
        # check the data<br>
        reactor.callFromThread(callback, x25_data, line_id)<br>
<br>
    p = pcapy.open_live(device, 65535, 1, 100)<br>
    p.setfilter(pcap_filter)<br>
    p.loop(-1, analyse_packet)<br>
<br>
<br>
class Oldimon(Protocol):<br>
<br>
    def __init__(self, factory):<br>
        self.factory = factory<br>
        self.line = None<br>
<br>
    def connectionMade(self):<br>
        # Check the server port to get the line<br>
        # associated to this protocol<br>
        port = self.transport.getHost().port<br>
        self.line = LINES_PORT[port]<br>
        # Add the callback for this line<br>
        self.factory.deferred[self.line] = defer.Deferred()<br>
        self.factory.deferred[self.line].addCallback(self.messageToSend)<br>
<br>
<br>
class OldimonFactory(ServerFactory):<br>
<br>
    def __init__(self, device, pcap_filter):<br>
        # pcapDataReceived callback is called everytime a message<br>
        # is received<br>
        reactor.callInThread(run_pcap, device, pcap_filter, self.pcapDataReceived)<br>
        # Dict with a deferred for each line<br>
        self.deferred = dict(zip(LINES_PORT.values(), [None] * len(LINES_PORT)))<br>
<br>
    def buildProtocol(self, addr):<br>
        return Oldimon(self)<br>
<br>
    def pcapDataReceived(self, data, line):<br>
        if self.deferred[line] is not None:<br>
            # Fire the callback for line<br>
            d, self.deferred[line] = self.deferred[line], None<br>
            d.callback(data)<br>
<br>
oldimon_factory = OldimonFactory(device, pcap_filter)<br>
for port in LINES_PORT.keys():<br>
    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></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>