Package com.torrent4j.model.peer

Examples of com.torrent4j.model.peer.TorrentPeer


        if (torrent == null) {
          e.getChannel().disconnect();
          return;
        }

        TorrentPeer peer = torrent.getSwarm().findPeer(
            (InetSocketAddress) e.getChannel().getRemoteAddress(),
            message.peerID);
        if (peer == null) {
          peer = new TorrentPeer(torrent);
          peer.setAddress((InetSocketAddress) e.getChannel()
              .getRemoteAddress());
        }
        peer.setPeerID(message.peerID);
       
        this.peer = (PeerWireProtocolPeer) peer.getProtocolPeer();

        e.getChannel().getPipeline()
            .get(PeerTrafficShapingHandler.class).setPeer(peer);
        e.getChannel().getPipeline()
            .get(TorrentTrafficShapingHandler.class)
            .setTorrent(torrent);

        peer.resetState();
        this.peer.getStrategy().getPeerStrategy()
            .peerConnected(torrent, peer);
      } else if (msg instanceof HaveMessage) {
        peer.getTorrentPeer()
            .getPieces()
            .addPiece(
                peer.getTorrent().getPiece(
                    ((HaveMessage) msg).pieceIndex));
        peer.getStrategy()
            .getPeerStrategy()
            .havePiece(
                peer.getTorrent(),
                peer.getTorrentPeer(),
                peer.getTorrent().getPiece(
                    ((HaveMessage) msg).pieceIndex));
      } else if (msg instanceof UnchokeMessage) {
        peer.getTorrentPeer().getState()
            .setRemoteChoked(TorrentPeerChoking.UNCHOKED);
        peer.getStrategy().getPeerStrategy()
            .unchoked(peer.getTorrent(), peer.getTorrentPeer());
      } else if (msg instanceof ChokeMessage) {
        peer.getTorrentPeer().getState()
            .setRemoteChoked(TorrentPeerChoking.CHOKED);
        peer.getStrategy().getPeerStrategy()
            .choked(peer.getTorrent(), peer.getTorrentPeer());
      } else if (msg instanceof InterestedMessage) {
        peer.getTorrentPeer().getState()
            .setRemoteInterest(TorrentPeerInterest.INTERESTED);
        peer.getStrategy().getPeerStrategy()
            .interested(peer.getTorrent(), peer.getTorrentPeer());
      } else if (msg instanceof NotInterestedMessage) {
        peer.getTorrentPeer().getState()
            .setRemoteInterest(TorrentPeerInterest.NOT_INTERESTED);
        peer.getStrategy()
            .getPeerStrategy()
            .notInterested(peer.getTorrent(), peer.getTorrentPeer());
      } else if (msg instanceof BitFieldMessage) {
        peer.getTorrentPeer().getPieces()
            .load(((BitFieldMessage) msg).bitSet);
        peer.getStrategy().getPeerStrategy()
            .bitField(peer.getTorrent(), peer.getTorrentPeer());
      } else if (msg instanceof RequestMessage) {
        final RequestMessage message = (RequestMessage) msg;

        final TorrentPiece piece = peer.getTorrent().getPiece(
            message.pieceIndex);
        final TorrentPieceBlock block = piece.getBlock(message.begin,
            message.length);
       
        if(peer.getTorrentPeer().getState().hasUploadRequestedBlock()) {
          peer.disconnect();
          return;
        }

        peer.getTorrentPeer().getState().setUploadRequestedBlock(block);
        peer.getTorrentPeer().getState()
            .setUploadRequestedDate(new Date());

        peer.getStrategy()
            .getUploadStrategy()
            .blockRequested(peer.getTorrent(), block,
                peer.getTorrentPeer());
      } else if (msg instanceof CancelMessage) {
        final CancelMessage message = (CancelMessage) msg;

        final TorrentPiece piece = peer.getTorrent().getPiece(
            message.pieceIndex);
        final TorrentPieceBlock block = piece.getBlock(message.begin,
            message.length);

        peer.getTorrentPeer().getState().setUploadRequestedBlock(null);
        peer.getTorrentPeer().getState().setUploadRequestedDate(null);

        peer.getStrategy()
            .getUploadStrategy()
            .blockRequestCancelled(peer.getTorrent(), block,
                peer.getTorrentPeer());
      } else if (msg instanceof BlockMessage) {
        final BlockMessage message = (BlockMessage) msg;

        final TorrentPiece piece = peer.getTorrent().getPiece(
            message.pieceIndex);
        final TorrentPieceBlock block = piece.getBlock(message.begin,
            message.data.remaining());

        peer.getTorrentPeer().getState().setLastDownloadedBlock(block);
        peer.getTorrentPeer().getState()
            .setLastDownloadedBlockDate(new Date());

        peer.getTorrentPeer().getState()
            .setDownloadRequestedBlock(null);
        peer.getTorrentPeer().getState().setDownloadRequestedDate(null);

        controller.getStorage().write(piece.getTorrent(),
            block.getTorrentRange(), message.data);
        block.setDownloaded(true);
        if (piece.isDownloaded()) {
          final Hash pieceHash = controller.getStorage().checksum(
              piece);
          if (!piece.getHash().equals(pieceHash)) {
            piece.getTorrent()
                .getStrategy()
                .getDownloadStrategy()
                .pieceChecksumFailed(piece.getTorrent(), piece,
                    peer.getTorrentPeer());
          } else {
            piece.getTorrent().getCompletedPieces().addPiece(piece);
            piece.getTorrent()
                .getStrategy()
                .getDownloadStrategy()
                .pieceComplete(piece.getTorrent(), piece,
                    peer.getTorrentPeer());
          }
        } else {
          piece.getTorrent()
              .getStrategy()
              .getDownloadStrategy()
              .blockReceived(block.getTorrent(), block,
                  peer.getTorrentPeer());
        }
      } else {
        System.out.println(msg);
      }
    } finally {
View Full Code Here


    System.out.println("Torrent hash is " + torrent.getHash().getString());

    // controller.checkExistingData(torrent);

    controller.registerTorrent(torrent);
    final TorrentPeer peer = new TorrentPeer(torrent);
    peer.setAddress(new InetSocketAddress(Inet4Address
        .getByName("127.0.0.1"), 34096));
    torrent.getSwarm().addPeer(peer);
   
    while(true) {
      Thread.sleep(1000);
      System.out.println((peer.getTrafficControl().getCurrentDownloadSpeed() / 1024) + " kb/s");
      //peer.getTrafficControl().setDownloadSpeedLimit(32 * 1024);
      torrent.getTrafficControl().setDownloadSpeedLimit(256 * 1024);
    }

    // System.out.println(((StandardTorrentStrategy)
View Full Code Here

TOP

Related Classes of com.torrent4j.model.peer.TorrentPeer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.