Search in sources :

Example 1 with PeerUID

use of com.turn.ttorrent.common.PeerUID in project teamcity-torrent-plugin by JetBrains.

the class TorrentTrackerConfiguratorTest method test_torrent_expire_timeout.

public void test_torrent_expire_timeout() throws IOException, TrackerMessage.MessageValidationException, InterruptedException {
    System.setProperty(TorrentConfiguration.TRACKER_TORRENT_EXPIRE_TIMEOUT, "5");
    myConfigurator.getConfigurationWatcher().checkForModifications();
    assertEquals(5, myConfigurator.getTrackerTorrentExpireTimeoutSec());
    final String uriCompleted = "http://localhost:8111/trackerAnnounce.html" + "?info_hash=12345678901234567890" + "&peer_id=ABCDEFGHIJKLMNOPQRST" + "&ip=172.20.240.249" + "&port=6884" + "&downloaded=1234" + "&left=0" + "&event=" + "completed";
    final String uriCompleted2 = "http://localhost:8111/trackerAnnounce.html" + "?info_hash=12345678901234567890" + "&peer_id=BBCDEFGHIJKLMNOPQRST" + "&ip=172.20.240.249" + "&port=6881" + "&downloaded=1234" + "&left=0" + "&event=" + "completed";
    // assertEquals(uriCompleted, uriCompleted2);
    final AtomicReference<byte[]> response = new AtomicReference<byte[]>();
    final TrackerRequestProcessor.RequestHandler requestHandler = new TrackerRequestProcessor.RequestHandler() {

        public void serveResponse(int code, String description, ByteBuffer responseData) {
            response.set(responseData.array());
        }
    };
    myTrackerManager.getTrackerService().process(uriCompleted, "http://localhost:8111/", requestHandler);
    assertEquals(1, myTrackerManager.getTorrents().size());
    final AtomicInteger complete = new AtomicInteger(100);
    final AtomicInteger peersSize = new AtomicInteger(100);
    final String torrentHash = "3132333435363738393031323334353637383930";
    new WaitFor(15 * 1000) {

        @Override
        protected boolean condition() {
            try {
                myTrackerManager.getTrackerService().process(uriCompleted2, "http://localhost:8111/", requestHandler);
                final HTTPAnnounceResponseMessage parse = (HTTPAnnounceResponseMessage) HTTPTrackerMessage.parse(new ByteArrayInputStream(response.get()));
                complete.set(parse.getComplete());
                peersSize.set(parse.getPeers().size());
                final TrackedTorrent trackedTorrent = myTrackerManager.getTorrents().get(torrentHash);
                return parse.getComplete() == 1 && trackedTorrent.getPeers().size() == 1;
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TrackerMessage.MessageValidationException e) {
                e.printStackTrace();
            }
            return false;
        }
    };
    final TrackedTorrent trackedTorrent = myTrackerManager.getTorrents().get(torrentHash);
    for (PeerUID peerUID : trackedTorrent.getPeers().keySet()) {
        if (peerUID.getTorrentHash().equals("4142434445464748494A4B4C4D4E4F5051525354"))
            fail();
    }
    new WaitFor(15 * 1000) {

        @Override
        protected boolean condition() {
            return !myTrackerManager.getTorrents().containsKey(torrentHash);
        }
    };
    assertNotContains(myTrackerManager.getTorrents().keySet(), torrentHash);
}
Also used : HTTPAnnounceResponseMessage(com.turn.ttorrent.common.protocol.http.HTTPAnnounceResponseMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) HTTPTrackerMessage(com.turn.ttorrent.common.protocol.http.HTTPTrackerMessage) TrackerMessage(com.turn.ttorrent.common.protocol.TrackerMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WaitFor(jetbrains.buildServer.util.WaitFor) ByteArrayInputStream(java.io.ByteArrayInputStream) TrackerRequestProcessor(com.turn.ttorrent.tracker.TrackerRequestProcessor) TrackedTorrent(com.turn.ttorrent.tracker.TrackedTorrent) PeerUID(com.turn.ttorrent.common.PeerUID)

Example 2 with PeerUID

use of com.turn.ttorrent.common.PeerUID in project teamcity-torrent-plugin by JetBrains.

the class TorrentTrackerManagerTest method testThatOnePeerWithManyTorrentsCalculatedAsOnePeer.

@Test
public void testThatOnePeerWithManyTorrentsCalculatedAsOnePeer() {
    TrackedTorrent firstTorrent = new TrackedTorrent(new byte[] { 1 });
    TrackedTorrent secondTorrent = new TrackedTorrent(new byte[] { 2 });
    myTorrentTrackerManager.startTracker();
    final String ip = "127.0.0.1";
    final int port = 6881;
    firstTorrent.getPeers().put(new PeerUID(new InetSocketAddress(ip, port), "1"), new TrackedPeer(firstTorrent, ip, port, ByteBuffer.allocate(10)));
    firstTorrent.getPeers().put(new PeerUID(new InetSocketAddress(ip, port), "2"), new TrackedPeer(secondTorrent, ip, port, ByteBuffer.allocate(10)));
    myTorrentTrackerManager.getTorrentsRepository().putIfAbsent("1", firstTorrent);
    myTorrentTrackerManager.getTorrentsRepository().putIfAbsent("2", secondTorrent);
    assertEquals(2, myTorrentTrackerManager.getTorrents().size());
    assertEquals(1, myTorrentTrackerManager.getConnectedClientsNum());
    firstTorrent.getPeers().put(new PeerUID(new InetSocketAddress(ip, port + 1), "1"), new TrackedPeer(firstTorrent, ip, port + 1, ByteBuffer.allocate(10)));
    assertEquals(2, myTorrentTrackerManager.getTorrents().size());
    assertEquals(2, myTorrentTrackerManager.getConnectedClientsNum());
}
Also used : TrackedPeer(com.turn.ttorrent.tracker.TrackedPeer) InetSocketAddress(java.net.InetSocketAddress) TrackedTorrent(com.turn.ttorrent.tracker.TrackedTorrent) PeerUID(com.turn.ttorrent.common.PeerUID) Test(org.testng.annotations.Test)

Aggregations

PeerUID (com.turn.ttorrent.common.PeerUID)2 TrackedTorrent (com.turn.ttorrent.tracker.TrackedTorrent)2 TrackerMessage (com.turn.ttorrent.common.protocol.TrackerMessage)1 HTTPAnnounceResponseMessage (com.turn.ttorrent.common.protocol.http.HTTPAnnounceResponseMessage)1 HTTPTrackerMessage (com.turn.ttorrent.common.protocol.http.HTTPTrackerMessage)1 TrackedPeer (com.turn.ttorrent.tracker.TrackedPeer)1 TrackerRequestProcessor (com.turn.ttorrent.tracker.TrackerRequestProcessor)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 WaitFor (jetbrains.buildServer.util.WaitFor)1 Test (org.testng.annotations.Test)1