Add command that returns information about a plugin
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 21 Aug 2015 22:39:42 +0000 (00:39 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 21 Aug 2015 22:39:42 +0000 (00:39 +0200)
src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java
src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java
src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommand.java [new file with mode: 0644]
src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommandImpl.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java

index 3f485c0..fe7300d 100644 (file)
@@ -129,5 +129,10 @@ public class DefaultFcpClient implements FcpClient {
                return new RemovePluginCommandImpl(threadPool, this::connect);
        }
 
+       @Override
+       public GetPluginInfoCommand getPluginInfo() {
+               return new GetPluginInfoCommandImpl(threadPool, this::connect);
+       }
+
 }
 
index 67c11ee..b799f27 100644 (file)
@@ -27,5 +27,6 @@ public interface FcpClient {
        LoadPluginCommand loadPlugin();
        ReloadPluginCommand reloadPlugin();
        RemovePluginCommand removePlugin();
+       GetPluginInfoCommand getPluginInfo();
 
 }
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommand.java
new file mode 100644 (file)
index 0000000..047024b
--- /dev/null
@@ -0,0 +1,16 @@
+package net.pterodactylus.fcp.quelaton;
+
+import java.util.Optional;
+
+import net.pterodactylus.fcp.PluginInfo;
+
+/**
+ * Returns information about a plugin.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public interface GetPluginInfoCommand {
+
+       Executable<Optional<PluginInfo>> plugin(String pluginName);
+
+}
diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommandImpl.java
new file mode 100644 (file)
index 0000000..8063bc7
--- /dev/null
@@ -0,0 +1,83 @@
+package net.pterodactylus.fcp.quelaton;
+
+import java.io.IOException;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+
+import net.pterodactylus.fcp.GetPluginInfo;
+import net.pterodactylus.fcp.PluginInfo;
+import net.pterodactylus.fcp.ProtocolError;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
+/**
+ * Default {@link GetPluginInfoCommand} implementation based on {@link FcpDialog}.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public class GetPluginInfoCommandImpl implements GetPluginInfoCommand {
+
+       private static final RandomIdentifierGenerator IDENTIFIER = new RandomIdentifierGenerator();
+       private final ListeningExecutorService threadPool;
+       private final ConnectionSupplier connectionSupplier;
+       private final GetPluginInfo getPluginInfo = new GetPluginInfo(IDENTIFIER.generate());
+
+       public GetPluginInfoCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+               this.threadPool = MoreExecutors.listeningDecorator(threadPool);
+               this.connectionSupplier = connectionSupplier;
+       }
+
+       @Override
+       public Executable<Optional<PluginInfo>> plugin(String pluginName) {
+               getPluginInfo.setPluginName(pluginName);
+               return this::execute;
+       }
+
+       private ListenableFuture<Optional<PluginInfo>> execute() {
+               return threadPool.submit(this::executeDialog);
+       }
+
+       private Optional<PluginInfo> executeDialog() throws IOException, ExecutionException, InterruptedException {
+               try (GetPluginInfoDialog getPluginInfoDialog = new GetPluginInfoDialog()) {
+                       return getPluginInfoDialog.send(getPluginInfo).get();
+               }
+       }
+
+       private class GetPluginInfoDialog extends FcpDialog<Optional<PluginInfo>> {
+
+               private final AtomicBoolean finished = new AtomicBoolean();
+               private final AtomicReference<PluginInfo> pluginInfo = new AtomicReference<>();
+
+               public GetPluginInfoDialog() throws IOException {
+                       super(threadPool, connectionSupplier.get());
+               }
+
+               @Override
+               protected boolean isFinished() {
+                       return finished.get();
+               }
+
+               @Override
+               protected Optional<PluginInfo> getResult() {
+                       return Optional.ofNullable(pluginInfo.get());
+               }
+
+               @Override
+               protected void consumePluginInfo(PluginInfo pluginInfo) {
+                       this.pluginInfo.set(pluginInfo);
+                       finished.set(true);
+               }
+
+               @Override
+               protected void consumeProtocolError(ProtocolError protocolError) {
+                       finished.set(true);
+               }
+
+       }
+
+}
index ed8a02e..002086d 100644 (file)
@@ -2240,6 +2240,27 @@ public class DefaultFcpClientTest {
 
                }
 
+               public class GetPluginInfo {
+
+                       @Test
+                       public void gettingPluginInfoWorks() throws InterruptedException, ExecutionException, IOException {
+                               Future<Optional<PluginInfo>> pluginInfo = fcpClient.getPluginInfo().plugin(CLASS_NAME).execute();
+                               connectAndAssert(() -> matchGetPluginInfoMessage());
+                               replyWithPluginInfo();
+                               verifyPluginInfo(pluginInfo);
+                       }
+
+                       private Matcher<List<String>> matchGetPluginInfoMessage() {
+                               return matchesFcpMessage(
+                                       "GetPluginInfo",
+                                       "Identifier=" + identifier,
+                                       "PluginName=" + CLASS_NAME,
+                                       "EndMessage"
+                               );
+                       }
+
+               }
+
        }
 
 }