Improvement multithreads
All checks were successful
flavien's git/Projet_JAVA_P2P_STRI2A/pipeline/head This commit looks good
flavien's git/Projet_JAVA_P2P_STRI2A/pipeline/pr-master This commit looks good

This commit is contained in:
Louis Royer 2020-03-16 15:56:47 +01:00
parent 733767c4de
commit 4c7fcab67d
4 changed files with 46 additions and 25 deletions

View File

@ -41,13 +41,15 @@ public class ClientDownloadPartUDP implements Runnable {
private volatile boolean noTask;
private String partsSubdir;
private static final long MAX_PARTIAL_SIZE = 4096;
private ClientDownloadUDP manager;
/** Constructor with filename, socket, and part subdir
* @param filename name of file to download
* @param socket socket to use
* @param partsSubdir directory to store .part files
*/
public ClientDownloadPartUDP(String filename, DatagramSocket socket, String partsSubdir) {
public ClientDownloadPartUDP(ClientDownloadUDP manager, String filename, DatagramSocket socket, String partsSubdir) {
this.manager = manager;
this.partsSubdir = partsSubdir;
this.filename = filename;
this.socket = socket;
@ -80,9 +82,15 @@ public class ClientDownloadPartUDP implements Runnable {
while(!stop) {
try {
doTasks();
synchronized(manager) {
manager.notify();
}
} catch(InterruptedException e) {
try {
setStop();
synchronized(manager) {
manager.notify();
}
} catch (InterruptedException e2) {
}
}
@ -168,6 +176,7 @@ public class ClientDownloadPartUDP implements Runnable {
} catch (IndexOutOfBoundsException e) {
noTask = true;
}
}
}

View File

@ -84,8 +84,9 @@ public class ClientDownloadUDP implements Runnable {
purgeList();
initThreads();
while(!stop) {
checkTasksStatus();
assignTasks();
checkTasksStatus();
}
}
System.err.println("Reassembling file parts.");
@ -101,7 +102,7 @@ public class ClientDownloadUDP implements Runnable {
*/
private void initThreads() {
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) {
Thread t = new Thread(c);
@ -114,25 +115,32 @@ public class ClientDownloadUDP implements Runnable {
* @throws InternalError
*/
private void checkTasksStatus() throws InternalError {
List<ClientDownloadPartUDP> sockListCpy = new ArrayList<>(sockList);
for(ClientDownloadPartUDP c: sockListCpy) {
if (c.hasFailed() == true) {
sockList.remove(c);
offsetsPending.removeAll(c.getFailed());
offsetsToAsk.addAll(c.getFailed());
try {
synchronized(this) {
this.wait();
List<ClientDownloadPartUDP> sockListCpy = new ArrayList<>(sockList);
for(ClientDownloadPartUDP c: sockListCpy) {
if (c.hasFailed() == true) {
sockList.remove(c);
offsetsPending.removeAll(c.getFailed());
offsetsToAsk.addAll(c.getFailed());
}
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();
}
}
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");
} catch (InterruptedException e) {
throw new InternalError();
}
}

View File

@ -33,6 +33,7 @@ public class ClientP2P {
switch(transportchoosen){
case "UDP":
case "udp":
case "upd": // alias typo
case "2" :
System.out.println("Starting with UDP");
ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.hostList, c.directories.getDataHomeDirectory() + c.parts + "/");

View File

@ -11,16 +11,19 @@ public class ServerP2P {
private Logger logger;
public ServerP2P() {
directories = new Directories("P2P_JAVA_PROJECT_SERVER");
public ServerP2P(String portStr) {
port = Integer.valueOf(Integer.parseInt(portStr));
directories = new Directories("P2P_JAVA_PROJECT_SERVER_" + port);
directories.createSubdir(subdir);
logger = new Logger(directories.getDataHomeDirectory() + "server.log");
port = 40001;
System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory() + subdir);
directories.askOpenDataHomeDirectory(subdir);
}
public static void main(String [] args) {
ServerP2P s = new ServerP2P();
/* first arg must be port number
* run with: java -ea serverP2P.ServerP2P -- <portNumber>
* */
ServerP2P s = new ServerP2P(args[1]);
ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger);
ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger);
Thread tudp = new Thread(smudp);