[REFRACTOR] Переименование интерфейсов WebSocket запросов/ответов на более понятные

This commit is contained in:
Gravit 2019-07-01 17:46:49 +07:00
parent 955020005a
commit b39c640cd6
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
34 changed files with 106 additions and 108 deletions

View file

@ -6,7 +6,7 @@
import pro.gravit.launcher.hasher.HashedEntryAdapter;
import pro.gravit.launcher.managers.GsonManager;
import pro.gravit.launcher.request.JsonResultSerializeAdapter;
import pro.gravit.launcher.request.ResultInterface;
import pro.gravit.launcher.request.WebSocketEvent;
import pro.gravit.launchserver.auth.handler.AuthHandler;
import pro.gravit.launchserver.auth.hwid.HWIDHandler;
import pro.gravit.launchserver.auth.permissions.PermissionsHandler;
@ -16,7 +16,7 @@
import pro.gravit.launchserver.components.Component;
import pro.gravit.launchserver.dao.provider.DaoProvider;
import pro.gravit.launchserver.socket.WebSocketService;
import pro.gravit.launchserver.socket.response.JsonResponseInterface;
import pro.gravit.launchserver.socket.response.WebSocketServerResponse;
import pro.gravit.utils.UniversalJsonAdapter;
public class LaunchServerGsonManager extends GsonManager {
@ -31,8 +31,8 @@ public void registerAdapters(GsonBuilder builder) {
builder.registerTypeAdapter(Component.class, new UniversalJsonAdapter<>(Component.providers));
builder.registerTypeAdapter(ProtectHandler.class, new UniversalJsonAdapter<>(ProtectHandler.providers));
builder.registerTypeAdapter(DaoProvider.class, new UniversalJsonAdapter<>(DaoProvider.providers));
builder.registerTypeAdapter(JsonResponseInterface.class, new UniversalJsonAdapter<>(WebSocketService.providers));
builder.registerTypeAdapter(WebSocketServerResponse.class, new UniversalJsonAdapter<>(WebSocketService.providers));
builder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter());
builder.registerTypeAdapter(ResultInterface.class, new JsonResultSerializeAdapter());
builder.registerTypeAdapter(WebSocketEvent.class, new JsonResultSerializeAdapter());
}
}

View file

