def _updateFromSelf(sock, instruction, awakener, sinfo, client_id, connection_type=''): try: if helper.isCmdUpdate(instruction): if not connection_type: sock.sendall(helper.getOkMessage()) awakener.set() logging.debug('Requested update sent') elif not connection_type and helper.isCmdGet(instruction): sock.sendall( helper.getOkMessage( '', '%c %s %c %s %c' % (gdata.SOH, client_id, gdata.ETX, buildXML(sinfo), gdata.ETX))) logging.debug('Requested get sent') else: sock.send(helper.getUnknownCmdError('Unknown command')) logging.debug('Request error sent') except socket.error, e: pass
def run(self): """run() -> void Handles the communication events and errors with a client. """ CommandHandler._registerThread(CommandHandler, self) sock = self.sock sock.settimeout(self.timeout) try: while True: data = common.recvEnd(sock, '\n') # Connection closed if not data: break data = data.strip() if helper.isCmdQuit(data): # QUIT break elif helper.isCmdList(data): # LIST self._sendMonitorsList() elif helper.isCmdGetAll(data): # GET ALL self._sendGetAll() elif helper.isCmdUpdateAll(data): # UPDATE ALL self._sendUpdateAll() else: cmd, sep, body = data.partition(' ') body = body.strip() if helper.isCmdGet(cmd): # GET self._handleGet(body) elif helper.isCmdUpdate(cmd): # UPDATE self._handleUpdate(body) else: logging.info("Unknown command from %s:%d" % self.addr) self.sock.sendall( helper.getUnknownCmdError( "Unknown command: %s" % data) ) except socket.timeout: logging.debug("Socket TIMEOUT %s:%d" % self.addr) sock.sendall( helper.getTimeoutError( "Reached timeout of %.1f seconds" % self.timeout) ) except IOError: logging.warning("Error sending data to %s:%d" % self.addr) finally: sock.close() logging.debug("Command handler closed socket to %s:%s" % self.addr) CommandHandler._unregisterThread(CommandHandler, self)
def run(self): """run() -> void Handles the communication events and errors with a client. """ CommandHandler._registerThread(CommandHandler, self) sock = self.sock sock.settimeout(self.timeout) try: while True: data = common.recvEnd(sock, '\n') # Connection closed if not data: break data = data.strip() if helper.isCmdQuit(data): # QUIT break elif helper.isCmdList(data): # LIST self._sendMonitorsList() elif helper.isCmdGetAll(data): # GET ALL self._sendGetAll() elif helper.isCmdUpdateAll(data): # UPDATE ALL self._sendUpdateAll() else: cmd, sep, body = data.partition(' ') body = body.strip() if helper.isCmdGet(cmd): # GET self._handleGet(body) elif helper.isCmdUpdate(cmd): # UPDATE self._handleUpdate(body) else: logging.info("Unknown command from %s:%d" % self.addr) self.sock.sendall( helper.getUnknownCmdError("Unknown command: %s" % data)) except socket.timeout: logging.debug("Socket TIMEOUT %s:%d" % self.addr) sock.sendall( helper.getTimeoutError("Reached timeout of %.1f seconds" % self.timeout)) except IOError: logging.warning("Error sending data to %s:%d" % self.addr) finally: sock.close() logging.debug("Command handler closed socket to %s:%s" % self.addr) CommandHandler._unregisterThread(CommandHandler, self)
def _updateFromOther(sock, instruction, params, timeout): try: ip, port = params.strip().split(':') port = int(port) response = '' sock_to_other = socket.create_connection((ip, port), timeout) sock_to_other.sendall(instruction + '\n') msg = common.recvEnd(sock_to_other, '\n\n') + '\n\n' response_head, sep, rest = msg.partition(' ') if response_head != gdata.K_OK: msg = helper.getGenericError('Bad response') except ValueError: msg = helper.getUnknownCmdError('Bad formatted parameters') except socket.error, e: msg = helper.getMonitorUnreachableError('Connection failed')
def _updateFromSelf(sock, instruction, awakener, sinfo, client_id, connection_type=''): try: if helper.isCmdUpdate(instruction): if not connection_type: sock.sendall(helper.getOkMessage()) awakener.set() logging.debug('Requested update sent') elif not connection_type and helper.isCmdGet(instruction): sock.sendall(helper.getOkMessage('', '%c %s %c %s %c' % (gdata.SOH, client_id, gdata.ETX, buildXML(sinfo), gdata.ETX))) logging.debug('Requested get sent') else: sock.send(helper.getUnknownCmdError('Unknown command')) logging.debug('Request error sent') except socket.error, e: pass