[FEATURE] AddLogListener

This commit is contained in:
Gravit 2019-02-17 14:24:20 +07:00
parent dd586f23eb
commit 0fcb3fd5e4
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
4 changed files with 68 additions and 0 deletions

View file

@ -2,6 +2,7 @@
import ru.gravit.launcher.ClientPermissions; import ru.gravit.launcher.ClientPermissions;
import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.utils.helper.LogHelper;
public class Client { public class Client {
public long session; public long session;
@ -13,6 +14,7 @@ public class Client {
public boolean checkSign; public boolean checkSign;
public ClientPermissions permissions; public ClientPermissions permissions;
public String username; public String username;
public LogHelper.Output logOutput;
public Client(long session) { public Client(long session) {
this.session = session; this.session = session;

View file

@ -16,6 +16,7 @@
import ru.gravit.launchserver.socket.websocket.json.EchoResponse; import ru.gravit.launchserver.socket.websocket.json.EchoResponse;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter; import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface; import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
import ru.gravit.launchserver.socket.websocket.json.admin.AddLogListenerResponse;
import ru.gravit.launchserver.socket.websocket.json.auth.*; import ru.gravit.launchserver.socket.websocket.json.auth.*;
import ru.gravit.launchserver.socket.websocket.json.update.LauncherResponse; import ru.gravit.launchserver.socket.websocket.json.update.LauncherResponse;
import ru.gravit.launchserver.socket.websocket.json.update.UpdateListResponse; import ru.gravit.launchserver.socket.websocket.json.update.UpdateListResponse;
@ -76,6 +77,7 @@ public void registerResponses() {
registerResponse("updateList", UpdateListResponse.class); registerResponse("updateList", UpdateListResponse.class);
registerResponse("cmdExec", UpdateListResponse.class); registerResponse("cmdExec", UpdateListResponse.class);
registerResponse("setProfile", SetProfileResponse.class); registerResponse("setProfile", SetProfileResponse.class);
registerResponse("addLogListener", AddLogListenerResponse.class);
} }
public void sendObject(ChannelHandlerContext ctx, Object obj) { public void sendObject(ChannelHandlerContext ctx, Object obj) {

View file

@ -0,0 +1,49 @@
package ru.gravit.launchserver.socket.websocket.json.admin;
import io.netty.channel.ChannelHandlerContext;
import ru.gravit.launcher.events.request.ErrorRequestEvent;
import ru.gravit.launcher.events.request.LogEvent;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.launchserver.socket.websocket.WebSocketService;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
import ru.gravit.utils.helper.LogHelper;
public class AddLogListenerResponse implements JsonResponseInterface {
@Override
public String getType() {
return "addLogListener";
}
@Override
public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception {
if (!client.isAuth) {
service.sendObject(ctx, new ErrorRequestEvent("Access denied"));
return;
}
if (!client.permissions.canAdmin) {
service.sendObject(ctx, new ErrorRequestEvent("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.info("Client %s remove log listener", client.username);
LogHelper.removeOutput(client.logOutput);
}
};
client.logOutput = output;
LogHelper.addOutput(output);
}
}
}

View file

@ -0,0 +1,15 @@
package ru.gravit.launcher.events.request;
import ru.gravit.launcher.request.ResultInterface;
public class LogEvent implements ResultInterface {
@Override
public String getType() {
return "log";
}
public String string;
public LogEvent(String string) {
this.string = string;
}
}