<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><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Mar 2, 2013 at 2:28 PM, Benjamin BERTRAND <span dir="ltr"><<a href="mailto:beenje@gmail.com" target="_blank">beenje@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"><br><div><div>Le 2 mars 2013 à 10:08, Laurens Van Houtven <_@lvh.cc> a écrit :</div>
<div class="im"><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: <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 class="im"><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 messageToSend method (that wasn'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 class="h5"><div> self.factory.deferred[self.line] = defer.Deferred()</div>
<div> self.factory.deferred[self.line].addCallback(self.messageToSend)</div></div></div></div><div><div class="h5"><div><br></div><br><blockquote type="cite">
<div class="gmail_quote">On Mar 2, 2013 9:50 AM, "Benjamin BERTRAND" <<a href="mailto:beenje@gmail.com" target="_blank">beenje@gmail.com</a>> 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>
> On 1 Mar, 09:52 pm, <a href="mailto:beenje@gmail.com" target="_blank">beenje@gmail.com</a> wrote:<br>
>> Thanks for the answer!<br>
>><br>
>> I was hoping to avoid having to put something like AMP in place,<br>
>> because it looked a bit overkill for my case.<br>
>> I think I actually found a way :-)<br>
><br>
> Unfortunately, it looks like the code that you shared will only work<br>
> accidentally (if at all). You cannot use Twisted APIs except in the<br>
> reactor thread. You will at least need to add in some code to send data<br>
> back to the reactor thread before you use Twisted APIs (such as<br>
> `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>
><br>
> Jean-Paul<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>
<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">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>