Launcher/LaunchServer/src/main/java/ru/gravit/launchserver/legacy/Response.java

89 lines
2.6 KiB
Java
Raw Normal View History

package ru.gravit.launchserver.legacy;
2018-09-17 10:07:32 +03:00
import ru.gravit.launcher.request.RequestException;
import ru.gravit.launcher.request.RequestType;
import ru.gravit.launcher.serialize.HInput;
import ru.gravit.launcher.serialize.HOutput;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.legacy.update.LauncherResponse;
import ru.gravit.launchserver.legacy.update.LegacyLauncherResponse;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.utils.helper.LogHelper;
2018-09-17 10:07:32 +03:00
2019-01-15 06:35:39 +03:00
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
2018-09-17 10:07:32 +03:00
public abstract class Response {
@FunctionalInterface
public interface Factory<R> {
2018-10-13 11:01:10 +03:00
Response newResponse(LaunchServer server, long id, HInput input, HOutput output, String ip, Client clientData);
2018-09-17 10:07:32 +03:00
}
2018-09-22 17:33:00 +03:00
2018-09-17 10:07:32 +03:00
private static final Map<Integer, Factory<?>> RESPONSES = new ConcurrentHashMap<>(8);
2018-09-22 17:33:00 +03:00
public static Response getResponse(int type, LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
return RESPONSES.get(type).newResponse(server, session, input, output, ip, clientData);
2018-09-17 10:07:32 +03:00
}
2018-09-22 17:33:00 +03:00
2018-09-17 10:07:32 +03:00
public static void registerResponse(int type, Factory<?> factory) {
RESPONSES.put(type, factory);
}
2018-09-22 17:33:00 +03:00
2018-09-17 10:07:32 +03:00
public static void registerResponses() {
registerResponse(RequestType.PING.getNumber(), PingResponse::new);
registerResponse(RequestType.LEGACYLAUNCHER.getNumber(), LegacyLauncherResponse::new);
2018-09-17 10:07:32 +03:00
registerResponse(RequestType.LAUNCHER.getNumber(), LauncherResponse::new);
}
2018-09-22 17:33:00 +03:00
2018-10-13 11:01:10 +03:00
2018-09-17 10:07:32 +03:00
public static void requestError(String message) throws RequestException {
throw new RequestException(message);
}
2018-10-13 11:01:10 +03:00
2018-09-17 10:07:32 +03:00
protected final LaunchServer server;
2018-10-13 11:01:10 +03:00
2018-09-17 10:07:32 +03:00
protected final HInput input;
2018-10-13 11:01:10 +03:00
2018-09-17 10:07:32 +03:00
protected final HOutput output;
2018-10-13 11:01:10 +03:00
2018-09-17 10:07:32 +03:00
protected final String ip;
protected final Client clientData;
2018-10-13 11:01:10 +03:00
2018-09-17 10:07:32 +03:00
protected final long session;
protected Response(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
2018-09-17 10:07:32 +03:00
this.server = server;
this.input = input;
this.output = output;
this.ip = ip;
this.session = session;
this.clientData = clientData;
2018-09-17 10:07:32 +03:00
}
2018-10-13 11:01:10 +03:00
2018-09-17 10:07:32 +03:00
protected final void debug(String message) {
LogHelper.subDebug("#%d %s", session, message);
}
2018-10-13 11:01:10 +03:00
2018-09-17 10:07:32 +03:00
protected final void debug(String message, Object... args) {
debug(String.format(message, args));
}
2018-10-13 11:01:10 +03:00
2018-09-17 10:07:32 +03:00
public abstract void reply() throws Exception;
2018-10-13 11:01:10 +03:00
2019-05-15 14:09:32 +03:00
protected static void writeNoError(HOutput output) throws IOException {
2018-09-17 10:07:32 +03:00
output.writeString("", 0);
}
}