implementovan user ping

verze3
Michal 2026-09-02 07:21:48 +02:00
parent 3fb6860de4
commit 0cc0c8ce7c
3 changed files with 84 additions and 7 deletions

View File

@ -70,7 +70,7 @@ public class Message implements Serializable {
public static final int SNMP_TEMPLATE_VALUE_MAP_LIST = 132;
// S->C snmp probe hodnota
public static final int SNMP_PROBE_VALUE = 133;
// S->C data z traffic monitoru
// S->C data z traffic monitoru
public static final int NODE_CONNECTION_TRAFFIC = 134;
// S->C informace o smazenem connection
public static final int NODE_CONNECTION_REMOVE = 135;
@ -96,10 +96,11 @@ public class Message implements Serializable {
public static final int DEVICE_TYPE_DELETE_RESULT = 145;
// S->C smazani uzovatele
public static final int USER_DELETE_RESULT = 146;
public static final int USER_PING_RESPONSE = 147;
public static final int USER_PING_STARTED = 148;
public static final int USER_PING_STOPPED = 149;
///////////////////////////////////////////////////
@ -171,8 +172,8 @@ public class Message implements Serializable {
public static final int DEVICE_TYPE_DELETE = 533;
// C->S smazani uzovatele
public static final int USER_DELETE = 534;
public static final int USER_PING_START = 535;
public static final int USER_PING_STOP = 536;
///////////////////////////////////////////////////

View File

@ -0,0 +1,47 @@
package jnet.lib;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jnet.lib.object.PingResult;
public class PingResponseParser {
// Regex pro Windows i Linux odpovědi
private static final Pattern PATTERN = Pattern.compile(
"(?:Reply from |bytes from )?(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})" + // IP
"(?:[: ])?" +
".*?bytes=(\\d+)" + // bytes
".*?time[=<](\\d+)" + // time (může být = nebo <)
"ms" +
".*?TTL=(\\d+)", // TTL
Pattern.CASE_INSENSITIVE | Pattern.DOTALL
);
public static PingResult parse(String line) {
if (line == null || line.isEmpty()) {
return null;
}
// Zkusíme najít shodu
Matcher m = PATTERN.matcher(line);
if (m.find()) {
try {
String ip = m.group(1);
int bytes = Integer.parseInt(m.group(2));
long timeMs = Long.parseLong(m.group(3));
int ttl = Integer.parseInt(m.group(4));
return new PingResult(ip, bytes, timeMs, ttl, true);
} catch (NumberFormatException e) {
// čísla nešla parsovat – vrátíme neúspěch
return new PingResult(null, 0, 0, 0, false);
}
}
// Pokud neodpovídá úspěšné odpovědi, může to být "Request timed out." apod.
if (line.toLowerCase().contains("timed out") || line.toLowerCase().contains("unreachable")) {
return new PingResult(null, 0, 0, 0, false);
}
return null; // neznámý formát
}
}

View File

@ -0,0 +1,29 @@
package jnet.lib.object;
public class PingResult {
private final String ip;
private final int bytes;
private final long timeMs;
private final int ttl;
private final boolean success;
public PingResult(String ip, int bytes, long timeMs, int ttl, boolean success) {
this.ip = ip;
this.bytes = bytes;
this.timeMs = timeMs;
this.ttl = ttl;
this.success = success;
}
public String getIp() { return ip; }
public int getBytes() { return bytes; }
public long getTimeMs() { return timeMs; }
public int getTtl() { return ttl; }
public boolean isSuccess() { return success; }
@Override
public String toString() {
return String.format("IP: %s, bytes=%d, time=%dms, TTL=%d", ip, bytes, timeMs, ttl);
}
}