increase number of udp packets in combined pinger and display total number of packets for dead hosts in PacketLossFetcher

This commit is contained in:
Anton Keks 2018-08-26 16:55:35 +03:00
parent 643a4e4705
commit ff88ded1fe
3 changed files with 18 additions and 13 deletions

View File

@ -30,12 +30,12 @@ public class CombinedUnprivilegedPinger implements Pinger {
public PingResult ping(ScanningSubject subject, int count) throws IOException {
// try UDP first - it should be more reliable in general
PingResult result = udpPinger.ping(subject, max(1, count / 2));
if (!result.isAlive()) {
// fallback to TCP - it may detect some hosts UDP cannot
result = tcpPinger.ping(subject, count);
}
return result;
PingResult udpResult = udpPinger.ping(subject, max(1, (int)Math.ceil(count / 1.5)));
if (udpResult.isAlive()) return udpResult;
// fallback to TCP - it may detect some hosts UDP cannot
PingResult tcpResult = tcpPinger.ping(subject, count);
return tcpResult.merge(udpResult);
}
public void close() throws IOException {

View File

@ -53,19 +53,18 @@ public class PingResult {
}
public int getPacketLoss() {
return (int)(packetCount - replyCount);
return packetCount - replyCount;
}
public int getPacketLossPercent() {
if(this.replyCount>0){
return (int) ((this.getPacketLoss() * 100) / packetCount);
}else{
if (replyCount > 0)
return (this.getPacketLoss() * 100) / packetCount;
else
return 100;
}
}
public int getPacketCount() {
return (int)(packetCount);
return packetCount;
}
public int getReplyCount() {
@ -87,4 +86,10 @@ public class PingResult {
public boolean isTimeoutAdaptationAllowed() {
return timeoutAdaptationAllowed;
}
PingResult merge(PingResult result) {
this.packetCount += result.packetCount;
this.replyCount += result.replyCount;
return this;
}
}

View File

@ -30,6 +30,6 @@ public class PacketLossFetcher extends PingFetcher {
PingResult result = executePing(subject);
subject.setResultType(result.isAlive() ? ALIVE : DEAD);
return result.getPacketLoss() + "/" + result.getPacketCount() + " ("+ result.getPacketLossPercent() + "%)";
return result.getPacketLoss() + "/" + result.getPacketCount() + " (" + result.getPacketLossPercent() + "%)";
}
}