From 6167699c20403ca4be65329f49a188f8626c4109 Mon Sep 17 00:00:00 2001 From: Gravit Date: Fri, 5 Oct 2018 16:20:40 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=20WebSocke?= =?UTF-8?q?ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../socket/websocket/WebSocketService.java | 4 +- .../request/websockets/ClientJSONPoint.java | 23 ++++--- .../websockets/ClientWebSocketService.java | 60 +++++++++++++++++++ .../websockets/JsonRequestAdapter.java | 34 +++++++++++ .../request/websockets/RequestInterface.java | 5 ++ .../request/websockets/ResultInterface.java | 5 ++ .../launcher/hasher}/HashedEntryAdapter.java | 11 +--- 7 files changed, 119 insertions(+), 23 deletions(-) create mode 100644 LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java create mode 100644 LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/JsonRequestAdapter.java create mode 100644 LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/RequestInterface.java create mode 100644 LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ResultInterface.java rename {LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json => libLauncher/src/main/java/ru/gravit/launcher/hasher}/HashedEntryAdapter.java (75%) diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketService.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketService.java index 67b02a06..45259b90 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketService.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketService.java @@ -11,7 +11,7 @@ import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.socket.websocket.json.EchoResponse; -import ru.gravit.launchserver.socket.websocket.json.HashedEntryAdapter; +import ru.gravit.launcher.hasher.HashedEntryAdapter; import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter; import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface; import ru.gravit.launchserver.socket.websocket.json.auth.AuthResponse; @@ -31,7 +31,7 @@ public WebSocketService(ChannelGroup channels, LaunchServer server, GsonBuilder this.gsonBuiler = gson; this. gsonBuiler.registerTypeAdapter(JsonResponseInterface.class,new JsonResponseAdapter(this)); - gsonBuiler.registerTypeAdapter(HashedEntry.class,new HashedEntryAdapter(this)); + gsonBuiler.registerTypeAdapter(HashedEntry.class,new HashedEntryAdapter()); this.gson = gsonBuiler.create(); } diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientJSONPoint.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientJSONPoint.java index 66069bc3..d4c63fbb 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientJSONPoint.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientJSONPoint.java @@ -9,10 +9,6 @@ import javax.websocket.OnOpen; import javax.websocket.Session; -import com.eclipsesource.json.Json; -import com.eclipsesource.json.JsonValue; -import com.eclipsesource.json.ParseException; - import ru.gravit.utils.helper.LogHelper; /* @@ -36,6 +32,11 @@ @ClientEndpoint public class ClientJSONPoint { public Session session = null; + private ClientWebSocketService service; + + public void setService(ClientWebSocketService service) { + this.service = service; + } @OnOpen public void onOpen(final Session session_r) { @@ -50,18 +51,14 @@ public void processError(final Throwable t) { @OnMessage public void processMessage(Reader message) { - try { - JsonValue json = Json.parse(message); - } catch (ParseException | IOException ex) { - LogHelper.error(ex); - } + service.processMessage(message); } - public void send(JsonValue js) throws IOException { - session.getBasicRemote().sendText(js.asString()); + public void send(String js) throws IOException { + session.getBasicRemote().sendText(js); } - public void sendAsync(JsonValue js) throws IOException { - session.getAsyncRemote().sendText(js.asString()); + public void sendAsync(String js) throws IOException { + session.getAsyncRemote().sendText(js); } } \ No newline at end of file diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java new file mode 100644 index 00000000..2e9d3846 --- /dev/null +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java @@ -0,0 +1,60 @@ +package ru.gravit.launcher.request.websockets; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import ru.gravit.launcher.hasher.HashedEntry; +import ru.gravit.launcher.hasher.HashedEntryAdapter; + +import java.io.IOException; +import java.io.Reader; +import java.util.HashMap; + +public class ClientWebSocketService { + public final GsonBuilder gsonBuilder; + public final Gson gson; + public final ClientJSONPoint point; + private HashMap requests; + private HashMap results; + + public ClientWebSocketService(GsonBuilder gsonBuilder,ClientJSONPoint point) { + requests = new HashMap<>(); + results = new HashMap<>(); + this.gsonBuilder = gsonBuilder; + gsonBuilder.registerTypeAdapter(RequestInterface.class, new JsonRequestAdapter(this)); + gsonBuilder.registerTypeAdapter(HashedEntry.class,new HashedEntryAdapter()); + this.gson = gsonBuilder.create(); + this.point = point; + point.setService(this); + } + public void processMessage(Reader reader) + { + ResultInterface result = gson.fromJson(reader,ResultInterface.class); + result.process(); + } + public Class getRequestClass(String key) + { + return requests.get(key); + } + public void registerRequest(String key, Class clazz) + { + requests.put(key,clazz); + } + public void registerRequests() + { + + } + public void registerResult(String key, Class clazz) + { + results.put(key,clazz); + } + public void registerResults() + { + + } + public void sendObjectAsync(Object obj) throws IOException { + point.sendAsync(gson.toJson(obj)); + } + public void sendObject(Object obj) throws IOException { + point.send(gson.toJson(obj)); + } +} diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/JsonRequestAdapter.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/JsonRequestAdapter.java new file mode 100644 index 00000000..da6d860d --- /dev/null +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/JsonRequestAdapter.java @@ -0,0 +1,34 @@ +package ru.gravit.launcher.request.websockets; + +import com.google.gson.*; + +import java.lang.reflect.Type; + +public class JsonRequestAdapter implements JsonSerializer, JsonDeserializer { + private final ClientWebSocketService service; + private static final String PROP_NAME = "type"; + + public JsonRequestAdapter(ClientWebSocketService service) { + this.service = service; + } + + @Override + public RequestInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); + Class cls = service.getRequestClass(typename); + + + return (RequestInterface) context.deserialize(json, cls); + } + + @Override + public JsonElement serialize(RequestInterface src, Type typeOfSrc, JsonSerializationContext context) { + // note : won't work, you must delegate this + JsonObject jo = context.serialize(src).getAsJsonObject(); + + String classPath = src.getType(); + jo.add(PROP_NAME, new JsonPrimitive(classPath)); + + return jo; + } +} diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/RequestInterface.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/RequestInterface.java new file mode 100644 index 00000000..4633413d --- /dev/null +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/RequestInterface.java @@ -0,0 +1,5 @@ +package ru.gravit.launcher.request.websockets; + +public interface RequestInterface { + String getType(); +} diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ResultInterface.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ResultInterface.java new file mode 100644 index 00000000..bc30c1f0 --- /dev/null +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ResultInterface.java @@ -0,0 +1,5 @@ +package ru.gravit.launcher.request.websockets; + +public interface ResultInterface { + void process(); +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/HashedEntryAdapter.java b/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedEntryAdapter.java similarity index 75% rename from LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/HashedEntryAdapter.java rename to libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedEntryAdapter.java index 062b4e2c..b57bd1eb 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/HashedEntryAdapter.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedEntryAdapter.java @@ -1,20 +1,15 @@ -package ru.gravit.launchserver.socket.websocket.json; +package ru.gravit.launcher.hasher; import com.google.gson.*; -import ru.gravit.launcher.hasher.HashedDir; -import ru.gravit.launcher.hasher.HashedEntry; -import ru.gravit.launcher.hasher.HashedFile; -import ru.gravit.launchserver.socket.websocket.WebSocketService; import java.lang.reflect.Type; public class HashedEntryAdapter implements JsonSerializer, JsonDeserializer { - private final WebSocketService service; private static final String PROP_NAME = "type"; - public HashedEntryAdapter(WebSocketService service) { - this.service = service; + public HashedEntryAdapter() { + } @Override