All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bt.peer.TrackerPeerSourceFactory Maven / Gradle / Ivy

There is a newer version: 1.10
Show newest version
package bt.peer;

import bt.metainfo.Torrent;
import bt.metainfo.TorrentId;
import bt.net.Peer;
import bt.service.IRuntimeLifecycleBinder;
import bt.torrent.TorrentRegistry;
import bt.tracker.AnnounceKey;
import bt.tracker.ITrackerService;

import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

class TrackerPeerSourceFactory implements PeerSourceFactory {

    private ITrackerService trackerService;
    private TorrentRegistry torrentRegistry;
    private Duration trackerQueryInterval;
    private ConcurrentMap> peerSources;

    private ExecutorService executor;

    public TrackerPeerSourceFactory(ITrackerService trackerService,
                                    TorrentRegistry torrentRegistry,
                                    IRuntimeLifecycleBinder lifecycleBinder,
                                    Duration trackerQueryInterval) {
        this.trackerService = trackerService;
        this.torrentRegistry = torrentRegistry;
        this.trackerQueryInterval = trackerQueryInterval;
        this.peerSources = new ConcurrentHashMap<>();

        this.executor = Executors.newCachedThreadPool(new ThreadFactory() {
            AtomicInteger i = new AtomicInteger();

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "bt.peer.tracker-peer-source-" + i.incrementAndGet());
            }
        });
        lifecycleBinder.onShutdown("Shutdown tracker peer sources", executor::shutdownNow);
    }

    @Override
    public PeerSource getPeerSource(TorrentId torrentId) {
        Optional torrentOptional = torrentRegistry.getTorrent(torrentId);
        if (!torrentOptional.isPresent()) {
            // return a mock peer source instead of failing, because torrent might be being fetched at the time
            return noopSource;
        }

        Torrent torrent = torrentOptional.get();
        Optional announceKey = torrent.getAnnounceKey();
        if (!announceKey.isPresent()) {
            throw new IllegalStateException("Torrent does not have an announce key");
        }

        return getOrCreateTrackerPeerSource(torrentId, announceKey.get());
    }

    /**
     * @since 1.3
     */
    public PeerSource getPeerSource(TorrentId torrentId, AnnounceKey announceKey) {
        return getOrCreateTrackerPeerSource(torrentId, announceKey);
    }

    private TrackerPeerSource getOrCreateTrackerPeerSource(TorrentId torrentId, AnnounceKey announceKey) {
        ConcurrentMap map = getOrCreateTrackerPeerSourcesMap(torrentId);
        TrackerPeerSource trackerPeerSource = map.get(announceKey);
        if (trackerPeerSource == null) {
            trackerPeerSource = createTrackerPeerSource(torrentId, announceKey);
            TrackerPeerSource existing = map.putIfAbsent(announceKey, trackerPeerSource);
            if (existing != null) {
                trackerPeerSource = existing;
            }
        }
        return trackerPeerSource;
    }

    private ConcurrentMap getOrCreateTrackerPeerSourcesMap(TorrentId torrentId) {
        ConcurrentMap map = peerSources.get(torrentId);
        if (map == null) {
            map = new ConcurrentHashMap<>();
            ConcurrentMap existing = peerSources.putIfAbsent(torrentId, map);
            if (existing != null) {
                map = existing;
            }
        }
        return map;
    }

    private TrackerPeerSource createTrackerPeerSource(TorrentId torrentId, AnnounceKey announceKey) {
        return new TrackerPeerSource(executor, trackerService.getTracker(announceKey), torrentId, trackerQueryInterval);
    }

    private static final PeerSource noopSource = new PeerSource() {
        @Override
        public boolean update() {
            return false;
        }

        @Override
        public Collection getPeers() {
            return Collections.emptyList();
        }
    };
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy