pythonclientasyncore

How to re-establish asyncore connection with server


I have an asyncore client which interacts with a server written in C. I need to be able to detect when the server closes the connection and keep re-trying to connect to it until it is available again. Here is the code I have: This is my asyncore client which i turn starts another threaded module (ReceiverBoard) to run in a separate thread. class DETClient(asyncore.dispatcher):

buffer = ""
t = None

def __init__(self, host, port):

    asyncore.dispatcher.__init__(self)
    self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
    self.connect((host,port))
    self.host=host
    self.port=port
    self.t = ReceiverBoard(self)
    self.t.start() 

def sendCommand(self, command):
    self.buffer = command

def handle_error(self):
    self.t.stop()
    self.close()

def handle_write(self):
    sent=self.send(self.buffer.encode())
    self.buffer=""

def handle_read(self):
    ##there is code here to parse the received message and call the appropriate
    ##method in the threaded module ReceiverBoard

My first problem was that I wanted the client (above) to keep retrying to connect to the server (developed in ANSI C) through the socket until a connection was made.


Solution

  • The change I made was to override the handle_error method in asyncore above to simply call another method to try to initialize the connection again instead of closing the socket. As in below: (following code is added to DETClient above)

    def initiate_connection_with_server(self):
        print("trying to initialize connection with server...")
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((self.host,self.port))
    
    def handle_error(self):
        print("problem reaching server.")
        self.initiate_connection_with_server()
    

    This solves the problem of the server not being available when this code is run. An exception is raised and handle_error is called which simply calls the initiate_connection method and tries to open the socket again. Also, after the connection is initially established, the code will call handle_error if the socket is lost for any reason and an attempt will be made to re-establish the connection. Problem solved!

    Here is the code for the threaded module (ReceiverBoard)

    class ReceiverBoard(threading.Thread):
        _stop = False           
        def __init__(self, client):
            self.client=client
            super(ReceiverBoard,self).__init__()
        def run(self):
            while True:
                block_to_send = ""
                ##code here to generate block_to_send
                self.client.sendCommand(block_to_send)
    
        def stop(self):
            self._stop = True