Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added
- REPEAT setting in PingAll + some output cleanup
[#9](https://github.com/netsec-ethz/scion-java-multiping/pull/9)

### Changed

- Post 0.3.0 release updates
Expand Down
66 changes: 48 additions & 18 deletions src/main/java/org/scion/multiping/PingAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@
* all available paths before it can report on the best path.
*/
public class PingAll {
private static final int REPEAT = 3;
private static final boolean SHOW_ONLY_ICMP = !true;
private static final Config config = new Config();

static {
config.tryICMP = false;
config.tryICMP = !false;
if (SHOW_ONLY_ICMP) {
DELAYED_PRINT = true;
}
}

private int nAsTried = 0;
Expand Down Expand Up @@ -74,8 +79,8 @@ private enum Policy {
SHORTEST_ECHO
}

private static final Policy POLICY = Policy.SHORTEST_TR;
private static final boolean SHOW_PATH = true;
private static final Policy POLICY = Policy.FASTEST_TR; // SHORTEST_TR;
private static final boolean SHOW_PATH = !true;

public static void main(String[] args) throws IOException {
PRINT = true;
Expand Down Expand Up @@ -134,48 +139,73 @@ private void runDemo(ParseAssignments.HostEntry remote) throws IOException {
InetSocketAddress destinationAddress =
new InetSocketAddress(InetAddress.getByAddress(new byte[] {1, 2, 3, 4}), 12345);
int nPaths;
Scmp.TimedMessage msg;
Scmp.TimedMessage[] msg = new Scmp.TimedMessage[REPEAT];
Ref<Path> bestPath = Ref.empty();
try {
List<Path> paths = service.getPaths(remote.getIsdAs(), destinationAddress);
if (paths.isEmpty()) {
String src = ScionUtil.toStringIA(service.getLocalIsdAs());
String dst = ScionUtil.toStringIA(remote.getIsdAs());
println("WARNING: No path found from " + src + " to " + dst);
if (!SHOW_ONLY_ICMP) {
println("WARNING: No path found from " + src + " to " + dst);
}
nAsNoPathFound++;
results.add(new Result(remote, Result.State.NO_PATH));
return;
}
nPaths = paths.size();
msg = findPaths(paths, bestPath);
msg[0] = findPaths(paths, bestPath);
if (msg[0] != null && REPEAT > 1) {
try (ScmpSender sender = Scmp.newSenderBuilder().build()) {
for (int i = 1; i < msg.length; i++) {
List<Scmp.TracerouteMessage> messages = sender.sendTracerouteRequest(bestPath.get());
msg[i] = messages.get(messages.size() - 1);
}
}
}
} catch (ScionRuntimeException e) {
println("ERROR: " + e.getMessage());
nAsError++;
results.add(new Result(remote, Result.State.ERROR));
return;
}
Result result = new Result(remote, msg, bestPath.get(), nPaths);
Result result = new Result(remote, msg[0], bestPath.get(), nPaths);
results.add(result);

if (msg == null) {
if (msg[0] == null) {
return;
}

// ICMP ping
String icmpMs = ICMP.pingICMP(msg.getPath().getRemoteAddress(), config);
result.setICMP(icmpMs);
StringBuilder icmpMs = new StringBuilder();
for (int i = 0; i < REPEAT; i++) {
String icmpMsStr = ICMP.pingICMP(msg[0].getPath().getRemoteAddress(), config);
icmpMs.append(icmpMsStr).append(" ");
if (icmpMsStr.startsWith("TIMEOUT") || icmpMsStr.startsWith("N/A")) {
break;
}
}
result.setICMP(icmpMs.toString());

// output
double millis = round(msg.getNanoSeconds() / (double) 1_000_000, 2);
int nHops = PathRawParser.create(msg.getPath().getRawPath()).getHopCount();
String addr = msg.getPath().getRemoteAddress().getHostAddress();
String out = addr + " nPaths=" + nPaths + " nHops=" + nHops;
out += " time=" + millis + "ms" + " ICMP=" + icmpMs;
int nHops = PathRawParser.create(msg[0].getPath().getRawPath()).getHopCount();
String addr = msg[0].getPath().getRemoteAddress().getHostAddress();
print(addr + " nPaths=" + nPaths + " nHops=" + nHops + " time= ");
for (Scmp.TimedMessage m : msg) {
double millis = round(m.getNanoSeconds() / (double) 1_000_000, 2);
print(millis + "ms ");
}
String icmpStr = icmpMs.toString();
print(" ICMP= " + icmpStr);
if (SHOW_PATH) {
out += " " + ScionUtil.toStringPath(bestPath.get().getMetadata());
print(" " + ScionUtil.toStringPath(bestPath.get().getMetadata()));
}
if (SHOW_ONLY_ICMP && (icmpStr.startsWith("N/A") || icmpStr.startsWith("TIMEOUT"))) {
clearPrintQueue();
} else {
println();
}
println(out);
if (msg.isTimedOut()) {
if (msg[0].isTimedOut()) {
nAsTimeout++;
} else {
nAsSuccess++;
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/org/scion/multiping/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
public class Util {

public static boolean PRINT = true;
public static boolean DELAYED_PRINT = false; // print only at newlines
private static final StringBuilder sb = new StringBuilder();

public static void sleep(long millis) {
try {
Expand All @@ -29,16 +31,33 @@ public static void sleep(long millis) {

public static void print(String msg) {
if (PRINT) {
System.out.print(msg);
if (DELAYED_PRINT) {
sb.append(msg);
} else {
System.out.print(msg);
}
}
}

public static void println(String msg) {
print(msg);
}

public static void println() {
if (PRINT) {
System.out.println(msg);
if (DELAYED_PRINT) {
System.out.println(sb);
sb.setLength(0);
} else {
System.out.println();
}
}
}

public static void clearPrintQueue() {
sb.setLength(0);
}

public static double round(double d, int nDigits) {
double div = Math.pow(10, nDigits);
return Math.round(d * div) / div;
Expand Down