Remane BandwithManager to BandwithDispatcher. Rename NewClient to Client

This commit is contained in:
anthonyraymond 2017-05-23 22:54:40 +02:00
parent ddff0c3b6f
commit c80a274f5a
4 changed files with 37 additions and 40 deletions

View file

@ -8,7 +8,7 @@ import org.araymond.joal.core.config.JoalConfigProvider;
import org.araymond.joal.core.events.*;
import org.araymond.joal.core.torrent.watcher.TorrentFileProvider;
import org.araymond.joal.core.ttorent.client.ConnectionHandler;
import org.araymond.joal.tmp.NewClient;
import org.araymond.joal.core.ttorent.client.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
@ -35,7 +35,7 @@ public class SeedManager {
private final ApplicationEventPublisher publisher;
private ConnectionHandler connectionHandler;
private NewClient currentClient;
private Client currentClient;
@PostConstruct
private void init() throws IOException {
@ -72,7 +72,7 @@ public class SeedManager {
this.connectionHandler.getSocketAddress().getPort(),
ByteBuffer.wrap(id.getBytes(Torrent.BYTE_ENCODING))
);
this.currentClient = new NewClient(
this.currentClient = new Client(
peer,
configProvider,
torrentFileProvider,

View file

@ -1,4 +1,4 @@
package org.araymond.joal.tmp;
package org.araymond.joal.core.ttorent.client;
import com.google.common.collect.Lists;
import com.turn.ttorrent.common.Peer;
@ -9,10 +9,9 @@ import org.araymond.joal.core.events.NoMoreLeechers;
import org.araymond.joal.core.events.NoMoreTorrentsFileAvailable;
import org.araymond.joal.core.events.announce.AnnounceRequestingEvent;
import org.araymond.joal.core.exception.NoMoreTorrentsFileAvailableException;
import org.araymond.joal.core.ttorent.client.bandwidth.BandwidthManager;
import org.araymond.joal.core.ttorent.client.bandwidth.BandwidthDispatcher;
import org.araymond.joal.core.ttorent.client.bandwidth.TorrentWithStats;
import org.araymond.joal.core.torrent.watcher.TorrentFileProvider;
import org.araymond.joal.core.ttorent.client.MockedTorrent;
import org.araymond.joal.core.ttorent.client.announce.Announcer;
import org.araymond.joal.core.ttorent.client.announce.AnnouncerEventListener;
import org.slf4j.Logger;
@ -25,8 +24,8 @@ import java.util.List;
/**
* Created by raymo on 14/05/2017.
*/
public class NewClient implements AnnouncerEventListener {
private static final Logger logger = LoggerFactory.getLogger(NewClient.class);
public class Client implements AnnouncerEventListener {
private static final Logger logger = LoggerFactory.getLogger(Client.class);
private final JoalConfigProvider configProvider;
private final TorrentFileProvider torrentFileProvider;
@ -34,10 +33,10 @@ public class NewClient implements AnnouncerEventListener {
private final BitTorrentClient bitTorrentClient;
private final List<Announcer> announcers;
private final Peer self;
private final BandwidthManager bandwidthManager;
private final BandwidthDispatcher bandwidthDispatcher;
private ClientState currentState = ClientState.STOPPED;
public NewClient(final Peer peer, final JoalConfigProvider configProvider, final TorrentFileProvider torrentFileProvider, final ApplicationEventPublisher publisher, final BitTorrentClient bitTorrentClient) {
public Client(final Peer peer, final JoalConfigProvider configProvider, final TorrentFileProvider torrentFileProvider, final ApplicationEventPublisher publisher, final BitTorrentClient bitTorrentClient) {
this.configProvider = configProvider;
this.torrentFileProvider = torrentFileProvider;
this.publisher = publisher;
@ -45,7 +44,7 @@ public class NewClient implements AnnouncerEventListener {
this.announcers = new ArrayList<>();
this.self = peer;
bandwidthManager = new BandwidthManager(configProvider);
bandwidthDispatcher = new BandwidthDispatcher(configProvider);
}
private void setState(final ClientState state) {
@ -54,7 +53,7 @@ public class NewClient implements AnnouncerEventListener {
public void share() {
this.setState(ClientState.STARTING);
this.bandwidthManager.start();
this.bandwidthDispatcher.start();
// TODO : number of torrent to seed should be in config
final int numberOfTorrentToSeed = 1;
for (int i = 0; i < numberOfTorrentToSeed; ++i) {
@ -84,7 +83,7 @@ public class NewClient implements AnnouncerEventListener {
this.setState(ClientState.STOPPING);
// We need to work with a copy of the list, because when stop, an event is launched that remove the announcer from the list.
// And it result in a concurrent modification exception.
this.bandwidthManager.stop();
this.bandwidthDispatcher.stop();
Lists.newArrayList(this.announcers).forEach(Announcer::stop);
this.setState(ClientState.STOPPED);
}
@ -111,14 +110,14 @@ public class NewClient implements AnnouncerEventListener {
@Override
public void onAnnouncerStart(final Announcer announcer, final TorrentWithStats torrent) {
this.bandwidthManager.registerTorrent(torrent);
this.bandwidthDispatcher.registerTorrent(torrent);
}
@Override
public void onAnnouncerStop(final Announcer announcer, final TorrentWithStats torrent) {
logger.debug("Removed announcer for Torrent {}", torrent.getTorrent().getName());
this.announcers.remove(announcer);
this.bandwidthManager.unRegisterTorrent(torrent);
this.bandwidthDispatcher.unRegisterTorrent(torrent);
if (this.currentState!= ClientState.STOPPING && this.currentState != ClientState.STOPPED) {
try {

View file

@ -11,7 +11,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* Created by raymo on 14/05/2017.
*/
public class BandwidthManager implements Runnable {
public class BandwidthDispatcher implements Runnable {
private final JoalConfigProvider configProvider;
/**
@ -27,11 +27,11 @@ public class BandwidthManager implements Runnable {
private Thread thread;
private boolean stop;
public BandwidthManager(final JoalConfigProvider configProvider) {
public BandwidthDispatcher(final JoalConfigProvider configProvider) {
this(configProvider, 1000);
}
public BandwidthManager(final JoalConfigProvider configProvider, final Integer updateInterval) {
public BandwidthDispatcher(final JoalConfigProvider configProvider, final Integer updateInterval) {
Preconditions.checkNotNull(configProvider, "Cannot build without ConfigProvider.");
this.configProvider = configProvider;
this.updateInterval = updateInterval;

View file

@ -3,8 +3,6 @@ package org.araymond.joal.core.ttorent.client.bandwidth;
import org.araymond.joal.core.config.AppConfiguration;
import org.araymond.joal.core.config.JoalConfigProvider;
import org.araymond.joal.core.ttorent.client.MockedTorrent;
import org.araymond.joal.core.ttorent.client.bandwidth.BandwidthManager;
import org.araymond.joal.core.ttorent.client.bandwidth.TorrentWithStats;
import org.junit.Test;
import org.mockito.Mockito;
@ -15,11 +13,11 @@ import static org.assertj.core.api.Assertions.fail;
/**
* Created by raymo on 14/05/2017.
*/
public class BandwidthManagerTest {
public class BandwidthDispatcherTest {
@Test
public void shouldNotBuildWithoutConfigProvider() {
assertThatThrownBy(() -> new BandwidthManager(null))
assertThatThrownBy(() -> new BandwidthDispatcher(null))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("Cannot build without ConfigProvider.");
}
@ -29,7 +27,7 @@ public class BandwidthManagerTest {
final JoalConfigProvider configProvider = Mockito.mock(JoalConfigProvider.class);
try {
final BandwidthManager bandwidthManager = new BandwidthManager(configProvider);
final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(configProvider);
} catch (final Throwable ignored) {
fail("should build");
}
@ -43,10 +41,10 @@ public class BandwidthManagerTest {
Mockito.when(conf.getMinUploadRate()).thenReturn(180);
Mockito.when(conf.getMaxUploadRate()).thenReturn(190);
final BandwidthManager bandwidthManager = new BandwidthManager(configProvider);
final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(configProvider);
for (int i = 0; i < 20; ++i) {
assertThat(bandwidthManager.generateRandomizedSpeedInBytes())
assertThat(bandwidthDispatcher.generateRandomizedSpeedInBytes())
.isBetween(
(long) conf.getMinUploadRate() * 1024,
(long) conf.getMaxUploadRate() * 1024
@ -62,10 +60,10 @@ public class BandwidthManagerTest {
Mockito.when(conf.getMinUploadRate()).thenReturn(0);
Mockito.when(conf.getMaxUploadRate()).thenReturn(1);
final BandwidthManager bandwidthManager = new BandwidthManager(configProvider);
final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(configProvider);
for (int i = 0; i < 500; ++i) {
assertThat(bandwidthManager.generateRandomizedSpeedInBytes())
assertThat(bandwidthDispatcher.generateRandomizedSpeedInBytes())
.isBetween(
(long) conf.getMinUploadRate() * 1024,
(long) conf.getMaxUploadRate() * 1024
@ -81,10 +79,10 @@ public class BandwidthManagerTest {
Mockito.when(conf.getMinUploadRate()).thenReturn(0);
Mockito.when(conf.getMaxUploadRate()).thenReturn(0);
final BandwidthManager bandwidthManager = new BandwidthManager(configProvider);
final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(configProvider);
for (int i = 0; i < 20; ++i) {
assertThat(bandwidthManager.generateRandomizedSpeedInBytes()).isEqualTo(0);
assertThat(bandwidthDispatcher.generateRandomizedSpeedInBytes()).isEqualTo(0);
}
}
@ -97,13 +95,13 @@ public class BandwidthManagerTest {
Mockito.when(conf.getMaxUploadRate()).thenReturn(190);
final int updateInterval = 4;
final BandwidthManager bandwidthManager = new BandwidthManager(configProvider, updateInterval);
final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(configProvider, updateInterval);
final TorrentWithStats torrent = new TorrentWithStats(Mockito.mock(MockedTorrent.class));
bandwidthManager.registerTorrent(torrent);
bandwidthDispatcher.registerTorrent(torrent);
bandwidthManager.start();
bandwidthDispatcher.start();
Thread.sleep(5);
bandwidthManager.stop();
bandwidthDispatcher.stop();
assertThat(torrent.getUploaded())
.isBetween(
@ -122,19 +120,19 @@ public class BandwidthManagerTest {
Mockito.when(conf.getMaxUploadRate()).thenReturn(180);
final int updateInterval = 4;
final BandwidthManager bandwidthManager = new BandwidthManager(configProvider, updateInterval);
final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(configProvider, updateInterval);
final TorrentWithStats torrent = new TorrentWithStats(Mockito.mock(MockedTorrent.class));
final TorrentWithStats torrent2 = new TorrentWithStats(Mockito.mock(MockedTorrent.class));
final TorrentWithStats torrent3 = new TorrentWithStats(Mockito.mock(MockedTorrent.class));
final TorrentWithStats torrent4 = new TorrentWithStats(Mockito.mock(MockedTorrent.class));
bandwidthManager.registerTorrent(torrent);
bandwidthManager.registerTorrent(torrent2);
bandwidthManager.registerTorrent(torrent3);
bandwidthManager.registerTorrent(torrent4);
bandwidthDispatcher.registerTorrent(torrent);
bandwidthDispatcher.registerTorrent(torrent2);
bandwidthDispatcher.registerTorrent(torrent3);
bandwidthDispatcher.registerTorrent(torrent4);
bandwidthManager.start();
bandwidthDispatcher.start();
Thread.sleep(5);
bandwidthManager.stop();
bandwidthDispatcher.stop();
assertThat(torrent.getUploaded())
.isBetween(