use of com.inceptai.dobby.model.PingStats in project dobby-android by InceptAi.
the class PingAction method pingAndReturnStats.
private PingStats pingAndReturnStats(String ipAddress, int pingSystemTimeOut, int numberOfPings, int killTimeOutMs, ScheduledExecutorService scheduledExecutorService) throws PingActionException, IndexOutOfBoundsException {
/*
PING 192.168.1.1 (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=2.191 ms
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=3.634 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=1.847 ms
--- 192.168.1.1 ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 1.847/2.557/3.634/0.774 ms
*/
PingStats pingStatsToReturn = new PingStats(ipAddress);
String patternForLatency = "min/avg/max/[a-z]+ = \\d+(\\.\\d+)?/\\d+(\\.\\d+)?/\\d+(\\.\\d+)?/\\d+(\\.\\d+)?";
String patternForPktLoss = "\\d+(\\.\\d+)?% packet loss";
//Get pkts stats
String pingOutput = pingIP(ipAddress, pingSystemTimeOut, numberOfPings, killTimeOutMs, scheduledExecutorService);
Pattern pktsPattern = Pattern.compile(patternForPktLoss);
Matcher pktsMatcher = pktsPattern.matcher(pingOutput);
if (pktsMatcher.find()) {
String[] matchingStrings = pktsMatcher.group(0).split(" ");
if (matchingStrings.length >= 3) {
pingStatsToReturn.lossRatePercent = Utils.parseDoubleWithDefault(-1.0, matchingStrings[0].split("%")[0]);
}
}
Pattern latencyPattern = Pattern.compile(patternForLatency);
Matcher latencyMatcher = latencyPattern.matcher(pingOutput);
if (latencyMatcher.find()) {
String[] matchingStrings = latencyMatcher.group(0).split(" ");
if (matchingStrings.length >= 3) {
String[] latencies = matchingStrings[2].split("/");
if (latencies.length >= 4) {
pingStatsToReturn.minLatencyMs = Utils.parseDoubleWithDefault(0, latencies[0]);
pingStatsToReturn.avgLatencyMs = Utils.parseDoubleWithDefault(0, latencies[1]);
pingStatsToReturn.maxLatencyMs = Utils.parseDoubleWithDefault(0, latencies[2]);
pingStatsToReturn.deviationMs = Utils.parseDoubleWithDefault(0, latencies[3]);
}
}
}
pingStatsToReturn.updatedAt = System.currentTimeMillis();
return pingStatsToReturn;
}
use of com.inceptai.dobby.model.PingStats in project dobby-android by InceptAi.
the class PingAnalyzer method selectPingAddressesBasedOnFreshnessOfCachedResults.
private String[] selectPingAddressesBasedOnFreshnessOfCachedResults(int maxAgeToReTriggerPingMs) {
HashMap<String, Boolean> addressToPingMap = new HashMap<>();
String[] addressList = { ipLayerInfo.gateway, ipLayerInfo.dns1, ipLayerInfo.referenceExternalAddress1, ipLayerInfo.publicDns1 };
//By default ping everyone
for (String address : addressList) {
addressToPingMap.put(address, true);
}
//Mark ones with fresh enough timestamp as false
for (PingStats stats : ipLayerPingStats.values()) {
if (checkIfPingStatsIsFresh(stats, maxAgeToReTriggerPingMs)) {
if (addressToPingMap.get(stats.ipAddress) != null) {
addressToPingMap.put(stats.ipAddress, false);
}
}
}
List<String> addressListToPing = new ArrayList<>();
//Get the list of ips to ping
for (HashMap.Entry<String, Boolean> entry : addressToPingMap.entrySet()) {
if (entry.getValue()) {
addressListToPing.add(entry.getKey());
}
}
DobbyLog.v("PA: List of IP addresses to ping " + addressListToPing);
return addressListToPing.toArray(new String[addressListToPing.size()]);
}
use of com.inceptai.dobby.model.PingStats in project dobby-android by InceptAi.
the class PingAnalyzer method checkIfShouldRedoPingStats.
public boolean checkIfShouldRedoPingStats(int minGapToRetriggerPing, int lossRateToTriggerPing) {
boolean redoPing = false;
long maxTimeUpdatedAt = 0;
for (PingStats pingStats : ipLayerPingStats.values()) {
maxTimeUpdatedAt = Math.max(maxTimeUpdatedAt, pingStats.updatedAt);
if (pingStats.lossRatePercent == -1 || pingStats.lossRatePercent > lossRateToTriggerPing) {
redoPing = true;
break;
}
}
long gap = System.currentTimeMillis() - maxTimeUpdatedAt;
if (gap > minGapToRetriggerPing) {
redoPing = true;
}
return redoPing;
}
use of com.inceptai.dobby.model.PingStats in project dobby-android by InceptAi.
the class FakePingAnalyzer method generateIndividualPingStats.
public PingStats generateIndividualPingStats(String pingAddress, @FakePingConfig.LatencyRangeMs int latencyRangeMs, @FakePingConfig.LossRangePercent int lossPercentRangePercent) {
PingStats pingStats = new PingStats(pingAddress);
pingStats.lossRatePercent = generateLossValue(lossPercentRangePercent);
double latency = generateLatencyValue(latencyRangeMs);
pingStats.avgLatencyMs = pingStats.maxLatencyMs = pingStats.minLatencyMs = latency;
pingStats.deviationMs = 0;
pingStats.updatedAt = System.currentTimeMillis();
return pingStats;
}
use of com.inceptai.dobby.model.PingStats in project dobby-android by InceptAi.
the class FakePingAnalyzer method scheduleEssentialPingTestsAsync.
@Override
protected ListenableFuture<HashMap<String, PingStats>> scheduleEssentialPingTestsAsync(int maxAgeToReTriggerPingMs) throws IllegalStateException {
eventBus.postEvent(new DobbyEvent(DobbyEvent.EventType.PING_STARTED));
if (ipLayerInfo == null) {
//Try to get new iplayerInfo
eventBus.postEvent(new DobbyEvent(DobbyEvent.EventType.PING_FAILED));
throw new IllegalStateException("Cannot schedule pings when iplayerInfo is null or own IP is 0.0.0.0");
}
fakePingResultsFuture = dobbyThreadpool.getListeningScheduledExecutorService().schedule(new Callable<HashMap<String, PingStats>>() {
@Override
public HashMap<String, PingStats> call() {
HashMap<String, PingStats> pingStatsHashMap = generateFakePingStats();
ipLayerPingStats = pingStatsHashMap;
DobbyLog.v("FAKE IP Layer Ping Stats " + ipLayerPingStats.toString());
eventBus.postEvent(new DobbyEvent(DobbyEvent.EventType.PING_INFO_AVAILABLE));
return pingStatsHashMap;
}
}, PING_LATENCY_MS, TimeUnit.MILLISECONDS);
return fakePingResultsFuture;
}
Aggregations