@ -6,7 +6,6 @@
import java.util.UUID;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
@ -20,10 +19,10 @@
import pro.gravit.launcher.events.request.ErrorRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.RequestException;
import pro.gravit.launcher.request.ResultInterface;
import pro.gravit.launcher.request.WebSocketEvent;
import pro.gravit.launcher.request.admin.ProxyRequest;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.socket.response.JsonResponseInterface;
import pro.gravit.launchserver.socket.response.WebSocketServerResponse;
import pro.gravit.launchserver.socket.response.SimpleResponse;
import pro.gravit.launchserver.socket.response.admin.AddLogListenerResponse;
import pro.gravit.launchserver.socket.response.admin.ExecCommandResponse;
@ -44,13 +43,13 @@
@SuppressWarnings("rawtypes")
public class WebSocketService {
public final ChannelGroup channels;
public static ProviderMap<JsonResponseInterface> providers = new ProviderMap<>();
public static ProviderMap<WebSocketServerResponse> providers = new ProviderMap<>();
public WebSocketService(ChannelGroup channels, LaunchServer server) {
this.channels = channels;
this.server = server;
//this.gsonBuiler.registerTypeAdapter(JsonResponseInterface.class, new JsonResponseAdapter(this));
//this.gsonBuiler.registerTypeAdapter(ResultInterface.class, new JsonResultSerializeAdapter());
//this.gsonBuiler.registerTypeAdapter(WebSocketServerResponse.class, new JsonResponseAdapter(this));
//this.gsonBuiler.registerTypeAdapter(WebSocketEvent.class, new JsonResultSerializeAdapter());
//this.gsonBuiler.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter());
this.gson = Launcher.gsonManager.gson;
}
@ -61,7 +60,7 @@ public WebSocketService(ChannelGroup channels, LaunchServer server) {
public void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client, String ip) {
String request = frame.text();
JsonResponseInterface response = gson.fromJson(request, JsonResponseInterface.class);
WebSocketServerResponse response = gson.fromJson(request, WebSocketServerResponse.class);
if (server.config.netty.proxy.enabled) {
if (server.config.netty.proxy.requests.contains(response.getType())) {
@ -83,7 +82,7 @@ public void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client
}
proxyRequest.isCheckSign = client.checkSign;
try {
ResultInterface result = proxyRequest.request();
WebSocketEvent result = proxyRequest.request();
if (result instanceof AuthRequestEvent) {
LogHelper.debug("Client auth params get successful");
AuthRequestEvent authRequestEvent = (AuthRequestEvent) result;
@ -117,7 +116,7 @@ public void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client
process(ctx, response, client, ip);
}
void process(ChannelHandlerContext ctx, JsonResponseInterface response, Client client, String ip) {
void process(ChannelHandlerContext ctx, WebSocketServerResponse response, Client client, String ip) {
if (response instanceof SimpleResponse) {
SimpleResponse simpleResponse = (SimpleResponse) response;
simpleResponse.server = server;
@ -174,7 +173,7 @@ public static void registerResponses() {
}
public void sendObject(ChannelHandlerContext ctx, Object obj) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, ResultInterface.class)));
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class)));
}
public void sendObject(ChannelHandlerContext ctx, Object obj, Type type) {
@ -183,7 +182,7 @@ public void sendObject(ChannelHandlerContext ctx, Object obj, Type type) {
public void sendObjectAll(Object obj) {
for (Channel ch : channels) {
ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, ResultInterface.class)));
ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class)));
}
}
@ -194,7 +193,7 @@ public void sendObjectAll(Object obj, Type type) {
}
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, ResultInterface.class))).addListener(ChannelFutureListener.CLOSE);
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class))).addListener(ChannelFutureListener.CLOSE);
}
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj, Type type) {
@ -205,7 +204,7 @@ public void sendEvent(EventResult obj) {
channels.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj)));
}
public static class EventResult implements ResultInterface {
public static class EventResult implements WebSocketEvent {
public EventResult() {
}

View file

@ -6,10 +6,9 @@
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.events.request.ErrorRequestEvent;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.launchserver.socket.WebSocketService;
public abstract class SimpleResponse implements JsonResponseInterface {
public abstract class SimpleResponse implements WebSocketServerResponse {
public UUID requestUUID;
public transient LaunchServer server;
public transient WebSocketService service;

View file

@ -1,10 +1,10 @@
package pro.gravit.launchserver.socket.response;
import io.netty.channel.ChannelHandlerContext;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.launchserver.socket.Client;
public interface JsonResponseInterface extends RequestInterface {
public interface WebSocketServerResponse extends WebSocketRequest {
String getType();
void execute(ChannelHandlerContext ctx, Client client) throws Exception;

View file

@ -2,11 +2,11 @@
import io.netty.channel.ChannelHandlerContext;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.launchserver.socket.response.JsonResponseInterface;
import pro.gravit.launchserver.socket.response.WebSocketServerResponse;
import pro.gravit.launchserver.socket.response.SimpleResponse;
public class ProxyCommandResponse extends SimpleResponse {
public JsonResponseInterface response;
public WebSocketServerResponse response;
public long session;
public boolean isCheckSign;

View file

@ -3,12 +3,12 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.request.LogEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.utils.command.Command;
import pro.gravit.utils.helper.LogHelper;
public class LogListenerCommand extends Command {
public class LogListenerRequest implements RequestInterface {
public class LogListenerRequest implements WebSocketRequest {
@LauncherNetworkAPI
public LogHelper.OutputTypes outputType;

View file

@ -3,12 +3,12 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.request.LogEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.utils.command.Command;
import pro.gravit.utils.helper.LogHelper;
public class LogListenerCommand extends Command {
public class LogListenerRequest implements RequestInterface {
public class LogListenerRequest implements WebSocketRequest {
@LauncherNetworkAPI
public LogHelper.OutputTypes outputType;

View file

@ -2,8 +2,8 @@
import java.util.UUID;
import pro.gravit.launcher.request.ResultInterface;
import pro.gravit.launcher.request.WebSocketEvent;
public abstract class RequestEvent implements ResultInterface {
public abstract class RequestEvent implements WebSocketEvent {
public UUID requestUUID;
}

View file

@ -1,9 +1,9 @@
package pro.gravit.launcher.events.request;
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.request.ResultInterface;
import pro.gravit.launcher.request.WebSocketEvent;
public class LogEvent implements ResultInterface {
public class LogEvent implements WebSocketEvent {
@Override
public String getType() {
return "log";

View file

@ -8,11 +8,11 @@
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class JsonResultSerializeAdapter implements JsonSerializer<ResultInterface> {
public class JsonResultSerializeAdapter implements JsonSerializer<WebSocketEvent> {
private static final String PROP_NAME = "type";
@Override
public JsonElement serialize(ResultInterface src, Type typeOfSrc, JsonSerializationContext context) {
public JsonElement serialize(WebSocketEvent src, Type typeOfSrc, JsonSerializationContext context) {
// note : won't work, you must delegate this
JsonObject jo = context.serialize(src).getAsJsonObject();

View file

@ -2,10 +2,10 @@
import pro.gravit.launcher.request.websockets.StandartClientWebSocketService;
public final class PingRequest extends Request<ResultInterface> {
public final class PingRequest extends Request<WebSocketEvent> {
@Override
protected ResultInterface requestDo(StandartClientWebSocketService service) throws Exception {
protected WebSocketEvent requestDo(StandartClientWebSocketService service) throws Exception {
return null;
}

View file

@ -6,11 +6,11 @@
import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.LauncherAPI;
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.launcher.request.websockets.StandartClientWebSocketService;
import pro.gravit.utils.helper.SecurityHelper;
public abstract class Request<R extends ResultInterface> implements RequestInterface {
public abstract class Request<R extends WebSocketEvent> implements WebSocketRequest {
private static long session = SecurityHelper.secureRandom.nextLong();
@LauncherNetworkAPI
public UUID requestUUID = UUID.randomUUID();

View file

@ -1,5 +1,5 @@
package pro.gravit.launcher.request;
public interface ResultInterface {
public interface WebSocketEvent {
String getType();
}

View file

@ -3,9 +3,9 @@
import pro.gravit.launcher.LauncherAPI;
import pro.gravit.launcher.events.request.ExecCommandRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
public class ExecCommandRequest extends Request<ExecCommandRequestEvent> implements RequestInterface {
public class ExecCommandRequest extends Request<ExecCommandRequestEvent> implements WebSocketRequest {
@LauncherAPI
public String cmd;

View file

@ -1,15 +1,15 @@
package pro.gravit.launcher.request.admin;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.ResultInterface;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.WebSocketEvent;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
public class ProxyRequest extends Request<ResultInterface> implements RequestInterface {
public RequestInterface response;
public class ProxyRequest extends Request<WebSocketEvent> implements WebSocketRequest {
public WebSocketRequest response;
public long session;
public boolean isCheckSign;
public ProxyRequest(RequestInterface response, long session) {
public ProxyRequest(WebSocketRequest response, long session) {
this.response = response;
this.session = session;
}

View file

@ -5,10 +5,10 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.request.AuthRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.utils.helper.VerifyHelper;
public final class AuthRequest extends Request<AuthRequestEvent> implements RequestInterface {
public final class AuthRequest extends Request<AuthRequestEvent> implements WebSocketRequest {
@LauncherNetworkAPI
private final String login;
@LauncherNetworkAPI

View file

@ -4,10 +4,10 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.request.CheckServerRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.utils.helper.VerifyHelper;
public final class CheckServerRequest extends Request<CheckServerRequestEvent> implements RequestInterface {
public final class CheckServerRequest extends Request<CheckServerRequestEvent> implements WebSocketRequest {
@LauncherNetworkAPI
private final String username;
@LauncherNetworkAPI

View file

@ -2,9 +2,9 @@
import pro.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
public class GetAvailabilityAuthRequest extends Request<GetAvailabilityAuthRequestEvent> implements RequestInterface {
public class GetAvailabilityAuthRequest extends Request<GetAvailabilityAuthRequestEvent> implements WebSocketRequest {
@Override
public String getType() {

View file

@ -4,11 +4,11 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.request.JoinServerRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.utils.helper.SecurityHelper;
import pro.gravit.utils.helper.VerifyHelper;
public final class JoinServerRequest extends Request<JoinServerRequestEvent> implements RequestInterface {
public final class JoinServerRequest extends Request<JoinServerRequestEvent> implements WebSocketRequest {
// Instance
@LauncherNetworkAPI

View file

@ -3,9 +3,9 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.request.RestoreSessionRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
public class RestoreSessionRequest extends Request<RestoreSessionRequestEvent> implements RequestInterface {
public class RestoreSessionRequest extends Request<RestoreSessionRequestEvent> implements WebSocketRequest {
@LauncherNetworkAPI
public long session;

View file

@ -4,9 +4,9 @@
import pro.gravit.launcher.events.request.SetProfileRequestEvent;
import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
public class SetProfileRequest extends Request<SetProfileRequestEvent> implements RequestInterface {
public class SetProfileRequest extends Request<SetProfileRequestEvent> implements WebSocketRequest {
@LauncherNetworkAPI
public String client;

View file

@ -11,14 +11,14 @@
import pro.gravit.launcher.downloader.ListDownloader;
import pro.gravit.launcher.events.request.LauncherRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.launcher.request.websockets.StandartClientWebSocketService;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.JVMHelper;
import pro.gravit.utils.helper.LogHelper;
import pro.gravit.utils.helper.SecurityHelper;
public final class LauncherRequest extends Request<LauncherRequestEvent> implements RequestInterface {
public final class LauncherRequest extends Request<LauncherRequestEvent> implements WebSocketRequest {
@LauncherNetworkAPI
public byte[] digest;
@LauncherNetworkAPI

View file

@ -2,9 +2,9 @@
import pro.gravit.launcher.events.request.ProfilesRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
public final class ProfilesRequest extends Request<ProfilesRequestEvent> implements RequestInterface {
public final class ProfilesRequest extends Request<ProfilesRequestEvent> implements WebSocketRequest {
@Override
public String getType() {

View file

@ -2,9 +2,9 @@
import pro.gravit.launcher.events.request.UpdateListRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
public final class UpdateListRequest extends Request<UpdateListRequestEvent> implements RequestInterface {
public final class UpdateListRequest extends Request<UpdateListRequestEvent> implements WebSocketRequest {
@Override
public String getType() {

View file

@ -21,12 +21,12 @@
import pro.gravit.launcher.hasher.HashedFile;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.update.UpdateRequest.State.Callback;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.launcher.request.websockets.StandartClientWebSocketService;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
public final class UpdateRequest extends Request<UpdateRequestEvent> implements RequestInterface {
public final class UpdateRequest extends Request<UpdateRequestEvent> implements WebSocketRequest {
public interface UpdateController {
void preUpdate(UpdateRequest request, UpdateRequestEvent e) throws IOException;

View file

@ -6,12 +6,12 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.request.BatchProfileByUsernameRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.launcher.serialize.SerializeLimits;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.VerifyHelper;
public final class BatchProfileByUsernameRequest extends Request<BatchProfileByUsernameRequestEvent> implements RequestInterface {
public final class BatchProfileByUsernameRequest extends Request<BatchProfileByUsernameRequestEvent> implements WebSocketRequest {
class Entry {
@LauncherNetworkAPI
String username;

View file

@ -7,9 +7,9 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.request.ProfileByUUIDRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
public final class ProfileByUUIDRequest extends Request<ProfileByUUIDRequestEvent> implements RequestInterface {
public final class ProfileByUUIDRequest extends Request<ProfileByUUIDRequestEvent> implements WebSocketRequest {
@LauncherNetworkAPI
private final UUID uuid;

View file

@ -4,10 +4,10 @@
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.request.ProfileByUsernameRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.RequestInterface;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.utils.helper.VerifyHelper;
public final class ProfileByUsernameRequest extends Request<ProfileByUsernameRequestEvent> implements RequestInterface {
public final class ProfileByUsernameRequest extends Request<ProfileByUsernameRequestEvent> implements WebSocketRequest {
@LauncherNetworkAPI
private final String username;

View file

@ -15,7 +15,7 @@
import pro.gravit.launcher.events.request.*;
import pro.gravit.launcher.hasher.HashedEntry;
import pro.gravit.launcher.hasher.HashedEntryAdapter;
import pro.gravit.launcher.request.ResultInterface;
import pro.gravit.launcher.request.WebSocketEvent;
import pro.gravit.utils.helper.LogHelper;
public class ClientWebSocketService extends ClientJSONPoint {
@ -24,8 +24,8 @@ public class ClientWebSocketService extends ClientJSONPoint {
public OnCloseCallback onCloseCallback;
public final Boolean onConnect;
public ReconnectCallback reconnectCallback;
private HashMap<String, Class<? extends RequestInterface>> requests;
private HashMap<String, Class<? extends ResultInterface>> results;
private HashMap<String, Class<? extends WebSocketRequest>> requests;
private HashMap<String, Class<? extends WebSocketEvent>> results;
private HashSet<EventHandler> handlers;
public ClientWebSocketService(GsonBuilder gsonBuilder, String address, int i) throws SSLException {
@ -34,8 +34,8 @@ public ClientWebSocketService(GsonBuilder gsonBuilder, String address, int i) th
results = new HashMap<>();
handlers = new HashSet<>();
this.gsonBuilder = gsonBuilder;
this.gsonBuilder.registerTypeAdapter(RequestInterface.class, new JsonRequestAdapter(this));
this.gsonBuilder.registerTypeAdapter(ResultInterface.class, new JsonResultAdapter(this));
this.gsonBuilder.registerTypeAdapter(WebSocketRequest.class, new JsonRequestAdapter(this));
this.gsonBuilder.registerTypeAdapter(WebSocketEvent.class, new JsonResultAdapter(this));
this.gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter());
this.gson = gsonBuilder.create();
this.onConnect = true;
@ -53,7 +53,7 @@ private static URI createURL(String address) {
@Override
void onMessage(String message) {
ResultInterface result = gson.fromJson(message, ResultInterface.class);
WebSocketEvent result = gson.fromJson(message, WebSocketEvent.class);
for (EventHandler handler : handlers) {
handler.process(result);
}
@ -81,15 +81,15 @@ public interface ReconnectCallback {
void onReconnect() throws IOException;
}
public Class<? extends RequestInterface> getRequestClass(String key) {
public Class<? extends WebSocketRequest> getRequestClass(String key) {
return requests.get(key);
}
public Class<? extends ResultInterface> getResultClass(String key) {
public Class<? extends WebSocketEvent> getResultClass(String key) {
return results.get(key);
}
public void registerRequest(String key, Class<? extends RequestInterface> clazz) {
public void registerRequest(String key, Class<? extends WebSocketRequest> clazz) {
requests.put(key, clazz);
}
@ -97,7 +97,7 @@ public void registerRequests() {
}
public void registerResult(String key, Class<? extends ResultInterface> clazz) {
public void registerResult(String key, Class<? extends WebSocketEvent> clazz) {
results.put(key, clazz);
}
@ -148,7 +148,7 @@ public void sendObject(Object obj) throws IOException {
if (ch == null || !ch.isActive()) reconnectCallback.onReconnect();
//if(isClosed() && reconnectCallback != null)
// reconnectCallback.onReconnect();
send(gson.toJson(obj, RequestInterface.class));
send(gson.toJson(obj, WebSocketRequest.class));
}
public void sendObject(Object obj, Type type) throws IOException {
@ -161,6 +161,6 @@ public void sendObject(Object obj, Type type) throws IOException {
@FunctionalInterface
public interface EventHandler {
void process(ResultInterface resultInterface);
void process(WebSocketEvent webSocketEvent);
}
}

View file

@ -13,7 +13,7 @@
import pro.gravit.utils.helper.LogHelper;
public class JsonRequestAdapter implements JsonSerializer<RequestInterface>, JsonDeserializer<RequestInterface> {
public class JsonRequestAdapter implements JsonSerializer<WebSocketRequest>, JsonDeserializer<WebSocketRequest> {
private final ClientWebSocketService service;
private static final String PROP_NAME = "type";
@ -22,19 +22,19 @@ public JsonRequestAdapter(ClientWebSocketService service) {
}
@Override
public RequestInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
public WebSocketRequest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class<? extends RequestInterface> cls = service.getRequestClass(typename);
Class<? extends WebSocketRequest> cls = service.getRequestClass(typename);
if (cls == null) {
LogHelper.error("Request type %s not found", typename);
}
return (RequestInterface) context.deserialize(json, cls);
return (WebSocketRequest) context.deserialize(json, cls);
}
@Override
public JsonElement serialize(RequestInterface src, Type typeOfSrc, JsonSerializationContext context) {
public JsonElement serialize(WebSocketRequest src, Type typeOfSrc, JsonSerializationContext context) {
// note : won't work, you must delegate this
JsonObject jo = context.serialize(src).getAsJsonObject();

View file

@ -11,10 +11,10 @@
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import pro.gravit.launcher.request.ResultInterface;
import pro.gravit.launcher.request.WebSocketEvent;
import pro.gravit.utils.helper.LogHelper;
public class JsonResultAdapter implements JsonSerializer<ResultInterface>, JsonDeserializer<ResultInterface> {
public class JsonResultAdapter implements JsonSerializer<WebSocketEvent>, JsonDeserializer<WebSocketEvent> {
private final ClientWebSocketService service;
private static final String PROP_NAME = "type";
@ -23,19 +23,19 @@ public JsonResultAdapter(ClientWebSocketService service) {
}
@Override
public ResultInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
public WebSocketEvent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class<? extends ResultInterface> cls = service.getResultClass(typename);
Class<? extends WebSocketEvent> cls = service.getResultClass(typename);
if (cls == null) {
LogHelper.error("Result type %s not found", typename);
}
return (ResultInterface) context.deserialize(json, cls);
return (WebSocketEvent) context.deserialize(json, cls);
}
@Override
public JsonElement serialize(ResultInterface src, Type typeOfSrc, JsonSerializationContext context) {
public JsonElement serialize(WebSocketEvent src, Type typeOfSrc, JsonSerializationContext context) {
// note : won't work, you must delegate this
JsonObject jo = context.serialize(src).getAsJsonObject();

View file

@ -12,7 +12,7 @@
import pro.gravit.launcher.events.request.ErrorRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.RequestException;
import pro.gravit.launcher.request.ResultInterface;
import pro.gravit.launcher.request.WebSocketEvent;
import pro.gravit.utils.helper.CommonHelper;
import pro.gravit.utils.helper.JVMHelper;
import pro.gravit.utils.helper.LogHelper;
@ -24,12 +24,12 @@ public StandartClientWebSocketService(GsonBuilder gsonBuilder, String address, i
super(gsonBuilder, address, i);
}
public class RequestFuture implements Future<ResultInterface> {
public class RequestFuture implements Future<WebSocketEvent> {
public final WaitEventHandler.ResultEvent event;
public boolean isCanceled = false;
@SuppressWarnings("rawtypes")
public RequestFuture(RequestInterface request) throws IOException {
public RequestFuture(WebSocketRequest request) throws IOException {
event = new WaitEventHandler.ResultEvent();
event.type = request.getType();
if (request instanceof Request) {
@ -57,14 +57,14 @@ public boolean isDone() {
}
@Override
public ResultInterface get() throws InterruptedException, ExecutionException {
public WebSocketEvent get() throws InterruptedException, ExecutionException {
if (isCanceled) return null;
while (!event.ready) {
synchronized (event) {
event.wait();
}
}
ResultInterface result = event.result;
WebSocketEvent result = event.result;
waitEventHandler.requests.remove(event);
if (event.result.getType().equals("error") || event.result.getType().equals("exception")) {
ErrorRequestEvent errorRequestEvent = (ErrorRequestEvent) event.result;
@ -74,14 +74,14 @@ public ResultInterface get() throws InterruptedException, ExecutionException {
}
@Override
public ResultInterface get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException {
public WebSocketEvent get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException {
if (isCanceled) return null;
while (!event.ready) {
synchronized (event) {
event.wait(timeout);
}
}
ResultInterface result = event.result;
WebSocketEvent result = event.result;
waitEventHandler.requests.remove(event);
if (event.result.getType().equals("error") || event.result.getType().equals("exception")) {
ErrorRequestEvent errorRequestEvent = (ErrorRequestEvent) event.result;
@ -91,9 +91,9 @@ public ResultInterface get(long timeout, TimeUnit unit) throws InterruptedExcept
}
}
public ResultInterface sendRequest(RequestInterface request) throws IOException, InterruptedException {
public WebSocketEvent sendRequest(WebSocketRequest request) throws IOException, InterruptedException {
RequestFuture future = new RequestFuture(request);
ResultInterface result;
WebSocketEvent result;
try {
result = future.get();
} catch (ExecutionException e) {
@ -102,7 +102,7 @@ public ResultInterface sendRequest(RequestInterface request) throws IOException,
return result;
}
public RequestFuture asyncSendRequest(RequestInterface request) throws IOException {
public RequestFuture asyncSendRequest(WebSocketRequest request) throws IOException {
return new RequestFuture(request);
}

View file

@ -5,14 +5,14 @@
import java.util.concurrent.ConcurrentHashMap;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.request.ResultInterface;
import pro.gravit.launcher.request.WebSocketEvent;
import pro.gravit.utils.helper.LogHelper;
public class WaitEventHandler implements ClientWebSocketService.EventHandler {
public Set<ResultEvent> requests = ConcurrentHashMap.newKeySet();
@Override
public void process(ResultInterface result) {
public void process(WebSocketEvent result) {
LogHelper.debug("Processing event %s type", result.getType());
UUID checkUUID = null;
if (result instanceof RequestEvent) {
@ -36,7 +36,7 @@ public void process(ResultInterface result) {
}
public static class ResultEvent {
public ResultInterface result;
public WebSocketEvent result;
public UUID uuid;
public String type;
public boolean ready;

View file

@ -1,5 +1,5 @@
package pro.gravit.launcher.request.websockets;
public interface RequestInterface {
public interface WebSocketRequest {
String getType();
}