<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 à 10:08, Laurens Van Houtven &lt;_@lvh.cc&gt; a écrit :</div><br class="Apple-interchange-newline"><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>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/">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><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>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>&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><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">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; position: static; z-index: auto; ">
<br>
Le 2 mars 2013 à 04:34, <a href="mailto:exarkun@twistedmatrix.com">exarkun@twistedmatrix.com</a> a écrit :<br>
<br>
&gt; On 1 Mar, 09:52 pm, <a href="mailto:beenje@gmail.com">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">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">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">Twisted-Python@twistedmatrix.com</a><br>http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python<br></blockquote></div><br></body></html>