Skip to content

Commit

Permalink
Implement storage manager
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Jun 29, 2024
1 parent 8c6a19e commit 040e37b
Show file tree
Hide file tree
Showing 15 changed files with 330 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.rtm516.mcxboxbroadcast.core.configs.ExtensionConfig;
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionCreationException;
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionUpdateException;
import com.rtm516.mcxboxbroadcast.core.storage.FileStorageManager;
import org.geysermc.event.subscribe.Subscribe;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.command.Command;
Expand Down Expand Up @@ -56,6 +57,8 @@ public void onCommandDefine(GeyserDefineCommandsEvent event) {
return;
}

logger.info("Dumping session responses to 'lastSessionResponse.json' and 'currentSessionResponse.json'");

sessionManager.dumpSession();
})
.build());
Expand Down Expand Up @@ -99,7 +102,7 @@ public void onCommandDefine(GeyserDefineCommandsEvent event) {
private void restart() {
sessionManager.shutdown();

sessionManager = new SessionManager(this.dataFolder().toString(), logger);
sessionManager = new SessionManager(new FileStorageManager(this.dataFolder().toString()), logger);

// Pull onto another thread so we don't hang the main thread
sessionManager.scheduledThread().execute(this::createSession);
Expand All @@ -108,7 +111,7 @@ private void restart() {
@Subscribe
public void onPostInitialize(GeyserPostInitializeEvent event) {
logger = new ExtensionLoggerImpl(this.logger());
sessionManager = new SessionManager(this.dataFolder().toString(), logger);
sessionManager = new SessionManager(new FileStorageManager(this.dataFolder().toString()), logger);

// Load the config file
config = ConfigLoader.load(this, MCXboxBroadcastExtension.class, ExtensionConfig.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ public class BotManager {
private final ServerManager serverManager;
private final BackendManager backendManager;
private final BotCollection botCollection;
private final ServerCollection serverCollection;

@Autowired
public BotManager(ServerManager serverManager, BackendManager backendManager, BotCollection botCollection, ServerCollection serverCollection) {
public BotManager(ServerManager serverManager, BackendManager backendManager, BotCollection botCollection) {
this.serverManager = serverManager;
this.backendManager = backendManager;
this.botCollection = botCollection;
Expand All @@ -39,7 +38,6 @@ public BotManager(ServerManager serverManager, BackendManager backendManager, Bo
botContainer.start();
}
});
this.serverCollection = serverCollection;
}

public BotCollection botCollection() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void update(HttpServletResponse response, @PathVariable ObjectId botId, @
// Update the server ID then save the bot to the database
BotContainer botContainer = botManager.bots().get(botId);
botContainer.bot().serverId(botUpdateRequest.serverId());
botCollection.save(botContainer.bot());
botContainer.save();

// Update the session info in a new thread
backendManager.scheduledThreadPool().execute(botContainer::updateSessionInfo);
Expand Down Expand Up @@ -154,15 +154,9 @@ public String session(HttpServletResponse response, @PathVariable ObjectId botId

botContainer.dumpSession();

Path sessionJson = Paths.get(botContainer.cacheFolder(), "currentSessionResponse.json");
if (!Files.exists(sessionJson)) {
response.setStatus(404);
return "";
}

String data;
try {
data = Files.readString(sessionJson);
data = botContainer.storageManager().currentSessionResponse();
} catch (IOException e) {
response.setStatus(500);
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,22 @@ public CustomResponse importLegacy(HttpServletResponse response, @RequestParam("

// Create a bot for the main session
BotContainer mainBot = botManager.addBot();
mainBot.cache(cacheFiles.get("cache.json"));
try {
mainBot.storageManager().cache(cacheFiles.get("cache.json"));
} catch (IOException e) {
// Ignored
}
importedBots.add(mainBot);

// For each sub session check for the cache file and create a bot
for (String subSession : subSessions) {
if (cacheFiles.containsKey(subSession + "/cache.json")) {
BotContainer subBot = botManager.addBot();
subBot.cache(cacheFiles.get(subSession + "/cache.json"));
try {
subBot.storageManager().cache(cacheFiles.get(subSession + "/cache.json"));
} catch (IOException e) {
// Ignored
}
importedBots.add(subBot);
}
}
Expand Down Expand Up @@ -135,7 +143,11 @@ public CustomResponse importCredentials(HttpServletResponse response, @RequestBo

// Create a bot for each credential
BotContainer bot = botManager.addBot();
bot.cache(cacheData);
try {
bot.storageManager().cache(cacheData);
} catch (IOException e) {
// Ignored
}
importedBots.add(bot);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public class Bot {
private String gamertag;
private String xid;
private ObjectId serverId;
private String authCache;

public Bot(ObjectId serverId) {
this.gamertag = "";
this.xid = "";
this.serverId = serverId;
this.authCache = "";
}

public ObjectId _id() {
Expand Down Expand Up @@ -48,6 +50,14 @@ public void serverId(ObjectId serverId) {
this.serverId = serverId;
}

public String authCache() {
return authCache;
}

public void authCache(String authCache) {
this.authCache = authCache;
}

public BotInfoResponse toResponse(BotContainer.Status status) {
return new BotInfoResponse(_id, gamertag, xid, status, serverId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.rtm516.mcxboxbroadcast.core.configs.FriendSyncConfig;
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionCreationException;
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionUpdateException;
import com.rtm516.mcxboxbroadcast.core.storage.FileStorageManager;
import com.rtm516.mcxboxbroadcast.core.storage.StorageManager;
import com.rtm516.mcxboxbroadcast.manager.BotManager;
import com.rtm516.mcxboxbroadcast.manager.database.model.Bot;
import com.rtm516.mcxboxbroadcast.manager.models.response.BotInfoResponse;
Expand All @@ -23,6 +25,7 @@ public class BotContainer {
private final StringBuilder logs = new StringBuilder();
private final DateTimeFormatter logTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
private final BotManager botManager;
private final StorageManager storageManager;

private Logger logger;
private SessionManager sessionManager;
Expand All @@ -31,6 +34,7 @@ public class BotContainer {
public BotContainer(BotManager botManager, Bot bot) {
this.botManager = botManager;
this.bot = bot;
this.storageManager = new MongoStorageManager(this);

status = Status.OFFLINE;
}
Expand All @@ -51,6 +55,10 @@ public BotInfoResponse toResponse() {
return bot.toResponse(status);
}

public void save() {
botManager.botCollection().save(bot);
}

public void start() {
// If the bot is already online, don't start it again
if (status != Status.OFFLINE) {
Expand All @@ -59,7 +67,7 @@ public void start() {

status = Status.STARTING;
logger = new Logger(this); // TODO Move to file based?
sessionManager = new SessionManager(cacheFolder(), logger);
sessionManager = new SessionManager(storageManager, logger);

sessionManager.restartCallback(this::restart);
try {
Expand All @@ -68,7 +76,7 @@ public void start() {

bot.gamertag(sessionManager.getGamertag());
bot.xid(sessionManager.getXuid());
botManager.botCollection().save(bot);
save();

sessionManager.scheduledThread().scheduleWithFixedDelay(this::updateSessionInfo, botManager.backendManager().updateTime(), botManager.backendManager().updateTime(), TimeUnit.SECONDS);
} catch (SessionCreationException | SessionUpdateException e) {
Expand Down Expand Up @@ -116,24 +124,8 @@ public void dumpSession() {
}
}

public String cacheFolder() {
String cache = "./cache/" + bot._id();

// Create the cache directory if it doesn't exist
File directory = new File(cache);
if (!directory.exists()) {
directory.mkdirs();
}

return cache;
}

public void cache(String cache) {
try {
Files.writeString(Path.of(cacheFolder(), "cache.json"), cache);
} catch (IOException e) {
// Ignore
}
public StorageManager storageManager() {
return storageManager;
}

public static class Logger implements com.rtm516.mcxboxbroadcast.core.Logger {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.rtm516.mcxboxbroadcast.manager.models;

import com.rtm516.mcxboxbroadcast.core.storage.StorageManager;

import java.io.IOException;

public class MongoStorageManager implements StorageManager {
private final BotContainer botContainer;

private String currentSessionResponse;

public MongoStorageManager(BotContainer botContainer) {
this.botContainer = botContainer;
this.currentSessionResponse = "";
}

@Override
public String cache() throws IOException {
return botContainer.bot().authCache();
}

@Override
public void cache(String data) throws IOException {
botContainer.bot().authCache(data);
botContainer.save();
}

@Override
public String subSessions() throws IOException {
// Not needed for this implementation
return "";
}

@Override
public void subSessions(String data) throws IOException {
// Not needed for this implementation
}

@Override
public String lastSessionResponse() throws IOException {
// Not needed for this implementation
return "";
}

@Override
public void lastSessionResponse(String data) throws IOException {
// Not needed for this implementation
}

@Override
public String currentSessionResponse() throws IOException {
return currentSessionResponse;
}

@Override
public void currentSessionResponse(String data) throws IOException {
currentSessionResponse = data;
}

@Override
public StorageManager subSession(String id) {
// Not needed for this implementation
return null;
}

@Override
public void cleanup() throws IOException {
// Not needed for this implementation
}

@Override
public String liveToken() throws IOException {
// Not needed for this implementation
return "";
}

@Override
public void liveToken(String data) throws IOException {
// No longer for this implementation
}

@Override
public void xboxToken(String data) throws IOException {
// No longer for this implementation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ protected void runCommand(String command) {
switch (commandNode) {
case "exit" -> System.exit(0);
case "restart" -> StandaloneMain.restart();
case "dumpsession" -> StandaloneMain.sessionManager.dumpSession();
case "dumpsession" -> {
info("Dumping session responses to 'lastSessionResponse.json' and 'currentSessionResponse.json'");
StandaloneMain.sessionManager.dumpSession();
}
case "accounts" -> {
String[] args = command.split(" ");
if (args.length < 3) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.rtm516.mcxboxbroadcast.core.configs.StandaloneConfig;
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionCreationException;
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionUpdateException;
import com.rtm516.mcxboxbroadcast.core.storage.FileStorageManager;
import org.slf4j.LoggerFactory;

import java.io.File;
Expand Down Expand Up @@ -60,7 +61,7 @@ public static void main(String[] args) throws Exception {

logger.setDebug(config.debugLog());

sessionManager = new SessionManager("./cache", logger);
sessionManager = new SessionManager(new FileStorageManager("./cache"), logger);

sessionInfo = config.session().sessionInfo();

Expand All @@ -76,7 +77,7 @@ public static void restart() {
try {
sessionManager.shutdown();

sessionManager = new SessionManager("./cache", logger);
sessionManager = new SessionManager(new FileStorageManager("./cache"), logger);

createSession();
} catch (SessionCreationException | SessionUpdateException e) {
Expand Down
Loading

0 comments on commit 040e37b

Please sign in to comment.