Search in sources :

Example 1 with BTPiece

use of com.biglybt.core.peermanager.messaging.bittorrent.BTPiece in project BiglyBT by BiglySoftware.

the class OutgoingBTPieceMessageHandler method getRequestedPieceNumbers.

/**
 * Get a list of piece numbers being requested
 *
 * @return list of Long values
 */
public int[] getRequestedPieceNumbers() {
    if (destroyed)
        return new int[0];
    /**
     * Cheap hack to reduce (but not remove all) the # of duplicate entries
     */
    int iLastNumber = -1;
    int pos = 0;
    int[] pieceNumbers;
    try {
        lock_mon.enter();
        // allocate max size needed (we'll shrink it later)
        pieceNumbers = new int[queued_messages.size() + loading_messages.size() + requests.size()];
        for (Iterator iter = queued_messages.keySet().iterator(); iter.hasNext(); ) {
            BTPiece msg = (BTPiece) iter.next();
            if (iLastNumber != msg.getPieceNumber()) {
                iLastNumber = msg.getPieceNumber();
                pieceNumbers[pos++] = iLastNumber;
            }
        }
        for (Iterator iter = loading_messages.iterator(); iter.hasNext(); ) {
            DiskManagerReadRequest dmr = (DiskManagerReadRequest) iter.next();
            if (iLastNumber != dmr.getPieceNumber()) {
                iLastNumber = dmr.getPieceNumber();
                pieceNumbers[pos++] = iLastNumber;
            }
        }
        for (Iterator iter = requests.iterator(); iter.hasNext(); ) {
            DiskManagerReadRequest dmr = (DiskManagerReadRequest) iter.next();
            if (iLastNumber != dmr.getPieceNumber()) {
                iLastNumber = dmr.getPieceNumber();
                pieceNumbers[pos++] = iLastNumber;
            }
        }
    } finally {
        lock_mon.exit();
    }
    int[] trimmed = new int[pos];
    System.arraycopy(pieceNumbers, 0, trimmed, 0, pos);
    return trimmed;
}
Also used : DiskManagerReadRequest(com.biglybt.core.disk.DiskManagerReadRequest) BTPiece(com.biglybt.core.peermanager.messaging.bittorrent.BTPiece)

Example 2 with BTPiece

use of com.biglybt.core.peermanager.messaging.bittorrent.BTPiece in project BiglyBT by BiglySoftware.

the class OutgoingBTPieceMessageHandler method removePieceRequest.

/**
 * Remove an outstanding piece data request.
 * @param piece_number
 * @param piece_offset
 * @param length
 */
public void removePieceRequest(int piece_number, int piece_offset, int length) {
    if (destroyed)
        return;
    DiskManagerReadRequest dmr = peer.getManager().getDiskManager().createReadRequest(piece_number, piece_offset, length);
    boolean entry_removed = false;
    try {
        lock_mon.enter();
        if (requests.contains(dmr)) {
            requests.remove(dmr);
            entry_removed = true;
            return;
        }
        if (loading_messages.contains(dmr)) {
            loading_messages.remove(dmr);
            entry_removed = true;
            return;
        }
        for (Iterator<Map.Entry<BTPiece, DiskManagerReadRequest>> i = queued_messages.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry<BTPiece, DiskManagerReadRequest> entry = i.next();
            if (entry.getValue().equals(dmr)) {
                // it's already been queued
                BTPiece msg = entry.getKey();
                if (outgoing_message_queue.removeMessage(msg, true)) {
                    entry_removed = true;
                    i.remove();
                    entry.getValue().setTimeSent(-1);
                }
                // do manual listener notify
                break;
            }
        }
    } finally {
        if (entry_removed) {
            int[] active = active_pieces.get(piece_number);
            if (active == null) {
                Debug.out("eh?");
            } else {
                if (--active[0] == 0) {
                    active_pieces.remove(piece_number);
                }
            }
        }
        lock_mon.exit();
        if (entry_removed) {
            peer.sendRejectRequest(dmr);
        }
    }
    outgoing_message_queue.doListenerNotifications();
}
Also used : DiskManagerReadRequest(com.biglybt.core.disk.DiskManagerReadRequest) BTPiece(com.biglybt.core.peermanager.messaging.bittorrent.BTPiece)

Example 3 with BTPiece

use of com.biglybt.core.peermanager.messaging.bittorrent.BTPiece in project BiglyBT by BiglySoftware.

the class OutgoingBTPieceMessageHandler method removeAllPieceRequests.

/**
 * Remove all outstanding piece data requests.
 */
public void removeAllPieceRequests() {
    if (destroyed)
        return;
    List<DiskManagerReadRequest> removed = new ArrayList<>();
    try {
        lock_mon.enter();
        for (Iterator<Map.Entry<BTPiece, DiskManagerReadRequest>> i = queued_messages.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry<BTPiece, DiskManagerReadRequest> entry = i.next();
            BTPiece msg = entry.getKey();
            if (outgoing_message_queue.removeMessage(msg, true)) {
                removed.add(queued_messages.get(msg));
            }
            entry.getValue().setTimeSent(-1);
        }
        queued_messages.clear();
        removed.addAll(requests);
        requests.clear();
        removed.addAll(loading_messages);
        loading_messages.clear();
        active_pieces.clear();
    } finally {
        lock_mon.exit();
    }
    for (DiskManagerReadRequest request : removed) {
        peer.sendRejectRequest(request);
    }
    outgoing_message_queue.doListenerNotifications();
}
Also used : DiskManagerReadRequest(com.biglybt.core.disk.DiskManagerReadRequest) BTPiece(com.biglybt.core.peermanager.messaging.bittorrent.BTPiece)

Aggregations

DiskManagerReadRequest (com.biglybt.core.disk.DiskManagerReadRequest)3 BTPiece (com.biglybt.core.peermanager.messaging.bittorrent.BTPiece)3