From 1bd00a519c60c3e9f2c3903d78c1c82a226ac7f6 Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Sat, 1 Jun 2024 13:05:38 +0000 Subject: [PATCH] tor: Implement BootstrapEventParser --- .../tor/controller/BootstrapEventParser.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 network/tor/tor/src/main/java/bisq/tor/controller/BootstrapEventParser.java diff --git a/network/tor/tor/src/main/java/bisq/tor/controller/BootstrapEventParser.java b/network/tor/tor/src/main/java/bisq/tor/controller/BootstrapEventParser.java new file mode 100644 index 0000000000..d72cd673dd --- /dev/null +++ b/network/tor/tor/src/main/java/bisq/tor/controller/BootstrapEventParser.java @@ -0,0 +1,55 @@ +package bisq.tor.controller; + +import bisq.tor.controller.events.events.BootstrapEvent; + +import java.util.Optional; + +public class BootstrapEventParser { + public static Optional tryParse(String line) { + // 650 STATUS_CLIENT NOTICE BOOTSTRAP PROGRESS=50 TAG=loading_descriptors SUMMARY="Loading relay descriptors" + String[] parts = line.split(" "); + + if (isBootstrapEvent(parts)) { + BootstrapEvent bootstrapEvent = parseBootstrapEvent(parts); + return Optional.of(bootstrapEvent); + } + + return Optional.empty(); + } + + + private static boolean isBootstrapEvent(String[] parts) { + // 650 STATUS_CLIENT NOTICE BOOTSTRAP PROGRESS=50 TAG=loading_descriptors SUMMARY="Loading relay descriptors" + return parts.length >= 7 && parts[3].equals("BOOTSTRAP"); + } + + + private static BootstrapEvent parseBootstrapEvent(String[] parts) { + String progress = parts[4].replace("PROGRESS=", ""); + String tag = parts[5].replace("TAG=", ""); + String summary = parseBootstrapSummary(parts); + + int progressInt = Integer.parseInt(progress); + return new BootstrapEvent(progressInt, tag, summary); + } + + private static String parseBootstrapSummary(String[] parts) { + StringBuilder summary = new StringBuilder(); + + // SUMMARY="Loading relay descriptors" has whitespaces in string + for (int i = 6; i < parts.length; i++) { + String summaryPart = parts[i]; + summary.append(summaryPart) + .append(" "); + } + + String summaryPrefix = "SUMMARY=\""; + summary.delete(0, summaryPrefix.length()); + + // ends with `" ` + int length = summary.length(); + summary.delete(length - 2, length); + + return summary.toString(); + } +}