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