mirror of
https://github.com/diamante0018/ServerList.git
synced 2025-02-09 05:09:02 +00:00
feat: better dump handling logic
This commit is contained in:
parent
9a57cc938e
commit
1d24dd6d91
@ -21,6 +21,7 @@ import java.lang.management.ManagementFactory;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.net.SocketException;
|
||||
import java.net.SocketTimeoutException;
|
||||
@ -28,6 +29,11 @@ import java.nio.channels.IllegalBlockingModeException;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
/**
|
||||
* The purpose of this module is to ping the servers on the server list.
|
||||
*
|
||||
@ -77,6 +83,34 @@ public class ClientEmulator implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
public void pingServers(String filePath) {
|
||||
try {
|
||||
var parser = new JSONParser();
|
||||
var root = (JSONObject) parser.parse(new FileReader(filePath));
|
||||
|
||||
var serversToParse = (JSONArray) root.get("servers");
|
||||
|
||||
for (Object obj : serversToParse) {
|
||||
var server = (JSONObject) obj;
|
||||
|
||||
var ip = (String) server.get("IP");
|
||||
var port = (long) server.get("port"); // Port is stored as a number
|
||||
|
||||
System.out.println(ip + ":" + port);
|
||||
var to = Utils.stringToServer(ip + ":" + port);
|
||||
if (to != null) {
|
||||
handleServer(to);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.err.println("Error reading the file: " + e.getMessage());
|
||||
}
|
||||
catch (ParseException e) {
|
||||
System.err.println("Error parsing JSON: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void sendDatagramPacket(DatagramPacket packet) {
|
||||
try {
|
||||
socket.send(packet);
|
||||
|
@ -67,6 +67,7 @@ public class Main {
|
||||
var emulator = new Option("emulator", "client emulator mode");
|
||||
var masterPing = new Option("master_ping", "ping the master server");
|
||||
var serverPing = new Option("server_ping", "ping the master server as a server");
|
||||
var dumpReplyFromMaster = new Option("dump_reply", "dump info from all the servers listed on the master");
|
||||
|
||||
var ping = Option.builder("ping")
|
||||
.argName("IP:Port")
|
||||
@ -74,11 +75,19 @@ public class Main {
|
||||
.desc("Server to ping")
|
||||
.build();
|
||||
|
||||
var fileList = Option.builder("file_list")
|
||||
.argName("<filename>")
|
||||
.hasArg()
|
||||
.desc("Servers to ping")
|
||||
.build();
|
||||
|
||||
options.addOption(master);
|
||||
options.addOption(emulator);
|
||||
options.addOption(masterPing);
|
||||
options.addOption(serverPing);
|
||||
options.addOption(dumpReplyFromMaster);
|
||||
options.addOption(ping);
|
||||
options.addOption(fileList);
|
||||
|
||||
return options;
|
||||
}
|
||||
@ -95,6 +104,8 @@ public class Main {
|
||||
var main = new Main();
|
||||
var options = main.createOptions();
|
||||
var ip = new String();
|
||||
var fileList = new String();
|
||||
boolean dumpReply = false;
|
||||
|
||||
var parser = new DefaultParser();
|
||||
try {
|
||||
@ -109,12 +120,21 @@ public class Main {
|
||||
main.setMode(Mode.ServerPing);
|
||||
}
|
||||
|
||||
if (line.hasOption("dump_reply")) {
|
||||
dumpReply = true;
|
||||
}
|
||||
|
||||
if (line.hasOption("ping")) {
|
||||
ip = line.getOptionValue("ping");
|
||||
}
|
||||
|
||||
if (line.hasOption("file_list")) {
|
||||
fileList = line.getOptionValue("file_list");
|
||||
}
|
||||
}
|
||||
catch (ParseException exp) {
|
||||
System.err.println("Parsing failed. Reason: " + exp.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
if (main.getMode() == Mode.Master) {
|
||||
@ -125,15 +145,22 @@ public class Main {
|
||||
}
|
||||
main.getServer().stop();
|
||||
} else if (main.getMode() == Mode.Emulator) {
|
||||
var emulator = new ClientEmulator();
|
||||
emulator.pingSingleServer(ip);
|
||||
if (!fileList.isEmpty()) {
|
||||
var emulator = new ClientEmulator();
|
||||
emulator.pingServers(fileList);
|
||||
}
|
||||
|
||||
if (!ip.isEmpty()) {
|
||||
var emulator = new ClientEmulator();
|
||||
emulator.pingSingleServer(ip);
|
||||
}
|
||||
} else if (main.getMode() == Mode.MasterPing) {
|
||||
var ping = new MasterServerPinger();
|
||||
ping.pingMaster();
|
||||
ping.readReplyFromMaster();
|
||||
ping.readReplyFromMaster(dumpReply);
|
||||
} else if (main.getMode() == Mode.ServerPing) {
|
||||
var ping = new ServerEmulator();
|
||||
ping.pingLoop();
|
||||
var ping = new ServerEmulator();
|
||||
ping.pingLoop();
|
||||
}
|
||||
|
||||
System.out.println("Normal shutdown");
|
||||
|
@ -16,6 +16,10 @@
|
||||
*/
|
||||
package com.diamante.serverlist;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
@ -64,7 +68,7 @@ public class MasterServerPinger {
|
||||
}
|
||||
}
|
||||
|
||||
public void readReplyFromMaster() {
|
||||
public void readReplyFromMaster(Boolean dump) {
|
||||
var out = new ByteArrayOutputStream();
|
||||
|
||||
try {
|
||||
@ -108,6 +112,8 @@ public class MasterServerPinger {
|
||||
var root = new JSONObject();
|
||||
var serverArray = new JSONArray();
|
||||
|
||||
Set<Server> serverList = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
// Process server data
|
||||
for (int i = 4; i < bytes.length; i += 6) {
|
||||
if (i + 6 > bytes.length) {
|
||||
@ -127,6 +133,13 @@ public class MasterServerPinger {
|
||||
|
||||
System.out.println(String.format("Server: %s:%d", ipAddress, port));
|
||||
|
||||
if (!dump) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var server = Utils.stringToServer(ipAddress + ":" + port);
|
||||
serverList.add(server);
|
||||
|
||||
var serverObject = new JSONObject();
|
||||
serverObject.put("IP", ipAddress);
|
||||
serverObject.put("port", port);
|
||||
@ -134,10 +147,15 @@ public class MasterServerPinger {
|
||||
serverArray.add(serverObject);
|
||||
}
|
||||
|
||||
root.put("totalServers", serverCountBE);
|
||||
root.put("servers", serverArray);
|
||||
if (dump) {
|
||||
var thread = new Thread(new ClientEmulator(serverList));
|
||||
thread.start();
|
||||
|
||||
Utils.saveJSONFile(String.format("server_dump_%d.json", System.currentTimeMillis() / 1000L), root);
|
||||
root.put("totalServers", serverCountBE);
|
||||
root.put("servers", serverArray);
|
||||
|
||||
Utils.saveJSONFile(String.format("server_dump_%d.json", System.currentTimeMillis() / 1000L), root);
|
||||
}
|
||||
|
||||
try {
|
||||
out.close();
|
||||
|
Loading…
x
Reference in New Issue
Block a user