def resp_request(self, source: str, packet: CSPacket): if self._step is SenderStep.NONE: deny_reason = None deny_packet = self.generate_packet(self._states.DENY) try: recv_token = packet.payload().decode() self._info["filename"] = self._detoken_fn(recv_token) except TimeoutError: deny_packet.payload(b"Expired token") deny_reason = "Expired token" except Exception as e: deny_packet.payload(b"Invalid token") deny_reason = "Invalid token ({})".format(e) if deny_reason: return SchemeResult( source, deny_packet, False, Done(False, reason=deny_reason) ) self._file = FileReader(self._info["filename"]) info_packet = self.__create_info_packet() self._step = SenderStep.WAITING return SchemeResult(source, info_packet, True) else: return self.ignore(source, reason="Invalid step")
def resp_success(self, source: str, packet: CSPacket): if self._step == SenderStep.SENDING or self._step == SenderStep.WAITING: return SchemeResult( None, None, False, Done(True, **self._info)) else: return self.ignore(source, reason="Invalid step")
def resp_accept(self, source: str, packet: CSPacket): if self._step == SenderStep.REQUESTING: info_packet = self.__create_info_packet() self._step = SenderStep.WAITING return SchemeResult(source, info_packet, True, Done(None)) else: return self.ignore(source, reason="Invalid step")
def resp_deny(self, source: str, packet: CSPacket): if self._step is SenderStep.REQUESTING: return SchemeResult( None, None, False, Done(False, reason="Deny", message=packet.payload()) ) else: return self.ignore(source, reason="Invalid step")
def resp_failure(self, source: str, packet: CSPacket): if self._step == SenderStep.SENDING or self._step == SenderStep.WAITING: message = packet.payload() return SchemeResult( None, None, False, Done(False, reason="Failure", message=message) ) else: return self.ignore(source, reason="Invalid step")
def resp_require(self, source: str, packet: CSPacket): if self._step == SenderStep.SENDING or self._step == SenderStep.WAITING: payload = packet.payload() boffset = payload[0: DEFAULT_INT_SIZE] bbuffer_size = payload[DEFAULT_INT_SIZE: 2 * DEFAULT_INT_SIZE] offset = int.from_bytes(boffset, "big") buffer_size = int.from_bytes(bbuffer_size, "big") nbytes_to_read = min(buffer_size, self._buffer_size) data = self._file.read(offset, nbytes_to_read) send_packet = self.generate_packet(self._states.SEND) send_packet.option(offset.to_bytes(DEFAULT_INT_SIZE, "big")) send_packet.payload(data) self._step = SenderStep.SENDING return SchemeResult(source, send_packet, True, Done(None)) else: return self.ignore(source, reason="Invalid payload")
def test_done(): done1 = Done(True, author="huykingsofm") done2 = Done(False, author="hks") done2.copy(done1, True) assert done2.author == "huykingsofm"
def resp_ignore(self, source: str, packet: CSPacket): return SchemeResult( None, None, False, Done(False, reason="Ignore", message=packet.payload()))