def handle_reply(self, conn): conn.settimeout(settings.READ_TIMEOUT_SEC) data = recv_all(conn) try: data = zlib.decompress(data) except zlib.error: pass try: reply = json.loads(data) except: logger.exception("Failed to parse reply: %s" % (str(data))) return try: validate(reply, REPLY_SCHEMA) except: logger.exception("Failed to validate reply.") logger.debug(json.dumps(reply)) return logger.debug("Got reply: %s" % (json.dumps(reply))) for t, handler in Request.HANDLER_MAPPING.items(): try: if reply['request'].get(t, False): handler(reply[t]) except: logger.exception("Failed to handle %s" % (t))
def server_task(self, *args, **kwargs): public_ip = get_iface_addr(settings.PUBLIC_IFACE) logger.debug("Starting server thread on %s." % (public_ip)) with open(SERVER_TASK_ID_FILE, 'w') as f: print >> f, self.request.id server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_sock.bind((public_ip, settings.PUBLIC_TCP_PORT)) server_sock.listen(settings.PUBLIC_BACKLOG) while True: try: conn, addr = server_sock.accept() data = recv_all(conn) try: prev_len = len(data) data = zlib.decompress(data) after_len = len(data) logger.debug( "Compressed msg: %d -> %d (%.2f%%)" % (after_len, prev_len, float(prev_len) / after_len * 100)) except zlib.error: pass try: reply = json.loads(data) except: logger.exception("Failed to read reply from %s: %s" % (str(addr), str(data))) continue try: validate(reply, REPLY_SCHEMA) except: logger.exception("Failed to validate reply.") logger.debug(json.dumps(reply)) return logger.debug("Got reply from %s: %s" % (str(addr), json.dumps(reply))) for t, handler in HANDLER_MAPPING.items(): try: if reply['request'].get(t, False): handler(reply[t]) except: logger.exception("Failed to handle %s" % (t)) except: logger.exception("Failed to handle request.")
def server_task(self, *args, **kwargs): public_ip = get_iface_addr(settings.PUBLIC_IFACE) logger.debug("Starting server thread on %s." % (public_ip)) with open(SERVER_TASK_ID_FILE, 'w') as f: print >>f, self.request.id server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_sock.bind((public_ip, settings.PUBLIC_TCP_PORT)) server_sock.listen(settings.PUBLIC_BACKLOG) while True: try: conn, addr = server_sock.accept() data = recv_all(conn) try: prev_len = len(data) data = zlib.decompress(data) after_len = len(data) logger.debug("Compressed msg: %d -> %d (%.2f%%)" % (after_len, prev_len, float(prev_len)/after_len*100)) except zlib.error: pass try: reply = json.loads(data) except: logger.exception("Failed to read reply from %s: %s" % (str(addr), str(data))) continue try: validate(reply, REPLY_SCHEMA) except: logger.exception("Failed to validate reply.") logger.debug(json.dumps(reply)) return logger.debug("Got reply from %s: %s" % (str(addr), json.dumps(reply))) for t, handler in HANDLER_MAPPING.items(): try: if reply['request'].get(t, False): handler(reply[t]) except: logger.exception("Failed to handle %s" % (t)) except: logger.exception("Failed to handle request.")