[Twisted-Python] ReconnectingClientFactory

Skinny Puppy skin_pup-twisted at damnable.happypoo.com
Mon Dec 23 10:09:03 MST 2002


ReconnectingClientFactory could not be pickled.  This did not seam to make
much sence :), well the cleanest way I could get it to is the
following.  It really is far from clean but works for me at least for me
now.  

Jeremy Rossi 


--- protocol.py	Mon Oct  7 20:29:22 2002
+++ protocol2.py	Mon Dec 23 12:00:10 2002
@@ -192,28 +192,24 @@
     retries = 0
     maxRetries = None
     _callID = None
+    _stop = None
     connector = None
 
     def clientConnectionFailed(self, connector, reason):
-        self.connector = connector
         if not reason.check(error.UserError):
-            self.retry()
+            self.retry(connector)
 
     def clientConnectionLost(self, connector, unused_reason):
-        self.connector = connector
-        self.retry()
+        self.retry(connector)
 
     def retry(self, connector=None):
         """Have this connector connect again, after a suitable delay.
         """
         if connector is None:
-            if self.connector is None:
-                raise ValueError("no connector to retry")
-            else:
-                connector = self.connector
+            raise ValueError("no connector to retry")
 
         self.retries += 1
-        if self.maxRetries is not None and (self.retries > self.maxRetries):
+        if self._stop or (self.maxRetries is not None and (self.retries > self.maxRetries)):
             log.msg("Abandoning %s after %d retries." %
                     (connector, self.retries))
             return
@@ -225,18 +221,13 @@
             
         log.msg("%s will retry in %d seconds" % (connector, self.delay,))
         from twisted.internet import reactor
-        self._callID = reactor.callLater(self.delay, connector.connect)
+        reactor.callLater(self.delay, connector.connect)
 
     def stopTrying(self):
         """I put a stop to any attempt to reconnect in progress.
         """
-        # ??? Is this function really stopFactory?
-        if self._callID:
-            self._callID.cancel()
-        if self.connector and self.connector.state == "connecting":
-            # Hopefully this doesn't just make clientConnectionFailed
-            # retry again.
-            self.connector.stopConnecting()
+        # XXX CHeap work around
+        self._stop = 1
 
     def resetDelay(self):
         """Call me after a successful connection to reset.





More information about the Twisted-Python mailing list