threads ! :D

This commit is contained in:
marc barbier
2021-07-08 17:10:29 +02:00
parent 2a3c112b67
commit 3d9a44c99a
3 changed files with 69 additions and 30 deletions
@@ -20,6 +20,8 @@ public class Main {
checkModsDirectory(); checkModsDirectory();
new DatabaseVersionManager().updateIfNeeded(); new DatabaseVersionManager().updateIfNeeded();
interpretArgs(args); interpretArgs(args);
//no threading option called => only one thread will be used
if( threadNb == null)threadNb = 1;
new ModUpdater().update(); new ModUpdater().update();
} }
@@ -5,6 +5,9 @@ import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import downloader.forgeSvc.ForgeSvcFile; import downloader.forgeSvc.ForgeSvcFile;
import downloader.helper.HttpHelper; import downloader.helper.HttpHelper;
@@ -29,12 +32,57 @@ public class ModUpdater {
} }
//parse and download //parse and download
List<Thread> threads = new ArrayList<>(Main.threadNb);
try { try {
//TODO: add threads
BufferedReader in = new BufferedReader(new FileReader(modsList)); BufferedReader in = new BufferedReader(new FileReader(modsList));
while (in.ready()) { while (in.ready()) {
String line = in.readLine(); String line = in.readLine();
if (!isAComment(line)) { if (!isAComment(line)) {
Thread t = new UpdaterThread(line, db, directUpdateManager, curseUpdateManager, threads);
t.start();
threads.add(t);
}
while(Main.threadNb == threads.size()) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}
}
in.close();
Log.i("Main", "finished");
} catch( IOException e ) {
e.printStackTrace();
System.exit(1);
}
}
public static boolean isAComment(String line)
{
return line.startsWith("//");
}
}
final class UpdaterThread extends Thread {
private final String line;
private final Database db;
private final DirectUpdateManager directUpdateManager;
private final CurseUpdateManager curseUpdateManager;
private final List<Thread> threads;
public UpdaterThread(String line, Database db, DirectUpdateManager directUpdateManager, CurseUpdateManager curseUpdateManager, List<Thread> threads) {
super();
this.line = line;
this.db = db;
this.directUpdateManager = directUpdateManager;
this.curseUpdateManager = curseUpdateManager;
this.threads = threads;
}
@Override
public void run() {
if(line.startsWith("direct=")) if(line.startsWith("direct="))
{ {
handleDirectDownload(line); handleDirectDownload(line);
@@ -47,18 +95,12 @@ public class ModUpdater {
handleCurseDownload(line); handleCurseDownload(line);
} }
} }
} super.run();
} //on se retire de la pool d'execution
in.close(); this.threads.remove(this);
Log.i("Main", "finished");
} catch( IOException e ) {
e.printStackTrace();
System.exit(1);
}
} }
public void handleDirectDownload(String line) { private void handleDirectDownload(String line) {
String url = line.replaceFirst("direct=.*@", ""); String url = line.replaceFirst("direct=.*@", "");
String filename=extractNameFromLink(line); String filename=extractNameFromLink(line);
@@ -78,7 +120,7 @@ public class ModUpdater {
} }
} }
public void handleCurseDownload(String line) { private void 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);
@@ -118,9 +160,10 @@ public class ModUpdater {
} }
} }
public static boolean isAComment(String line) private static String extractNameFromLink(String line) {
{ char[] chars = new char[line.indexOf("@")-line.indexOf(";")];
return line.startsWith("//"); line.getChars(line.indexOf(";")+1, line.indexOf("@"), chars, 0);
return new String(chars);
} }
private static String extractSlugFromLink(String line) { private static String extractSlugFromLink(String line) {
@@ -128,10 +171,4 @@ public class ModUpdater {
line.getChars("direct=".length(), line.indexOf(";"), chars, 0); line.getChars("direct=".length(), line.indexOf(";"), chars, 0);
return new String(chars); return new String(chars);
} }
private static String extractNameFromLink(String line) {
char[] chars = new char[line.indexOf("@")-line.indexOf(";")];
line.getChars(line.indexOf(";")+1, line.indexOf("@"), chars, 0);
return new String(chars);
}
} }