public void bitfield(byte[] bitfield, Peer peer) { for (int i = 0; i < metafile.getPieces().size(); i++) { if (peer.hasPiece(i) && !torrentDisk.isCompleted(i)) { peer.setAmInterested(true); return; } } }
public Torrent( Metafile metafile, TorrentDisk torrentDisk, IncomingPeerListener incomingPeerListener, BandwidthLimiter uploadBandwidthLimiter, PieceChooser pieceChooser) { this.uploadBandwidthLimiter = uploadBandwidthLimiter; this.incomingPeerListener = incomingPeerListener; this.metafile = metafile; this.torrentDisk = torrentDisk; if (pieceChooser != null) { this.pieceChooser = pieceChooser; } else { this.pieceChooser = new RouletteWheelPieceChooser(); } this.pieceChooser.setTorrent(this); peerId = new byte[20]; Random random = new Random(System.currentTimeMillis()); random.nextBytes(peerId); System.arraycopy("-WT-0001".getBytes(), 0, peerId, 0, 8); peerIdEncoded = Utils.byteArrayToURLString(peerId); if (Torrent.verbose) { addEvent(new Event(this, "peerId generated: " + peerIdEncoded, Level.INFO)); } this.incomingPeerListener = incomingPeerListener; this.port = incomingPeerListener.getPort(); incomingPeerListener.register(this); List announceList = metafile.getAnnounceList(); if (announceList != null) { for (Object elem : announceList) { List tier = (List) elem; List trackerTier = new LinkedList<Tracker>(); for (Object trackerElem : tier) { ByteBuffer trackerAnnounce = (ByteBuffer) trackerElem; Tracker tracker = new Tracker(new String(trackerAnnounce.array())); byte[] keyBytes = new byte[4]; random.nextBytes(keyBytes); tracker.setKey(Utils.byteArrayToURLString(keyBytes)); trackerTier.add(tracker); } Collections.shuffle(trackerTier); trackerTiers.add(trackerTier); } } else { List<Tracker> uninqueTracker = new LinkedList<Tracker>(); Tracker tracker = new Tracker(metafile.getAnnounce()); byte[] keyBytes = new byte[4]; random.nextBytes(keyBytes); tracker.setKey(Utils.byteArrayToURLString(keyBytes)); uninqueTracker.add(tracker); trackerTiers.add(uninqueTracker); } activeTracker = trackerTiers.get(0).get(0); }
public boolean isCompleted() { return torrentDisk.getCompleted() == metafile.getLength(); }