예제 #1
0
    def test_responding_to_file_request_message_for_huge_file(self):

        print(file_util.get_file_size('./huge_file.txt'))
        another_peer = Peer(22, "127.33.21.22", 3233, False)
        message = putils.create_file_request_message(
            self.business_layer.get_myself(),
            receiver=another_peer,
            path='./huge_file.txt')
        _run(self.lower[0].put(message))

        chunk1 = _run(self.lower[1].get())
        chunk2 = _run(self.lower[1].get())
        file_size = file_util.get_file_size('./huge_file.txt')
        binary_data1 = file_util.get_file_binary_data('./huge_file.txt', 8192)
        binary_data2 = file_util.get_file_binary_data('./huge_file.txt')[8192:]

        self.assertEqual(
            chunk1.fileChunk.fileName, './huge_file.txt' +
            '.{}'.format(self.business_layer.get_myself().id))
        self.assertEqual(chunk1.fileChunk.fileSize, file_size)
        self.assertEqual(chunk1.fileChunk.ordinal, 0)
        self.assertEqual(chunk1.fileChunk.data, binary_data1)
        self.assertEqual(
            chunk2.fileChunk.fileName, './huge_file.txt' +
            '.{}'.format(self.business_layer.get_myself().id))
        self.assertEqual(chunk2.fileChunk.fileSize, file_size)
        self.assertEqual(chunk2.fileChunk.ordinal, 1)
        self.assertEqual(chunk2.fileChunk.data, binary_data2)
        self.assertEqual(chunk1.fileChunk.uuid, chunk2.fileChunk.uuid)
예제 #2
0
    async def _handle_file_request_message(self, message):
        log.debug("Handling FILE_REQUEST message")
        sender = message.sender
        sender_peer = putils.create_peer_from_contact(sender)
        path = message.fileRequest.path
        log.debug("Peer {} is requesting file {}".format(sender_peer.get_info(), path))

        uuid = str(random.Random().getrandbits(64))
        ordinal = 0
        file_size = file_util.get_file_size(path)
        file_name = path + ".{}".format(self.get_myself().id)
        log.debug("Start creating file chunks and sending them in messages")
        for chunk in file_util.chunks_generator(path=path):
            log.debug("Send file chunk: [ {}, {}, {}, {}, data_chunk_size: {} ]".format(uuid, file_name, file_size, ordinal, len(chunk)))
            status = await self._file_chunk_message(receiver=sender_peer,
                                                    uuid=uuid,
                                                    file_name=file_name,
                                                    file_size = file_size,
                                                    ordinal=ordinal,
                                                    data=chunk
                                                    )
            ordinal += 1
            if status is StatusMessage.FAILURE:
                log.warning("Could not create file chunk message")
                return status
            elif status is StatusMessage.SUCCESS:
                continue
        log.debug("Whole file was sent")
        return StatusMessage.SUCCESS
예제 #3
0
 async def send_file(self, id_of_peer, path):
     receiver = self.lower_layer.get_peer_by_id(id_of_peer)
     uuid = str(random.Random().getrandbits(64))
     ordinal = 0
     file_size = file_util.get_file_size(path)
     file_name = path + ".{}".format(self.get_myself().id)
     log.debug("Start creating file chunks and sending them in messages")
     for chunk in file_util.chunks_generator(path=path):
         log.debug(
             "Send file chunk: [ {}, {}, {}, {}, data_chunk_size: {} ]".format(uuid, file_name, file_size, ordinal,
                                                                               len(chunk)))
         status = await self._file_chunk_message(receiver=receiver,
                                                 uuid=uuid,
                                                 file_name=file_name,
                                                 file_size=file_size,
                                                 ordinal=ordinal,
                                                 data=chunk
                                                 )
         ordinal += 1
         if status is StatusMessage.FAILURE:
             log.warning("Could not create file chunk message")
             return status
         elif status is StatusMessage.SUCCESS:
             continue
     log.debug("Whole file was sent")
     return StatusMessage.SUCCESS
예제 #4
0
    async def _handle_file_chunk_message(self, message):
        """
        Handle FILE_CHUNK Message
        :param message: message
        :return: SUCCESS or FAILURE
        """
        log.debug("Handling FILE_CHUNK message")
        uuid = message.fileChunk.uuid
        file_name = message.fileChunk.fileName
        file_size = message.fileChunk.fileSize
        ordinal = message.fileChunk.ordinal
        data = message.fileChunk.data

        # We are currently writing this file
        for file_being_written_uuid in self._files_being_written:
            if file_being_written_uuid == uuid:
                log.debug("Adding another chunk to file {}".format(file_name))
                with open(file_name, 'ab') as file:
                    file.seek(ordinal * 8192, 0)
                    file.write(data)
                    log.debug("Added another chunk to file {}".format(file_name))

                if file_size == file_util.get_file_size(path=file_name):
                    log.debug("Whole file {} has been writte".format(file_name))
                    self._files_being_written.remove(file_being_written_uuid)
                return StatusMessage.SUCCESS

        # That file was not being written
        self._files_being_written.append(uuid)
        log.debug("Adding first chunk to file {}".format(file_name))
        with open(file_name, 'wb') as file:
            file.seek(ordinal * 8192, 0)
            file.write(data)
            log.debug("Added first chunk to file {}".format(file_name))

        if file_size == file_util.get_file_size(path=file_name):
            log.debug("Whole file {} has been writte".format(file_name))

            self._files_being_written.remove(uuid)
        return StatusMessage.SUCCESS
예제 #5
0
    def test_responding_to_file_request_message(self):
        another_peer = Peer(22, "127.33.21.22", 3233, False)
        message = putils.create_file_request_message(
            self.business_layer.get_myself(),
            receiver=another_peer,
            path='./test_file.txt')
        _run(self.lower[0].put(message))

        response_message = _run(self.lower[1].get())
        file_size = file_util.get_file_size('./test_file.txt')
        binary_data = file_util.get_file_binary_data('./test_file.txt')

        self.assertEqual(
            response_message.fileChunk.fileName, './test_file.txt' +
            '.{}'.format(self.business_layer.get_myself().id))
        self.assertEqual(response_message.fileChunk.fileSize, file_size)
        self.assertEqual(response_message.fileChunk.ordinal, 0)
        self.assertEqual(response_message.fileChunk.data, binary_data)
예제 #6
0
    def test_handling_file_chunks_message(self):
        print(file_util.get_file_size('./huge_file.txt'))
        another_peer = Peer(22, "127.33.21.22", 3233, False)
        message = putils.create_file_request_message(
            self.business_layer.get_myself(),
            receiver=another_peer,
            path='./huge_file.txt')
        _run(self.lower[0].put(message))

        chunk1 = _run(self.lower[1].get())
        chunk2 = _run(self.lower[1].get())

        _run(self.lower[0].put(chunk1))
        _run(self.lower[0].put(chunk2))

        _run(asyncio.sleep(4))

        true_binary_data = file_util.get_file_binary_data('./huge_file.txt')
        binary_data = file_util.get_file_binary_data(
            path='./huge.file.txt.{}'.format(
                self.business_layer.get_myself().id))
        self.assertEqual(binary_data, true_binary_data)