Exemplo n.º 1
0
    def peer_requests_piece(self, request=None, peer=None):
        if not request or not peer:
            logging.error("empty request/peer message")

        piece_index, block_offset, block_length = request.piece_index, request.block_offset, request.block_length

        block = self.pieces_manager.get_block(piece_index, block_offset, block_length)
        if block:
            piece = message.Piece(piece_index, block_offset, block_length, block).to_bytes()
            peer.send_to_peer(piece)
            logging.info("Sent piece index {} to peer : {}".format(request.piece_index, peer.ip))
Exemplo n.º 2
0
def parseNonHandshakeMessage(data,sock):
        bytestring = data
        if (bytestring[0:4] == '\x00\x00\x00\x00'): 
            # Its a Keep Alive message #
            message_obj = message.KeepAlive(response=bytestring)
        else:
            message_obj  = {
              0: lambda: message.Choke(response=bytestring),
              1: lambda: message.Unchoke(response=bytestring),
              2: lambda: message.Interested(response=bytestring),
              3: lambda: message.Interested(response=bytestring),
              4: lambda: message.Have(response=bytestring),
              5: lambda: message.Bitfield(response=bytestring),
              6: lambda: message.Request(response=bytestring),
              7: lambda: message.Piece(response=bytestring),
              8: lambda: message.Cancel(response=bytestring),
              9: lambda: message.Port(response=bytestring),
            }[    struct.unpack('!b',data[4])[0]   ]()     # The 5th byte in 'data' is the message type/id 
        process_message(message_obj,sock)