|
|
@ -84,8 +84,9 @@ public class ClientDownloadUDP implements Runnable {
|
|
|
|
purgeList();
|
|
|
|
purgeList();
|
|
|
|
initThreads();
|
|
|
|
initThreads();
|
|
|
|
while(!stop) {
|
|
|
|
while(!stop) {
|
|
|
|
checkTasksStatus();
|
|
|
|
|
|
|
|
assignTasks();
|
|
|
|
assignTasks();
|
|
|
|
|
|
|
|
checkTasksStatus();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.err.println("Reassembling file parts.");
|
|
|
|
System.err.println("Reassembling file parts.");
|
|
|
@ -101,7 +102,7 @@ public class ClientDownloadUDP implements Runnable {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private void initThreads() {
|
|
|
|
private void initThreads() {
|
|
|
|
for(HostItem hostItem: hostList) {
|
|
|
|
for(HostItem hostItem: hostList) {
|
|
|
|
sockList.add(new ClientDownloadPartUDP(filename, hostItem.getUDPSocket(), partsSubdir));
|
|
|
|
sockList.add(new ClientDownloadPartUDP(this, filename, hostItem.getUDPSocket(), partsSubdir));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(ClientDownloadPartUDP c: sockList) {
|
|
|
|
for(ClientDownloadPartUDP c: sockList) {
|
|
|
|
Thread t = new Thread(c);
|
|
|
|
Thread t = new Thread(c);
|
|
|
@ -114,25 +115,32 @@ public class ClientDownloadUDP implements Runnable {
|
|
|
|
* @throws InternalError
|
|
|
|
* @throws InternalError
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private void checkTasksStatus() throws InternalError {
|
|
|
|
private void checkTasksStatus() throws InternalError {
|
|
|
|
List<ClientDownloadPartUDP> sockListCpy = new ArrayList<>(sockList);
|
|
|
|
try {
|
|
|
|
for(ClientDownloadPartUDP c: sockListCpy) {
|
|
|
|
synchronized(this) {
|
|
|
|
if (c.hasFailed() == true) {
|
|
|
|
this.wait();
|
|
|
|
sockList.remove(c);
|
|
|
|
List<ClientDownloadPartUDP> sockListCpy = new ArrayList<>(sockList);
|
|
|
|
offsetsPending.removeAll(c.getFailed());
|
|
|
|
for(ClientDownloadPartUDP c: sockListCpy) {
|
|
|
|
offsetsToAsk.addAll(c.getFailed());
|
|
|
|
if (c.hasFailed() == true) {
|
|
|
|
}
|
|
|
|
sockList.remove(c);
|
|
|
|
try {
|
|
|
|
offsetsPending.removeAll(c.getFailed());
|
|
|
|
offsetsPending.removeAll(c.getDone());
|
|
|
|
offsetsToAsk.addAll(c.getFailed());
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
}
|
|
|
|
throw new InternalError();
|
|
|
|
try {
|
|
|
|
|
|
|
|
offsetsPending.removeAll(c.getDone());
|
|
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
|
|
|
throw new InternalError();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
System.err.println("Task check status: " + offsetsToAsk.size() + " to asks, " + offsetsPending.size() + " pending");
|
|
|
|
|
|
|
|
if (offsetsToAsk.isEmpty() && offsetsPending.isEmpty()) {
|
|
|
|
|
|
|
|
stop = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sockList.size() == 0) {
|
|
|
|
|
|
|
|
System.err.println("No thread working");
|
|
|
|
|
|
|
|
throw new InternalError();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
System.err.println("Task check status: " + offsetsToAsk.size() + " to asks, " + offsetsPending.size() + " pending");
|
|
|
|
|
|
|
|
if (offsetsToAsk.isEmpty() && offsetsPending.isEmpty()) {
|
|
|
|
|
|
|
|
stop = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sockList.size() == 0) {
|
|
|
|
|
|
|
|
System.err.println("No thread working");
|
|
|
|
|
|
|
|
throw new InternalError();
|
|
|
|
throw new InternalError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|