diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/CachedAuthHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/CachedAuthHandler.java index fa7c4805..1784b3d0 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/CachedAuthHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/CachedAuthHandler.java @@ -104,8 +104,25 @@ public synchronized boolean joinServer(String username, String accessToken, Stri public synchronized void garbageCollection() { entryCache.clear(); + usernamesCache.clear(); } + public Map getEntryCache() { + return entryCache; + } + + public Map getUsernamesCache() { + return usernamesCache; + } + + public void loadEntryCache(Map map) + { + entryCache.putAll(map); + } + public void loadUsernameCache(Map map) + { + usernamesCache.putAll(map); + } protected abstract boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/dump/DumpEntryCacheCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/dump/DumpEntryCacheCommand.java new file mode 100644 index 00000000..9ff5d1ff --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/dump/DumpEntryCacheCommand.java @@ -0,0 +1,76 @@ +package ru.gravit.launchserver.command.dump; + +import com.google.gson.reflect.TypeToken; +import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.auth.handler.CachedAuthHandler; +import ru.gravit.launchserver.command.Command; +import ru.gravit.launchserver.socket.Client; +import ru.gravit.utils.helper.IOHelper; +import ru.gravit.utils.helper.LogHelper; + +import java.io.Reader; +import java.io.Writer; +import java.lang.reflect.Type; +import java.nio.file.Paths; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +public class DumpEntryCacheCommand extends Command { + public DumpEntryCacheCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[load/unload] [filename]"; + } + + @Override + public String getUsageDescription() { + return "Load or unload AuthHandler Entry cache"; + } + + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args,2); + if(!(server.config.authHandler instanceof CachedAuthHandler)) throw new UnsupportedOperationException("This command used only CachedAuthHandler"); + CachedAuthHandler authHandler = (CachedAuthHandler) server.config.authHandler; + if(args[0].equals("unload")) + { + LogHelper.info("CachedAuthHandler write to %s",args[1]); + Map entryCache = authHandler.getEntryCache(); + Map usernamesCache = authHandler.getUsernamesCache(); + EntryAndUsername serializable = new EntryAndUsername(); + serializable.entryCache = entryCache; + serializable.usernameCache = usernamesCache; + try(Writer writer = IOHelper.newWriter(Paths.get(args[1]))) + { + LaunchServer.gson.toJson(serializable,writer); + } + LogHelper.subInfo("Write %d entryCache, %d usernameCache",entryCache.size(),usernamesCache.size()); + } else if(args[0].equals("load")) + { + LogHelper.info("CachedAuthHandler read from %s",args[1]); + int size_entry = 0; + int size_username = 0; + try(Reader reader = IOHelper.newReader(Paths.get(args[1]))) + { + EntryAndUsername entryAndUsername = LaunchServer.gson.fromJson(reader,EntryAndUsername.class); + size_entry = entryAndUsername.entryCache.size(); + size_username = entryAndUsername.usernameCache.size(); + authHandler.loadEntryCache(entryAndUsername.entryCache); + authHandler.loadUsernameCache(entryAndUsername.usernameCache); + + } + LogHelper.subInfo("Readed %d entryCache %d usernameCache",size_entry,size_username); + } + } + + public class EntryAndUsername + { + public Map entryCache; + public Map usernameCache; + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java index 8bc7b94d..e1b87094 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java @@ -5,6 +5,7 @@ import ru.gravit.launchserver.command.CommandException; import ru.gravit.launchserver.command.auth.*; import ru.gravit.launchserver.command.basic.*; +import ru.gravit.launchserver.command.dump.DumpEntryCacheCommand; import ru.gravit.launchserver.command.dump.DumpSessionsCommand; import ru.gravit.launchserver.command.hash.*; import ru.gravit.launchserver.command.modules.LoadModuleCommand; @@ -106,6 +107,7 @@ protected CommandHandler(LaunchServer server) { //Register dump commands registerCommand("dumpSessions", new DumpSessionsCommand(server)); + registerCommand("dumpEntryCache", new DumpEntryCacheCommand(server)); }