From 53d8f9f9c39b88896439970762e5eef7adfdd340 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 29 Feb 2020 12:05:06 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Implement=20GetConfig=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/fcp/highlevel/FcpClient.java | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java index 65bc227..0c13505 100644 --- a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java @@ -18,6 +18,7 @@ package net.pterodactylus.fcp.highlevel; import static com.google.common.collect.FluentIterable.from; +import static java.util.stream.Collectors.toMap; import java.io.Closeable; import java.io.IOException; @@ -40,6 +41,7 @@ import net.pterodactylus.fcp.AllData; import net.pterodactylus.fcp.ClientGet; import net.pterodactylus.fcp.ClientHello; import net.pterodactylus.fcp.CloseConnectionDuplicateClientName; +import net.pterodactylus.fcp.ConfigData; import net.pterodactylus.fcp.DataFound; import net.pterodactylus.fcp.EndListPeerNotes; import net.pterodactylus.fcp.EndListPeers; @@ -50,6 +52,7 @@ import net.pterodactylus.fcp.FcpAdapter; import net.pterodactylus.fcp.FcpConnection; import net.pterodactylus.fcp.FcpListener; import net.pterodactylus.fcp.GenerateSSK; +import net.pterodactylus.fcp.GetConfig; import net.pterodactylus.fcp.GetFailed; import net.pterodactylus.fcp.GetNode; import net.pterodactylus.fcp.ListPeerNotes; @@ -1128,6 +1131,49 @@ public class FcpClient implements Closeable { } // + // CONFIG MANAGEMENT + // + + public Map getConfig() throws IOException, FcpException { + Map results = new HashMap<>(); + new ExtendedFcpAdapter() { + @Override + public void run() throws IOException { + GetConfig getConfig = new GetConfig(createIdentifier("get-config")); + getConfig.setWithCurrent(true); + getConfig.setWithDefaults(true); + getConfig.setWithShortDescription(true); + getConfig.setWithLongDescription(true); + getConfig.setWithDataTypes(true); + getConfig.setWithExpertFlag(true); + getConfig.setWithForceWriteFlag(true); + getConfig.setWithSortOrder(true); + fcpConnection.sendMessage(getConfig); + } + + @Override + public void receivedConfigData(FcpConnection fcpConnection, ConfigData configData) { + results.putAll(filterByResponseType(configData, "current")); + results.putAll(filterByResponseType(configData, "default")); + results.putAll(filterByResponseType(configData, "shortDescription")); + results.putAll(filterByResponseType(configData, "longDescription")); + results.putAll(filterByResponseType(configData, "expertFlag")); + results.putAll(filterByResponseType(configData, "dataType")); + results.putAll(filterByResponseType(configData, "sortOrder")); + results.putAll(filterByResponseType(configData, "forceWriteFlag")); + complete(); + } + + private Map filterByResponseType(ConfigData configData, String responseType) { + return configData.getFields().entrySet().stream() + .filter(e -> e.getKey().startsWith(responseType + ".")) + .collect(toMap(Entry::getKey, Entry::getValue)); + } + }.execute(); + return results; + } + + // // PRIVATE METHODS // -- 2.7.4