Beispiel #1
0
  // @Test
  public void peerShouldDownloadBlock()
      throws InterruptedException, THPRequestException, HashException, CannotUnserializeException,
          CannotReadTokenException, CannotReadBencodedException, CannotReadMessageException {
    lock = new CountDownLatch(2);
    initGuice();
    initEventBus();
    initTorrentStream();
    initClient();

    client.loadMetaInfo(torrentStream);
    client.refreshPeerList();

    System.out.println("Refreshing peer list");
    lock.await(30, TimeUnit.SECONDS);
    assertNotNull(peers);
    assertFalse(peers.isEmpty());

    expectedMessageType = MessageType.PIECE;
    expectedMessageReceived = false;
    atLastOneExpected = true;
    lock = new CountDownLatch(1);

    for (Peer testedPeer : peers) {
      testedPeer.connect();

      // We have to wait for a while until the peer has been connected to and initialized
      Thread.sleep(15000);
      // We test if the peer has some pieces
      BitSet havePieces = testedPeer.getAvailablePieces();

      if (havePieces.cardinality() > 0) {
        assertNotEquals(0, havePieces.cardinality());

        // We request the first available piece
        int pieceIndex = havePieces.nextSetBit(0);
        assertTrue(pieceIndex >= 0);

        // We need to unchoke and signal our interest to the peer first
        testedPeer.unchoke();
        testedPeer.interested();

        if (!testedPeer.isChokingUs()) {
          // We can now request
          testedPeer.requestPieceBlock(pieceIndex, 0);
        }
      }
    }

    // Now that the piece has been requested we need to wait for a block message
    lock.await();
    assertTrue(expectedMessageReceived);
  }
Beispiel #2
0
  // @Test
  public void clientShouldStart()
      throws InterruptedException, THPRequestException, HashException, CannotUnserializeException,
          CannotReadTokenException, CannotReadBencodedException, CannotReadMessageException {
    lock = new CountDownLatch(2);
    initGuice();
    initEventBus();
    initTorrentStream();
    initClient();

    client.configure(
        ClientSettings.settingsBuilder()
            .basePath(Paths.get(this.getClass().getResource("").getPath()))
            .metainfoSource(torrentStream));

    client.start();

    while (true) {
      Thread.sleep(200);
    }
  }