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!
sample code for a multi-connect client:
-----------------------------------------------------------------------------------------------------------
from twisted.internet import reactor, protocol
# a client protocol
class EchoClient(protocol.Protocol):
"""Once connected, send a message, then print the result."""
def __init__(self, id):
self.id = id
def connectionMade(self):
self.transport.write("sender# %d says: hello, world!" % self.id)
def dataReceived(self, data):
"As soon as any data is received, write it back."
print "Server said:", data
self.transport.loseConnection()
def connectionLost(self, reason):
print "connection lost"
from twisted.internet import reactor
#reactor.stop()
class EchoFactory(protocol.ClientFactory):
protocol = EchoClient
def __init__(self):
self.next_client_id = 0
def buildProtocol(self, addr):
print "buildProtocol called"
self.next_client_id += 1
p = self.protocol(self.next_client_id)
p.factory = self
return p
def clientConnectionFailed(self, connector, reason):
print "Connection failed - goodbye!"
reactor.stop()
def clientConnectionLost(self, connector, reason):
print "Connection lost - goodbye!"
print "numPorts= %d" % self.numPorts
if self.numPorts == 1:
reactor.stop()
# this connects the protocol to a server runing on port 8000
def main():
f = EchoFactory()
reactor.connectTCP("localhost", 8000, f)
reactor.connectTCP("localhost", 8000, f)
reactor.connectTCP("localhost", 8000, f)
reactor.run()
# this only runs if the module was *not* imported
if __name__ == '__main__':
main()
Jean-Paul Calderone <exarkun at divmod.com> wrote: On Thu, 26 Jan 2006 20:27:34 -0800 (PST), john peter wrote:
> makes more than one (say, three) connections to the same server instance?
> i was thinking of something like this: an 'ADD' connection that sends new data items to the server for saving, an 'UPDATE' connection that sends data update requests, and a 'DELETE' connection that sends data deletion requests. All three connections share DATA ID information, but should otherwise have "independent" existence so that they can be configured to have different "behavioral" attributes; that is, the ADD connection saves the data ids for items successfully persisted to an 'in-memory' table for later lookups, and UPDATE and DELETE requests can only be issued for DATA IDs known to be valid for the "current" session (thus, the "in-memory" table lookups) and each connection may have attributes such as "send" rates, etc.
>> If this is possible, could someone please give me some pointers on what twisted code to study/ look at? thank you very much for your help!
>
This is as easy as calling reactor.connectTCP() three times. There's nothing special about the first call, or the second call, or the third call. Each one just sets up a connection attempt.
Try taking echoclient.py from the core examples and adding some more connectTCP calls to it, see what happens.
Jean-Paul
_______________________________________________
Twisted-Python mailing list
Twisted-Python at twistedmatrix.comhttp://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
---------------------------------
What are the most popular cars? Find out at Yahoo! Autos
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20060127/182254d4/attachment.htm