diff --git a/src/main/java/org/araymond/joal/core/SeedManager.java b/src/main/java/org/araymond/joal/core/SeedManager.java index 427f014..d216edb 100644 --- a/src/main/java/org/araymond/joal/core/SeedManager.java +++ b/src/main/java/org/araymond/joal/core/SeedManager.java @@ -1,13 +1,13 @@ package org.araymond.joal.core; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.collect.Maps; import org.araymond.joal.core.bandwith.BandwidthDispatcher; import org.araymond.joal.core.bandwith.RandomSpeedProvider; import org.araymond.joal.core.client.emulated.BitTorrentClient; import org.araymond.joal.core.client.emulated.BitTorrentClientProvider; import org.araymond.joal.core.config.AppConfiguration; import org.araymond.joal.core.config.JoalConfigProvider; +import org.araymond.joal.core.events.config.ListOfClientFilesEvent; import org.araymond.joal.core.events.global.state.GlobalSeedStartedEvent; import org.araymond.joal.core.events.global.state.GlobalSeedStoppedEvent; import org.araymond.joal.core.events.speed.SeedingSpeedsHasChangedEvent; @@ -22,6 +22,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.List; /** * Created by raymo on 27/01/2017. @@ -55,14 +56,15 @@ public class SeedManager { this.joalFoldersPath = new JoalFoldersPath(Paths.get(joalConfFolder)); this.torrentFileProvider = new TorrentFileProvider(joalFoldersPath); this.configProvider = new JoalConfigProvider(mapper, joalFoldersPath, publisher); - this.bitTorrentClientProvider = new BitTorrentClientProvider(configProvider, mapper, joalFoldersPath, publisher); + this.bitTorrentClientProvider = new BitTorrentClientProvider(configProvider, mapper, joalFoldersPath); this.publisher = publisher; this.connectionHandler = new ConnectionHandler(); } public void startSeeding() throws IOException { this.configProvider.init(); - this.bitTorrentClientProvider.init(); + final List clientFiles = this.bitTorrentClientProvider.listClientFiles(); + this.publisher.publishEvent(new ListOfClientFilesEvent(clientFiles)); this.bitTorrentClientProvider.generateNewClient(); final BitTorrentClient bitTorrentClient = bitTorrentClientProvider.get(); diff --git a/src/main/java/org/araymond/joal/core/client/emulated/BitTorrentClientProvider.java b/src/main/java/org/araymond/joal/core/client/emulated/BitTorrentClientProvider.java index 89bb1b5..254ddbc 100644 --- a/src/main/java/org/araymond/joal/core/client/emulated/BitTorrentClientProvider.java +++ b/src/main/java/org/araymond/joal/core/client/emulated/BitTorrentClientProvider.java @@ -3,10 +3,8 @@ package org.araymond.joal.core.client.emulated; import com.fasterxml.jackson.databind.ObjectMapper; import org.araymond.joal.core.SeedManager; import org.araymond.joal.core.config.JoalConfigProvider; -import org.araymond.joal.core.events.old.config.ClientFilesDiscoveredEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.context.ApplicationEventPublisher; import javax.inject.Provider; import java.io.FileNotFoundException; @@ -27,21 +25,18 @@ public class BitTorrentClientProvider implements Provider { private final JoalConfigProvider configProvider; private final ObjectMapper objectMapper; private final Path clientsFolderPath; - private final ApplicationEventPublisher publisher; - public BitTorrentClientProvider(final JoalConfigProvider configProvider, final ObjectMapper objectMapper, final SeedManager.JoalFoldersPath joalFoldersPath, final ApplicationEventPublisher publisher) { + public BitTorrentClientProvider(final JoalConfigProvider configProvider, final ObjectMapper objectMapper, final SeedManager.JoalFoldersPath joalFoldersPath) { this.configProvider = configProvider; this.objectMapper = objectMapper; this.clientsFolderPath = joalFoldersPath.getClientsFilesPath(); - this.publisher = publisher; } - public void init() { + public List listClientFiles() { try (Stream paths = Files.walk(this.clientsFolderPath)) { - final List clients = paths.filter(p -> p.toString().endsWith(".client")) + return paths.filter(p -> p.toString().endsWith(".client")) .map(p -> p.getFileName().toString()) .collect(Collectors.toList()); - publisher.publishEvent(new ClientFilesDiscoveredEvent(clients)); } catch (final IOException e) { throw new IllegalStateException("Failed to walk through .clients files", e); } diff --git a/src/main/java/org/araymond/joal/core/events/old/config/ClientFilesDiscoveredEvent.java b/src/main/java/org/araymond/joal/core/events/config/ListOfClientFilesEvent.java similarity index 59% rename from src/main/java/org/araymond/joal/core/events/old/config/ClientFilesDiscoveredEvent.java rename to src/main/java/org/araymond/joal/core/events/config/ListOfClientFilesEvent.java index 699bc90..e527bf8 100644 --- a/src/main/java/org/araymond/joal/core/events/old/config/ClientFilesDiscoveredEvent.java +++ b/src/main/java/org/araymond/joal/core/events/config/ListOfClientFilesEvent.java @@ -1,16 +1,13 @@ -package org.araymond.joal.core.events.old.config; +package org.araymond.joal.core.events.config; import com.google.common.base.Preconditions; import java.util.List; -/** - * Created by raymo on 08/07/2017. - */ -public class ClientFilesDiscoveredEvent { +public class ListOfClientFilesEvent { private final List clients; - public ClientFilesDiscoveredEvent(final List clients) { + public ListOfClientFilesEvent(final List clients) { Preconditions.checkNotNull(clients, "Clients list must not be null"); this.clients = clients; } @@ -18,6 +15,4 @@ public class ClientFilesDiscoveredEvent { public List getClients() { return clients; } - } - diff --git a/src/main/java/org/araymond/joal/web/messages/outgoing/impl/config/ClientFilesDiscoveredPayload.java b/src/main/java/org/araymond/joal/web/messages/outgoing/impl/config/ClientFilesDiscoveredPayload.java index c6d8ce3..23e6ee7 100644 --- a/src/main/java/org/araymond/joal/web/messages/outgoing/impl/config/ClientFilesDiscoveredPayload.java +++ b/src/main/java/org/araymond/joal/web/messages/outgoing/impl/config/ClientFilesDiscoveredPayload.java @@ -1,6 +1,6 @@ package org.araymond.joal.web.messages.outgoing.impl.config; -import org.araymond.joal.core.events.old.config.ClientFilesDiscoveredEvent; +import org.araymond.joal.core.events.config.ListOfClientFilesEvent; import org.araymond.joal.web.messages.outgoing.MessagePayload; import java.util.List; @@ -12,7 +12,7 @@ public class ClientFilesDiscoveredPayload implements MessagePayload { private final List clients; - public ClientFilesDiscoveredPayload(final ClientFilesDiscoveredEvent event) { + public ClientFilesDiscoveredPayload(final ListOfClientFilesEvent event) { this.clients = event.getClients(); } diff --git a/src/main/java/org/araymond/joal/web/services/corelistener/WebConfigEventListener.java b/src/main/java/org/araymond/joal/web/services/corelistener/WebConfigEventListener.java index a3a6c70..d2549c5 100644 --- a/src/main/java/org/araymond/joal/web/services/corelistener/WebConfigEventListener.java +++ b/src/main/java/org/araymond/joal/web/services/corelistener/WebConfigEventListener.java @@ -1,6 +1,6 @@ package org.araymond.joal.web.services.corelistener; -import org.araymond.joal.core.events.old.config.ClientFilesDiscoveredEvent; +import org.araymond.joal.core.events.config.ListOfClientFilesEvent; import org.araymond.joal.core.events.old.config.ConfigHasBeenLoadedEvent; import org.araymond.joal.core.events.old.config.ConfigHasChangedEvent; import org.araymond.joal.web.annotations.ConditionalOnWebUi; @@ -48,8 +48,8 @@ public class WebConfigEventListener extends WebEventListener { @Order(Ordered.LOWEST_PRECEDENCE) @EventListener - void handleClientFilesDiscovered(final ClientFilesDiscoveredEvent event) { - logger.debug("Send ClientFilesDiscoveredEvent to clients."); + void handleClientFilesDiscovered(final ListOfClientFilesEvent event) { + logger.debug("Send ListOfClientFilesEvent to clients."); this.messagingTemplate.convertAndSend("/config", new ClientFilesDiscoveredPayload(event)); } diff --git a/src/test/java/org/araymond/joal/core/client/emulated/BitTorrentClientProviderTest.java b/src/test/java/org/araymond/joal/core/client/emulated/BitTorrentClientProviderTest.java index b913141..631ee93 100644 --- a/src/test/java/org/araymond/joal/core/client/emulated/BitTorrentClientProviderTest.java +++ b/src/test/java/org/araymond/joal/core/client/emulated/BitTorrentClientProviderTest.java @@ -4,14 +4,12 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.araymond.joal.core.SeedManager; import org.araymond.joal.core.config.JoalConfigProvider; import org.araymond.joal.core.config.JoalConfigProviderTest; -import org.araymond.joal.core.events.old.config.ClientFilesDiscoveredEvent; import org.junit.Test; -import org.mockito.ArgumentCaptor; import org.mockito.Mockito; -import org.springframework.context.ApplicationEventPublisher; import java.io.FileNotFoundException; import java.nio.file.Paths; +import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -26,14 +24,14 @@ public class BitTorrentClientProviderTest { private static BitTorrentClientProvider createProvider() { final JoalConfigProvider configProvider = Mockito.mock(JoalConfigProvider.class); Mockito.when(configProvider.get()).thenReturn(JoalConfigProviderTest.defaultConfig); - return new BitTorrentClientProvider(configProvider, new ObjectMapper(), joalFoldersPath, Mockito.mock(ApplicationEventPublisher.class)); + return new BitTorrentClientProvider(configProvider, new ObjectMapper(), joalFoldersPath); } @Test public void shouldFailIfClientFileDoesNotExists() { final JoalConfigProvider configProvider = Mockito.mock(JoalConfigProvider.class); Mockito.when(configProvider.get()).thenReturn(JoalConfigProviderTest.defaultConfig); - final BitTorrentClientProvider provider = new BitTorrentClientProvider(configProvider, new ObjectMapper(), new SeedManager.JoalFoldersPath(Paths.get("nop")), Mockito.mock(ApplicationEventPublisher.class)); + final BitTorrentClientProvider provider = new BitTorrentClientProvider(configProvider, new ObjectMapper(), new SeedManager.JoalFoldersPath(Paths.get("nop"))); assertThatThrownBy(provider::generateNewClient) .isInstanceOf(FileNotFoundException.class) @@ -70,18 +68,12 @@ public class BitTorrentClientProviderTest { } @Test - public void shouldPublishClientFilesDiscoveredOnInit() { - final ApplicationEventPublisher publisher = Mockito.mock(ApplicationEventPublisher.class); - final BitTorrentClientProvider provider = new BitTorrentClientProvider(Mockito.mock(JoalConfigProvider.class), new ObjectMapper(), joalFoldersPath, publisher); + public void shouldistClientFiles() { + final BitTorrentClientProvider provider = new BitTorrentClientProvider(Mockito.mock(JoalConfigProvider.class), new ObjectMapper(), joalFoldersPath); - provider.init(); - - final ArgumentCaptor captor = ArgumentCaptor.forClass(ClientFilesDiscoveredEvent.class); - Mockito.verify(publisher, Mockito.times(1)).publishEvent(captor.capture()); - - final ClientFilesDiscoveredEvent event = captor.getValue(); - assertThat(event.getClients()).hasSize(1); - assertThat(event.getClients().get(0)).isEqualTo("azureus-5.7.5.0.client"); + final List clientFiles = provider.listClientFiles(); + assertThat(clientFiles).hasSize(1); + assertThat(clientFiles.get(0)).isEqualTo("azureus-5.7.5.0.client"); } diff --git a/src/test/java/org/araymond/joal/core/events/old/config/ClientFilesDiscoveredEventTest.java b/src/test/java/org/araymond/joal/core/events/old/config/ClientFilesDiscoveredEventTest.java deleted file mode 100644 index cd05eee..0000000 --- a/src/test/java/org/araymond/joal/core/events/old/config/ClientFilesDiscoveredEventTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.araymond.joal.core.events.old.config; - -import com.google.common.collect.Lists; -import org.junit.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -/** - * Created by raymo on 09/07/2017. - */ -public class ClientFilesDiscoveredEventTest { - - @Test - public void shouldNotBuildWithoutClientList() { - assertThatThrownBy(() -> new ClientFilesDiscoveredEvent(null)) - .isInstanceOf(NullPointerException.class) - .hasMessage("Clients list must not be null"); - } - - @Test - public void shouldBuild() { - final List clients = Lists.newArrayList("az.client", "ut.client"); - final ClientFilesDiscoveredEvent event = new ClientFilesDiscoveredEvent(clients); - - assertThat(event.getClients()).isEqualTo(clients); - } - -}