Improvement multithreads
This commit is contained in:
parent
733767c4de
commit
4c7fcab67d
@ -41,13 +41,15 @@ public class ClientDownloadPartUDP implements Runnable {
|
|||||||
private volatile boolean noTask;
|
private volatile boolean noTask;
|
||||||
private String partsSubdir;
|
private String partsSubdir;
|
||||||
private static final long MAX_PARTIAL_SIZE = 4096;
|
private static final long MAX_PARTIAL_SIZE = 4096;
|
||||||
|
private ClientDownloadUDP manager;
|
||||||
|
|
||||||
/** Constructor with filename, socket, and part subdir
|
/** Constructor with filename, socket, and part subdir
|
||||||
* @param filename name of file to download
|
* @param filename name of file to download
|
||||||
* @param socket socket to use
|
* @param socket socket to use
|
||||||
* @param partsSubdir directory to store .part files
|
* @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.partsSubdir = partsSubdir;
|
||||||
this.filename = filename;
|
this.filename = filename;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
@ -80,9 +82,15 @@ public class ClientDownloadPartUDP implements Runnable {
|
|||||||
while(!stop) {
|
while(!stop) {
|
||||||
try {
|
try {
|
||||||
doTasks();
|
doTasks();
|
||||||
|
synchronized(manager) {
|
||||||
|
manager.notify();
|
||||||
|
}
|
||||||
} catch(InterruptedException e) {
|
} catch(InterruptedException e) {
|
||||||
try {
|
try {
|
||||||
setStop();
|
setStop();
|
||||||
|
synchronized(manager) {
|
||||||
|
manager.notify();
|
||||||
|
}
|
||||||
} catch (InterruptedException e2) {
|
} catch (InterruptedException e2) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -168,6 +176,7 @@ public class ClientDownloadPartUDP implements Runnable {
|
|||||||
} catch (IndexOutOfBoundsException e) {
|
} catch (IndexOutOfBoundsException e) {
|
||||||
noTask = true;
|
noTask = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
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 {
|
} catch (InterruptedException e) {
|
||||||
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();
|
throw new InternalError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,7 @@ public class ClientP2P {
|
|||||||
switch(transportchoosen){
|
switch(transportchoosen){
|
||||||
case "UDP":
|
case "UDP":
|
||||||
case "udp":
|
case "udp":
|
||||||
|
case "upd": // alias typo
|
||||||
case "2" :
|
case "2" :
|
||||||
System.out.println("Starting with UDP");
|
System.out.println("Starting with UDP");
|
||||||
ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.hostList, c.directories.getDataHomeDirectory() + c.parts + "/");
|
ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.hostList, c.directories.getDataHomeDirectory() + c.parts + "/");
|
||||||
|
@ -11,16 +11,19 @@ public class ServerP2P {
|
|||||||
private Logger logger;
|
private Logger logger;
|
||||||
|
|
||||||
|
|
||||||
public ServerP2P() {
|
public ServerP2P(String portStr) {
|
||||||
directories = new Directories("P2P_JAVA_PROJECT_SERVER");
|
port = Integer.valueOf(Integer.parseInt(portStr));
|
||||||
|
directories = new Directories("P2P_JAVA_PROJECT_SERVER_" + port);
|
||||||
directories.createSubdir(subdir);
|
directories.createSubdir(subdir);
|
||||||
logger = new Logger(directories.getDataHomeDirectory() + "server.log");
|
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);
|
System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory() + subdir);
|
||||||
directories.askOpenDataHomeDirectory(subdir);
|
directories.askOpenDataHomeDirectory(subdir);
|
||||||
}
|
}
|
||||||
public static void main(String [] args) {
|
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);
|
ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger);
|
||||||
ServerManagementTCP smtcp = new ServerManagementTCP(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);
|
Thread tudp = new Thread(smudp);
|
||||||
|
Loading…
Reference in New Issue
Block a user