[Twisted-Python] UDP Factories

I have a UDP protocol that subclasses DatagramProtocol. It is working fine, but I decided I wanted a protocol factory so I can keep track of the list of clients. I added the following class, which doesn't nothing currently... class MyProtoFactory(Factory): protocol = MyProto And then changed my reactor.listenUDP line to... reactor.listenUDP(config.SERVER_PORT, MyProtoFactory()) [justinj@chewie travis]$ ./udpserver.py Traceback (most recent call last): File "./udpserver.py", line 166, in ? reactor.listenUDP(config.SERVER_PORT, MyProtoFactory()) File "/usr/local/lib/python2.3/site-packages/twisted/internet/default.py", line 180, in listenUDP p = udp.Port(port, protocol, interface, maxPacketSize, self) File "/usr/local/lib/python2.3/site-packages/twisted/internet/udp.py", line 66, in __init__ assert isinstance(proto, protocol.DatagramProtocol) AssertionError Any idea what I'm doing wrong? The examples showed a UDP factory, but I don't quite see how this would work. Thanks. -Justin

On Fri, 19 Sep 2003 05:42:08 -0600 "Justin Johnson" <justinjohnson@fastmail.fm> wrote:
I have a UDP protocol that subclasses DatagramProtocol. It is working fine, but I decided I wanted a protocol factory so I can keep track of the list of clients. I added the following class, which doesn't nothing currently...
You will notice the docs say nothing about factories. This is because listenUDP does not accept factories :) If you want to keep track of clients you will have to do it yourself by dispatching based on the host/port of a received datagram. Why are you using UDP, by the way? -- Itamar Shtull-Trauring http://itamarst.org/ Available for Python & Twisted consulting

Yeah I sent this email out real quick without looking at the doc. Sorry. I'm using UDP because the device I'm coding for uses UDP. Twisted sure saved me a ton of time. Out of curiosity, why doesn't listenUDP accept Factories? On Fri, 19 Sep 2003 11:03:20 -0400, "Itamar Shtull-Trauring" <itamar@itamarst.org> said:
On Fri, 19 Sep 2003 05:42:08 -0600 "Justin Johnson" <justinjohnson@fastmail.fm> wrote:
I have a UDP protocol that subclasses DatagramProtocol. It is working fine, but I decided I wanted a protocol factory so I can keep track of the list of clients. I added the following class, which doesn't nothing currently...
You will notice the docs say nothing about factories. This is because listenUDP does not accept factories :) If you want to keep track of clients you will have to do it yourself by dispatching based on the host/port of a received datagram.
Why are you using UDP, by the way?
-- Itamar Shtull-Trauring http://itamarst.org/ Available for Python & Twisted consulting
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

On Fri, 19 Sep 2003 16:18:32 -0600 "Justin Johnson" <justinjohnson@fastmail.fm> wrote:
Yeah I sent this email out real quick without looking at the doc. Sorry.
I'm using UDP because the device I'm coding for uses UDP. Twisted sure saved me a ton of time.
Out of curiosity, why doesn't listenUDP accept Factories?
This would, to be a general purpose thing, need to create a protocol per datagram. This would be... inefficient. In TCP you have a protocol per connection. In UDP there are no connections. You might build one on top of UDP, in which case you would have a protocol-ish object you dispatch to, but how you determine this object and its lifecycle would be something you would have to determine. -- Itamar Shtull-Trauring http://itamarst.org/ Available for Python & Twisted consulting

On Fri, Sep 19, 2003 at 05:42:08AM -0600, Justin Johnson wrote:
I have a UDP protocol that subclasses DatagramProtocol. It is working fine, but I decided I wanted a protocol factory so I can keep track of the list of clients. I added the following class, which doesn't nothing currently...
class MyProtoFactory(Factory): protocol = MyProto
Factories aren't to be used with DatagramProtocols, since a single DatagramProtocol instance manages all traffic to and from the ports you designate. If you want to share state between different DatagramProtocol instances, give them a reference to a common object manually (not a factory, but perhaps a service), or reconsider and use a single instance to talk to all your clients. Nothing prevents you from listening on multiple ports with the same instance. Jp -- "Pascal is Pascal is Pascal is dog meat." -- M. Devine and P. Larson, Computer Science 340

On Fri, 19 Sep 2003 18:00:11 -0400 Jp Calderone <exarkun@intarweb.us> wrote:
If you want to share state between different DatagramProtocol instances, give them a reference to a common object manually (not a factory, but perhaps a service), or reconsider and use a single instance to talk to all your clients. Nothing prevents you from listening on multiple ports with the same instance.
Not really, there's a self.transport, so you can only connect a datagramprotocol to a single port. -- Itamar Shtull-Trauring http://itamarst.org/ Available for Python & Twisted consulting
participants (3)
-
Itamar Shtull-Trauring
-
Jp Calderone
-
Justin Johnson