1 package net.pterodactylus.fcp.quelaton;
3 import java.io.IOException;
4 import java.util.Optional;
5 import java.util.concurrent.ExecutionException;
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.atomic.AtomicBoolean;
8 import java.util.concurrent.atomic.AtomicReference;
10 import net.pterodactylus.fcp.GetPluginInfo;
11 import net.pterodactylus.fcp.PluginInfo;
12 import net.pterodactylus.fcp.ProtocolError;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import com.google.common.util.concurrent.MoreExecutors;
19 * Default {@link GetPluginInfoCommand} implementation based on {@link FcpDialog}.
21 * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
23 public class GetPluginInfoCommandImpl implements GetPluginInfoCommand {
25 private static final RandomIdentifierGenerator IDENTIFIER = new RandomIdentifierGenerator();
26 private final ListeningExecutorService threadPool;
27 private final ConnectionSupplier connectionSupplier;
28 private final GetPluginInfo getPluginInfo = new GetPluginInfo(IDENTIFIER.generate());
30 public GetPluginInfoCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
31 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
32 this.connectionSupplier = connectionSupplier;
36 public Executable<Optional<PluginInfo>> plugin(String pluginName) {
37 getPluginInfo.setPluginName(pluginName);
41 private ListenableFuture<Optional<PluginInfo>> execute() {
42 return threadPool.submit(this::executeDialog);
45 private Optional<PluginInfo> executeDialog() throws IOException, ExecutionException, InterruptedException {
46 try (GetPluginInfoDialog getPluginInfoDialog = new GetPluginInfoDialog()) {
47 return getPluginInfoDialog.send(getPluginInfo).get();
51 private class GetPluginInfoDialog extends FcpDialog<Optional<PluginInfo>> {
53 private final AtomicBoolean finished = new AtomicBoolean();
54 private final AtomicReference<PluginInfo> pluginInfo = new AtomicReference<>();
56 public GetPluginInfoDialog() throws IOException {
57 super(threadPool, connectionSupplier.get());
61 protected boolean isFinished() {
62 return finished.get();
66 protected Optional<PluginInfo> getResult() {
67 return Optional.ofNullable(pluginInfo.get());
71 protected void consumePluginInfo(PluginInfo pluginInfo) {
72 this.pluginInfo.set(pluginInfo);
77 protected void consumeProtocolError(ProtocolError protocolError) {