Skip to content

Commit

Permalink
Only have one bedrock client instance for server pings
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Jun 26, 2024
1 parent 829cd75 commit 93de44b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.rtm516.mcxboxbroadcast.manager.database.repository.ServerCollection;
import com.rtm516.mcxboxbroadcast.manager.database.repository.UserCollection;
import org.java_websocket.util.NamedThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.crypto.password.PasswordEncoder;
Expand All @@ -20,6 +22,8 @@ public class BackendManager {
private final UserCollection userCollection;
private final ServerCollection serverCollection;

public static final Logger LOGGER = LoggerFactory.getLogger("Backend");

@Autowired
public BackendManager(UserCollection userCollection, PasswordEncoder passwordEncoder, ServerCollection serverCollection) {
this.userCollection = userCollection;
Expand All @@ -28,7 +32,6 @@ public BackendManager(UserCollection userCollection, PasswordEncoder passwordEnc
// TODO Allow configuration of thread pool size
this.scheduledThreadPool = Executors.newScheduledThreadPool(Math.max(1, Runtime.getRuntime().availableProcessors() * 3 / 8), new NamedThreadFactory("MCXboxBroadcast Manager Thread"));


// Create the admin user if it doesn't exist
if (authEnabled() && userCollection.findUserByUsername("admin").isEmpty()) {
userCollection.save(new User("admin", passwordEncoder.encode("password")));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rtm516.mcxboxbroadcast.manager;

import com.nukkitx.protocol.bedrock.BedrockClient;
import com.rtm516.mcxboxbroadcast.manager.database.model.Server;
import com.rtm516.mcxboxbroadcast.manager.database.repository.ServerCollection;
import com.rtm516.mcxboxbroadcast.manager.models.ServerContainer;
Expand All @@ -8,6 +9,7 @@
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand All @@ -19,14 +21,16 @@ public class ServerManager {
private final BackendManager backendManager;
private final ServerCollection serverCollection;

private BedrockClient client;

@Autowired
public ServerManager(BackendManager backendManager, ServerCollection serverCollection) {
this.backendManager = backendManager;
this.serverCollection = serverCollection;

// Load all servers from the database
serverCollection.findAll().forEach(server -> {
servers.put(server._id(), new ServerContainer(server));
servers.put(server._id(), new ServerContainer(this, server));
});

// Start the bots in a new thread so the web server can start
Expand All @@ -44,7 +48,7 @@ public Map<ObjectId, ServerContainer> servers() {
public ServerContainer addServer() {
Server server = serverCollection.save(new Server("mc.example.com", 19132));

ServerContainer serverContainer = new ServerContainer(server);
ServerContainer serverContainer = new ServerContainer(this, server);
servers.put(server._id(), serverContainer);
return serverContainer;
}
Expand All @@ -60,4 +64,23 @@ public ObjectId firstServer() {

return addServer().server()._id();
}

public BedrockClient bedrockClient() {
if (client != null && !client.getRakNet().isClosed()) {
return client;
}

try {
InetSocketAddress bindAddress = new InetSocketAddress("0.0.0.0", 0);
client = new BedrockClient(bindAddress);

client.bind().join();

return client;
} catch (Exception e) {
BackendManager.LOGGER.error("Error creating bedrock client for ping", e);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void restart() {
start();
}

public class Logger implements com.rtm516.mcxboxbroadcast.core.Logger {
public static class Logger implements com.rtm516.mcxboxbroadcast.core.Logger {
private final BotContainer botContainer;
private final String prefixString;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.rtm516.mcxboxbroadcast.manager.models;

import com.nukkitx.protocol.bedrock.BedrockClient;
import com.nukkitx.protocol.bedrock.BedrockPong;
import com.rtm516.mcxboxbroadcast.core.SessionInfo;
import com.rtm516.mcxboxbroadcast.manager.ServerManager;
import com.rtm516.mcxboxbroadcast.manager.database.model.Server;
import com.rtm516.mcxboxbroadcast.manager.models.response.ServerInfoResponse;

Expand All @@ -14,11 +14,14 @@
public class ServerContainer {
private final Server server;
private final SessionInfo sessionInfo;
private final ServerManager serverManager;

private Date lastUpdated;

public ServerContainer(Server server) {
public ServerContainer(ServerManager serverManager, Server server) {
this.server = server;
this.sessionInfo = new SessionInfo("", "", "", 0, 0, 0, server.hostname(), server.port());
this.serverManager = serverManager;
}

public Server server() {
Expand All @@ -38,15 +41,9 @@ public ServerInfoResponse toResponse() {
}

public void updateSessionInfo() {
BedrockClient client = null;
try {
InetSocketAddress bindAddress = new InetSocketAddress("0.0.0.0", 0);
client = new BedrockClient(bindAddress);

client.bind().join();

InetSocketAddress addressToPing = new InetSocketAddress(server.hostname(), server.port());
BedrockPong pong = client.ping(addressToPing, 1500, TimeUnit.MILLISECONDS).get();
BedrockPong pong = serverManager.bedrockClient().ping(addressToPing, 1500, TimeUnit.MILLISECONDS).get();

// Update the session information
sessionInfo.setHostName(pong.getMotd());
Expand All @@ -65,10 +62,6 @@ public void updateSessionInfo() {
sessionInfo.setPlayers(0);
sessionInfo.setMaxPlayers(0);
} finally {
if (client != null) {
client.close();
}

// Update the last updated time
lastUpdated = new Date();
}
Expand Down

0 comments on commit 93de44b

Please sign in to comment.