From 3233fad6a7cf276d13286f3ab7b5836fcabcf668 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 28 Sep 2014 20:47:00 +0200 Subject: [PATCH] Use event bus to activate and deactivate the FCP interface. --- .../java/net/pterodactylus/sone/core/Core.java | 2 +- .../net/pterodactylus/sone/core/Preferences.java | 7 ++++++ .../net/pterodactylus/sone/fcp/FcpInterface.java | 26 +++++++++------------- .../sone/fcp/event/FcpInterfaceActivatedEvent.java | 13 +++++++++++ .../fcp/event/FcpInterfaceDeactivatedEvent.java | 13 +++++++++++ .../pterodactylus/sone/core/PreferencesTest.java | 17 ++++++++++++++ .../pterodactylus/sone/fcp/FcpInterfaceTest.java | 12 +++++----- 7 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/fcp/event/FcpInterfaceActivatedEvent.java create mode 100644 src/main/java/net/pterodactylus/sone/fcp/event/FcpInterfaceDeactivatedEvent.java diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index f7b35e9..2e2cf3e 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1729,7 +1729,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, options.addIntegerOption("PositiveTrust", new DefaultOption(75, new IntegerRangePredicate(0, 100))); options.addIntegerOption("NegativeTrust", new DefaultOption(-25, new IntegerRangePredicate(-100, 100))); options.addStringOption("TrustComment", new DefaultOption("Set from Sone Web Interface")); - options.addBooleanOption("ActivateFcpInterface", new DefaultOption(false, fcpInterface.new SetActive())); + options.addBooleanOption("ActivateFcpInterface", new DefaultOption(false)); options.addIntegerOption("FcpFullAccessRequired", new DefaultOption(2, fcpInterface.new SetFullAccessRequired())); loadConfigurationValue("InsertionDelay"); diff --git a/src/main/java/net/pterodactylus/sone/core/Preferences.java b/src/main/java/net/pterodactylus/sone/core/Preferences.java index c413f89..1f28c69 100644 --- a/src/main/java/net/pterodactylus/sone/core/Preferences.java +++ b/src/main/java/net/pterodactylus/sone/core/Preferences.java @@ -20,6 +20,8 @@ package net.pterodactylus.sone.core; import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent; import net.pterodactylus.sone.fcp.FcpInterface; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; +import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent; +import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent; import com.google.common.eventbus.EventBus; @@ -344,6 +346,11 @@ public class Preferences { */ public Preferences setFcpInterfaceActive(boolean fcpInterfaceActive) { options.getBooleanOption("ActivateFcpInterface").set(fcpInterfaceActive); + if (fcpInterfaceActive) { + eventBus.post(new FcpInterfaceActivatedEvent()); + } else { + eventBus.post(new FcpInterfaceDeactivatedEvent()); + } return this; } diff --git a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java index 1ddf0a9..763a06e 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java +++ b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java @@ -30,6 +30,8 @@ import java.util.logging.Logger; import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.core.Options.Option; import net.pterodactylus.sone.core.Options.OptionWatcher; +import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent; +import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent; import net.pterodactylus.sone.freenet.fcp.Command.AccessType; import net.pterodactylus.sone.freenet.fcp.Command.ErrorResponse; import net.pterodactylus.sone.freenet.fcp.Command.Response; @@ -42,6 +44,7 @@ import freenet.support.SimpleFieldSet; import freenet.support.api.Bucket; import com.google.common.annotations.VisibleForTesting; +import com.google.common.eventbus.Subscribe; import com.google.inject.Inject; import com.google.inject.Singleton; @@ -118,15 +121,7 @@ public class FcpInterface { return active.get(); } - /** - * Sets whether the FCP interface should handle requests. If {@code active} - * is {@code false}, all requests are answered with an error. - * - * @param active - * {@code true} to activate the FCP interface, {@code false} to - * deactivate the FCP interface - */ - public void setActive(boolean active) { + private void setActive(boolean active) { this.active.set(active); } @@ -233,13 +228,14 @@ public class FcpInterface { } } - public class SetActive implements OptionWatcher { - - @Override - public void optionChanged(Option option, Boolean oldValue, Boolean newValue) { - setActive(newValue); - } + @Subscribe + public void fcpInterfaceActivated(FcpInterfaceActivatedEvent fcpInterfaceActivatedEvent) { + setActive(true); + } + @Subscribe + public void fcpInterfaceDeactivated(FcpInterfaceDeactivatedEvent fcpInterfaceDeactivatedEvent) { + setActive(false); } public class SetFullAccessRequired implements OptionWatcher { diff --git a/src/main/java/net/pterodactylus/sone/fcp/event/FcpInterfaceActivatedEvent.java b/src/main/java/net/pterodactylus/sone/fcp/event/FcpInterfaceActivatedEvent.java new file mode 100644 index 0000000..56b658a --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/fcp/event/FcpInterfaceActivatedEvent.java @@ -0,0 +1,13 @@ +package net.pterodactylus.sone.fcp.event; + +import net.pterodactylus.sone.fcp.FcpInterface; + +/** + * Event that signals that the {@link FcpInterface} was activated in the + * configuration. + * + * @author David ‘Bombe’ Roden + */ +public class FcpInterfaceActivatedEvent { + +} diff --git a/src/main/java/net/pterodactylus/sone/fcp/event/FcpInterfaceDeactivatedEvent.java b/src/main/java/net/pterodactylus/sone/fcp/event/FcpInterfaceDeactivatedEvent.java new file mode 100644 index 0000000..b97ef76 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/fcp/event/FcpInterfaceDeactivatedEvent.java @@ -0,0 +1,13 @@ +package net.pterodactylus.sone.fcp.event; + +import net.pterodactylus.sone.fcp.FcpInterface; + +/** + * Event that signals that the {@link FcpInterface} was deactivated in the + * configuration. + * + * @author David ‘Bombe’ Roden + */ +public class FcpInterfaceDeactivatedEvent { + +} diff --git a/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java b/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java index e2caaae..c79e6ee 100644 --- a/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java +++ b/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java @@ -7,11 +7,14 @@ import static org.hamcrest.Matchers.is; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import net.pterodactylus.sone.core.Options.Option; import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; +import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent; +import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent; import com.google.common.eventbus.EventBus; import org.junit.Before; @@ -212,6 +215,20 @@ public class PreferencesTest { } @Test + public void settingFcpInterfaceActiveIsForwardedToEventBus() { + preferences.setFcpInterfaceActive(true); + verify(eventBus).post(any(FcpInterfaceActivatedEvent.class)); + verifyNoMoreInteractions(eventBus); + } + + @Test + public void settingFcpInterfaceInactiveIsForwardedToEventBus() { + preferences.setFcpInterfaceActive(false); + verify(eventBus).post(any(FcpInterfaceDeactivatedEvent.class)); + verifyNoMoreInteractions(eventBus); + } + + @Test public void settingFcpInterfaceActiveIsForwardedToOption() { preferences.setFcpInterfaceActive(true); verify(booleanOption).set(true); diff --git a/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java b/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java index 2c8a910..337b664 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java @@ -6,8 +6,9 @@ import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import net.pterodactylus.sone.fcp.FcpInterface.SetActive; import net.pterodactylus.sone.fcp.FcpInterface.SetFullAccessRequired; +import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent; +import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent; import org.junit.Test; @@ -19,18 +20,17 @@ import org.junit.Test; public class FcpInterfaceTest { private final FcpInterface fcpInterface = new FcpInterface(null); - private final SetActive setActive = fcpInterface.new SetActive(); private final SetFullAccessRequired setFullAccessRequired = fcpInterface.new SetFullAccessRequired(); @Test - public void setActiveCanActivateFcpInterface() { - setActive.optionChanged(null, null, true); + public void fcpInterfaceCanBeActivated() { + fcpInterface.fcpInterfaceActivated(new FcpInterfaceActivatedEvent()); assertThat(fcpInterface.isActive(), is(true)); } @Test - public void setActiveCanDeactivateFcpInterface() { - setActive.optionChanged(null, null, false); + public void fcpInterfaceCanBeDeactivated() { + fcpInterface.fcpInterfaceDeactivated(new FcpInterfaceDeactivatedEvent()); assertThat(fcpInterface.isActive(), is(false)); } -- 2.7.4