Use event bus to activate and deactivate the FCP interface.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Sep 2014 18:47:00 +0000 (20:47 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Sep 2014 20:23:39 +0000 (22:23 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/Preferences.java
src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java
src/main/java/net/pterodactylus/sone/fcp/event/FcpInterfaceActivatedEvent.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/fcp/event/FcpInterfaceDeactivatedEvent.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/core/PreferencesTest.java
src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java

index f7b35e9..2e2cf3e 100644 (file)
@@ -1729,7 +1729,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75, new IntegerRangePredicate(0, 100)));
                options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25, new IntegerRangePredicate(-100, 100)));
                options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
-               options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false, fcpInterface.new SetActive()));
+               options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false));
                options.addIntegerOption("FcpFullAccessRequired", new DefaultOption<Integer>(2, fcpInterface.new SetFullAccessRequired()));
 
                loadConfigurationValue("InsertionDelay");
index c413f89..1f28c69 100644 (file)
@@ -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;
        }
 
index 1ddf0a9..763a06e 100644 (file)
@@ -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<Boolean> {
-
-               @Override
-               public void optionChanged(Option<Boolean> 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<Integer> {
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 (file)
index 0000000..56b658a
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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 (file)
index 0000000..b97ef76
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class FcpInterfaceDeactivatedEvent {
+
+}
index e2caaae..c79e6ee 100644 (file)
@@ -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);
index 2c8a910..337b664 100644 (file)
@@ -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));
        }