def handle_request(self, request): """ Store Blockwise parameter required by clients :param request: the request message :return: M bit, request """ ret = True for option in request.options: if option.number == defines.inv_options["Block2"]: host, port = request.source key = hash(str(host) + str(port) + str(request.token)) num, m, size = parse_blockwise(option.raw_value) # remember choices if key in self._parent.blockwise: block, byte, num2, m2, size2 = self._parent.blockwise[key] if block == 2: self._parent.blockwise[key] = (2, byte, num, m, size) else: self._parent.blockwise[key] = (2, 0, num, m, size) else: self._parent.blockwise[key] = (2, 0, num, m, size) elif option.number == defines.inv_options["Block1"]: host, port = request.source key = hash(str(host) + str(port) + str(request.token)) num, m, size = parse_blockwise(option.raw_value) # remember choices self._parent.blockwise[key] = (1, 0, num, m, size) if m == 0: del self._parent.blockwise[key] ret = False return ret, request
def line_print(self): """ Return the message as a one-line string. :return: the string representing the message """ inv_types = {v: k for k, v in defines.Types.items()} if self._code is None: self._code = defines.Codes.EMPTY.number token = binascii.hexlify(self._token).decode("utf-8") if self._token is not None else str(None) msg = "From {source}, To {destination}, {type}-{mid}, {code}-{token}, ["\ .format(source=self._source, destination=self._destination, type=inv_types[self._type], mid=self._mid, code=defines.Codes.LIST[self._code].name, token=token) for opt in self._options: if 'Block' in opt.name: msg += "{name}: {value}, ".format(name=opt.name, value=utils.parse_blockwise(opt.value)) else: msg += "{name}: {value}, ".format(name=opt.name, value=opt.value) msg += "]" if self.payload is not None: if isinstance(self.payload, dict): tmp = list(self.payload.values())[0][0:20] else: tmp = self.payload[0:20] msg += " {payload}...{length} bytes".format(payload=tmp, length=len(self.payload)) else: msg += " No payload" return msg
def block2(self): """ :rtype : String """ value = None for option in self.options: if option.number == defines.OptionRegistry.BLOCK2.number: value = parse_blockwise(option.value) return value
def block2(self): """ Get the Block2 option. :return: the Block2 value """ value = None for option in self.options: if option.number == defines.OptionRegistry.BLOCK2.number: value = parse_blockwise(option.value) return value
def block1(self): """ Get the Block1 option. :return: the Block1 value """ value = None for option in self.options: if option.number == defines.OptionRegistry.BLOCK1.number: value = parse_blockwise(option.value) return value
def block1(self): """ Get the Block1 option. :return: the Block1 value """ value = 0 for option in self.options: if option.number == defines.inv_options['Block1']: value = option.raw_value value = parse_blockwise(value) return value