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 eb4c9aa0..1d6c7ff0 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 @@ -25,11 +25,6 @@ @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) { @@ -44,7 +39,7 @@ public void processError(final Throwable t) { @OnMessage public void processMessage(Reader message) { - service.processMessage(message); + } public void send(String js) throws IOException { 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 index 5ce34a4e..6c770a25 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java @@ -8,35 +8,41 @@ import java.io.IOException; import java.io.Reader; import java.util.HashMap; +import java.util.HashSet; -public class ClientWebSocketService { +public class ClientWebSocketService extends ClientJSONPoint { public final GsonBuilder gsonBuilder; public final Gson gson; - public final ClientJSONPoint point; - private HashMap> requests; - private HashMap> results; + private HashMap> requests; + private HashMap> results; + private HashSet handlers; - public ClientWebSocketService(GsonBuilder gsonBuilder, ClientJSONPoint point) { + public ClientWebSocketService(GsonBuilder gsonBuilder) { requests = new HashMap<>(); results = new HashMap<>(); + handlers = new HashSet<>(); 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); } - + @Override public void processMessage(Reader reader) { ResultInterface result = gson.fromJson(reader, ResultInterface.class); - result.process(); + for(EventHandler handler : handlers) + { + handler.process(result); + } } - public Class getRequestClass(String key) { + public Class getRequestClass(String key) { return requests.get(key); } + public Class getResultClass(String key) { + return results.get(key); + } - public void registerRequest(String key, Class clazz) { + public void registerRequest(String key, Class clazz) { requests.put(key, clazz); } @@ -44,7 +50,7 @@ public void registerRequests() { } - public void registerResult(String key, Class clazz) { + public void registerResult(String key, Class clazz) { results.put(key, clazz); } @@ -52,11 +58,21 @@ public void registerResults() { } + public void registerHandler(EventHandler eventHandler) + { + handlers.add(eventHandler); + } + public void sendObjectAsync(Object obj) throws IOException { - point.sendAsync(gson.toJson(obj)); + sendAsync(gson.toJson(obj)); } public void sendObject(Object obj) throws IOException { - point.send(gson.toJson(obj)); + send(gson.toJson(obj)); + } + @FunctionalInterface + public interface EventHandler + { + void process(ResultInterface resultInterface); } } 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 index da6d860d..be546079 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/JsonRequestAdapter.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/JsonRequestAdapter.java @@ -15,7 +15,7 @@ public JsonRequestAdapter(ClientWebSocketService 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); + Class cls = service.getRequestClass(typename); return (RequestInterface) context.deserialize(json, cls); diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/JsonResultAdapter.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/JsonResultAdapter.java new file mode 100644 index 00000000..babe8b78 --- /dev/null +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/JsonResultAdapter.java @@ -0,0 +1,35 @@ +package ru.gravit.launcher.request.websockets; + +import com.google.gson.*; +import ru.gravit.launcher.request.update.LauncherRequest; + +import java.lang.reflect.Type; + +public class JsonResultAdapter implements JsonSerializer, JsonDeserializer { + private final ClientWebSocketService service; + private static final String PROP_NAME = "type"; + + public JsonResultAdapter(ClientWebSocketService service) { + this.service = service; + } + + @Override + public ResultInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); + Class cls = service.getResultClass(typename); + + + return (ResultInterface) context.deserialize(json, cls); + } + + @Override + public JsonElement serialize(ResultInterface 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/ResultInterface.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ResultInterface.java index bc30c1f0..57e7d2b5 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ResultInterface.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ResultInterface.java @@ -1,5 +1,5 @@ package ru.gravit.launcher.request.websockets; public interface ResultInterface { - void process(); + String getType(); } diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/WaitEventHandler.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/WaitEventHandler.java new file mode 100644 index 00000000..364e7299 --- /dev/null +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/WaitEventHandler.java @@ -0,0 +1,28 @@ +package ru.gravit.launcher.request.websockets; + +import java.util.HashSet; + +public class WaitEventHandler implements ClientWebSocketService.EventHandler { + public HashSet requests; + @Override + public void process(ResultInterface result) { + for(ResultEvent r : requests) + { + if(r.type.equals(result.getType())) + { + synchronized (r) + { + r.result = result; + r.ready = true; + r.notifyAll(); + } + } + } + } + public class ResultEvent + { + public ResultInterface result; + public String type; + public boolean ready; + } +}