2019-04-04 15:10:28 +03:00
|
|
|
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;
|
2019-04-04 15:10:28 +03:00
|
|
|
import ru.gravit.launchserver.legacy.update.LauncherResponse;
|
|
|
|
import ru.gravit.launchserver.legacy.update.LegacyLauncherResponse;
|
2019-03-12 11:05:41 +03:00
|
|
|
import ru.gravit.launchserver.socket.Client;
|
2018-12-06 05:29:34 +03:00
|
|
|
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
|
|
|
|
2019-03-12 11:05:41 +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
|
|
|
|
2019-03-12 11:05:41 +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);
|
2018-10-25 15:36:57 +03:00
|
|
|
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;
|
|
|
|
|
2019-03-12 11:05:41 +03:00
|
|
|
protected final Client clientData;
|
|
|
|
|
2018-10-13 11:01:10 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
protected final long session;
|
|
|
|
|
2019-03-12 11:05:41 +03:00
|
|
|
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;
|
2019-03-12 11:05:41 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|