Compare commits

..

8 Commits

Author SHA1 Message Date
Michal 0304db98a5 Merge pull request 'NettyPing' (#1) from NettyPing into main
Reviewed-on: http://cucky.cz:3000/Michal/jNetServer/pulls/1
2025-02-13 18:35:08 +01:00
Michal ca6161b505 drobne opravy 2025-02-13 18:29:11 +01:00
Michal 9bbbcb7e9f pridaní metody pro restart serveru 2025-02-13 18:28:55 +01:00
Michal 00bf11e673 2025-02-13 18:28:35 +01:00
Michal 839231901f uprava ping probe 2025-02-13 18:28:03 +01:00
Michal 8276df1596 uprava linux service 2025-02-13 18:27:10 +01:00
Michal e688eeae12 2025-02-10 20:44:57 +01:00
Michal e8cf68d71a 2025-02-10 20:44:08 +01:00
13 changed files with 283 additions and 412 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.log

View File

@ -6,8 +6,9 @@ mysql_password=Michal2407
mysql_database=jnet
server_port=1225
debug=true
ping_attempt=2
ping_timeout=2000
ping_attempt=4
ping_timeout=2
ping_repeat=3
instability_attempt=2
instability_limit=1000
instability_limit=1

18
jnetserver.service Normal file
View File

@ -0,0 +1,18 @@
[Unit]
Description=jNetServer
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/var/jNetServer/
ExecStart=/usr/bin/java -jar /var/jNetServer/jNetServer.jar
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=jNetServer-service
[Install]
WantedBy=multi-user.target

7
log
View File

@ -1,7 +0,0 @@
03.02.25 16:34:13 I ---------------------------------------------
03.02.25 16:34:13 I jNet Server
03.02.25 16:34:13 I Version: 1 (Mon Feb 03 16:33:44 CET 2025)
03.02.25 16:34:13 I ---------------------------------------------
03.02.25 16:34:13 I
03.02.25 16:34:13 I Loading config ...
03.02.25 16:34:13 E fail. Soubor konfigurace nenalezen config.properties (Systém nemůže nalézt uvedený soubor)

282
log.log
View File

@ -1,222 +1,60 @@
04.02.25 18:20:10 I ---------------------------------------------
04.02.25 18:20:10 I jNet Server
04.02.25 18:20:10 I Version: 1 (Tue Feb 04 17:54:52 CET 2025)
04.02.25 18:20:10 I ---------------------------------------------
04.02.25 18:20:10 I
04.02.25 18:20:10 I Loading config ...
04.02.25 18:20:10 I successfully
04.02.25 18:20:10 D Debug mode on
04.02.25 18:20:10 I Connecting to a selected database ...
04.02.25 18:20:10 I successfully
04.02.25 18:20:10 I Loading user list ...
04.02.25 18:20:10 I load 2 user(s)
04.02.25 18:20:10 I Loading map list ...
04.02.25 18:20:10 I load 6 map(s)
04.02.25 18:20:10 I Loading object list ...
04.02.25 18:20:10 I load 5 object(s)
04.02.25 18:20:10 I Loading object type ...
04.02.25 18:20:10 I load 12 type(s)
04.02.25 18:20:10 I Loading SNMP profile ...
04.02.25 18:20:10 I load 2 profile(s)
04.02.25 18:20:10 I Loading events ...
04.02.25 18:20:11 I load 259 event(s)
04.02.25 18:20:11 I Ping start ...
04.02.25 18:20:11 I Server is ready and listening on port 1225
04.02.25 18:20:13 D -> Message type: 1
04.02.25 18:20:13 I Authorization request send (kubernetes.docker.internal)
04.02.25 18:20:13 D <- Message type: 3
04.02.25 18:20:13 D -> Message type: 4
04.02.25 18:20:13 I User: michal logged in (kubernetes.docker.internal)
04.02.25 18:20:13 D <- Message type: 5
04.02.25 18:20:13 D <- Message type: 9
04.02.25 18:20:13 D <- For all. Message type: 7
04.02.25 18:20:13 D <- Message type: 13
04.02.25 18:20:13 D <- Message type: 8
04.02.25 18:20:13 D <- Message type: 8
04.02.25 18:20:13 D <- Message type: 8
04.02.25 18:20:13 D <- Message type: 8
04.02.25 18:20:13 D <- Message type: 8
04.02.25 18:20:13 D <- Message type: 8
04.02.25 18:20:13 D <- Message type: 8
04.02.25 18:20:13 D <- Message type: 8
04.02.25 18:20:13 D <- Message type: 8
04.02.25 18:20:13 D <- Message type: 8
04.02.25 18:20:13 D <- Message type: 8
04.02.25 18:20:13 D <- Message type: 8
04.02.25 18:20:13 D <- Message type: 11
04.02.25 18:20:13 D <- Message type: 11
04.02.25 18:20:13 D <- Message type: 100
04.02.25 18:20:13 D <- Message type: 100
04.02.25 18:20:13 D <- Message type: 100
04.02.25 18:20:13 D <- Message type: 100
04.02.25 18:20:13 D <- Message type: 100
04.02.25 18:20:13 D <- Message type: 100
04.02.25 18:20:13 D <- Message type: 14
04.02.25 18:20:13 D <- Message type: 110
04.02.25 18:20:13 D <- Message type: 107
04.02.25 18:20:13 D <- Message type: 107
04.02.25 18:28:05 D <- For all. Message type: 108
04.02.25 18:28:05 D <- For all. Message type: 101
04.02.25 18:28:05 D <- For all. Message type: 101
04.02.25 18:29:45 D <- For all. Message type: 108
04.02.25 18:29:45 D <- For all. Message type: 101
04.02.25 18:29:45 D <- For all. Message type: 101
04.02.25 18:34:14 D <- For all. Message type: 108
04.02.25 18:34:14 D <- For all. Message type: 101
04.02.25 18:34:14 D <- For all. Message type: 101
04.02.25 18:35:53 D <- For all. Message type: 108
04.02.25 18:35:53 D <- For all. Message type: 101
04.02.25 18:35:54 D <- For all. Message type: 101
04.02.25 18:38:18 D <- For all. Message type: 108
04.02.25 18:38:18 D <- For all. Message type: 101
04.02.25 18:38:18 D <- For all. Message type: 101
04.02.25 18:39:31 D <- For all. Message type: 108
04.02.25 18:39:31 D <- For all. Message type: 101
04.02.25 18:39:31 D <- For all. Message type: 101
04.02.25 18:40:31 D <- For all. Message type: 108
04.02.25 18:40:31 D <- For all. Message type: 101
04.02.25 18:40:31 D <- For all. Message type: 101
04.02.25 18:44:12 D <- For all. Message type: 108
04.02.25 18:44:12 D <- For all. Message type: 101
04.02.25 18:44:13 D <- For all. Message type: 101
04.02.25 18:44:43 D <- For all. Message type: 108
04.02.25 18:44:43 D <- For all. Message type: 101
04.02.25 18:44:44 D <- For all. Message type: 101
04.02.25 18:46:19 D <- For all. Message type: 108
04.02.25 18:46:19 D <- For all. Message type: 101
04.02.25 18:46:20 D <- For all. Message type: 101
04.02.25 18:48:13 D <- For all. Message type: 108
04.02.25 18:48:13 D <- For all. Message type: 101
04.02.25 18:48:14 D <- For all. Message type: 101
04.02.25 18:49:37 D <- For all. Message type: 108
04.02.25 18:49:37 D <- For all. Message type: 101
04.02.25 18:49:37 D <- For all. Message type: 101
04.02.25 18:54:18 D <- For all. Message type: 108
04.02.25 18:54:18 D <- For all. Message type: 101
04.02.25 18:54:18 D <- For all. Message type: 101
04.02.25 18:59:24 D <- For all. Message type: 108
04.02.25 18:59:24 D <- For all. Message type: 101
04.02.25 18:59:27 D <- For all. Message type: 101
04.02.25 18:59:56 D <- For all. Message type: 108
04.02.25 18:59:56 D <- For all. Message type: 101
04.02.25 18:59:56 D <- For all. Message type: 101
04.02.25 19:11:38 D <- For all. Message type: 108
04.02.25 19:11:38 D <- For all. Message type: 101
04.02.25 19:11:39 D <- For all. Message type: 101
04.02.25 19:14:33 D <- For all. Message type: 108
04.02.25 19:14:33 D <- For all. Message type: 101
04.02.25 19:14:33 D <- For all. Message type: 101
04.02.25 19:20:55 D <- For all. Message type: 108
04.02.25 19:20:55 D <- For all. Message type: 101
04.02.25 19:20:55 D <- For all. Message type: 101
04.02.25 19:24:30 D <- For all. Message type: 108
04.02.25 19:24:30 D <- For all. Message type: 101
04.02.25 19:24:30 D <- For all. Message type: 101
04.02.25 19:29:23 D <- For all. Message type: 108
04.02.25 19:29:23 D <- For all. Message type: 101
04.02.25 19:29:24 D <- For all. Message type: 101
04.02.25 19:34:17 D <- For all. Message type: 108
04.02.25 19:34:17 D <- For all. Message type: 101
04.02.25 19:34:18 D <- For all. Message type: 101
04.02.25 19:44:31 D <- For all. Message type: 108
04.02.25 19:44:31 D <- For all. Message type: 101
04.02.25 19:44:31 D <- For all. Message type: 101
04.02.25 19:48:14 D <- For all. Message type: 108
04.02.25 19:48:14 D <- For all. Message type: 101
04.02.25 19:48:14 D <- For all. Message type: 101
04.02.25 19:59:37 D <- For all. Message type: 108
04.02.25 19:59:37 D <- For all. Message type: 101
04.02.25 19:59:38 D <- For all. Message type: 101
04.02.25 20:0:02 D <- For all. Message type: 108
04.02.25 20:0:02 D <- For all. Message type: 101
04.02.25 20:0:03 D <- For all. Message type: 101
04.02.25 20:8:49 D <- For all. Message type: 108
04.02.25 20:8:49 D <- For all. Message type: 101
04.02.25 20:8:50 D <- For all. Message type: 101
04.02.25 20:9:35 D <- For all. Message type: 108
04.02.25 20:9:35 D <- For all. Message type: 101
04.02.25 20:9:35 D <- For all. Message type: 101
04.02.25 20:17:35 D <- For all. Message type: 108
04.02.25 20:17:35 D <- For all. Message type: 101
04.02.25 20:17:35 D <- For all. Message type: 101
04.02.25 20:19:33 D <- For all. Message type: 108
04.02.25 20:19:33 D <- For all. Message type: 101
04.02.25 20:19:33 D <- For all. Message type: 101
04.02.25 20:19:47 D <- For all. Message type: 108
04.02.25 20:19:47 D <- For all. Message type: 101
04.02.25 20:19:47 D <- For all. Message type: 101
04.02.25 20:23:18 D <- For all. Message type: 108
04.02.25 20:23:18 D <- For all. Message type: 101
04.02.25 20:23:19 D <- For all. Message type: 101
04.02.25 20:24:28 D <- For all. Message type: 108
04.02.25 20:24:28 D <- For all. Message type: 101
04.02.25 20:24:29 D <- For all. Message type: 101
04.02.25 20:24:31 D <- For all. Message type: 108
04.02.25 20:24:31 D <- For all. Message type: 101
04.02.25 20:24:31 D <- For all. Message type: 101
04.02.25 20:26:06 D <- For all. Message type: 108
04.02.25 20:26:06 D <- For all. Message type: 101
04.02.25 20:26:06 D <- For all. Message type: 101
04.02.25 20:34:17 D <- For all. Message type: 108
04.02.25 20:34:17 D <- For all. Message type: 101
04.02.25 20:34:18 D <- For all. Message type: 101
04.02.25 20:44:50 D <- For all. Message type: 108
04.02.25 20:44:50 D <- For all. Message type: 101
04.02.25 20:44:53 D <- For all. Message type: 108
04.02.25 20:44:53 D <- For all. Message type: 101
04.02.25 20:44:53 D <- For all. Message type: 101
04.02.25 20:44:53 D <- For all. Message type: 101
04.02.25 20:52:08 D <- For all. Message type: 108
04.02.25 20:52:08 D <- For all. Message type: 101
04.02.25 20:52:09 D <- For all. Message type: 101
04.02.25 20:54:29 D <- For all. Message type: 108
04.02.25 20:54:29 D <- For all. Message type: 101
04.02.25 20:54:30 D <- For all. Message type: 101
04.02.25 21:8:32 D <- For all. Message type: 108
04.02.25 21:8:32 D <- For all. Message type: 101
04.02.25 21:8:32 D <- For all. Message type: 101
04.02.25 21:13:54 D <- For all. Message type: 108
04.02.25 21:13:54 D <- For all. Message type: 101
04.02.25 21:13:55 D <- For all. Message type: 101
04.02.25 21:29:31 D <- For all. Message type: 108
04.02.25 21:29:31 D <- For all. Message type: 101
04.02.25 21:29:31 D <- For all. Message type: 101
04.02.25 21:30:11 D <- For all. Message type: 108
04.02.25 21:30:11 D <- For all. Message type: 101
04.02.25 21:30:12 D <- For all. Message type: 101
04.02.25 21:30:18 D <- For all. Message type: 108
04.02.25 21:30:18 D <- For all. Message type: 101
04.02.25 21:30:19 D <- For all. Message type: 101
04.02.25 21:39:31 D <- For all. Message type: 108
04.02.25 21:39:31 D <- For all. Message type: 101
04.02.25 21:39:31 D <- For all. Message type: 101
04.02.25 21:40:04 D <- For all. Message type: 108
04.02.25 21:40:04 D <- For all. Message type: 101
04.02.25 21:40:05 D <- For all. Message type: 101
04.02.25 21:41:08 D <- For all. Message type: 108
04.02.25 21:41:08 D <- For all. Message type: 101
04.02.25 21:41:08 D <- For all. Message type: 101
04.02.25 21:53:54 D <- For all. Message type: 108
04.02.25 21:53:54 D <- For all. Message type: 101
04.02.25 21:53:55 D <- For all. Message type: 101
04.02.25 21:54:05 D <- For all. Message type: 108
04.02.25 21:54:05 D <- For all. Message type: 101
04.02.25 21:54:05 D <- For all. Message type: 101
04.02.25 21:54:38 D <- For all. Message type: 108
04.02.25 21:54:38 D <- For all. Message type: 101
04.02.25 21:54:38 D <- For all. Message type: 101
04.02.25 22:4:46 D <- For all. Message type: 108
04.02.25 22:4:46 D <- For all. Message type: 101
04.02.25 22:4:47 D <- For all. Message type: 101
04.02.25 22:10:03 D <- For all. Message type: 108
04.02.25 22:10:03 D <- For all. Message type: 101
04.02.25 22:10:03 D <- For all. Message type: 101
04.02.25 22:39:33 D <- For all. Message type: 108
04.02.25 22:39:33 D <- For all. Message type: 101
04.02.25 22:39:34 D <- For all. Message type: 101
04.02.25 22:52:23 D <- For all. Message type: 108
04.02.25 22:52:23 D <- For all. Message type: 101
04.02.25 22:52:24 D <- For all. Message type: 101
04.02.25 22:54:16 D <- For all. Message type: 108
04.02.25 22:54:16 D <- For all. Message type: 101
04.02.25 22:54:16 D <- For all. Message type: 101
13.02.25 18:7:16 I ---------------------------------------------
13.02.25 18:7:16 I jNet Server
13.02.25 18:7:16 I Version: 1 (Thu Feb 13 09:04:35 CET 2025)
13.02.25 18:7:16 I ---------------------------------------------
13.02.25 18:7:16 I
13.02.25 18:7:16 I Loading config ...
13.02.25 18:7:16 I successfully
13.02.25 18:7:16 D Debug mode on
13.02.25 18:7:16 I Connecting to a selected database ...
13.02.25 18:7:16 I successfully
13.02.25 18:7:16 I Loading user list ...
13.02.25 18:7:16 I load 2 user(s)
13.02.25 18:7:16 I Loading map list ...
13.02.25 18:7:16 I load 6 map(s)
13.02.25 18:7:16 I Loading object list ...
13.02.25 18:7:17 I load 8 object(s)
13.02.25 18:7:17 I Loading object type ...
13.02.25 18:7:17 I load 12 type(s)
13.02.25 18:7:17 I Loading SNMP profile ...
13.02.25 18:7:17 I load 2 profile(s)
13.02.25 18:7:17 I Loading events ...
13.02.25 18:7:17 I load 30 event(s)
13.02.25 18:7:17 I Ping probe start
13.02.25 18:7:17 I Server is ready and listening on port 1225
13.02.25 18:7:49 D -> Message type: 1
13.02.25 18:7:49 I Authorization request send (kubernetes.docker.internal)
13.02.25 18:7:49 D <- Message type: 3
13.02.25 18:7:49 D -> Message type: 4
13.02.25 18:7:49 I User: michal logged in (kubernetes.docker.internal)
13.02.25 18:7:49 D <- Message type: 5
13.02.25 18:7:49 D <- Message type: 9
13.02.25 18:7:49 D <- For all. Message type: 7
13.02.25 18:7:49 D <- Message type: 13
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 8
13.02.25 18:7:49 D <- Message type: 11
13.02.25 18:7:49 D <- Message type: 11
13.02.25 18:7:49 D <- Message type: 100
13.02.25 18:7:49 D <- Message type: 100
13.02.25 18:7:49 D <- Message type: 100
13.02.25 18:7:49 D <- Message type: 100
13.02.25 18:7:49 D <- Message type: 100
13.02.25 18:7:49 D <- Message type: 100
13.02.25 18:7:49 D <- Message type: 14
13.02.25 18:7:49 D <- Message type: 110
13.02.25 18:7:49 D <- Message type: 107
13.02.25 18:7:49 D <- Message type: 107
13.02.25 18:8:31 D -> Message type: 105
13.02.25 18:8:31 D <- For all. Message type: 105
13.02.25 18:8:31 D <- For all. Message type: 108

View File

@ -35,6 +35,7 @@ dist.jlink.dir=${dist.dir}/jlink
dist.jlink.output=${dist.jlink.dir}/jNetServer
endorsed.classpath=
excludes=
file.reference.apache-commons-configuration2-2.9.0.jar=C:\\Users\\cucky\\Documents\\NetBeansProjects\\jar_files\\apache-commons-configuration2-2.9.0.jar
file.reference.apache-commons-lang3-3.12.0.jar=C:\\Users\\cucky\\Documents\\NetBeansProjects\\jar_files\\apache-commons-lang3-3.12.0.jar
file.reference.apache-commons-logging-1.2.jar=C:\\Users\\cucky\\Documents\\NetBeansProjects\\jar_files\\apache-commons-logging-1.2.jar
file.reference.apache-commons-text-1.10.0.jar=C:\\Users\\cucky\\Documents\\NetBeansProjects\\jar_files\\apache-commons-text-1.10.0.jar
@ -56,7 +57,8 @@ javac.classpath=\
${file.reference.apache-commons-text-1.10.0.jar}:\
${file.reference.jna-5.3.1.jar}:\
${file.reference.jna-platform-5.9.0.jar}:\
${file.reference.spring-security-crypto-5.8.0.jar}
${file.reference.spring-security-crypto-5.8.0.jar}:\
${file.reference.apache-commons-configuration2-2.9.0.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false

View File

@ -635,7 +635,6 @@ public class Database {
try {
String select_query = "SELECT * FROM events WHERE object = '" + objectId + "' && type = '" + type + "' ORDER BY id DESC LIMIT 1";
System.err.println(select_query);
PreparedStatement stmt = getCurrentConnection().prepareStatement(select_query);
ResultSet rs = stmt.executeQuery();
// Process the result

View File

@ -1,16 +1,13 @@
package jnet.server;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jnet.lib.BuilddDate;
import jnet.lib.LogFile;
import jnet.lib.LogWindow;
import jnet.lib.OSValidator;
import jnet.lib.PasswordHashing;
import jnet.lib.PropertiesManager;
import jnet.lib.object.Event;
import jnet.lib.object.Map;
@ -21,7 +18,7 @@ import jnet.lib.object.SnmpProbe;
import jnet.lib.object.SnmpProfile;
import jnet.lib.object.User;
import jnet.server.network.NettyServer;
import jnet.server.probe.Ping2;
import jnet.server.probe.NettyPing;
public class Server {
@ -50,7 +47,6 @@ public class Server {
}
LogWindow.addRow("tset");
LogFile.clear();
LogFile.setDebug(false);
@ -141,9 +137,37 @@ public class Server {
// Ping pp = new Ping();
// pp.start();
Ping2 p2 = new Ping2();
p2.start();
// Ping2 p2 = new Ping2();
// p2.start();
NettyPing np = new NettyPing();
np.start();
}
public static void restartServer() {
new Thread(() -> {
try {
Thread.sleep(5000);
try {
String java = System.getProperty("java.home") + "/bin/java";
File jarFile = new File(Server.class
.getProtectionDomain().getCodeSource().getLocation().getPath());
List<String> command = new ArrayList<>();
command.add(java);
command.add("-jar");
command.add(jarFile.getAbsolutePath());
new ProcessBuilder(command).start();
System.exit(0);
} catch (IOException ex) {
LogFile.printErr("Restart failed! " + ex.getMessage());
}
} catch (InterruptedException e) {
LogFile.printErr("Restart failed! " + e.getMessage());
}
}).start();
}
}

View File

@ -4,6 +4,7 @@ import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import java.lang.management.ManagementFactory;
import java.net.InetSocketAddress;
import java.util.ListIterator;
import jnet.lib.LogFile;
@ -79,6 +80,7 @@ public class ServerMessageParser {
sendAll(new Message(Message.ONLINE_CLIENTS, Server.onlineClients));
// odeslat nastaveni serveru
ServerConfig sc = new ServerConfig(
Server.config.getInt("ping_repeat"),
Server.config.getInt("ping_attempt"),
Server.config.getInt("ping_timeout"),
Server.config.getInt("instability_attempt"),
@ -110,6 +112,16 @@ public class ServerMessageParser {
}
break;
case Message.RESTART:
LogFile.printInfo("Prijat pozadavek na restart serveru");
Server.restartServer();
break;
case Message.UPTIME:
long uptime = ManagementFactory.getRuntimeMXBean().getUptime();
send(Message.UPTIME, uptime);
break;
case Message.NEW_OBJECT_TYPE:
ObjectType not = (ObjectType) msg.getMsg();
int id = Database.addObjectType(not);
@ -136,7 +148,7 @@ public class ServerMessageParser {
case Message.ADD_OBJECT:
MapObject mo = Database.addMapObject((MapObject) msg.getMsg());
// pridat do seznamu
iteratorMapObject.add(mo);
Server.mapObject.add(mo);
// odesle všem novy objekt
sendAll(new Message(Message.ADD_OBJECT, mo));
// prida event a odesle ho
@ -273,6 +285,7 @@ public class ServerMessageParser {
case Message.SERVER_CONFIG:
ServerConfig sc = (ServerConfig) msg.getMsg();
// ulozeni
Server.config.setInt("ping_repeat", sc.getPingRepeat());
Server.config.setInt("ping_attempt", sc.getPingAttempt());
Server.config.setInt("ping_timeout", sc.getPingTimeout());
Server.config.setInt("instability_attempt", sc.getInstabilityAttempt());

View File

@ -0,0 +1,85 @@
package jnet.server.probe;
import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;
import jnet.lib.LogFile;
import jnet.lib.Status;
import jnet.lib.object.MapObject;
import jnet.server.Server;
public class NettyPing extends Thread {
private static final HashedWheelTimer TIMER = new HashedWheelTimer(); // Netty časovač
public NettyPing() {
}
@Override
public void run() {
LogFile.printInfo("Ping probe start");
for (MapObject mapObject : Server.mapObject) {
schedulePing(mapObject);
}
}
private static void schedulePing(MapObject object) {
TIMER.newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) {
pingHost(object);
timeout.timer().newTimeout(this, Server.config.getInt("ping_repeat"), TimeUnit.SECONDS); // Opakuje každých x sekund
}
}, 0, TimeUnit.SECONDS);
}
private static void pingHost(MapObject object) {
try {
String command = System.getProperty("os.name").startsWith("Windows")
? "ping -n 1 -w " + String.valueOf((Server.config.getInt("ping_timeout")*1000)) + " " + object.getIp() // Timeout 2000 ms (Windows)
: "ping -c 1 -W " + String.valueOf(Server.config.getInt("ping_timeout")) + " " + object.getIp(); // Timeout 2 sec (Linux)
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
boolean success = false;
int timeMs = -1;
while ((line = reader.readLine()) != null) {
if (line.contains("TTL=") || line.contains("bytes from")) {
success = true;
timeMs = extractTime(line);
}
}
//System.out.println("Ping na " + object.getIp() + (success ? " uspesny! RTT: " + timeMs + " ms" : " selhal!"));
if (success) {
if (object.getStatus() != Status.OK) {
PingResult.stateOnline(object);
}
} else {
PingResult.stateOffline(object);
}
process.waitFor();
} catch (Exception e) {
PingResult.stateOffline(object);
LogFile.printErr("Chyba pri pingu na " + object.getIp() + " " + e.getMessage());
}
}
private static int extractTime(String line) {
String[] parts = line.split(" ");
for (String part : parts) {
if (part.contains("time=") || part.contains("čas=")) {
return Integer.parseInt(part.replaceAll("[^0-9]", ""));
}
}
return -1;
}
}

View File

@ -1,78 +0,0 @@
package jnet.server.probe;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import jnet.lib.LogFile;
import jnet.lib.Status;
import jnet.lib.object.MapObject;
import jnet.server.Server;
import org.icmp4j.IcmpPingRequest;
import org.icmp4j.IcmpPingResponse;
import org.icmp4j.IcmpPingUtil;
public class Ping2 extends Thread {
// Maximální počet paralelně běžících pingů
private static final int MAX_COUNT = 1000;
// Čekání před dalším opakováním pingu (ms)
private static final int WAITING = 1000;
private static final ExecutorService executor = Executors.newFixedThreadPool(MAX_COUNT);
private static final List<String> hosts = new CopyOnWriteArrayList<>(); // Dynamický seznam hostů
public Ping2() {
}
@Override
public void start() {
LogFile.printInfo("Ping start ...");
startPingLoop();
}
// Metoda pro spuštění opakovaného pingování
private static void startPingLoop() {
while (true) {
Iterator<MapObject> itMapObject = Server.mapObject.iterator();
while (itMapObject.hasNext()) {
MapObject obj = itMapObject.next();
if (obj.isActive()) {
executor.submit(() -> pingHost(obj));
}
}
try {
Thread.sleep(WAITING);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
// Metoda pro provedení jednoho pingu na host
private static void pingHost(MapObject obj) {
try {
IcmpPingRequest request = IcmpPingUtil.createIcmpPingRequest();
request.setHost(obj.getIp());
// Nastavení timeoutu na 3000 ms (3 sekundy)
request.setTimeout(3000);
// Odeslání pingu a zpracování odpovědi
IcmpPingResponse response = IcmpPingUtil.executePingRequest(request);
if (response.getSuccessFlag()) {
//System.out.println("Ping na " + obj.getIp() + " úspěšný. Latence: " + response.getDuration() + " ms");
new PingTestResult(obj).test(Status.OK, response.getDuration());
} else {
System.out.println("Ping na " + obj.getName() + " (obj id: " + obj.getId() + ") selhal. Status: " + response.getErrorMessage());
new PingTestResult(obj).test(Status.OFFLINE, response.getDuration());
}
} catch (Exception e) {
LogFile.printErr("Ping error: " + obj.getIp() + ": " + e.getMessage());
}
}
}

View File

@ -0,0 +1,66 @@
package jnet.server.probe;
import jnet.lib.Message;
import jnet.lib.Status;
import jnet.lib.object.Event;
import jnet.lib.object.MapObject;
import jnet.server.Database;
import jnet.server.Server;
import jnet.server.ServerMessageParser;
public class PingResult {
private static final int POKUS = Server.config.getInt("ping_attempt");
public static void stateOnline(MapObject obj) {
// oveří jestli neni zarizeni vypnuto
if (obj.isActive() && obj.getStatus() != Status.OK) {
// vynuluje ping pokusy
obj.setPingAttemp(0);
// pokud byl předchozí stav offline ukončí event
if (obj.getStatus() == Status.OFFLINE) {
// aktualizace eventu
Event event = Database.endEvent(obj.getId(), Event.TYPE_PING);
for (int i = 0; i < Server.events.size(); i++) {
if (Server.events.get(i).getId() == event.getId()) {
Server.events.set(i, event);
}
}
// odeslani aktualizovaného eventu
ServerMessageParser.sendAll(Message.EVENT_UPDATE, event);
}
// nastavi status objektu
obj.setStatus(Status.OK);
// odesle informaci o novem stavu klientum
sendState(obj.getId(), Status.OK);
}
}
public static void stateOffline(MapObject obj) {
// oveří jestli neni zarizeni vypnuto
if (obj.isActive()) {
if (obj.getPingAttemp() >= POKUS && obj.getStatus() != Status.OFFLINE) {
System.err.println("objekt: " + obj.getName() + " pokus: " + obj.getPingAttemp());
// nastavi status objektu
obj.setStatus(Status.OFFLINE);
// odesle informaci o novem stavu klientum
sendState(obj.getId(), Status.OFFLINE);
// pridáni eventu
Event event = Database.addEvent(obj.getId(), "Offline", Event.TYPE_PING);
Server.events.add(event);
// Odeslani eventu klientum
ServerMessageParser.sendAll(new Message(Message.EVENT_NEW, event));
} else {
obj.pingAttemp++;
}
}
}
private static void sendState(int object, int state) {
int[] o = {object, state};
ServerMessageParser.sendAll(new Message(Message.STATUS_UPDATE, o));
}
}

View File

@ -1,91 +0,0 @@
package jnet.server.probe;
import java.util.ListIterator;
import jnet.lib.Message;
import jnet.lib.Status;
import jnet.lib.object.Event;
import jnet.lib.object.MapObject;
import jnet.server.Database;
import jnet.server.Server;
import jnet.server.ServerMessageParser;
public class PingTestResult {
private static MapObject obj;
private static ListIterator<Event> it_event = Server.events.listIterator();
public PingTestResult(MapObject obj) {
this.obj = obj;
}
public static void test(int state, long latency) {
// oveří jestli neni zarizeni vypnuto
if (obj.isActive()) {
// oveří zda je stav rozdilny od predchozího
if (obj.getStatus() != state) {
switch (state) {
case Status.OK:
obj.setPingAttemp(0);
if (obj.getStatus() == Status.OFFLINE) {
// pokud je predchozi stav offline ukonci event
// Event event = Database.endEvent(obj.getId(), Event.TYPE_PING);
// if (event != null) {
// // odesle event klientum
// ServerMessageParser.sendAll(new Message(Message.EVENT_UPDATE, event));
// // aktualizuje v seznamu
// while (it_event.hasNext()) {
// Event e = it_event.next();
// if (e.getId() == event.getId()) {
// it_event.set(e);
// break;
// }
// }
// }
}
// zmena stavu v seznamu
System.out.println("online obj id: " + obj.getId());
changeStateOnObject(state);
// odesle informaci o novem stavu klientum
int[] o = {obj.getId(), state};
ServerMessageParser.sendAll(new Message(Message.STATUS_UPDATE, o));
break;
case Status.OFFLINE:
if (obj.getPingAttemp() >= Server.config.getInt("ping_attempt")) {
// pridáni eventu
Event event = Database.addEvent(obj.getId(), "Offline", Event.TYPE_PING);
it_event.add(event);
// Odeslani eventu klientum
ServerMessageParser.sendAll(new Message(Message.EVENT_NEW, event));
// zmena stavu v seznamu
System.err.println("offline obj id: " + obj.getId());
changeStateOnObject(state);
// odesle informaci o novem stavu klientum
int[] oo = {obj.getId(), state};
ServerMessageParser.sendAll(new Message(Message.STATUS_UPDATE, oo));
} else {
obj.pingAttemp++;
System.out.println("offline obj id: " + obj.getId() + " pokus: " + obj.pingAttemp);
}
break;
default:
System.err.println("default -> " + state);
throw new AssertionError();
}
}
}
}
private static synchronized void changeStateOnObject(int state) {
System.out.println("Change status " + obj.getId() + " -> " + state);
// upravi stav v listu
obj.setStatus(state);
}
}