I agree that rotate(-1) is more intuitive. I barely gave it any thought---almost forgot to include an argument for rotate as I initially incorrectly assumed no argument would do something like rotate(-1) :-)
Jason On Tue, Jan 11, 2011 at 1:36 PM, Kevin Horn <kevin.h...@gmail.com> wrote: > Using a deque for this is a fantastic idea, though I would have used > rotate(-1), as your example looks like it rotate's "backwards" to me. > > Matter of taste I suppose. It doesn't make much actual difference. > > Kevin Horn > > > On Tue, Jan 11, 2011 at 7:11 AM, Jason Rennie <jren...@gmail.com> wrote: > >> 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 >> >> > > _______________________________________________ > 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