[Twisted-Python] is it possible to have a (single) twisted client that
Jean-Paul Calderone
exarkun at divmod.com
Fri Jan 27 23:36:15 EST 2006
On Fri, 27 Jan 2006 20:16:49 -0800 (PST), john peter <neuzhoundxx at yahoo.com> wrote:
>Thank you. I wonder if I could ask a follow-on question.
> Below is a version of echoclient that seems to work.
> I overrode EchoFactory's __init__ method to initialize the client id
> "generator" to zero and the buildProtocol method to create a protocol
> instance and give that instance its own unique id. I stop the reactor when the last connection "signs off".
>
> I'm now wondering: what's the best way to enable the protocol instances to have different behaviors? Here's my current thinking: Maybe I can have EchoFactory pass an appropriate "delegate" object to each protocol instance so that the implementation of methods such as connectionMade would be something like this:
>
> def connectionMade(self):
> self.delegate.connectionMade()
>
> The approach I was thinking of using to pass this delegate from EchoFactory to a protocol instance was something "hokey" like if next_client_id = 1, then pass to the protocol instance the delegate object associated with a key of 1 in the dictionary { 1:delegate1, 2:delegate2, ...}. Are there better approaches available? Again, thanks for any help or advice!
>
You could make the "delegates" into the actual protocols:
from twisted.internet import protocol
class ClientOne(protocol.Protocol):
# Stuff for the first client to do
class ClientTwo(protocol.Protocol):
# Stuff for the second client to do
# etc
for protoClass in ClientOne, ClientTwo:
f = protocol.ClientFactory()
f.protocol = protoClass
reactor.connectTCP("host", 8000, f)
Jean-Paul
More information about the Twisted-Python
mailing list