[Twisted-Python] connectionLost issue

Mark Papazyan Mark.Papazyan at gmx.de
Thu Nov 22 01:27:19 MST 2007


Hello,

i'm currently writing distributed system consisted of several components,
which have both input and output ports. To solve the initial startup
problem i simply use the retry approach: as soon as my components are
initialized the try to connect to their peers in a loop.
The problem is that the connectionLost function is not called for each
Factories independently, but stops callbacking as soon as my component
receives an incoming connection (although, the function belongs to the
class responsible for the outgoing connection). Thus, the component
assumes it has found all its peers, although only incoming connection is
initiated, not the outgoing. Is there a way to tell the framework to
handle the connection lost events for each Factory/Protocol objects
separately?

The code is trivial:


class InputSession(Factory):

   def start(self):
       reactor.listenTCP(self.port, self)

   def startedConnecting(self, connector):
       print 'Started to connect.'


   def buildProtocol(self, addr):
       print 'Connected.'
       self.connector = InputConnector(self)
       return self.connector


   def clientConnectionLost(self, connector, reason):
       print 'Lost connection. Reason:', reason


   def clientConnectionFailed(self, connector, reason):
       print 'Connection failed. Reason:', reason

################################################################################

class InputConnector(Protocol):

   def __init__(self, session):
       self.session = session

   def connectionMade(self):
       print "Connection established"

   def connectionLost(self, reason):
       print "Connection lost"

   def dataReceived(self, input_stream):
       print "Receiving: " + input_stream



################################################################################

class OutputConnector (Protocol):

   def __init__(self, session):
       self.session = session


   def sendStream(self, stream):
       self.transport.writeSomeData(stream)

   def clientConnectionLost(self, connector, reason):
       print 'Lost connection.'# Reason:', reason



################################################################################

class OutputSession(ClientFactory):

   def start(self):
       reactor.connectTCP(self.host, self.port, self)


   def buildProtocol(self, addr):
       print 'Connected.'
       self.curr_tries = 0
       self.connector = OutputConnector(self)

       return self.connector

   def clientConnectionLost(self, connector, reason):
       print 'Lost connection.'# Reason:', reason

       self.curr_tries = self.curr_tries + 1

       print "Trying to reconnect: %d" % self.curr_tries
       connector.connect()

   def clientConnectionFailed(self, connector, reason):
       print 'Connection failed.'# Reason:', reason

       self.curr_tries = self.curr_tries + 1

       connector.connect()


Best Regards,
Mark Papazyan
-- 
GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail




More information about the Twisted-Python mailing list