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
@@ -55,7 +55,7 @@ public class Database {
public ProjectInfo getProjectInfo(int projectId) { public ProjectInfo getProjectInfo(int projectId) {
ResultSet rs = this.connector.executeRequest( ResultSet rs = this.connector.executeRequest(
"select slug, name, description from projects where projectid = " + projectId + " and type = 0"); "select slug, name, description from projects where projectid = " + projectId + " and type = 0");
try { try {
if (rs.next()) if (rs.next())
return new ProjectInfo(rs.getString("slug"), rs.getString("name"), rs.getString("description")); return new ProjectInfo(rs.getString("slug"), rs.getString("name"), rs.getString("description"));
@@ -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,25 +32,22 @@ 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)) {
if(line.startsWith("direct=")) Thread t = new UpdaterThread(line, db, directUpdateManager, curseUpdateManager, threads);
{ t.start();
handleDirectDownload(line); threads.add(t);
} else if (line.startsWith("del=")) { }
String[] delete = line.split("del=");
new File(delete[1]).delete(); while(Main.threadNb == threads.size()) {
} try {
else { Thread.sleep(10);
if (line.split("/").length >= 5) { } catch (InterruptedException e) {}
handleCurseDownload(line); }
}
}
}
} }
in.close(); in.close();
Log.i("Main", "finished"); Log.i("Main", "finished");
@@ -58,7 +58,49 @@ public class ModUpdater {
} }
} }
public void handleDirectDownload(String line) { 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="))
{
handleDirectDownload(line);
} else if (line.startsWith("del=")) {
String[] delete = line.split("del=");
new File(delete[1]).delete();
}
else {
if (line.split("/").length >= 5) {
handleCurseDownload(line);
}
}
super.run();
//on se retire de la pool d'execution
this.threads.remove(this);
}
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,20 +160,15 @@ public class ModUpdater {
} }
} }
public static boolean isAComment(String line)
{
return line.startsWith("//");
}
private static String extractSlugFromLink(String line) {
char[] chars = new char[line.length()-line.indexOf(";")-"direct=".length()+1];
line.getChars("direct=".length(), line.indexOf(";"), chars, 0);
return new String(chars);
}
private static String extractNameFromLink(String line) { private static String extractNameFromLink(String line) {
char[] chars = new char[line.indexOf("@")-line.indexOf(";")]; char[] chars = new char[line.indexOf("@")-line.indexOf(";")];
line.getChars(line.indexOf(";")+1, line.indexOf("@"), chars, 0); line.getChars(line.indexOf(";")+1, line.indexOf("@"), chars, 0);
return new String(chars); return new String(chars);
} }
private static String extractSlugFromLink(String line) {
char[] chars = new char[line.length()-line.indexOf(";")-"direct=".length()+1];
line.getChars("direct=".length(), line.indexOf(";"), chars, 0);
return new String(chars);
}
} }