[FEATURE] Клиент метод на основе notifyAll

This commit is contained in:
Gravit 2019-01-28 23:08:51 +07:00
parent 2e4acb721a
commit 92cb0b0628
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
6 changed files with 96 additions and 22 deletions

View file

@ -25,11 +25,6 @@
@ClientEndpoint @ClientEndpoint
public class ClientJSONPoint { public class ClientJSONPoint {
public Session session = null; public Session session = null;
private ClientWebSocketService service;
public void setService(ClientWebSocketService service) {
this.service = service;
}
@OnOpen @OnOpen
public void onOpen(final Session session_r) { public void onOpen(final Session session_r) {
@ -44,7 +39,7 @@ public void processError(final Throwable t) {
@OnMessage @OnMessage
public void processMessage(Reader message) { public void processMessage(Reader message) {
service.processMessage(message);
} }
public void send(String js) throws IOException { public void send(String js) throws IOException {

View file

@ -8,35 +8,41 @@
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
public class ClientWebSocketService { public class ClientWebSocketService extends ClientJSONPoint {
public final GsonBuilder gsonBuilder; public final GsonBuilder gsonBuilder;
public final Gson gson; public final Gson gson;
public final ClientJSONPoint point; private HashMap<String, Class<? extends RequestInterface>> requests;
private HashMap<String, Class<RequestInterface>> requests; private HashMap<String, Class<? extends ResultInterface>> results;
private HashMap<String, Class<ResultInterface>> results; private HashSet<EventHandler> handlers;
public ClientWebSocketService(GsonBuilder gsonBuilder, ClientJSONPoint point) { public ClientWebSocketService(GsonBuilder gsonBuilder) {
requests = new HashMap<>(); requests = new HashMap<>();
results = new HashMap<>(); results = new HashMap<>();
handlers = new HashSet<>();
this.gsonBuilder = gsonBuilder; this.gsonBuilder = gsonBuilder;
gsonBuilder.registerTypeAdapter(RequestInterface.class, new JsonRequestAdapter(this)); gsonBuilder.registerTypeAdapter(RequestInterface.class, new JsonRequestAdapter(this));
gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter()); gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter());
this.gson = gsonBuilder.create(); this.gson = gsonBuilder.create();
this.point = point;
point.setService(this);
} }
@Override
public void processMessage(Reader reader) { public void processMessage(Reader reader) {
ResultInterface result = gson.fromJson(reader, ResultInterface.class); ResultInterface result = gson.fromJson(reader, ResultInterface.class);
result.process(); for(EventHandler handler : handlers)
{
handler.process(result);
}
} }
public Class<RequestInterface> getRequestClass(String key) { public Class<? extends RequestInterface> getRequestClass(String key) {
return requests.get(key); return requests.get(key);
} }
public Class<? extends ResultInterface> getResultClass(String key) {
return results.get(key);
}
public void registerRequest(String key, Class<RequestInterface> clazz) { public void registerRequest(String key, Class<? extends RequestInterface> clazz) {
requests.put(key, clazz); requests.put(key, clazz);
} }
@ -44,7 +50,7 @@ public void registerRequests() {
} }
public void registerResult(String key, Class<ResultInterface> clazz) { public void registerResult(String key, Class<? extends ResultInterface> clazz) {
results.put(key, 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 { public void sendObjectAsync(Object obj) throws IOException {
point.sendAsync(gson.toJson(obj)); sendAsync(gson.toJson(obj));
} }
public void sendObject(Object obj) throws IOException { public void sendObject(Object obj) throws IOException {
point.send(gson.toJson(obj)); send(gson.toJson(obj));
}
@FunctionalInterface
public interface EventHandler
{
void process(ResultInterface resultInterface);
} }
} }

View file

@ -15,7 +15,7 @@ public JsonRequestAdapter(ClientWebSocketService service) {
@Override @Override
public RequestInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public RequestInterface 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<RequestInterface> cls = service.getRequestClass(typename); Class<? extends RequestInterface> cls = service.getRequestClass(typename);
return (RequestInterface) context.deserialize(json, cls); return (RequestInterface) context.deserialize(json, cls);

View file

@ -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<ResultInterface>, JsonDeserializer<ResultInterface> {
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<? extends ResultInterface> 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;
}
}

View file

@ -1,5 +1,5 @@
package ru.gravit.launcher.request.websockets; package ru.gravit.launcher.request.websockets;
public interface ResultInterface { public interface ResultInterface {
void process(); String getType();
} }

View file

@ -0,0 +1,28 @@
package ru.gravit.launcher.request.websockets;
import java.util.HashSet;
public class WaitEventHandler implements ClientWebSocketService.EventHandler {
public HashSet<ResultEvent> 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;
}
}