def __enter__(self): """ Implement the client with enter block :returns: The current instance of the client """ if not self.connect(): raise ConnectionException('Failed to connect[{0}]'.format( self.__str__())) return self
def execute(self, request=None): """ :param request: The request to process :returns: The result of the request execution """ if not self.connect(): raise ConnectionException('Failed to connect[{0}]'.format( self.__str__())) return self.transaction.execute(request)
def receive(self, size): """ Reads data from the underlying descriptor :param size: The number of bytes to read :return: The bytes read """ if not self.socket: raise ConnectionException(self.__str__()) return self.socket.read(size)
def connection_lost(self, reason): """ Called upon a client disconnect :param reason: The reason for the disconnect """ _logger.debug('Client disconnected from modbus server: ' + str(reason)) self._connected = False for tid in list(self.transaction): self.transaction.get_transaction(tid).errback( Failure(ConnectionException('Connection lost during request')))
def send(self, request): """ Sends data on the underlying socket :param request: The encoded request to send :return: The number of bytes written """ if not self.socket: raise ConnectionException(self.__str__()) if request: return self.socket.write(request) return 0
def _build_response(self, tid): """ Helper method to return a deferred response for the current request. :param tid: The transaction identifier for this response :returns: A defer linked to the latest request """ if not self._connected: return defer.fail( Failure(ConnectionException('Client is not connected'))) d = defer.Deferred() self.transaction.add_transaction(d, tid) return d