From 991d578c2f8a1e77eb9f451e8b4d77e170e13f4e Mon Sep 17 00:00:00 2001 From: anthonyraymond Date: Sat, 17 Feb 2018 02:02:14 +0100 Subject: [PATCH] Fix few bugs --- src/main/java/org/araymond/joal/core/SeedManager.java | 1 - .../joal/core/bandwith/BandwidthDispatcher.java | 1 + .../core/torrent/watcher/TorrentFileProvider.java | 2 +- .../araymond/joal/core/ttorrent/client/Client.java | 1 + .../core/ttorrent/client/announcer/Announcer.java | 11 ++++++++--- .../ttorrent/client/announcer/AnnouncerFacade.java | 1 + .../{NewTrackerClient.java => TrackerClient.java} | 8 ++++---- .../outgoing/impl/announce/AnnouncePayload.java | 5 +++++ .../impl/speed/SeedingSpeedHasChangedPayload.java | 8 ++++---- .../joal/web/resources/WebSocketController.java | 8 +++++++- 10 files changed, 32 insertions(+), 14 deletions(-) rename src/main/java/org/araymond/joal/core/ttorrent/client/announcer/tracker/{NewTrackerClient.java => TrackerClient.java} (97%) diff --git a/src/main/java/org/araymond/joal/core/SeedManager.java b/src/main/java/org/araymond/joal/core/SeedManager.java index 8fc87bf..9394388 100644 --- a/src/main/java/org/araymond/joal/core/SeedManager.java +++ b/src/main/java/org/araymond/joal/core/SeedManager.java @@ -180,7 +180,6 @@ public class SeedManager { public void stop() { this.isSeeding = false; - this.bandwidthDispatcher = null; if (client != null) { this.client.stop(); this.publisher.publishEvent(new GlobalSeedStoppedEvent()); diff --git a/src/main/java/org/araymond/joal/core/bandwith/BandwidthDispatcher.java b/src/main/java/org/araymond/joal/core/bandwith/BandwidthDispatcher.java index 10b3199..dca556b 100644 --- a/src/main/java/org/araymond/joal/core/bandwith/BandwidthDispatcher.java +++ b/src/main/java/org/araymond/joal/core/bandwith/BandwidthDispatcher.java @@ -85,6 +85,7 @@ public class BandwidthDispatcher implements BandwidthDispatcherFacade, Runnable // refresh bandwidth every 1200000 milliseconds (20 minutes) if (this.threadLoopCounter == 1200000 / this.threadPauseInterval) { this.refreshCurrentBandwidth(); + this.threadLoopCounter = 0; } // This method as to run as fast as possible to avoid blocking other ones. Because we wan't this loop diff --git a/src/main/java/org/araymond/joal/core/torrent/watcher/TorrentFileProvider.java b/src/main/java/org/araymond/joal/core/torrent/watcher/TorrentFileProvider.java index 425c962..d10e6cc 100644 --- a/src/main/java/org/araymond/joal/core/torrent/watcher/TorrentFileProvider.java +++ b/src/main/java/org/araymond/joal/core/torrent/watcher/TorrentFileProvider.java @@ -144,7 +144,7 @@ public class TorrentFileProvider extends FileAlterationListenerAdaptor { public void moveToArchiveFolder(final String torrentInfoHash) { final Optional first = this.torrentFiles.entrySet().stream() - .filter(entry -> entry.getValue().getHexInfoHash().equals(torrentInfoHash)) + .filter(entry -> entry.getValue().getTorrentInfoHash().value().equals(torrentInfoHash)) .map(Map.Entry::getKey) .findFirst(); if (first.isPresent()) { diff --git a/src/main/java/org/araymond/joal/core/ttorrent/client/Client.java b/src/main/java/org/araymond/joal/core/ttorrent/client/Client.java index e803e42..ea2ee01 100644 --- a/src/main/java/org/araymond/joal/core/ttorrent/client/Client.java +++ b/src/main/java/org/araymond/joal/core/ttorrent/client/Client.java @@ -66,6 +66,7 @@ public class Client implements TorrentFileChangeAware, ClientFacade { this.announcerExecutor.execute(req, this.announceResponseCallback); try { this.lock.writeLock().lock(); + this.currentlySeedingAnnouncer.removeIf(an -> an.equals(req.getAnnouncer())); // remove the last recorded event this.currentlySeedingAnnouncer.add(req.getAnnouncer()); } finally { this.lock.writeLock().unlock(); diff --git a/src/main/java/org/araymond/joal/core/ttorrent/client/announcer/Announcer.java b/src/main/java/org/araymond/joal/core/ttorrent/client/announcer/Announcer.java index 17cfb25..acabc10 100644 --- a/src/main/java/org/araymond/joal/core/ttorrent/client/announcer/Announcer.java +++ b/src/main/java/org/araymond/joal/core/ttorrent/client/announcer/Announcer.java @@ -8,7 +8,7 @@ import org.araymond.joal.core.torrent.torrent.MockedTorrent; import org.araymond.joal.core.ttorrent.client.announcer.exceptions.TooMuchAnnouncesFailedInARawException; import org.araymond.joal.core.ttorrent.client.announcer.request.AnnounceDataAccessor; import org.araymond.joal.core.ttorrent.client.announcer.request.SuccessAnnounceResponse; -import org.araymond.joal.core.ttorrent.client.announcer.tracker.NewTrackerClient; +import org.araymond.joal.core.ttorrent.client.announcer.tracker.TrackerClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,12 +24,12 @@ public class Announcer implements AnnouncerFacade { private Integer lastKnownSeeders = null; private LocalDateTime lastAnnouncedAt = null; private final MockedTorrent torrent; - private final NewTrackerClient trackerClient; + private final TrackerClient trackerClient; private final AnnounceDataAccessor announceDataAccessor; public Announcer(final MockedTorrent torrent, final AnnounceDataAccessor announceDataAccessor) { this.torrent = torrent; - this.trackerClient = new NewTrackerClient(torrent); + this.trackerClient = new TrackerClient(torrent); this.announceDataAccessor = announceDataAccessor; } @@ -104,6 +104,11 @@ public class Announcer implements AnnouncerFacade { return this.torrent.getName(); } + @Override + public long getTorrentSize() { + return this.torrent.getSize(); + } + @Override public InfoHash getTorrentInfoHash() { return this.getTorrent().getTorrentInfoHash(); diff --git a/src/main/java/org/araymond/joal/core/ttorrent/client/announcer/AnnouncerFacade.java b/src/main/java/org/araymond/joal/core/ttorrent/client/announcer/AnnouncerFacade.java index fee28b4..f85e063 100644 --- a/src/main/java/org/araymond/joal/core/ttorrent/client/announcer/AnnouncerFacade.java +++ b/src/main/java/org/araymond/joal/core/ttorrent/client/announcer/AnnouncerFacade.java @@ -12,5 +12,6 @@ public interface AnnouncerFacade { Optional getLastKnownSeeders(); Optional getLastAnnouncedAt(); String getTorrentName(); + long getTorrentSize(); InfoHash getTorrentInfoHash(); } diff --git a/src/main/java/org/araymond/joal/core/ttorrent/client/announcer/tracker/NewTrackerClient.java b/src/main/java/org/araymond/joal/core/ttorrent/client/announcer/tracker/TrackerClient.java similarity index 97% rename from src/main/java/org/araymond/joal/core/ttorrent/client/announcer/tracker/NewTrackerClient.java rename to src/main/java/org/araymond/joal/core/ttorrent/client/announcer/tracker/TrackerClient.java index be05451..1d6cf48 100644 --- a/src/main/java/org/araymond/joal/core/ttorrent/client/announcer/tracker/NewTrackerClient.java +++ b/src/main/java/org/araymond/joal/core/ttorrent/client/announcer/tracker/TrackerClient.java @@ -25,12 +25,12 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -public class NewTrackerClient { - private static final Logger logger = LoggerFactory.getLogger(NewTrackerClient.class); +public class TrackerClient { + private static final Logger logger = LoggerFactory.getLogger(TrackerClient.class); private final TrackerClientUriProvider trackerClientUriProvider; - public NewTrackerClient(final MockedTorrent torrent) { + public TrackerClient(final MockedTorrent torrent) { final List trackerURIs = torrent.getAnnounceList().stream() // Use a list to keep it ordered .sequential() .flatMap(Collection::stream) @@ -55,7 +55,7 @@ public class NewTrackerClient { if (responseMessage instanceof ErrorMessage) { final ErrorMessage error = (ErrorMessage) responseMessage; - throw new AnnounceException(error.getReason()); + throw new AnnounceException(baseUri + ": " + error.getReason()); } } catch (final AnnounceException e) { // If the request has failed we need to move to the next tracker. diff --git a/src/main/java/org/araymond/joal/web/messages/outgoing/impl/announce/AnnouncePayload.java b/src/main/java/org/araymond/joal/web/messages/outgoing/impl/announce/AnnouncePayload.java index ed83733..4e6fb77 100644 --- a/src/main/java/org/araymond/joal/web/messages/outgoing/impl/announce/AnnouncePayload.java +++ b/src/main/java/org/araymond/joal/web/messages/outgoing/impl/announce/AnnouncePayload.java @@ -9,6 +9,7 @@ import java.time.LocalDateTime; public abstract class AnnouncePayload implements MessagePayload { private final InfoHash infoHash; private final String torrentName; + private final long torrentSize; private final int lastKnownInterval; private final int consecutiveFails; private final LocalDateTime lastAnnouncedAt; @@ -18,6 +19,7 @@ public abstract class AnnouncePayload implements MessagePayload { protected AnnouncePayload(final AnnouncerFacade announcerFacade) { this.infoHash = announcerFacade.getTorrentInfoHash(); this.torrentName = announcerFacade.getTorrentName(); + this.torrentSize = announcerFacade.getTorrentSize(); this.lastKnownInterval = announcerFacade.getLastKnownInterval(); this.consecutiveFails = announcerFacade.getConsecutiveFails(); this.lastAnnouncedAt = announcerFacade.getLastAnnouncedAt().orElse(null); @@ -31,6 +33,9 @@ public abstract class AnnouncePayload implements MessagePayload { public String getTorrentName() { return torrentName; } + public long getTorrentSize() { + return torrentSize; + } public int getLastKnownInterval() { return lastKnownInterval; } diff --git a/src/main/java/org/araymond/joal/web/messages/outgoing/impl/speed/SeedingSpeedHasChangedPayload.java b/src/main/java/org/araymond/joal/web/messages/outgoing/impl/speed/SeedingSpeedHasChangedPayload.java index 0ccb265..aa1d1de 100644 --- a/src/main/java/org/araymond/joal/web/messages/outgoing/impl/speed/SeedingSpeedHasChangedPayload.java +++ b/src/main/java/org/araymond/joal/web/messages/outgoing/impl/speed/SeedingSpeedHasChangedPayload.java @@ -12,7 +12,7 @@ public class SeedingSpeedHasChangedPayload implements MessagePayload { public SeedingSpeedHasChangedPayload(final SeedingSpeedsHasChangedEvent event) { this.speeds = event.getSpeeds().entrySet().stream() - .map(entry -> new SpeedPayload(entry.getKey(), String.valueOf(entry.getValue().getBytesPerSeconds()))) + .map(entry -> new SpeedPayload(entry.getKey(), Long.valueOf(entry.getValue().getBytesPerSeconds()))) .collect(Collectors.toList()); } @@ -22,9 +22,9 @@ public class SeedingSpeedHasChangedPayload implements MessagePayload { public static final class SpeedPayload { private final InfoHash infoHash; - private final String bytesPerSeconds; + private final Long bytesPerSeconds; - public SpeedPayload(final InfoHash infoHash, final String bytesPerSeconds) { + public SpeedPayload(final InfoHash infoHash, final Long bytesPerSeconds) { this.infoHash = infoHash; this.bytesPerSeconds = bytesPerSeconds; } @@ -33,7 +33,7 @@ public class SeedingSpeedHasChangedPayload implements MessagePayload { return infoHash; } - public String getBytesPerSeconds() { + public Long getBytesPerSeconds() { return bytesPerSeconds; } } diff --git a/src/main/java/org/araymond/joal/web/resources/WebSocketController.java b/src/main/java/org/araymond/joal/web/resources/WebSocketController.java index f77edf0..c96830c 100644 --- a/src/main/java/org/araymond/joal/web/resources/WebSocketController.java +++ b/src/main/java/org/araymond/joal/web/resources/WebSocketController.java @@ -3,12 +3,14 @@ package org.araymond.joal.web.resources; import com.turn.ttorrent.common.protocol.TrackerMessage.AnnounceRequestMessage.RequestEvent; import org.apache.commons.codec.binary.Base64; import org.araymond.joal.core.SeedManager; +import org.araymond.joal.core.bandwith.Speed; import org.araymond.joal.core.config.AppConfigurationIntegrityException; import org.araymond.joal.core.events.announce.SuccessfullyAnnounceEvent; import org.araymond.joal.core.events.config.ConfigHasBeenLoadedEvent; import org.araymond.joal.core.events.config.ListOfClientFilesEvent; import org.araymond.joal.core.events.speed.SeedingSpeedsHasChangedEvent; import org.araymond.joal.core.events.torrent.files.TorrentFileAddedEvent; +import org.araymond.joal.core.torrent.torrent.InfoHash; import org.araymond.joal.core.torrent.torrent.MockedTorrent; import org.araymond.joal.core.ttorrent.client.announcer.AnnouncerFacade; import org.araymond.joal.web.annotations.ConditionalOnWebUi; @@ -34,6 +36,7 @@ import javax.inject.Inject; import java.io.IOException; import java.util.LinkedList; import java.util.List; +import java.util.Map; /** * Created by raymo on 28/07/2017. @@ -126,7 +129,10 @@ public class WebSocketController { } // speeds - events.addFirst(StompMessage.wrap(new SeedingSpeedHasChangedPayload(new SeedingSpeedsHasChangedEvent(this.seedManager.getSpeedMap())))); + final Map speedMap = this.seedManager.getSpeedMap(); + if (!speedMap.isEmpty()) { + events.addFirst(StompMessage.wrap(new SeedingSpeedHasChangedPayload(new SeedingSpeedsHasChangedEvent(speedMap)))); + } // Announcers are the most likely to change due to a concurrent access, so we gather them as late as possible, and we put them at the top of the list. for (final AnnouncerFacade announcerFacade : this.seedManager.getCurrentlySeedingAnnouncer()) {