nb thread fix + corruption check

This commit is contained in:
marc barbier
2021-07-09 19:26:04 +02:00
parent 47d9e8e392
commit b0c1519063
5 changed files with 56 additions and 18 deletions
+2 -2
View File
@@ -4,8 +4,8 @@ then
fi fi
-rm release-${VER}.zip 2> /dev/null -rm release-${VER}.zip 2> /dev/null
mv mcDownloader.jar ModPackDl.jar mv mcDownloader.jar ModPackDl.jar
echo "java -jar ModPackDl.jar -t -v" > run.sh echo "java -jar ModPackDl.jar --thread 2 -v" > run.sh
echo "java -jar ModPackDl.jar -t -v" > run.bat echo "java -jar ModPackDl.jar --thread 2 -v" > run.bat
echo "pause" >> run.bat echo "pause" >> run.bat
zip release-${VER}.zip ModPackDl.jar run.sh run.bat zip release-${VER}.zip ModPackDl.jar run.sh run.bat
rm run.sh rm run.sh
@@ -22,6 +22,7 @@ public class Main {
interpretArgs(args); interpretArgs(args);
//no threading option called => only one thread will be used //no threading option called => only one thread will be used
if( threadNb == null)threadNb = 1; if( threadNb == null)threadNb = 1;
Log.i("Main", "Running on " + threadNb + " thread");
new ModUpdater().update(); new ModUpdater().update();
} }
@@ -9,6 +9,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import downloader.forgeSvc.ForgeSvcFile; import downloader.forgeSvc.ForgeSvcFile;
import downloader.helper.ArchiveHelper;
import downloader.helper.HttpHelper; import downloader.helper.HttpHelper;
public class ModUpdater { public class ModUpdater {
@@ -61,6 +62,31 @@ public class ModUpdater {
Thread.sleep(100); Thread.sleep(100);
} catch (InterruptedException e) {} } catch (InterruptedException e) {}
} }
if(! UpdaterThread.failedLines.isEmpty()) {
List<String> failures = new ArrayList<>(UpdaterThread.failedLines);
UpdaterThread.failedLines.clear();
Log.e(ME, "filed to download " + failures.size());
Log.e(ME, "Retrying but slower");
for(String line : failures) {
Thread t = new UpdaterThread(line, db, directUpdateManager, curseUpdateManager, threads);
t.start();
try {
t.join();
} catch (InterruptedException e) {}
}
if(! UpdaterThread.failedLines.isEmpty()) {
Log.e(ME, "could not download the following mods : ");
for(String s: failures) {
Log.e(ME, " " + s);
}
}
}
Log.i(ME, "finished"); Log.i(ME, "finished");
curseUpdateManager.updateFile(); curseUpdateManager.updateFile();
} }
@@ -79,6 +105,7 @@ final class UpdaterThread extends Thread {
private final CurseUpdateManager curseUpdateManager; private final CurseUpdateManager curseUpdateManager;
private final List<Thread> threads; private final List<Thread> threads;
private static final String ME = "ModUpdaterThread"; private static final String ME = "ModUpdaterThread";
public static final List<String> failedLines = new ArrayList<>();
public UpdaterThread(String line, Database db, DirectUpdateManager directUpdateManager, CurseUpdateManager curseUpdateManager, List<Thread> threads) { public UpdaterThread(String line, Database db, DirectUpdateManager directUpdateManager, CurseUpdateManager curseUpdateManager, List<Thread> threads) {
super(line); super(line);
@@ -91,9 +118,11 @@ final class UpdaterThread extends Thread {
@Override @Override
public void run() { public void run() {
//used for post download checks
File downloadedFile = null;
if(line.startsWith("direct=")) if(line.startsWith("direct="))
{ {
handleDirectDownload(line); downloadedFile = handleDirectDownload(line);
} else if (line.startsWith("del=")) { } else if (line.startsWith("del=")) {
String[] delete = line.split("del="); String[] delete = line.split("del=");
if(new File("mods/" + delete[1]).delete()) { if(new File("mods/" + delete[1]).delete()) {
@@ -102,15 +131,21 @@ final class UpdaterThread extends Thread {
} }
else { else {
if (line.split("/").length >= 5) { if (line.split("/").length >= 5) {
handleCurseDownload(line); downloadedFile = handleCurseDownload(line);
} }
} }
if(downloadedFile != null && !ArchiveHelper.checkJarIntegrity(downloadedFile)) {
downloadedFile.delete();
failedLines.add(line);
}
super.run(); super.run();
//on se retire de la pool d'execution //on se retire de la pool d'execution
this.threads.remove(this); this.threads.remove(this);
} }
private void handleDirectDownload(String line) { private File handleDirectDownload(String line) {
String url = line.replaceFirst("direct=.*@", ""); String url = line.replaceFirst("direct=.*@", "");
String filename=extractNameFromLink(line); String filename=extractNameFromLink(line);
@@ -122,15 +157,16 @@ final class UpdaterThread extends Thread {
{ {
Log.i(ME,"downloading "+ filename); Log.i(ME,"downloading "+ filename);
try { try {
HttpHelper.readFileFromUrlToFolder(url,"mods",filename);
Log.i(ME,"done"); Log.i(ME,"done");
return HttpHelper.readFileFromUrlToFolder(url,"mods",filename);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
Log.e(ME, "could not update mod, error while downloading"); Log.e(ME, "could not update mod, error while downloading");
} }
} }
return null;
} }
private void handleCurseDownload(String line) { private File handleCurseDownload(String line) {
String name = line.split("/")[5]; String name = line.split("/")[5];
int modID = db.findModBySlug(name); int modID = db.findModBySlug(name);
ProjectInfo pj = db.getProjectInfo(modID); ProjectInfo pj = db.getProjectInfo(modID);
@@ -157,13 +193,14 @@ final class UpdaterThread extends Thread {
if (!upToDate) { if (!upToDate) {
Log.i("MOD", "downloading " + pj.getName().replace(" ", "-")); Log.i("MOD", "downloading " + pj.getName().replace(" ", "-"));
HttpHelper.readFileFromUrlToFolder(downloadUrl, "mods"); File downloaded = HttpHelper.readFileFromUrlToFolder(downloadUrl, "mods");
Log.i("MOD", "download finished"); Log.i("MOD", "download finished");
if (!this.curseUpdateManager.isModIdKnown(modID)) { if (!this.curseUpdateManager.isModIdKnown(modID)) {
this.curseUpdateManager.addModsToTheList(HttpHelper.getFileNameFromURL(downloadUrl), modID); this.curseUpdateManager.addModsToTheList(HttpHelper.getFileNameFromURL(downloadUrl), modID);
Log.i("DB", "added a mod to the registry"); Log.i("DB", "added a mod to the registry");
} }
return downloaded;
} }
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
e.printStackTrace(); e.printStackTrace();
@@ -171,6 +208,7 @@ final class UpdaterThread extends Thread {
} else { } else {
Log.e("DB", "no file in server"); Log.e("DB", "no file in server");
} }
return null;
} }
private static String extractNameFromLink(String line) { private static String extractNameFromLink(String line) {
@@ -121,7 +121,7 @@ public class HttpHelper {
int bytesRead = in.read(buffer); int bytesRead = in.read(buffer);
if (bytesRead < 0) if (bytesRead < 0)
break; break;
{
double elapsed = ( System.currentTimeMillis() - start ) / 1000; double elapsed = ( System.currentTimeMillis() - start ) / 1000;
if(elapsed != 0) { if(elapsed != 0) {
long estimatedBandwith = (long) (bytesRead / elapsed); long estimatedBandwith = (long) (bytesRead / elapsed);
@@ -129,7 +129,6 @@ public class HttpHelper {
Log.setStaticPrint(formatBandwidth(estimatedBandwith)); Log.setStaticPrint(formatBandwidth(estimatedBandwith));
start = System.currentTimeMillis(); start = System.currentTimeMillis();
} }
}
downloadingWriter.write(buffer, 0, bytesRead); downloadingWriter.write(buffer, 0, bytesRead);
} }
downloadingWriter.close(); downloadingWriter.close();