Search in sources :

Example 11 with DiskManagerReadRequest

use of com.biglybt.core.disk.DiskManagerReadRequest 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<BTPiece> i = queued_messages.keySet().iterator(); i.hasNext(); ) {
            BTPiece msg = i.next();
            if (outgoing_message_queue.removeMessage(msg, true)) {
                removed.add(queued_messages.get(msg));
            }
        }
        // this replaces stuff above
        queued_messages.clear();
        removed.addAll(requests);
        requests.clear();
        removed.addAll(loading_messages);
        loading_messages.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)

Example 12 with DiskManagerReadRequest

use of com.biglybt.core.disk.DiskManagerReadRequest in project BiglyBT by BiglySoftware.

the class DMReaderImpl method hasOutstandingReadRequestForPiece.

@Override
public boolean hasOutstandingReadRequestForPiece(int piece_number) {
    try {
        this_mon.enter();
        Iterator it = read_requests.iterator();
        while (it.hasNext()) {
            DiskManagerReadRequest request = (DiskManagerReadRequest) ((Object[]) it.next())[0];
            if (request.getPieceNumber() == piece_number) {
                return (true);
            }
        }
        return (false);
    } finally {
        this_mon.exit();
    }
}
Also used : DiskManagerReadRequest(com.biglybt.core.disk.DiskManagerReadRequest)

Example 13 with DiskManagerReadRequest

use of com.biglybt.core.disk.DiskManagerReadRequest in project BiglyBT by BiglySoftware.

the class PEPeerTransportProtocol method decodeRejectRequest.

protected void decodeRejectRequest(BTRejectRequest reject) {
    int number = reject.getPieceNumber();
    int offset = reject.getPieceOffset();
    int length = reject.getLength();
    reject.destroy();
    final DiskManagerReadRequest request = manager.createDiskManagerRequest(number, offset, length);
    if (hasBeenRequested(request)) {
        removeRequest(request);
        manager.requestCanceled(request);
        try {
            general_mon.enter();
            List<Integer> pieces = (List<Integer>) getUserData(KEY_ALLOWED_FAST_RECEIVED);
            if (pieces != null) {
                pieces.remove(new Integer(number));
                if (pieces.size() == 0) {
                    setUserData(KEY_ALLOWED_FAST_RECEIVED, null);
                }
            }
            int[] priorities = piece_priority_offsets;
            if (priorities != null) {
                priorities[number] = Integer.MIN_VALUE;
            }
            calculatePiecePriorities();
        } finally {
            general_mon.exit();
        }
    }
}
Also used : DiskManagerReadRequest(com.biglybt.core.disk.DiskManagerReadRequest)

Example 14 with DiskManagerReadRequest

use of com.biglybt.core.disk.DiskManagerReadRequest in project BiglyBT by BiglySoftware.

the class PEPeerTransportProtocol method decodePiece.

