Skip to content

Commit

Permalink
tor: Implement BootstrapEventParser
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed Jun 1, 2024
1 parent dde2d57 commit 1bd00a5
Showing 1 changed file with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<BootstrapEvent> 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();
}
}

0 comments on commit 1bd00a5

Please sign in to comment.