[FEATURE] Новые фитчи MirrorManager

This commit is contained in:
Gravit 2019-09-30 10:43:41 +07:00
parent 8e1000ec41
commit 81f50a57f3
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
4 changed files with 101 additions and 28 deletions

View file

@ -30,7 +30,8 @@ public String getUsageDescription() {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 2);
Version version = Version.byName(args[0]);
//Version version = Version.byName(args[0]);
String versionName = args[0];
String dirName = IOHelper.verifyFileName(args[1]);
Path assetDir = server.updatesDir.resolve(dirName);
@ -40,7 +41,8 @@ public void invoke(String... args) throws Exception {
// Download required asset
LogHelper.subInfo("Downloading asset, it may take some time");
HttpDownloader.downloadZip(server.mirrorManager.getDefaultMirror().getAssetsURL(version.name), assetDir);
//HttpDownloader.downloadZip(server.mirrorManager.getDefaultMirror().getAssetsURL(version.name), assetDir);
server.mirrorManager.downloadZip(assetDir,"assets/%s.zip", versionName);
// Finished
server.syncUpdatesDir(Collections.singleton(dirName));

View file

@ -7,6 +7,7 @@
import java.nio.file.Path;
import java.util.Collections;
import com.google.gson.JsonElement;
import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.launcher.profiles.ClientProfile.Version;
@ -36,7 +37,8 @@ public String getUsageDescription() {
@Override
public void invoke(String... args) throws IOException, CommandException {
verifyArgs(args, 2);
Version version = Version.byName(args[0]);
//Version version = Version.byName(args[0]);
String versionName = args[0];
String dirName = IOHelper.verifyFileName(args[1]);
Path clientDir = server.updatesDir.resolve(args[1]);
@ -46,14 +48,19 @@ public void invoke(String... args) throws IOException, CommandException {
// Download required client
LogHelper.subInfo("Downloading client, it may take some time");
HttpDownloader.downloadZip(server.mirrorManager.getDefaultMirror().getClientsURL(version.name), clientDir);
//HttpDownloader.downloadZip(server.mirrorManager.getDefaultMirror().getClientsURL(version.name), clientDir);
server.mirrorManager.downloadZip(clientDir,"clients/%s.zip", versionName);
// Create profile file
LogHelper.subInfo("Creaing profile file: '%s'", dirName);
ClientProfile client;
String profilePath = String.format("pro/gravit/launchserver/defaults/profile%s.cfg", version.name);
String profilePath = String.format("pro/gravit/launchserver/defaults/profile%s.cfg", versionName);
try (BufferedReader reader = IOHelper.newReader(IOHelper.getResourceURL(profilePath))) {
client = Launcher.gsonManager.configGson.fromJson(reader, ClientProfile.class);
} catch (IOException e)
{
JsonElement clientJson = server.mirrorManager.jsonRequest(null, "GET", "clients/%s.json", versionName);
client = Launcher.gsonManager.configGson.fromJson(clientJson, ClientProfile.class);
}
client.setTitle(dirName);
client.setDir(dirName);

View file

@ -1,33 +1,36 @@
package pro.gravit.launchserver.manangers;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import com.google.gson.JsonElement;
import pro.gravit.utils.HTTPRequest;
import pro.gravit.utils.HttpDownloader;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
public class MirrorManager {
public class Mirror {
URL url;
String assetsURLMask;
String clientsURLMask;
public static class Mirror {
String baseUrl;
boolean enabled;
Mirror(String url) {
assetsURLMask = url.concat("assets/%s.zip");
clientsURLMask = url.concat("clients/%s.zip");
//assetsURLMask = url.concat("assets/%s.zip");
//clientsURLMask = url.concat("clients/%s.zip");
baseUrl = url;
}
private URL formatArg(String mask, String arg) throws MalformedURLException {
return new URL(String.format(mask, IOHelper.urlEncode(arg)));
private URL formatArgs(String mask, Object... args) throws MalformedURLException {
Object[] data = Arrays.stream(args).map(e -> IOHelper.urlEncode(e.toString())).toArray();
return new URL(baseUrl.concat(String.format(mask, data)));
}
public URL getAssetsURL(String assets) throws MalformedURLException {
return formatArg(assetsURLMask, assets);
}
public URL getClientsURL(String client) throws MalformedURLException {
return formatArg(clientsURLMask, client);
public URL getURL(String mask, Object... args) throws MalformedURLException {
return formatArgs(mask, args);
}
}
@ -38,13 +41,14 @@ public void addMirror(String mirror) {
Mirror m = new Mirror(mirror);
m.enabled = true;
if (defaultMirror == null) defaultMirror = m;
list.add(m);
}
public void addMirror(String mirror, boolean enabled) throws MalformedURLException {
Mirror m = new Mirror(mirror);
m.url = new URL(mirror);
m.enabled = enabled;
if (defaultMirror == null && enabled) defaultMirror = m;
list.add(m);
}
public Mirror getDefaultMirror() {
@ -66,4 +70,61 @@ public void enableMirror(int index) {
public int size() {
return list.size();
}
public boolean downloadZip(Mirror mirror, Path path, String mask, Object... args) throws IOException
{
if(!mirror.enabled) return false;
URL url = mirror.getURL(mask, args);
LogHelper.debug("Try download %s", url.toString());
try {
HttpDownloader.downloadZip(url, path);
} catch (IOException e)
{
LogHelper.error("Download %s failed(%s: %s)", url.toString(), e.getClass().getName(), e.getMessage());
return false;
}
return true;
}
public void downloadZip(Path path, String mask, Object... args) throws IOException
{
if(downloadZip(defaultMirror, path, mask, args))
{
return;
}
for(Mirror mirror : list)
{
if(mirror != defaultMirror)
{
if(downloadZip(mirror, path, mask, args)) return;
}
}
throw new IOException(String.format("Error download %s. All mirrors return error", path.toString()));
}
public JsonElement jsonRequest(Mirror mirror, JsonElement request, String method, String mask, Object... args) throws IOException
{
if(!mirror.enabled) return null;
URL url = mirror.getURL(mask, args);
try {
return HTTPRequest.jsonRequest(request, method, url);
} catch (IOException e)
{
LogHelper.error("JsonRequest %s failed(%s: %s)", url.toString(), e.getClass().getName(), e.getMessage());
return null;
}
}
public JsonElement jsonRequest(JsonElement request, String method, String mask, Object... args) throws IOException
{
JsonElement result = jsonRequest(defaultMirror, request, method, mask, args);
if(result != null) return result;
for(Mirror mirror : list)
{
if(mirror != defaultMirror)
{
result = jsonRequest(mirror, request, method, mask, args);
if(result != null) return result;
}
}
throw new IOException("Error jsonRequest. All mirrors return error");
}
}

View file

@ -39,19 +39,22 @@ public static int sendCrashreport(String strurl, String data) throws IOException
}
public static JsonElement jsonRequest(JsonElement request, URL url) throws IOException {
return jsonRequest(request, "POST", url);
}
public static JsonElement jsonRequest(JsonElement request, String method, URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
if(request != null) connection.setDoOutput(true);
connection.setRequestMethod(method);
if(request != null) connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("Accept", "application/json");
if (TIMEOUT > 0)
connection.setConnectTimeout(TIMEOUT);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), Charset.forName("UTF-8"));
writer.write(request.toString());
writer.flush();
writer.close();
if(request != null) try (OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8)) {
writer.write(request.toString());
writer.flush();
}
InputStreamReader reader;
int statusCode = connection.getResponseCode();