// retoor <retoor@molodetz.nl>
import "subprocess" for Popen
System.print("=== Concurrent Ping Demo (Parallel Streaming) ===\n")
class PingTask {
construct new(host, label) {
_host = host
_label = label
_done = false
_proc = Popen.new(["ping", "-c", "3", host])
}
tick() {
if (_done) return
var chunk = _proc.stdout.read()
if (chunk == "") {
_done = true
} else {
for (line in chunk.split("\n")) {
if (line.trim() != "") {
System.print("[%(_label)] %(line)")
}
}
}
}
isDone { _done }
label { _label }
host { _host }
}
var tasks = [
PingTask.new("127.0.0.1", "LOCAL"),
PingTask.new("8.8.8.8", "GOOGLE"),
PingTask.new("1.1.1.1", "CLOUDFLARE"),
PingTask.new("9.9.9.9", "QUAD9"),
PingTask.new("208.67.222.222", "OPENDNS")
]
System.print("Starting 5 concurrent pings...")
for (t in tasks) System.print(" %(t.label) -> %(t.host)")
System.print("")
while (true) {
var allDone = true
for (task in tasks) {
if (!task.isDone) {
allDone = false
task.tick()
}
}
if (allDone) break
}
System.print("")
System.print("All 5 pings completed concurrently.")