[Twisted-Python] subclassing and mixing protocol.ReconnectingClientFactory and pb.PBClientFactory

Fabrizio Mancini mr.file at gmail.com
Thu Apr 30 17:03:06 EDT 2009


Hi,
i've implemented a pb client by writing a class which inherits from
object and a class which inherits from ReconnectingClientFactory and
PBClientFactory.
Till now it's all ok, the client detects the disconnection and try to
reconnect with an exponential delay if the connection is not
immediatly reestabilished.
The calls to the callRemote("get_item") method are repetitive, but for
sake of simplicity i've cut off the code that controls if there is
already another call running.
The problem:
i'd like to manage something like the clientConnectionMade method when
the client reconnect (thanks to the ReconnectingClientFactory) after a
disconnection and launch again the MypbClient.connect() method as i do
in the "if __name__ " statement, but i can't guess how to do it.
This is the code i'm using.
Thanks in advanche, any help appreciated, and hope to have been clear enough
Fabrizio

class MypbClient(object):

   def __init__(self):
       self.clientfactory = MypbClientFactory()
       self._scheduledCall] = task.LoopingCall(self.get_item)
       self._scheduledCall].start(10, False)

   def connect(self):
       reactor.connectTCP(server_ip, server_port, self.clientfactory)
       self.rootObj = self.clientfactory.getRootObject()
       self.rootObj.addCallback(self.initiateChain).addErrback(self._eb)

   def initiateChain(self, broker):
       self.broker = broker
       self.d = self.broker.callRemote("get_item")
       self.d.addCallback(self.got_item).addErrback(self._eb)

   def get_item(self, result):
       self.d = self.broker.callRemote("get_item")
       self.d.addCallback(self.got_item).addErrback(self._eb)

   def got_item(self, record):
       self.op = doSomethingWith(record) #returns a deferred

class MypbClientFactory(pb.PBClientFactory, protocol.ReconnectingClientFactory):
   def __init__(self):
       pb.PBClientFactory.__init__(self)

   def clientConnectionMade(self, broker):
       self.resetDelay()
       pb.PBClientFactory.clientConnectionMade(self, broker)

   def clientConnectionFailed(self, connector, reason):
       pb.PBClientFactory.clientConnectionFailed(self, connector, reason)
       if self.continueTrying:
           self.connector = connector
           self.retry()

   def clientConnectionLost(self, connector, reason, reconnecting=1):
       pb.PBClientFactory.clientConnectionLost(self, connector,
reason, reconnecting=reconnecting)
       protocol.ReconnectingClientFactory.clientConnectionLost(self,
connector, reason)

if __name__ == "__main__":
   dpc = MypbClient()
   dpc.connect()
   reactor.run()




More information about the Twisted-Python mailing list