SimpleResponse

This commit is contained in:
Gravit 2018-10-01 15:36:19 +07:00
parent df0d9f37a2
commit b76e009aaa
7 changed files with 28 additions and 23 deletions

View file

@ -2,8 +2,6 @@
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup; import io.netty.channel.group.ChannelGroup;
@ -12,14 +10,11 @@
import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor; import io.netty.util.concurrent.GlobalEventExecutor;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.websocket.json.JsonResponse; import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter; import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
import java.io.InputStreamReader;
import java.io.Reader;
public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> { public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
public static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); public static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
public static Gson gson; public static Gson gson;
@ -28,7 +23,7 @@ public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocket
public static LaunchServer server; public static LaunchServer server;
public static GsonBuilder builder = new GsonBuilder(); public static GsonBuilder builder = new GsonBuilder();
static { static {
builder.registerTypeAdapter(JsonResponse.class,new JsonResponseAdapter()); builder.registerTypeAdapter(JsonResponseInterface.class,new JsonResponseAdapter());
gson = builder.create(); gson = builder.create();
} }
@Override @Override

View file

@ -4,7 +4,7 @@
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.websocket.json.JsonResponse; import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
public class WebSocketService { public class WebSocketService {
@ -19,7 +19,7 @@ public WebSocketService(LaunchServer server, Gson gson) {
void process(ChannelHandlerContext ctx, TextWebSocketFrame frame) void process(ChannelHandlerContext ctx, TextWebSocketFrame frame)
{ {
String request = frame.text(); String request = frame.text();
JsonResponse response = gson.fromJson(request, JsonResponse.class); JsonResponseInterface response = gson.fromJson(request, JsonResponseInterface.class);
try { try {
response.execute(this,ctx); response.execute(this,ctx);
} catch (Exception e) } catch (Exception e)

View file

@ -1,13 +1,10 @@
package ru.gravit.launchserver.socket.websocket.json; package ru.gravit.launchserver.socket.websocket.json;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import ru.gravit.launchserver.socket.websocket.WebSocketFrameHandler;
import ru.gravit.launchserver.socket.websocket.WebSocketService; import ru.gravit.launchserver.socket.websocket.WebSocketService;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
public class EchoResponse implements JsonResponse { public class EchoResponse implements JsonResponseInterface {
public final String echo; public final String echo;
public EchoResponse(String echo) { public EchoResponse(String echo) {

View file

@ -5,23 +5,23 @@
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.HashMap; import java.util.HashMap;
public class JsonResponseAdapter implements JsonSerializer<JsonResponse>, JsonDeserializer<JsonResponse> { public class JsonResponseAdapter implements JsonSerializer<JsonResponseInterface>, JsonDeserializer<JsonResponseInterface> {
static HashMap<String,Class> map = new HashMap<>(); static HashMap<String,Class> map = new HashMap<>();
private static final String PROP_NAME = "type"; private static final String PROP_NAME = "type";
static { static {
map.put("echo",EchoResponse.class); map.put("echo",EchoResponse.class);
} }
@Override @Override
public JsonResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public JsonResponseInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class cls = map.get(typename); Class cls = map.get(typename);
return (JsonResponse) context.deserialize(json, cls); return (JsonResponseInterface) context.deserialize(json, cls);
} }
@Override @Override
public JsonElement serialize(JsonResponse src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(JsonResponseInterface src, Type typeOfSrc, JsonSerializationContext context) {
// note : won't work, you must delegate this // note : won't work, you must delegate this
JsonObject jo = context.serialize(src).getAsJsonObject(); JsonObject jo = context.serialize(src).getAsJsonObject();

View file

@ -4,7 +4,7 @@
import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import ru.gravit.launchserver.socket.websocket.WebSocketService; import ru.gravit.launchserver.socket.websocket.WebSocketService;
public interface JsonResponse { public interface JsonResponseInterface {
String getType(); String getType();
void execute(WebSocketService service,ChannelHandlerContext ctx) throws Exception; void execute(WebSocketService service,ChannelHandlerContext ctx) throws Exception;
} }

View file

@ -0,0 +1,16 @@
package ru.gravit.launchserver.socket.websocket.json;
import io.netty.channel.ChannelHandlerContext;
import ru.gravit.launchserver.socket.websocket.WebSocketService;
public class SimpleResponse implements JsonResponseInterface {
@Override
public String getType() {
return null;
}
@Override
public void execute(WebSocketService service, ChannelHandlerContext ctx) throws Exception {
}
}

View file

@ -1,8 +1,6 @@
package ru.gravit.launchserver.socket.websocket.json.auth; package ru.gravit.launchserver.socket.websocket.json.auth;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launcher.serialize.signed.SignedObjectHolder; import ru.gravit.launcher.serialize.signed.SignedObjectHolder;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
@ -11,15 +9,14 @@
import ru.gravit.launchserver.auth.hwid.HWIDException; import ru.gravit.launchserver.auth.hwid.HWIDException;
import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.launchserver.auth.provider.AuthProviderResult; import ru.gravit.launchserver.auth.provider.AuthProviderResult;
import ru.gravit.launchserver.socket.websocket.WebSocketFrameHandler;
import ru.gravit.launchserver.socket.websocket.WebSocketService; import ru.gravit.launchserver.socket.websocket.WebSocketService;
import ru.gravit.launchserver.socket.websocket.json.JsonResponse; import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.utils.helper.VerifyHelper;
import java.util.Collection; import java.util.Collection;
public class AuthResponse implements JsonResponse { public class AuthResponse implements JsonResponseInterface {
public String login; public String login;
public String client; public String client;
private LaunchServer server = LaunchServer.server; private LaunchServer server = LaunchServer.server;