commit 0fae24059a8bb1046005386233833858663b7791
parent fd7ee8e76d61c2b6d9e4e3a25a3756c71c00b433
Author: Matthias Jaros <jaros@mailbox.org>
Date: Mon, 9 Mar 2026 12:59:56 +0100
Refactored printing of progress bars to be able to handle adding of more
hosts during time
Diffstat:
2 files changed, 28 insertions(+), 13 deletions(-)
diff --git a/src/main/java/com/it_jaros/networkScanner/ProgressBar.java b/src/main/java/com/it_jaros/networkScanner/ProgressBar.java
@@ -1,32 +1,48 @@
package com.it_jaros.networkScanner;
+import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
public class ProgressBar {
private ScheduledExecutorService ui = Executors.newSingleThreadScheduledExecutor();
private List<Progress> hosts;
+ private ReentrantLock lock = new ReentrantLock();
- public void start(List<Progress> hosts) {
- this.hosts = hosts;
+ public void start() {
+ System.out.println("Progress:");
+ this.hosts = new ArrayList<>();
// move cursor down depending on how many hosts have to be scanned
- hosts.forEach((h) -> System.out.print("\n"));
+ System.out.print("\n");
ui.scheduleAtFixedRate(() -> {
- drawProgress();
+ lock.lock();
+ drawProgress(false);
+ lock.unlock();
}, 1, 200, TimeUnit.MILLISECONDS);
}
- private void drawProgress() {
- // clear bars by moving cursor up
- System.out.print("\u001b[" + hosts.size() + "A");
+ private void drawProgress(boolean last) {
+ // Make space for drawing of progress bars
+ //System.out.print("\u001b[" + hosts.size() + "B");
// paint a bar per host
for (Progress progress : hosts) {
drawBar(progress);
}
+
+ // move cursor up again
+ if (!last)
+ System.out.print("\u001b[" + hosts.size() + "A");
+ }
+
+ public void submit(Progress progress) {
+ lock.lock();
+ hosts.add(progress);
+ lock.unlock();
}
private void drawBar(Progress progress) {
@@ -54,7 +70,7 @@ public class ProgressBar {
try {
ui.awaitTermination(10, TimeUnit.SECONDS);
// we need to draw one last time for the 100% to appear
- drawProgress();
+ drawProgress(true);
} catch (InterruptedException ignored) {
}
}
diff --git a/src/main/java/com/it_jaros/networkScanner/Scanner.java b/src/main/java/com/it_jaros/networkScanner/Scanner.java
@@ -18,13 +18,13 @@ import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.LockSupport;
public class Scanner {
-
+
private final Counter counter = new Counter();
private final ProgressBar progressBar = new ProgressBar();
private final Semaphore socketLimit;
private final int timeoutInMillis;
private final long delayInNanos;
-
+
public Scanner(int socketLimit, int timeoutInMillis, int delayInMillis) {
this.timeoutInMillis = timeoutInMillis;
this.delayInNanos = TimeUnit.MILLISECONDS.toNanos(Math.max(0, delayInMillis));
@@ -38,13 +38,12 @@ public class Scanner {
public List<ScanResult> scanTargets(List<String> targets, String ports) {
List<ScanResult> results = new ArrayList<>();
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();) {
- List<Progress> progresses = new ArrayList<>();
+ progressBar.start();
for (String target : targets) {
ScanResult result = scanTargetPorts(target, ports, executor);
results.add(result);
- progresses.add(result.progress());
+ progressBar.submit(result.progress());
}
- progressBar.start(progresses);
}
progressBar.stop();