Search in sources :

Example 1 with HashListener

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

the class PEPeerControlHashHandlerImpl method receivedOrRejectedHashes.

private void receivedOrRejectedHashes(PEPeerTransport peer, byte[] root_hash, int base_layer, int index, int length, int proof_layers, // null if rejected
byte[][] hashes) {
    try {
        TOTorrentFileHashTree tree = file_map.get(root_hash);
        if (tree != null) {
            if (hashes != null) {
                tree.receivedHashes(root_hash, base_layer, index, length, proof_layers, hashes);
            }
            List<HashListener> listeners = null;
            synchronized (peer_requests) {
                List<PeerHashRequest> peer_reqs = peer_requests.get(peer);
                if (peer_reqs != null) {
                    Iterator<PeerHashRequest> it = peer_reqs.iterator();
                    PeerHashRequest match = null;
                    while (it.hasNext()) {
                        PeerHashRequest peer_request = it.next();
                        HashRequest req = peer_request.getRequest();
                        if (Arrays.equals(root_hash, req.getRootHash()) && base_layer == req.getBaseLayer() && index == req.getOffset() && length == req.getLength() && proof_layers == req.getProofLayers()) {
                            match = peer_request;
                            it.remove();
                            break;
                        }
                    }
                    if (match != null) {
                        if (peer_reqs.isEmpty()) {
                            peer_requests.remove(peer);
                        }
                        if (!active_requests.remove(match)) {
                            Debug.out("entry not found");
                        }
                        removeFromPieceRequests(match);
                        match.setComplete();
                        listeners = match.getListeners();
                    }
                }
            }
            if (listeners != null) {
                for (HashListener l : listeners) {
                    try {
                        l.complete(hashes != null);
                    } catch (Throwable e) {
                        Debug.out(e);
                    }
                }
            }
        }
    } catch (Throwable e) {
        Debug.out(e);
    }
}
Also used : TOTorrentFileHashTree(com.biglybt.core.torrent.TOTorrentFileHashTree) HashListener(com.biglybt.core.disk.DiskManagerCheckRequestListener.HashListener) HashRequest(com.biglybt.core.torrent.TOTorrentFileHashTree.HashRequest)

Example 2 with HashListener

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

the class PEPeerControlHashHandlerImpl method update.

