Added additional commands

This commit is contained in:
Nan1t 2022-07-01 16:35:47 -05:00
parent 1f2558602c
commit e4f90b4de7
8 changed files with 162 additions and 9 deletions

View File

@ -60,7 +60,7 @@ public class PacketEncoder extends MessageToByteEncoder<Packet> {
try {
packet.encode(msg, version);
if (Logger.getLevel() >= 3) {
if (Logger.getLevel() >= Logger.Level.DEBUG.getIndex()) {
Logger.debug("Sending %s[0x%s] packet (%d bytes)", packet.toString(), Integer.toHexString(packetId), msg.readableBytes());
}
} catch (Exception e) {

View File

@ -0,0 +1,9 @@
package ru.nanit.limbo.server;
public interface Command {
void execute();
String description();
}

View File

@ -1,29 +1,59 @@
package ru.nanit.limbo.server;
import java.util.NoSuchElementException;
import java.util.Scanner;
import ru.nanit.limbo.server.commands.CmdConn;
import ru.nanit.limbo.server.commands.CmdHelp;
import ru.nanit.limbo.server.commands.CmdMem;
import ru.nanit.limbo.server.commands.CmdStop;
import java.util.*;
public final class CommandManager extends Thread {
private final Map<String, Command> commands = new HashMap<>();
public Map<String, Command> getCommands() {
return Collections.unmodifiableMap(commands);
}
public Command getCommand(String name) {
return commands.get(name.toLowerCase());
}
public void register(String name, Command cmd) {
commands.put(name.toLowerCase(), cmd);
}
@Override
public void run() {
Scanner scanner = new Scanner(System.in);
String line;
String command;
while (true) {
try {
line = scanner.nextLine();
command = scanner.nextLine().trim();
} catch (NoSuchElementException e) {
break;
}
if (line.equalsIgnoreCase("stop")) {
System.exit(0);
Command handler = getCommand(command);
if (handler != null) {
try {
handler.execute();
} catch (Throwable t) {
Logger.error("Cannot execute command:", t);
}
continue;
}
Logger.info("Unknown command");
Logger.info("Unknown command. Type \"help\" to get commands list");
}
}
public void registerAll(LimboServer server) {
register("help", new CmdHelp(server));
register("conn", new CmdConn(server));
register("mem", new CmdMem());
register("stop", new CmdStop());
}
}

View File

@ -49,6 +49,8 @@ public final class LimboServer {
private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;
private CommandManager commandManager;
public LimboConfig getConfig() {
return config;
}
@ -65,6 +67,10 @@ public final class LimboServer {
return dimensionRegistry;
}
public CommandManager getCommandManager() {
return commandManager;
}
public void start() throws Exception {
Logger.info("Starting server...");
@ -90,7 +96,11 @@ public final class LimboServer {
Logger.setLevel(config.getDebugLevel());
new CommandManager().start();
commandManager = new CommandManager();
commandManager.registerAll(this);
commandManager.start();
System.gc();
}
private void startBootstrap() {

View File

@ -0,0 +1,24 @@
package ru.nanit.limbo.server.commands;
import ru.nanit.limbo.server.Command;
import ru.nanit.limbo.server.LimboServer;
import ru.nanit.limbo.server.Logger;
public class CmdConn implements Command {
private final LimboServer server;
public CmdConn(LimboServer server) {
this.server = server;
}
@Override
public void execute() {
Logger.info("Connections: %d", server.getConnections().getCount());
}
@Override
public String description() {
return "Display connections count";
}
}

View File

@ -0,0 +1,37 @@
package ru.nanit.limbo.server.commands;
import ru.nanit.limbo.server.Command;
import ru.nanit.limbo.server.LimboServer;
import java.util.Map;
public class CmdHelp implements Command {
private final LimboServer server;
public CmdHelp(LimboServer server) {
this.server = server;
}
@Override
public void execute() {
StringBuilder msg = new StringBuilder();
Map<String, Command> commands = server.getCommandManager().getCommands();
for (Map.Entry<String, Command> entry : commands.entrySet()) {
msg.append("\n");
msg.append(entry.getKey());
msg.append(" - ");
msg.append(entry.getValue().description());
}
msg.append("\n");
System.out.println(msg);
}
@Override
public String description() {
return "Show this message";
}
}

View File

@ -0,0 +1,27 @@
package ru.nanit.limbo.server.commands;
import ru.nanit.limbo.server.Command;
import ru.nanit.limbo.server.Logger;
public class CmdMem implements Command {
@Override
public void execute() {
Runtime runtime = Runtime.getRuntime();
long mb = 1024 * 1024;
long used = (runtime.totalMemory() - runtime.freeMemory()) / mb;
long total = runtime.totalMemory() / mb;
long free = runtime.freeMemory() / mb;
long max = runtime.maxMemory() / mb;
Logger.info("Used: %d MB", used);
Logger.info("Total: %d MB", total);
Logger.info("Free: %d MB", free);
Logger.info("Max: %d MB", max);
}
@Override
public String description() {
return "Display memory usage";
}
}

View File

@ -0,0 +1,16 @@
package ru.nanit.limbo.server.commands;
import ru.nanit.limbo.server.Command;
public class CmdStop implements Command {
@Override
public void execute() {
System.exit(0);
}
@Override
public String description() {
return "Stop the server";
}
}