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

102 lines
3.6 KiB
Java
Raw Normal View History

2018-09-17 10:07:32 +03:00
package ru.gravit.launchserver.response;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import ru.gravit.launchserver.response.auth.*;
import ru.gravit.launchserver.response.update.*;
2018-09-17 10:20:34 +03:00
import ru.gravit.utils.helper.LogHelper;
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.response.profile.BatchProfileByUsernameResponse;
import ru.gravit.launchserver.response.profile.ProfileByUUIDResponse;
import ru.gravit.launchserver.response.profile.ProfileByUsernameResponse;
public abstract class Response {
@FunctionalInterface
public interface Factory<R> {
2018-10-13 11:01:10 +03:00
2018-09-17 10:07:32 +03:00
Response newResponse(LaunchServer server, long id, HInput input, HOutput output, String ip);
}
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
2018-09-17 10:07:32 +03:00
public static Response getResponse(int type, LaunchServer server, long session, HInput input, HOutput output, String ip) {
return RESPONSES.get(type).newResponse(server, session, input, output, ip);
}
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.AUTH.getNumber(), AuthResponse::new);
registerResponse(RequestType.CHECK_SERVER.getNumber(), CheckServerResponse::new);
registerResponse(RequestType.JOIN_SERVER.getNumber(), JoinServerResponse::new);
registerResponse(RequestType.BATCH_PROFILE_BY_USERNAME.getNumber(), BatchProfileByUsernameResponse::new);
registerResponse(RequestType.PROFILE_BY_USERNAME.getNumber(), ProfileByUsernameResponse::new);
registerResponse(RequestType.PROFILE_BY_UUID.getNumber(), ProfileByUUIDResponse::new);
registerResponse(RequestType.LEGACYLAUNCHER.getNumber(), LegacyLauncherResponse::new);
2018-09-17 10:07:32 +03:00
registerResponse(RequestType.LAUNCHER.getNumber(), LauncherResponse::new);
registerResponse(RequestType.UPDATE_LIST.getNumber(), UpdateListResponse::new);
registerResponse(RequestType.UPDATE.getNumber(), UpdateResponse::new);
registerResponse(RequestType.PROFILES.getNumber(), ProfilesResponse::new);
2018-09-27 00:18:26 +03:00
registerResponse(RequestType.SERVERAUTH.getNumber(), AuthServerResponse::new);
registerResponse(RequestType.SETPROFILE.getNumber(), SetProfileResponse::new);
2018-09-17 10:07:32 +03:00
}
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;
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) {
this.server = server;
this.input = input;
this.output = output;
this.ip = ip;
this.session = session;
}
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
2018-09-17 10:07:32 +03:00
@SuppressWarnings("MethodMayBeStatic") // Intentionally not static
protected final void writeNoError(HOutput output) throws IOException {
output.writeString("", 0);
}
}