def sendThroughSocket(sock, to_send, delim='\n\n', wait_for_response=True): code = '' detail = '' msg = '' try: sock.sendall(to_send) logging.debug('Message sent to server') if wait_for_response: first, sep, msg = common.recvEnd(sock, delim).partition('\n') code, sep, detail = first.partition(' ') msg, sep, garbage = msg.partition('\n') code.strip() detail.strip() msg.strip() logging.debug('Response from the server (response code): %s %s' % (code, detail)) logging.debug('Response from the server (response): %s' % msg) except socket.error, e: code = helper.getMonitorUnreachableError() msg = 'Connection error'
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 _sendUpdateToMonitor(self, ip, port): # Send Update to monitor and get response s = socket.create_connection((ip, port), self.timeout) s.sendall("%s\n" % gdata.CMD_UPDATE) ret = common.recvEnd(s, '\n').strip() code, sep, desc = ret.partition(' ') # Send result to client msg = None if code == gdata.K_OK: msg = helper.getOkMessage(desc) else: msg = helper.getGenericError("Received error: %s" % ret) self.sock.sendall(msg)
def _sendUpdateToMonitor(self, ip, port): # Send Update to monitor and get response s = socket.create_connection( (ip, port), self.timeout ) s.sendall("%s\n" % gdata.CMD_UPDATE) ret = common.recvEnd(s, '\n').strip() code, sep, desc = ret.partition(' ') # Send result to client msg = None if code == gdata.K_OK: msg = helper.getOkMessage(desc) else: msg = helper.getGenericError("Received error: %s" % ret) self.sock.sendall(msg)
def _updateMonitor(self, mid): if srvdata.existsMonitorData(mid): srvdata.keepAliveMonitor(mid) data = common.recvEnd(self.sock, gdata.ETX).strip() #logging.debug("%s:%d Data:\n%s" % (self.addr[0], self.addr[1], data)) infoparser = common.SysInfoXMLParser() infoparser.parseXML(data) infodao = infoparser.getSysInfoData() srvdata.updateMonitorData(mid, infodao) self.sock.sendall(helper.getOkMessage("Update successful")) logging.debug("Update %s successful" % mid) else: self.sock.sendall( helper.getMonitorNotFoundError( "There isn't any monitor with id: %s" % mid))
def _updateMonitor(self, mid): if srvdata.existsMonitorData(mid): srvdata.keepAliveMonitor(mid) data = common.recvEnd(self.sock, gdata.ETX).strip() #logging.debug("%s:%d Data:\n%s" % (self.addr[0], self.addr[1], data)) infoparser = common.SysInfoXMLParser() infoparser.parseXML(data) infodao = infoparser.getSysInfoData() srvdata.updateMonitorData(mid, infodao) self.sock.sendall( helper.getOkMessage("Update successful") ) logging.debug("Update %s successful" % mid) else: self.sock.sendall( helper.getMonitorNotFoundError( "There isn't any monitor with id: %s" % mid) )
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 run(self): """run() -> void Handles the communication events and errors with a resource monitor. """ MonitorHandler._registerThread(MonitorHandler, self) sock = self.sock sock.settimeout(self.timeout) try: data = common.recvEnd(sock, gdata.ETX) if data: logging.debug("%s:%d Data:\n%s" % (self.addr[0], self.addr[1], data)) head, sep, body = data.partition(' ') head = head.strip() body = body.strip() if helper.isBEL(head): # New monitor self._newMonitor(body) elif helper.isSOH(head): # Monitor data update self._updateMonitor(body) else: # Unknown data logging.info("Unknown monitor message '%s' from %s:%d" % (data, self.addr[0], self.addr[1])) sock.sendall(helper.getBadMessageError("Wrong message")) except socket.timeout: logging.debug("Socket TIMEOUT %s:%d" % self.addr) sock.sendall( helper.getTimeoutError("Reached timeout of %.1f seconds" % self.timeout)) except AttributeError, e: sock.sendall(helper.getGenericError(str(e)))
def run(self): """run() -> void Handles the communication events and errors with a resource monitor. """ MonitorHandler._registerThread(MonitorHandler, self) sock = self.sock sock.settimeout( self.timeout ) try: data = common.recvEnd(sock, gdata.ETX) if data: logging.debug("%s:%d Data:\n%s" % (self.addr[0], self.addr[1], data)) head, sep, body = data.partition(' ') head = head.strip() body = body.strip() if helper.isBEL(head): # New monitor self._newMonitor(body) elif helper.isSOH(head): # Monitor data update self._updateMonitor(body) else: # Unknown data logging.info("Unknown monitor message '%s' from %s:%d" % (data, self.addr[0], self.addr[1]) ) sock.sendall( helper.getBadMessageError("Wrong message") ) except socket.timeout: logging.debug("Socket TIMEOUT %s:%d" % self.addr) sock.sendall( helper.getTimeoutError( "Reached timeout of %.1f seconds" % self.timeout) ) except AttributeError, e: sock.sendall( helper.getGenericError(str(e)) )