<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; ">Thanks for the answer!<div><br></div><div>I was hoping to avoid having to put something like AMP in place, because it looked a bit overkill for my case.</div><div>I think I actually found a way :-)</div><div><br></div><div>I guess I can use the same factory to start all my servers.&nbsp;</div><div><div>So I start my sniffer (in a thread) in my ServerFactory and I keep a dictionary of deferred for each line (corresponds to an emitter and a server port).</div><div>It allows me to call the messageToSend method corresponding to the proper server when receiving data.</div><div><br></div><div>Here is a code extract of what I implemented:</div><div><br></div><div><div>class Oldimon(Protocol):</div><div><br></div><div>&nbsp; &nbsp; def __init__(self, factory):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; self.factory = factory</div><div>&nbsp; &nbsp; &nbsp; &nbsp; self.line = None</div><div><br></div><div>&nbsp; &nbsp; def connectionMade(self):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; # Check the server port to get the line</div><div>&nbsp; &nbsp; &nbsp; &nbsp; # associated to this protocol</div><div>&nbsp; &nbsp; &nbsp; &nbsp; port = self.transport.getHost().port</div><div>&nbsp; &nbsp; &nbsp; &nbsp; self.line = LINES_PORT[port]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; # Add the callback for this line</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><div><br></div><div><div>class OldimonFactory(ServerFactory):</div><div><br></div><div>&nbsp; &nbsp; def __init__(self, device, pcap_filter):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; # pcapDataReceived callback is called everytime a message</div><div>&nbsp; &nbsp; &nbsp; &nbsp; # is received</div><div>&nbsp; &nbsp; &nbsp; &nbsp; reactor.callInThread(run_pcap, device, pcap_filter, self.pcapDataReceived)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; # Dict with a deferred for each line</div><div>&nbsp; &nbsp; &nbsp; &nbsp; self.deferred = dict(zip(LINES_PORT.values(), [None] * len(LINES_PORT)))</div><div><br></div><div>&nbsp; &nbsp; def buildProtocol(self, addr):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return Oldimon(self)</div><div><br></div><div>&nbsp; &nbsp; def pcapDataReceived(self, data, line):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if self.deferred[line] is not None:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Fire the callback for line</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d, self.deferred[line] = self.deferred[line], None</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d.callback(data)</div></div><div><br></div><div>oldimon_factory&nbsp;=&nbsp;OldimonFactory(device, pcap_filter)</div><div>for port in LINES_PORT.keys():</div><div>&nbsp; &nbsp; reactor.listenTCP(port,&nbsp;oldimon_factory)</div><div>reactor.run()</div><div><br></div><div><br><div><div>Le 1 mars 2013 à 15:31, Laurens Van Houtven &lt;_@lvh.cc&gt; a écrit :</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr"><div><div>Well, you'd presumably have a connection to each of the servers in the form of a client factory and a protocol instance. Then, every time you get a message, you figure out which protocol instance you want (the one for the appropriate server) and send a message to it. You could do that with self.transport.write, of course, but it would be much easier to just use a ready-made RPC thing.<br>

<br>One such RPC thing is AMP, which comes with Twisted. You can read more about it here:<br><br><a href="http://amp-protocol.net/">http://amp-protocol.net/</a><br><a href="https://twistedmatrix.com/documents/current/core/howto/amp.html">https://twistedmatrix.com/documents/current/core/howto/amp.html</a><br>

<br></div>You will probably end up having a command like HandlePacket or something (presumably you can come up with a more apt domain-specific name), and something close to self.servers[serverFor(packet.origin)].callRemote(HandlePacket, packet.data), or whatever.<br>

<br></div>I realize this is still pretty vague and high level, so feel free to ask more questions about the parts that are unclear :)<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Feb 28, 2013 at 10:59 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">Hi,<br>
<br>
I have to replace a piece of software that sniffs the traffic on one interface. It gets different messages that are each associated to a specific emitter.<br>
On the same machine, one server is started for each emitter (on a different port).<br>
And the application is just supposed to use the proper server to send the messages captured (to a client on another machine).<br>
<br>
Not sure if it's clear, but basically if I have 2 emitters A and B, I'll start 2 servers (a and b).<br>
My sniffer will get messages A1, A2, B1, B2, B3...<br>
I have to pass messages A1, A2 to server a, that will just send them to the client (if it is connected of course).<br>
And B1, B2, B3 to server b.<br>
I don't need any buffering. If no client is connected, messages captured are just discarded.<br>
<br>
To sniff the network, I want to use pylibpcap or pcapy.<br>
I found this example to make it work with twisted: <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><br>


<br>
Starting several servers that use the same protocol is not a problem.<br>
But how do I pass the messages captured to the right server?<br>
How do I make the link between the function sniffing the network and the servers?<br>
<br>
Thanks<br>
<br>
Benjamin<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><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></div></div></body></html>