protected void decodePiece(BTPiece piece) {
    final int pieceNumber = piece.getPieceNumber();
    final int offset = piece.getPieceOffset();
    final DirectByteBuffer payload = piece.getPieceData();
    final int length = payload.remaining(DirectByteBuffer.SS_PEER);
    /*
    if ( AEDiagnostics.CHECK_DUMMY_FILE_DATA ){
      int pos = payload.position( DirectByteBuffer.SS_PEER );
      long  off = ((long)number) * getControl().getPieceLength(0) + offset;
      for (int i=0;i<length;i++){
        byte  v = payload.get( DirectByteBuffer.SS_PEER );
        if ((byte)off != v ){
          System.out.println( "piece: read is bad at " + off + ": expected = " + (byte)off + ", actual = " + v );
          break;
        }
        off++;
      }
      payload.position( DirectByteBuffer.SS_PEER, pos );
    }
		 */
    final Object error_msg = new Object() {

        public final String toString() {
            return ("decodePiece(): Peer has sent piece #" + pieceNumber + ":" + offset + "->" + (offset + length - 1) + ", ");
        }
    };
    if (!manager.validatePieceReply(this, pieceNumber, offset, payload)) {
        peer_stats.bytesDiscarded(length);
        manager.discarded(this, length);
        requests_discarded++;
        printRequestStats();
        piece.destroy();
        if (Logger.isEnabled())
            Logger.log(new LogEvent(this, LOGID, LogEvent.LT_ERROR, error_msg + "but piece block discarded as invalid."));
        return;
    }
    if (DEBUG_FAST) {
        if (fast_extension_enabled && really_choked_by_other_peer) {
            System.out.println("Received allow-fast piece for " + pieceNumber + "/" + offset + "/" + length + " from " + getIp());
        }
    }
    final DiskManagerReadRequest existing_request = lookupRequest(pieceNumber, offset, length);
    boolean piece_error = true;
    if (existing_request != null) {
        // from active request
        if (existing_request.isLatencyTest()) {
            long latency = SystemTime.getMonotonousTime() - existing_request.getTimeSent();
            if (latency > 0) {
                request_latency = latency;
            }
        }
        removeRequest(existing_request);
        final long now = SystemTime.getCurrentTime();
        resetRequestsTime(now);
        if (manager.isWritten(pieceNumber, offset)) {
            // oops, looks like this block has already been written
            peer_stats.bytesDiscarded(length);
            manager.discarded(this, length);
            if (manager.isInEndGameMode()) {
                // we're probably in end-game mode then
                if (last_good_data_time != -1 && now - last_good_data_time <= 60 * 1000)
                    setSnubbed(false);
                last_good_data_time = now;
                requests_discarded_endgame++;
                if (Logger.isEnabled())
                    Logger.log(new LogEvent(this, LogIDs.PIECES, LogEvent.LT_INFORMATION, error_msg + "but piece block ignored as already written in end-game mode."));
            } else {
                // so give credit to this peer anyway for having delivered a block at this time
                if (!isSnubbed())
                    last_good_data_time = now;
                if (Logger.isEnabled())
                    Logger.log(new LogEvent(this, LogIDs.PIECES, LogEvent.LT_WARNING, error_msg + "but piece block discarded as already written."));
                requests_discarded++;
            }
            printRequestStats();
        } else {
            // successfully received block!
            manager.writeBlock(pieceNumber, offset, payload, this, false);
            if (last_good_data_time != -1 && now - last_good_data_time <= 60 * 1000)
                setSnubbed(false);
            last_good_data_time = now;
            requests_completed++;
            // dont destroy message, as we've passed the payload on to the disk manager for writing
            piece_error = false;
        }
    } else {
        // initial request may have already expired, but check if we can use the data anyway
        if (!manager.isWritten(pieceNumber, offset)) {
            final boolean ever_requested;
            DiskManagerReadRequest request = manager.createDiskManagerRequest(pieceNumber, offset, length);
            try {
                recent_outgoing_requests_mon.enter();
                ever_requested = recent_outgoing_requests.containsKey(request);
            } finally {
                recent_outgoing_requests_mon.exit();
            }
            if (ever_requested) {
                // security-measure: we dont want to be accepting any ol' random block
                manager.writeBlock(pieceNumber, offset, payload, this, true);
                final long now = SystemTime.getCurrentTime();
                if (last_good_data_time != -1 && now - last_good_data_time <= 60 * 1000)
                    setSnubbed(false);
                resetRequestsTime(now);
                last_good_data_time = now;
                requests_recovered++;
                printRequestStats();
                // dont destroy message, as we've passed the payload on to the disk manager for writing
                piece_error = false;
                if (Logger.isEnabled())
                    Logger.log(new LogEvent(this, LogIDs.PIECES, LogEvent.LT_INFORMATION, error_msg + "expired piece block data recovered as useful."));
            } else {
                System.out.println("[" + client + "]" + error_msg + "but expired piece block discarded as never requested.");
                peer_stats.bytesDiscarded(length);
                manager.discarded(this, length);
                requests_discarded++;
                printRequestStats();
                if (Logger.isEnabled())
                    Logger.log(new LogEvent(this, LogIDs.PIECES, LogEvent.LT_ERROR, error_msg + "but expired piece block discarded as never requested."));
            }
        } else {
            peer_stats.bytesDiscarded(length);
            manager.discarded(this, length);
            requests_discarded++;
            printRequestStats();
            if (Logger.isEnabled())
                Logger.log(new LogEvent(this, LogIDs.PIECES, LogEvent.LT_WARNING, error_msg + "but expired piece block discarded as already written."));
        }
    }
    if (piece_error)
        piece.destroy();
    else
        allowReconnect = true;
}
Also used : DiskManagerReadRequest(com.biglybt.core.disk.DiskManagerReadRequest)

Aggregations

DiskManagerReadRequest (com.biglybt.core.disk.DiskManagerReadRequest)14 BTPiece (com.biglybt.core.peermanager.messaging.bittorrent.BTPiece)3 DiskManagerReadRequestListener (com.biglybt.core.disk.DiskManagerReadRequestListener)2 DMPieceList (com.biglybt.core.disk.impl.piecemapper.DMPieceList)1 DMPieceMapEntry (com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry)1 Message (com.biglybt.core.peermanager.messaging.Message)1