From 81abc3fdc279467ffbe13290f15898f265a89e5d Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 30 May 2020 04:06:15 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=20Ser?= =?UTF-8?q?verStatus.=20=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=20execCommand?= =?UTF-8?q?=20=D0=B8=20logListen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../launchserver/socket/WebSocketService.java | 58 +++++++++++++++++-- .../admin/AddLogListenerResponse.java | 45 -------------- .../response/admin/ExecCommandResponse.java | 26 --------- .../management/ServerStatusResponse.java | 28 +++++++++ .../request/ExecCommandRequestEvent.java | 18 ------ .../request/ServerStatusRequestEvent.java | 25 ++++++++ .../request/admin/ExecCommandRequest.java | 19 ------ .../management/ServerStatusRequest.java | 11 ++++ .../websockets/ClientWebSocketService.java | 1 - 9 files changed, 118 insertions(+), 113 deletions(-) delete mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/admin/AddLogListenerResponse.java delete mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/admin/ExecCommandResponse.java create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/management/ServerStatusResponse.java delete mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/events/request/ExecCommandRequestEvent.java create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/events/request/ServerStatusRequestEvent.java delete mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/request/admin/ExecCommandRequest.java create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/request/management/ServerStatusRequest.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java index 6e6b2681..53defc97 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java @@ -15,9 +15,8 @@ import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.socket.response.SimpleResponse; import pro.gravit.launchserver.socket.response.WebSocketServerResponse; -import pro.gravit.launchserver.socket.response.admin.AddLogListenerResponse; -import pro.gravit.launchserver.socket.response.admin.ExecCommandResponse; import pro.gravit.launchserver.socket.response.auth.*; +import pro.gravit.launchserver.socket.response.management.ServerStatusResponse; import pro.gravit.launchserver.socket.response.profile.BatchProfileByUsername; import pro.gravit.launchserver.socket.response.profile.ProfileByUUIDResponse; import pro.gravit.launchserver.socket.response.profile.ProfileByUsername; @@ -34,6 +33,7 @@ import pro.gravit.utils.helper.LogHelper; import java.lang.reflect.Type; +import java.util.concurrent.atomic.AtomicLong; public class WebSocketService { public static final ProviderMap providers = new ProviderMap<>(); @@ -42,6 +42,18 @@ public class WebSocketService { private final LaunchServer server; private final Gson gson; + //Statistic data + public AtomicLong shortRequestLatency = new AtomicLong(); + public AtomicLong shortRequestCounter = new AtomicLong(); + + public AtomicLong middleRequestLatency = new AtomicLong(); + public AtomicLong middleRequestCounter = new AtomicLong(); + + public AtomicLong longRequestLatency = new AtomicLong(); + public AtomicLong longRequestCounter = new AtomicLong(); + + public AtomicLong lastRequestTime = new AtomicLong(); + public WebSocketService(ChannelGroup channels, LaunchServer server) { this.channels = channels; this.server = server; @@ -58,9 +70,7 @@ public static void registerResponses() { providers.register("profiles", ProfilesResponse.class); providers.register("launcher", LauncherResponse.class); providers.register("updateList", UpdateListResponse.class); - providers.register("cmdExec", ExecCommandResponse.class); providers.register("setProfile", SetProfileResponse.class); - providers.register("addLogListener", AddLogListenerResponse.class); providers.register("update", UpdateResponse.class); providers.register("restoreSession", RestoreSessionResponse.class); providers.register("batchProfileByUsername", BatchProfileByUsername.class); @@ -74,16 +84,56 @@ public static void registerResponses() { providers.register("verifySecureLevelKey", VerifySecureLevelKeyResponse.class); providers.register("securityReport", SecurityReportResponse.class); providers.register("hardwareReport", HardwareReportResponse.class); + providers.register("serverStatus", ServerStatusResponse.class); } public void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client, String ip) { + long startTimeNanos = System.nanoTime(); String request = frame.text(); WebSocketServerResponse response = gson.fromJson(request, WebSocketServerResponse.class); if (response == null) { RequestEvent event = new ErrorRequestEvent("This type of request is not supported"); sendObject(ctx, event); + return; } process(ctx, response, client, ip); + long executeTime = System.nanoTime() - startTimeNanos; + if(executeTime > 0) + { + addRequestTimeToStats(executeTime); + } + } + + public void addRequestTimeToStats(long nanos) + { + if(nanos < 100_000_000L) // < 100 millis + { + shortRequestCounter.getAndIncrement(); + shortRequestLatency.getAndAdd(nanos); + } + else if(nanos < 1_000_000_000L) // > 100 millis and < 1 second + { + middleRequestCounter.getAndIncrement(); + middleRequestLatency.getAndAdd(nanos); + } + else // > 1 second + { + longRequestCounter.getAndIncrement(); + longRequestLatency.getAndAdd(nanos); + } + long lastTime = lastRequestTime.get(); + long currentTime = System.currentTimeMillis(); + if(currentTime - lastTime > 60*1000) //1 minute + { + lastRequestTime.set(currentTime); + shortRequestLatency.set(0); + shortRequestCounter.set(0); + middleRequestCounter.set(0); + middleRequestLatency.set(0); + longRequestCounter.set(0); + longRequestLatency.set(0); + } + } void process(ChannelHandlerContext ctx, WebSocketServerResponse response, Client client, String ip) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/admin/AddLogListenerResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/admin/AddLogListenerResponse.java deleted file mode 100644 index 00e8482f..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/admin/AddLogListenerResponse.java +++ /dev/null @@ -1,45 +0,0 @@ -package pro.gravit.launchserver.socket.response.admin; - -import io.netty.channel.ChannelHandlerContext; -import pro.gravit.launcher.ClientPermissions; -import pro.gravit.launcher.events.request.LogEvent; -import pro.gravit.launchserver.socket.Client; -import pro.gravit.launchserver.socket.response.SimpleResponse; -import pro.gravit.utils.helper.LogHelper; - -public class AddLogListenerResponse extends SimpleResponse { - public final LogHelper.OutputTypes outputType = LogHelper.OutputTypes.PLAIN; - - @Override - public String getType() { - return "addLogListener"; - } - - @Override - public void execute(ChannelHandlerContext ctx, Client client) { - if (!client.isAuth) { - sendError("Access denied"); - return; - } - if (!client.permissions.isPermission(ClientPermissions.PermissionConsts.ADMIN)) { - sendError("Access denied"); - return; - } - if (client.logOutput != null) { - LogHelper.info("Client %s remove log listener", client.username); - LogHelper.removeOutput(client.logOutput); - } else { - LogHelper.info("Client %s add log listener", client.username); - LogHelper.Output output = (str) -> { - if (!ctx.isRemoved()) { - service.sendObject(ctx, new LogEvent(str)); - } else { - LogHelper.removeOutput(client.logOutput); - LogHelper.info("Client %s remove log listener", client.username); - } - }; - client.logOutput = new LogHelper.OutputEnity(output, outputType); - LogHelper.addOutput(client.logOutput); - } - } -} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/admin/ExecCommandResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/admin/ExecCommandResponse.java deleted file mode 100644 index b8d34d38..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/admin/ExecCommandResponse.java +++ /dev/null @@ -1,26 +0,0 @@ -package pro.gravit.launchserver.socket.response.admin; - -import io.netty.channel.ChannelHandlerContext; -import pro.gravit.launcher.ClientPermissions; -import pro.gravit.launcher.events.request.ExecCommandRequestEvent; -import pro.gravit.launchserver.socket.Client; -import pro.gravit.launchserver.socket.response.SimpleResponse; - -public class ExecCommandResponse extends SimpleResponse { - public String cmd; - - @Override - public String getType() { - return "cmdExec"; - } - - @Override - public void execute(ChannelHandlerContext ctx, Client client) { - if (!client.isAuth || !client.permissions.isPermission(ClientPermissions.PermissionConsts.ADMIN)) { - sendError("Access denied"); - return; - } - server.commandHandler.eval(cmd, false); - sendResult(new ExecCommandRequestEvent(true)); - } -} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/management/ServerStatusResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/management/ServerStatusResponse.java new file mode 100644 index 00000000..d8631c80 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/management/ServerStatusResponse.java @@ -0,0 +1,28 @@ +package pro.gravit.launchserver.socket.response.management; + +import io.netty.channel.ChannelHandlerContext; +import pro.gravit.launcher.events.request.ServerStatusRequestEvent; +import pro.gravit.launchserver.socket.Client; +import pro.gravit.launchserver.socket.WebSocketService; +import pro.gravit.launchserver.socket.response.SimpleResponse; +import pro.gravit.utils.helper.JVMHelper; + +public class ServerStatusResponse extends SimpleResponse { + @Override + public String getType() { + return "serverStatus"; + } + + @Override + public void execute(ChannelHandlerContext ctx, Client client) throws Exception { + ServerStatusRequestEvent event = new ServerStatusRequestEvent(server.config.projectName); + event.totalJavaMemory = JVMHelper.RUNTIME.totalMemory(); + event.freeJavaMemory = JVMHelper.RUNTIME.freeMemory(); + event.shortLatency = ( service.shortRequestLatency.get() / service.shortRequestCounter.get() ) / 1_000_000; + event.middleLatency = ( service.middleRequestLatency.get() / service.middleRequestCounter.get() ) / 1_000_000; + event.longLatency = ( service.longRequestLatency.get() / service.longRequestCounter.get() ) / 1_000_000; + event.latency = ( ( service.shortRequestLatency.get() + service.middleRequestLatency.get() + service.longRequestLatency.get() ) / + ( service.shortRequestCounter.get() + service.middleRequestCounter.get() + service.longRequestCounter.get() ) ) / 1_000_000; + sendResult(event); + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/ExecCommandRequestEvent.java b/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/ExecCommandRequestEvent.java deleted file mode 100644 index c20104d7..00000000 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/ExecCommandRequestEvent.java +++ /dev/null @@ -1,18 +0,0 @@ -package pro.gravit.launcher.events.request; - -import pro.gravit.launcher.LauncherNetworkAPI; -import pro.gravit.launcher.events.RequestEvent; - -public class ExecCommandRequestEvent extends RequestEvent { - @LauncherNetworkAPI - public final boolean success; - - public ExecCommandRequestEvent(boolean success) { - this.success = success; - } - - @Override - public String getType() { - return "cmdExec"; - } -} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/ServerStatusRequestEvent.java b/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/ServerStatusRequestEvent.java new file mode 100644 index 00000000..ddf97ba8 --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/ServerStatusRequestEvent.java @@ -0,0 +1,25 @@ +package pro.gravit.launcher.events.request; + +import pro.gravit.launcher.events.RequestEvent; +import pro.gravit.utils.helper.JVMHelper; + +public class ServerStatusRequestEvent extends RequestEvent { + public final String projectName; + public long totalJavaMemory; + public long freeJavaMemory; + + //Latency + public long shortLatency; //Millis + public long middleLatency; //Millis + public long longLatency; //Millis + public long latency; + + public ServerStatusRequestEvent(String projectName) { + this.projectName = projectName; + } + + @Override + public String getType() { + return "serverStatus"; + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/admin/ExecCommandRequest.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/admin/ExecCommandRequest.java deleted file mode 100644 index 69cf6f79..00000000 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/admin/ExecCommandRequest.java +++ /dev/null @@ -1,19 +0,0 @@ -package pro.gravit.launcher.request.admin; - -import pro.gravit.launcher.events.request.ExecCommandRequestEvent; -import pro.gravit.launcher.request.Request; -import pro.gravit.launcher.request.websockets.WebSocketRequest; - -public class ExecCommandRequest extends Request implements WebSocketRequest { - - public final String cmd; - - public ExecCommandRequest(String cmd) { - this.cmd = cmd; - } - - @Override - public String getType() { - return "cmdExec"; - } -} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/management/ServerStatusRequest.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/management/ServerStatusRequest.java new file mode 100644 index 00000000..e40199ca --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/management/ServerStatusRequest.java @@ -0,0 +1,11 @@ +package pro.gravit.launcher.request.management; + +import pro.gravit.launcher.events.request.ServerStatusRequestEvent; +import pro.gravit.launcher.request.Request; + +public class ServerStatusRequest extends Request { + @Override + public String getType() { + return "serverStatus"; + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java index d1d5948a..7360155d 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java @@ -90,7 +90,6 @@ public void registerResults() { results.register("update", UpdateRequestEvent.class); results.register("restoreSession", RestoreSessionRequestEvent.class); results.register("log", LogEvent.class); - results.register("cmdExec", ExecCommandRequestEvent.class); results.register("getAvailabilityAuth", GetAvailabilityAuthRequestEvent.class); results.register("exception", ExceptionEvent.class); results.register("register", RegisterRequestEvent.class);