@Override
public void update() {
    long now = SystemTime.getMonotonousTime();
    if (!save_done_on_complete && disk_manager.getRemaining() == 0 && piece_hashes_received.get() > 0) {
        save_done_on_complete = true;
        peer_manager.getAdapter().saveTorrentState();
    }
    if (last_piece_tree_request > 0 && now - last_piece_tree_request > 60 * 1000) {
        last_piece_tree_request = -1;
        synchronized (piece_tree_cache) {
            piece_tree_cache.clear();
        }
    }
    if (now - last_update >= 30 * 1000) {
        List<PieceTreeRequest> expired = new ArrayList<>();
        synchronized (piece_tree_requests) {
            Iterator<PieceTreeRequest> it = piece_tree_requests.values().iterator();
            while (it.hasNext()) {
                PieceTreeRequest req = it.next();
                if (now - req.getCreateTime() > 30 * 1000) {
                    it.remove();
                    expired.add(req);
                }
            }
        }
        for (PieceTreeRequest req : expired) {
            Debug.out("PieceTreeRequest expired, derp");
            req.complete(null);
        }
    }
    List<PeerHashRequest> expired = new ArrayList<>();
    List<PeerHashRequest> retry = new ArrayList<>();
    synchronized (peer_requests) {
        {
            Iterator<PeerHashRequest> request_it = active_requests.iterator();
            while (request_it.hasNext()) {
                PeerHashRequest peer_request = request_it.next();
                boolean remove = false;
                long age = now - peer_request.getCreateTime();
                if (age > 10 * 1000) {
                    expired.add(peer_request);
                    remove = true;
                } else if (age > 5 * 1000) {
                    if (peer_request.getPeer().getPeerState() != PEPeer.TRANSFERING) {
                        retry.add(peer_request);
                        remove = true;
                    }
                }
                if (remove) {
                    request_it.remove();
                    PEPeerTransport peer = peer_request.getPeer();
                    List<PeerHashRequest> peer_reqs = peer_requests.get(peer);
                    if (peer_reqs == null) {
                        Debug.out("entry not found");
                    } else {
                        peer_reqs.remove(peer_request);
                        if (peer_reqs.isEmpty()) {
                            peer_requests.remove(peer);
                        }
                    }
                    removeFromPieceRequests(peer_request);
                    peer_request.setComplete();
                } else {
                    break;
                }
            }
        }
        if (incomplete_trees.isEmpty()) {
            if (!save_done_on_complete && piece_hashes_received.get() > 0) {
                save_done_on_complete = true;
                peer_manager.getAdapter().saveTorrentState();
            }
        } else {
            byte[][] pieces = null;
            int[] peer_availability = null;
            boolean has_seeds = false;
            Iterator<PeerHashRequest> request_it = incomplete_tree_reqs.values().iterator();
            while (request_it.hasNext()) {
                PeerHashRequest peer_request = request_it.next();
                if (peer_request.isComplete()) {
                    request_it.remove();
                }
            }
            Iterator<TOTorrentFileHashTree> tree_it = incomplete_trees.iterator();
            while (tree_it.hasNext()) {
                if (incomplete_tree_reqs.size() >= 10) {
                    break;
                }
                TOTorrentFileHashTree tree = tree_it.next();
                PeerHashRequest peer_request = incomplete_tree_reqs.get(tree);
                if (peer_request == null) {
                    if (tree.isPieceLayerComplete()) {
                        tree_it.remove();
                    } else {
                        if (pieces == null) {
                            try {
                                pieces = torrent.getPieces();
                            } catch (Throwable e) {
                                break;
                            }
                        }
                        TOTorrentFile file = tree.getFile();
                        int start = file.getFirstPieceNumber();
                        int end = file.getLastPieceNumber();
                        for (int i = start; i <= end; i++) {
                            if (pieces[i] == null) {
                                if (peer_availability == null) {
                                    PiecePicker piece_picker = peer_manager.getPiecePicker();
                                    if (piece_picker.getMinAvailability() >= 1) {
                                        has_seeds = true;
                                    } else {
                                        peer_availability = piece_picker.getAvailability();
                                    }
                                }
                                if (has_seeds || peer_availability[i] >= 1) {
                                    PeerHashRequest req = hashRequestSupport(i, null);
                                    if (req != null) {
                                        incomplete_tree_reqs.put(tree, req);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    // System.out.println( "Active requests: " + active_requests.size() + ", peers=" + peer_requests + ", piece_req=" + piece_requests + ", incomplete tree req=" + incomplete_tree_reqs);
    }
    for (PeerHashRequest peer_request : retry) {
        List<HashListener> listeners = peer_request.getListeners();
        if (listeners != null) {
            for (HashListener l : listeners) {
                if (!hashRequest(l.getPieceNumber(), l)) {
                    l.complete(false);
                }
            }
        }
    }
    for (PeerHashRequest peer_request : expired) {
        List<HashListener> listeners = peer_request.getListeners();
        if (listeners != null) {
            for (HashListener l : listeners) {
                try {
                    l.complete(false);
                } catch (Throwable e) {
                    Debug.out(e);
                }
            }
        }
    }
    last_update = now;
}
Also used : HashListener(com.biglybt.core.disk.DiskManagerCheckRequestListener.HashListener) PiecePicker(com.biglybt.core.peermanager.piecepicker.PiecePicker) TOTorrentFileHashTree(com.biglybt.core.torrent.TOTorrentFileHashTree) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) PEPeerTransport(com.biglybt.core.peer.impl.PEPeerTransport) DMPieceList(com.biglybt.core.disk.impl.piecemapper.DMPieceList)

Aggregations

HashListener (com.biglybt.core.disk.DiskManagerCheckRequestListener.HashListener)2 TOTorrentFileHashTree (com.biglybt.core.torrent.TOTorrentFileHashTree)2 DMPieceList (com.biglybt.core.disk.impl.piecemapper.DMPieceList)1 PEPeerTransport (com.biglybt.core.peer.impl.PEPeerTransport)1 PiecePicker (com.biglybt.core.peermanager.piecepicker.PiecePicker)1 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)1 HashRequest (com.biglybt.core.torrent.TOTorrentFileHashTree.HashRequest)1