Nice, this looks much cleaner. I thought about the connection "lost" case. It's quite similar to "fail" (but client should always try to reconnect to host1 in that case). So easy to implement. Thanks a lot!
Benjamin Från: twisted-python-boun...@twistedmatrix.com [mailto:twisted-python-boun...@twistedmatrix.com] För Jason Rennie Skickat: den 11 januari 2011 14:11 Till: Twisted general discussion Ämne: Re: [Twisted-Python] client connecting to 2 servers (nonsimultaneously) You could probably generalize and simplify by using a collections.deque of hosts/ports and using the rotate() method before each reactor.connectTCP. Also, no need for multiple reactor imports---one at the top of the code is fine. Note that if the connection is "lost" in a non-clean fashion, you may also want to reconnect. IIUC, "fail" only handles the case that no connection is made (protocol is never created). import collections from twisted.internet import reactor class MyClientFactory(object): protocol = MyClientProtocol def __init__(self, hosts): self.hosts = collections.deque(hosts) reactor.callWhenRunning(reactor.connectTCP, self.hosts[0][0], self.hosts[0][1], self) self.hosts.rotate(1) def clientConnectionFailed(self, connector, reason): reactor.callLater(2.0, connectTCP, self.hosts[0][0], self.hosts[0][1], self) self.hosts.rotate(1) factory = MyClientFactory([('host1', port1), ('host2', port2), ...]) reactor.run() Cheers, Jason On Tue, Jan 11, 2011 at 5:17 AM, <benjamin.bertr...@lfv.se> wrote: Hi, I'm new to twisted and I have started to write a new protocol with a TCP client and server. In my protocol, a client should be able to connect to 2 servers (master/slave node - only the master accepts connection). The client should try to connect to server1. If it fails, try to connect to server2 (after a specific timeout). If that fails, try server1... I came up with a solution (see below). As I'm new to twisted and I haven't seen anything like that in the examples, I'd like to check if that's a proper way to do it. Any comments is welcome. Thanks Benjamin *********************************************** class MyClientFactory(ClientFactory): protocol = MyClientProtocol def __init__(self, host2=None): self.host1 = None self.host2 = host2 def clientConnectionFailed(self, connector, reason): from twisted.internet import reactor if self.host2 is None: # host2 is not defined, reconnect to host1 reactor.callLater(2.0, connector.connect) else: destination = connector.getDestination() if self.host1 is None: # First connection failed, initialize host1, and try host2 self.host1 = destination.host host = self.host2 elif destination.host == self.host1: # Connection to host1 failed, try host2 host = self.host2 else: # Connection to host2 failed, try host1 host = self.host1 reactor.callLater(2.0, reactor.connectTCP, host, destination.port, self) factory = MyClientFactory(server2) from twisted.internet import reactor reactor.connectTCP(server1, 8010, factory) reactor.run() *********************************************** _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python -- Jason Rennie Research Scientist, ITA Software 617-714-2645 http://www.itasoftware.com/ _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python