Search in sources :

Example 1 with TrackedTorrent

use of com.turn.ttorrent.tracker.TrackedTorrent in project teamcity-torrent-plugin by JetBrains.

the class TorrentTrackerConfiguratorTest method test_announce_interval.

public void test_announce_interval() throws IOException, TrackerMessage.MessageValidationException {
    System.setProperty(TorrentConfiguration.ANNOUNCE_INTERVAL, "10");
    myConfigurator.getConfigurationWatcher().checkForModifications();
    assertEquals(10, myConfigurator.getAnnounceIntervalSec());
    assertEquals(10, myTrackerManager.getTrackerService().getAnnounceInterval());
    final String uri = "http://localhost:8111/trackerAnnounce.html" + "?info_hash=12345678901234567890" + "&peer_id=ABCDEFGHIJKLMNOPQRST" + "&ip=127.0.0.0" + "&port=6881" + "&downloaded=1234" + "&left=98765" + "&event=stopped";
    final AtomicReference<String> response = new AtomicReference<String>();
    myTrackerManager.getTrackerService().process(uri, "http://localhost:8111/", new TrackerRequestProcessor.RequestHandler() {

        public void serveResponse(int code, String description, ByteBuffer responseData) {
            response.set(new String(responseData.array()));
        }

        public ConcurrentMap<String, TrackedTorrent> getTorrentsMap() {
            return new ConcurrentHashMap<String, TrackedTorrent>();
        }
    });
    final HTTPAnnounceResponseMessage parse = (HTTPAnnounceResponseMessage) HTTPTrackerMessage.parse(new ByteArrayInputStream(response.get().getBytes()));
    assertEquals(10, parse.getInterval());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ConcurrentMap(java.util.concurrent.ConcurrentMap) HTTPAnnounceResponseMessage(com.turn.ttorrent.common.protocol.http.HTTPAnnounceResponseMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) TrackerRequestProcessor(com.turn.ttorrent.tracker.TrackerRequestProcessor) TrackedTorrent(com.turn.ttorrent.tracker.TrackedTorrent)

Example 2 with TrackedTorrent

use of com.turn.ttorrent.tracker.TrackedTorrent 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 3 with TrackedTorrent

use of com.turn.ttorrent.tracker.TrackedTorrent 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

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