mirror of
https://github.com/Melledy/LunarCore.git
synced 2025-12-13 05:44:36 +01:00
(feat.) Check if the MongoDB port is open before binding to it
This commit is contained in:
@@ -2,6 +2,7 @@ package emu.lunarcore.database;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import emu.lunarcore.util.Utils;
|
||||
import org.bson.codecs.configuration.CodecRegistries;
|
||||
import org.reflections.Reflections;
|
||||
|
||||
@@ -36,22 +37,23 @@ import lombok.Getter;
|
||||
public final class DatabaseManager {
|
||||
@Getter private static MongoServer server;
|
||||
private Datastore datastore;
|
||||
|
||||
|
||||
private final DeleteOptions DELETE_MANY = new DeleteOptions().multi(true);
|
||||
|
||||
public DatabaseManager(DatabaseInfo info, ServerType type) {
|
||||
// Variables
|
||||
var internalConfig = LunarCore.getConfig().getInternalMongoServer();
|
||||
String connectionString = info.getUri();
|
||||
|
||||
// Local mongo server
|
||||
if (info.isUseInternal()) {
|
||||
connectionString = startInternalMongoServer(LunarCore.getConfig().getInternalMongoServer());
|
||||
if (info.isUseInternal() && Utils.isPortOpen(internalConfig.getAddress(), internalConfig.getPort())) {
|
||||
connectionString = startInternalMongoServer(internalConfig);
|
||||
LunarCore.getLogger().info("Using local mongo server at " + server.getConnectionString());
|
||||
}
|
||||
|
||||
// Initialize
|
||||
MongoClient gameMongoClient = MongoClients.create(connectionString);
|
||||
|
||||
|
||||
// Add our custom fastutil codecs
|
||||
var codecProvider = CodecRegistries.fromCodecs(
|
||||
new IntSetCodec(), new Int2IntMapCodec()
|
||||
@@ -76,13 +78,13 @@ public final class DatabaseManager {
|
||||
return e != null && !e.value().equals(Mapper.IGNORED_FIELDNAME);
|
||||
})
|
||||
.toList();
|
||||
|
||||
|
||||
if (type.runDispatch()) {
|
||||
// Only map account related entities
|
||||
var map = entities.stream().filter(cls -> {
|
||||
return cls.getAnnotation(AccountDatabaseOnly.class) != null;
|
||||
}).toArray(Class<?>[]::new);
|
||||
|
||||
|
||||
datastore.getMapper().map(map);
|
||||
}
|
||||
if (type.runGame()) {
|
||||
@@ -90,7 +92,7 @@ public final class DatabaseManager {
|
||||
var map = entities.stream().filter(cls -> {
|
||||
return cls.getAnnotation(AccountDatabaseOnly.class) == null;
|
||||
}).toArray(Class<?>[]::new);
|
||||
|
||||
|
||||
datastore.getMapper().map(map);
|
||||
}
|
||||
|
||||
@@ -171,7 +173,7 @@ public final class DatabaseManager {
|
||||
getDatastore().save(counter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Internal MongoDB server
|
||||
|
||||
public static String startInternalMongoServer(InternalMongoInfo internalMongo) {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package emu.lunarcore.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
@@ -75,7 +79,7 @@ public class Utils {
|
||||
public static long getCurrentSeconds() {
|
||||
return Math.floorDiv(System.currentTimeMillis(), 1000);
|
||||
}
|
||||
|
||||
|
||||
public static int getMinPromotionForLevel(int level) {
|
||||
return Math.max(Math.min((int) ((level - 11) / 10D), 6), 0);
|
||||
}
|
||||
@@ -84,7 +88,7 @@ public class Utils {
|
||||
if (s == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int i = 0;
|
||||
|
||||
try {
|
||||
@@ -100,7 +104,7 @@ public class Utils {
|
||||
if (s == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
long i = 0;
|
||||
|
||||
try {
|
||||
@@ -127,7 +131,7 @@ public class Utils {
|
||||
public static <T> T randomElement(List<T> list) {
|
||||
return list.get(ThreadLocalRandom.current().nextInt(0, list.size()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if an integer array contains a value
|
||||
* @param array
|
||||
@@ -135,7 +139,7 @@ public class Utils {
|
||||
*/
|
||||
public static boolean arrayContains(int[] array, int value) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (array[i] == value) return true;
|
||||
if (array[i] == value) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -157,4 +161,21 @@ public class Utils {
|
||||
public static byte[] base64Decode(String toDecode) {
|
||||
return Base64.getDecoder().decode(toDecode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a port is open on a given host.
|
||||
*
|
||||
* @param host The host to check.
|
||||
* @param port The port to check.
|
||||
* @return True if the port is open, false otherwise.
|
||||
*/
|
||||
public static boolean isPortOpen(String host, int port) {
|
||||
try (var serverSocket = new ServerSocket()) {
|
||||
serverSocket.setReuseAddress(false);
|
||||
serverSocket.bind(new InetSocketAddress(InetAddress.getByName(host), port), 1);
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user