Twisted Async Client - Reconnection after connectionLost

849 views
Skip to first unread message

chi...@tecnoelettricabrianza.it

unread,
Sep 2, 2013, 5:14:27 AM9/2/13
to pymo...@googlegroups.com
Debian 7.0 with ii  python-pymodbus                       1.2.0-2                       all          full Modbus protocol implementation

I'm trying to expand the base example of the Twisted async Client to get a the client that reconnect if connection is lost.
I've looked at documentation and find:

Reconnects:  The default number of times a client should attempt to reconnect before deciding the server is down (0)

How can I overwrite this constant? 
tried with this without luck at the start of the script: 

from pymodbus.constants import Defaults
Defaults.Reconnects = 15


2) from twistedmatrix documentation I've added the funcions to my Protocol class: 

class ModbusClientPrt(protocol.ProcessProtocol):
    def __init__(self ):
        self.data = ""

    def connectionMade(self):
        print "connectionMade!"
        
    def connectionLost(self,reason):
        print "connectionLost!:",reason
        
    def clientConnectionLost(self, connector, reason):
        print ': Lost connection.  Reason:', reason
        ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
        
    def clientConnectionFailed(self, connector, reason):
        print 'Connessione MODBUS CLIENT: Connection failed. Reason:', reason
        ReconnectingClientFactory.clientConnectionFailed(self, connector,reason)

if I disconect the server got:

connectionLost!: [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.

but if I restore the server connection will never be made.
What am I missing?

Chiara

unread,
Sep 23, 2013, 10:57:12 AM9/23/13
to pymo...@googlegroups.com
Up

Bashwork

unread,
Oct 7, 2013, 5:17:31 PM10/7/13
to pymo...@googlegroups.com
I actually let twisted handle all of the parameters for this directly:


If you want to do more advanced things, simply change the instance variables that control the reconnection or subclass and change the behavior to be whatever you want.  Here is the API guide and a twisted client guide:
Message has been deleted

Chiara

unread,
Oct 14, 2013, 6:07:10 AM10/14/13
to pymo...@googlegroups.com
Thanks for your kindly answer! I've looked at this documentation, but please be patient:

on my system is stored at  /usr/share/pyshared/pymodbus/client/async.py with read only permission, this means, I can edit the class ModbusClientFactory without edit the library file?

2) I don't  get the point: is it the factory or the protocol that  should manage the reconnection ?
In twisted documentation seems to be EchoClientFactory(ReconnectingClientFactory
In the pymodbus the connectionMade and other function are in  ModbusClientProtocol(protocol.Protocol, ModbusClientMixin)

3) In my script I've created a class that derive from the one in the lib:

class MyModbusClientProtocol(ModbusClientProtocol):

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

    def buildProtocol(self, addr):
        print 'Connected.'
        print 'Resetting reconnection delay'
        self.resetDelay()

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

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

The client connect to server, then if I shutdown the server the script return this error:

 in connectionLost
            protocol.connectionLost(reason)
        exceptions.TypeError: connectionLost() takes exactly 3 arguments (2 given)


Galen Collins

unread,
Oct 15, 2013, 11:11:40 AM10/15/13
to pymo...@googlegroups.com

So the factory generates clients and the protocol describes how the clients communicate.  The factory can decorate the created client with extra code to control how it works.  For more information read this please:

http://twistedmatrix.com/documents/13.0.0/core/howto/clients.html
Don't edit the existing file, recreate the needed code in your project.

You can build a reconnecting client this way, but twisted has already written all this code for you.  All you have to do is configure it correctly.

--
You received this message because you are subscribed to the Google Groups "pymodbus" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pymodbus+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages