implementovan user ping
parent
a0d147ee90
commit
40f97ff189
|
|
@ -4,7 +4,6 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
import jnet.lib.Message;
|
||||
|
||||
|
||||
public class MessageRegister {
|
||||
|
||||
private final Map<Integer, Register> register = new HashMap<>();
|
||||
|
|
@ -44,12 +43,13 @@ public class MessageRegister {
|
|||
register.put(Message.SNMP_TEMPLATE_DELETE, new RegisterSnmpTemplateDelete());
|
||||
register.put(Message.DEVICE_TYPE_DELETE, new RegisterDeviceTypeDelete());
|
||||
register.put(Message.USER_DELETE, new RegisterUserDelete());
|
||||
|
||||
register.put(Message.USER_PING_START, new RegisterUserPingStart());
|
||||
register.put(Message.USER_PING_STOP, new RegisterUserPingStop());
|
||||
|
||||
}
|
||||
|
||||
public Register getRegister(int typ) {
|
||||
return register.get(typ);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
package jnet.server.message;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import jnet.lib.Message;
|
||||
import jnet.server.userping.PingManager;
|
||||
|
||||
public class RegisterUserPingStart implements Register {
|
||||
|
||||
@Override
|
||||
public void processing(ChannelHandlerContext ctx, Message msg) {
|
||||
Object[] data = (Object[]) msg.getMsg();
|
||||
if (data == null || data.length < 2) {
|
||||
ctx.writeAndFlush(new Message(Message.USER_PING_STARTED, "ERR: Missing IP or frameName"));
|
||||
return;
|
||||
}
|
||||
String ip = data[0].toString();
|
||||
String frameName = data[1].toString();
|
||||
|
||||
if (ip.trim().isEmpty() || frameName.trim().isEmpty()) {
|
||||
ctx.writeAndFlush(new Message(Message.USER_PING_STARTED, "ERR: Empty IP or frameName"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Sender pro odesílání odpovědí – každý řádek pošleme s frameName
|
||||
java.util.function.Consumer<String> sender = (line) -> {
|
||||
if (ctx.channel().isActive()) {
|
||||
ctx.writeAndFlush(new Message(Message.USER_PING_RESPONSE, new Object[]{frameName, line}));
|
||||
}
|
||||
};
|
||||
|
||||
String pingId = PingManager.startPing(ctx.channel(), ip.trim(), frameName, sender);
|
||||
ctx.writeAndFlush(new Message(Message.USER_PING_STARTED, "Ping started for " + ip + " frame " + frameName + " (ID: " + pingId + ")"));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package jnet.server.message;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import jnet.lib.Message;
|
||||
import jnet.server.userping.PingManager;
|
||||
|
||||
public class RegisterUserPingStop implements Register {
|
||||
|
||||
@Override
|
||||
public void processing(ChannelHandlerContext ctx, Message msg) {
|
||||
Object[] data = (Object[]) msg.getMsg();
|
||||
if (data == null || data.length < 2) {
|
||||
ctx.writeAndFlush(new Message(Message.USER_PING_STOPPED, "ERR: Missing IP or frameName"));
|
||||
return;
|
||||
}
|
||||
String ip = data[0].toString();
|
||||
String frameName = data[1].toString();
|
||||
String pingId = ip + ":" + frameName;
|
||||
|
||||
PingManager.stopPing(ctx.channel(), pingId);
|
||||
ctx.writeAndFlush(new Message(Message.USER_PING_STOPPED, "Ping stopped for " + pingId));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
package jnet.server.networkserver;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.channel.group.ChannelGroup;
|
||||
|
|
@ -8,10 +7,10 @@ import io.netty.channel.group.DefaultChannelGroup;
|
|||
import io.netty.util.concurrent.GlobalEventExecutor;
|
||||
import jnet.lib.LogFile;
|
||||
import jnet.lib.Message;
|
||||
import jnet.lib.object.OnlineClients;
|
||||
import jnet.server.Server;
|
||||
import jnet.server.message.MessageRegister;
|
||||
import jnet.server.message.Register;
|
||||
import jnet.server.userping.PingManager;
|
||||
|
||||
public class ServerHandler extends SimpleChannelInboundHandler<Object> {
|
||||
|
||||
|
|
@ -37,6 +36,9 @@ public class ServerHandler extends SimpleChannelInboundHandler<Object> {
|
|||
);
|
||||
// odesle zmenu stavajicim klientum
|
||||
sendAll(new Message(Message.ONLINE_CLIENTS, Server.onlineClients));
|
||||
// Zastavíme všechny pingy pro tohoto klienta
|
||||
PingManager.stopAllPings(ctx.channel());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
package jnet.server.userping;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import jnet.lib.LogFile;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class PingManager {
|
||||
|
||||
// Mapa: Channel -> (pingId -> UserPing)
|
||||
private static final ConcurrentHashMap<Channel, ConcurrentHashMap<String, UserPing>> pingMap = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Spustí nový ping pro daný kanál.
|
||||
* @param channel klient
|
||||
* @param ipAddress IP adresa
|
||||
* @param frameName identifikátor okna (použijeme jako součást pingId)
|
||||
* @param sender funkce pro odeslání odpovědi (zavolá se s řádkem výstupu)
|
||||
* @return pingId unikátní identifikátor spuštěného pingu
|
||||
*/
|
||||
public static String startPing(Channel channel, String ipAddress, String frameName, Consumer<String> sender) {
|
||||
String pingId = ipAddress + ":" + frameName;
|
||||
|
||||
// Získáme nebo vytvoříme vnitřní mapu pro tento kanál
|
||||
ConcurrentHashMap<String, UserPing> userPings = pingMap.computeIfAbsent(channel, k -> new ConcurrentHashMap<>());
|
||||
|
||||
// Pokud už ping s tímto ID existuje, nejprve ho zastavíme
|
||||
UserPing oldPing = userPings.remove(pingId);
|
||||
if (oldPing != null) {
|
||||
oldPing.stopPing();
|
||||
}
|
||||
|
||||
UserPing newPing = new UserPing(pingId, ipAddress, sender);
|
||||
userPings.put(pingId, newPing);
|
||||
Thread thread = new Thread(newPing, "Ping-" + pingId + "-" + channel.id());
|
||||
thread.start();
|
||||
|
||||
LogFile.printInfo("Ping started for " + channel.remoteAddress() + " IP: " + ipAddress + " frame: " + frameName);
|
||||
return pingId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zastaví konkrétní ping podle pingId.
|
||||
*/
|
||||
public static void stopPing(Channel channel, String pingId) {
|
||||
ConcurrentHashMap<String, UserPing> userPings = pingMap.get(channel);
|
||||
if (userPings != null) {
|
||||
UserPing ping = userPings.remove(pingId);
|
||||
if (ping != null) {
|
||||
ping.stopPing();
|
||||
LogFile.printInfo("Ping stopped for " + channel.remoteAddress() + " ID: " + pingId);
|
||||
}
|
||||
if (userPings.isEmpty()) {
|
||||
pingMap.remove(channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Zastaví všechny pingy pro daný kanál (při odpojení).
|
||||
*/
|
||||
public static void stopAllPings(Channel channel) {
|
||||
ConcurrentHashMap<String, UserPing> userPings = pingMap.remove(channel);
|
||||
if (userPings != null) {
|
||||
for (UserPing ping : userPings.values()) {
|
||||
ping.stopPing();
|
||||
}
|
||||
LogFile.printInfo("All pings stopped for " + channel.remoteAddress());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package jnet.server.userping;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class UserPing implements Runnable {
|
||||
|
||||
private final String pingId;
|
||||
private final String ipAddress;
|
||||
private final Consumer<String> messageSender;
|
||||
private Process pingProcess;
|
||||
private volatile boolean running = true;
|
||||
|
||||
public UserPing(String pingId, String ipAddress, Consumer<String> messageSender) {
|
||||
this.pingId = pingId;
|
||||
this.ipAddress = ipAddress;
|
||||
this.messageSender = messageSender;
|
||||
}
|
||||
|
||||
public String getPingId() {
|
||||
return pingId;
|
||||
}
|
||||
|
||||
public void stopPing() {
|
||||
this.running = false;
|
||||
if (pingProcess != null && pingProcess.isAlive()) {
|
||||
pingProcess.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
String os = System.getProperty("os.name").toLowerCase();
|
||||
ProcessBuilder pb;
|
||||
if (os.contains("win")) {
|
||||
pb = new ProcessBuilder("ping", "-t", ipAddress);
|
||||
} else {
|
||||
pb = new ProcessBuilder("ping", ipAddress);
|
||||
}
|
||||
pb.redirectErrorStream(true);
|
||||
|
||||
pingProcess = pb.start();
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(pingProcess.getInputStream(), "UTF-8"))) {
|
||||
|
||||
String line;
|
||||
while (running && (line = reader.readLine()) != null) {
|
||||
// Odesíláme pouze řádky obsahující odpověď (lze upravit)
|
||||
if (line.contains("time=") || line.contains("bytes from") || line.contains("Reply from")) {
|
||||
messageSender.accept(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
messageSender.accept("CHYBA_PINGU: " + e.getMessage());
|
||||
} finally {
|
||||
if (pingProcess != null && pingProcess.isAlive()) {
|
||||
pingProcess.destroy();
|
||||
}
|
||||
messageSender.accept("PING_STOPPED");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
INSERT INTO snmp_value_map (name) VALUES ('1 = ano, 2 = ne');
|
||||
INSERT INTO snmp_value_map_item (map_id, raw_value, mapped_value) VALUES ('1', '1', 'ano');
|
||||
INSERT INTO snmp_value_map_item (map_id, raw_value, mapped_value) VALUES ('1', '2', 'ne');
|
||||
Loading…
Reference in New Issue