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");
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;
*/
public Preferences setFcpInterfaceActive(boolean fcpInterfaceActive) {
options.getBooleanOption("ActivateFcpInterface").set(fcpInterfaceActive);
+ if (fcpInterfaceActive) {
+ eventBus.post(new FcpInterfaceActivatedEvent());
+ } else {
+ eventBus.post(new FcpInterfaceDeactivatedEvent());
+ }
return this;
}
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;
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;
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);
}
}
}
- 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> {
--- /dev/null
+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 {
+
+}
--- /dev/null
+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 {
+
+}
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;
}
@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);
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;
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));
}