forked from bisq-network/bisq2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
network/tor/tor/src/main/java/bisq/tor/controller/BootstrapEventParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |