[Twisted-Python] How to dispatch message to different servers

Benjamin BERTRAND beenje at gmail.com
Sat Mar 2 03:40:04 EST 2013


Le 2 mars 2013 à 04:34, exarkun at twistedmatrix.com a écrit :

> On 1 Mar, 09:52 pm, beenje at gmail.com wrote:
>> Thanks for the answer!
>> 
>> I was hoping to avoid having to put something like AMP in place, 
>> because it looked a bit overkill for my case.
>> I think I actually found a way :-)
> 
> Unfortunately, it looks like the code that you shared will only work 
> accidentally (if at all).  You cannot use Twisted APIs except in the 
> reactor thread.  You will at least need to add in some code to send data 
> back to the reactor thread before you use Twisted APIs (such as 
> `Deferred.callback`).


In run_pcap, I call reactor.callFromThread(callback, x25_data, line_id). See below.
That seems to work with the tests I did.
Am I missing something?

/Benjamin

def run_pcap(device, pcap_filter, callback):

    def analyse_packet(hdr, data):
        # check the data
        reactor.callFromThread(callback, x25_data, line_id)

    p = pcapy.open_live(device, 65535, 1, 100)
    p.setfilter(pcap_filter)
    p.loop(-1, analyse_packet)


class Oldimon(Protocol):

    def __init__(self, factory):
        self.factory = factory
        self.line = None

    def connectionMade(self):
        # Check the server port to get the line
        # associated to this protocol
        port = self.transport.getHost().port
        self.line = LINES_PORT[port]
        # Add the callback for this line
        self.factory.deferred[self.line] = defer.Deferred()
        self.factory.deferred[self.line].addCallback(self.messageToSend)


class OldimonFactory(ServerFactory):

    def __init__(self, device, pcap_filter):
        # pcapDataReceived callback is called everytime a message
        # is received
        reactor.callInThread(run_pcap, device, pcap_filter, self.pcapDataReceived)
        # Dict with a deferred for each line
        self.deferred = dict(zip(LINES_PORT.values(), [None] * len(LINES_PORT)))

    def buildProtocol(self, addr):
        return Oldimon(self)

    def pcapDataReceived(self, data, line):
        if self.deferred[line] is not None:
            # Fire the callback for line
            d, self.deferred[line] = self.deferred[line], None
            d.callback(data)

oldimon_factory = OldimonFactory(device, pcap_filter)
for port in LINES_PORT.keys():
    reactor.listenTCP(port, oldimon_factory)
reactor.run()


> 
> Jean-Paul
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python




More information about the Twisted-Python mailing list