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

bt.tracker.udp.UdpTracker Maven / Gradle / Ivy

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

import bt.metainfo.TorrentId;
import bt.service.IRuntimeLifecycleBinder;
import bt.service.IdentityService;
import bt.tracker.Tracker;
import bt.tracker.TrackerRequestBuilder;
import bt.tracker.TrackerResponse;
import bt.tracker.udp.AnnounceRequest.EventType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.SocketAddress;
import java.net.URL;
import java.util.Objects;
import java.util.Optional;

/**
 * Simple implementation of a UDP tracker client
 *
 * @since 1.0
 */
class UdpTracker implements Tracker {

    private static final Logger LOGGER = LoggerFactory.getLogger(UdpTracker.class);

    private IdentityService idService;
    private int listeningPort;
    private URL trackerUrl;
    private UdpMessageWorker worker;

    /**
     * @param trackerUrl String representation of the tracker's URL.
     *                   Must start with "udp://" pseudo-protocol.
     * @since 1.0
     */
    public UdpTracker(IdentityService idService,
                      IRuntimeLifecycleBinder lifecycleBinder,
                      InetAddress localAddress,
                      int listeningPort,
                      String trackerUrl) {
        this.idService = idService;
        this.listeningPort = listeningPort;
        // TODO: one UDP socket for all outgoing tracker connections
        this.trackerUrl = toUrl(trackerUrl);
        this.worker = new UdpMessageWorker(new InetSocketAddress(localAddress, 0), getSocketAddress(this.trackerUrl), lifecycleBinder);
    }

    private URL toUrl(String s) {
        if (!s.startsWith("udp://")) {
            throw new IllegalArgumentException("Unexpected URL format: " + s);
        }
        // workaround for java.net.MalformedURLException (unsupported protocol: udp)
        s = s.replace("udp://", "http://");

        try {
            return new URL(s);
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Invalid URL: " + s);
        }
    }

    private SocketAddress getSocketAddress(URL url) {
        String host = Objects.requireNonNull(url.getHost(), "Host name is required");
        int port = getPort(url);
        return new InetSocketAddress(host, port);
    }

    private int getPort(URL url) {
        int port = url.getPort();
        if (port < 0) {
            port = url.getDefaultPort();
            if (port < 0) {
                throw new IllegalArgumentException("Can't determine port from URL: " + url.toExternalForm());
            }
        }
        return port;
    }

    @Override
    public TrackerRequestBuilder request(TorrentId torrentId) {
        return new TrackerRequestBuilder(torrentId) {
            @Override
            public TrackerResponse start() {
                return announceEvent(EventType.START);
            }

            @Override
            public TrackerResponse stop() {
                return announceEvent(EventType.STOP);
            }

            @Override
            public TrackerResponse complete() {
                return announceEvent(EventType.COMPLETE);
            }

            @Override
            public TrackerResponse query() {
                return announceEvent(EventType.QUERY);
            }

            private TrackerResponse announceEvent(EventType eventType) {
                AnnounceRequest request = new AnnounceRequest();
                request.setTorrentId(getTorrentId());
                request.setPeerId(idService.getLocalPeerId());
                request.setDownloaded(getDownloaded());
                request.setLeft(getLeft());
                request.setUploaded(getUploaded());
                request.setEventType(eventType);
                request.setListeningPort((short) listeningPort);
                getRequestString(trackerUrl).ifPresent(request::setRequestString);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Executing tracker UDP request of type {}: {}", eventType.name(), request);
                }
                try {
                    return worker.sendMessage(request, AnnounceResponseHandler.handler());
                } catch (Exception e) {
                    return TrackerResponse.exceptional(e);
                }
            }
        };
    }

    private Optional getRequestString(URL url) {
        String result = url.getPath();
        if (url.getQuery() != null) {
            result += "?" + url.getQuery();
        }
        return result.isEmpty() ? Optional.empty() : Optional.of(result);
    }

    @Override
    public String toString() {
        return "UdpTracker{" +
                "trackerUrl=" + trackerUrl +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy