K
kneitzel
Gast
Also um einmal zu skizzieren, was mir so als Idee vorgeschwebt ist:
Das ist jetzt einfach eine Klasse, dessen Instanz eine Start und eine End IP hat. Und auf dieser IP Range kann dann eine Action ausgeführt werden, welche als Consumer angegeben wird dem eine Inet4Address übergeben wird.
Die main ist nur ein ganz kleines Beispiel - statt der Ausgabe kann da dann halt die Erreichbarkeit geprüft werden mir Ausgabe oder was auch immer gewünscht wird.
Java:
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.function.Consumer;
import java.util.stream.IntStream;
public class ParallelInet4Action {
Inet4Address startIp;
Inet4Address endIp;
public ParallelInet4Action(final Inet4Address startIp, final Inet4Address endIp) {
if (intFromIp(endIp) < intFromIp(startIp)) throw new IllegalArgumentException("Start IP was before End IP!");
this.startIp = startIp;
this.endIp = endIp;
}
public void doAction(final Consumer<Inet4Address> action) {
IntStream
.range(intFromIp(startIp), intFromIp(endIp))
.parallel()
.forEach(i -> action.accept(ipFromInt(i)));
}
private int intFromIp(final Inet4Address ip) {
return ByteBuffer.wrap(ip.getAddress()).getInt();
}
private Inet4Address ipFromInt(final int ip) {
try {
return (Inet4Address) Inet4Address.getByAddress(ByteBuffer.allocate(4).putInt(ip).array());
} catch (UnknownHostException ex) {
System.out.println("Unknown Host Exception - given ip address was invalid!");
throw new IllegalStateException("Given Address was invalid!");
}
}
public static void main (String[] args) throws UnknownHostException {
Inet4Address start = (Inet4Address) Inet4Address.getByAddress( new byte[]{127, 1, 2, 3});
Inet4Address end = (Inet4Address) Inet4Address.getByAddress( new byte[]{127, 1, 2, 18});
ParallelInet4Action action = new ParallelInet4Action(start, end);
action.doAction(i -> System.out.println(i));
}
}
Die main ist nur ein ganz kleines Beispiel - statt der Ausgabe kann da dann halt die Erreichbarkeit geprüft werden mir Ausgabe oder was auch immer gewünscht wird.