def cancel_observing(self, response, send_rst): host, port = response.source key = hash(str(host) + str(port) + str(response.token)) del self.relation[key] if send_rst: rst = Message.new_rst(response) self.send(rst)
def datagramReceived(self, data, addr): """ Handler for received UDP datagram. :param data: the UDP datagram :param host: source host :param port: source port """ try: host, port = addr except ValueError: host, port, tmp1, tmp2 = addr log.msg("Datagram received from " + str(host) + ":" + str(port)) serializer = Serializer() message = serializer.deserialize(data, host, port) print "Message received from " + host + ":" + str(port) print "----------------------------------------" print message print "----------------------------------------" if isinstance(message, Request): log.msg("Received request") ret = self.request_layer.handle_request(message) if isinstance(ret, Request): response = self.request_layer.process(ret) else: response = ret self.schedule_retrasmission(message, response, None) log.msg("Send Response") self.send(response, host, port) elif isinstance(message, Response): log.err("Received response") rst = Message.new_rst(message) rst = self.message_layer.matcher_response(rst) log.msg("Send RST") self.send(rst, host, port) elif isinstance(message, tuple): message, error = message response = Response() response.destination = (host, port) response.code = defines.responses[error] response = self.reliability_response(message, response) response = self.message_layer.matcher_response(response) log.msg("Send Error") self.send(response, host, port) elif message is not None: # ACK or RST log.msg("Received ACK or RST") self.message_layer.handle_message(message)
def finish_request(self, args): """ Handler for received UDP datagram. :param args: (data, (client_ip, client_port) """ data, client_address = args host = client_address[0] port = client_address[1] # logging.log(logging.INFO, "Datagram received from " + str(host) + ":" + str(port)) serializer = Serializer() message = serializer.deserialize(data, host, port) # print "Message received from " + host + ":" + str(port) # print "----------------------------------------" # print message # print "----------------------------------------" if isinstance(message, Request): # log.msg("Received request") ret = self.request_layer.handle_request(message) if isinstance(ret, Request): response = self.request_layer.process(ret) else: response = ret self.schedule_retrasmission(message, response, None) # log.msg("Send Response") return response, host, port elif isinstance(message, Response): # log.err("Received response") rst = Message.new_rst(message) rst = self.message_layer.matcher_response(rst) # log.msg("Send RST") return rst, host, port elif isinstance(message, tuple): message, error = message response = Response() response.destination = (host, port) response.code = defines.responses[error] response = self.message_layer.reliability_response(message, response) response = self.message_layer.matcher_response(response) # log.msg("Send Error") return response, host, port elif message is not None: # ACK or RST # log.msg("Received ACK or RST") self.message_layer.handle_message(message) return None
def handle_request(self, request): """ Handles requests. :param request: the request :return: the response """ host, port = request.source key = hash(str(host) + str(port) + str(request.mid)) if key not in self._parent.received: if request.blockwise: # Blockwise last, request = self._parent.blockwise_transfer(request) if last: self._parent.received[key] = (request, time.time()) return request else: self._parent.received[key] = (request, time.time()) return request else: request, timestamp = self._parent.received.get(key) request.duplicated = True self._parent.received[key] = (request, timestamp) try: response, timestamp = self._parent.sent.get(key) except TypeError: response = None if isinstance(response, Response): return response elif request.acknowledged: ack = Message.new_ack(request) return ack elif request.rejected: rst = Message.new_rst(request) return rst else: # The server has not yet decided, whether to acknowledge or # reject the request. We know for sure that the server has # received the request though and can drop this duplicate here. return None
def finish_request(self, request, client_address): host = client_address[0] port = client_address[1] data = request[0] self.socket = request[1] # log.msg("Datagram received from " + str(host) + ":" + str(port)) serializer = Serializer() message = serializer.deserialize(data, host, port) # print "Message received from " + host + ":" + str(port) # print "----------------------------------------" # print message # print "----------------------------------------" if isinstance(message, Request): # log.msg("Received request") ret = self.request_layer.handle_request(message) if isinstance(ret, Request): self.forward_request(ret) elif isinstance(message, Response): # log.err("Received response") rst = Message.new_rst(message) rst = self.message_layer.matcher_response(rst) # log.msg("Send RST") self.send(rst, host, port) elif isinstance(message, tuple): message, error = message response = Response() response.destination = (host, port) response.code = defines.responses[error] response = self.message_layer.reliability_response(message, response) response = self.message_layer.matcher_response(response) # log.msg("Send Error") self.send(response, host, port) elif message is not None: # ACK or RST # log.msg("Received ACK or RST") self.message_layer.handle_message(message)