diff --git a/src/clientP2P/ClientDownloadPartUDP.java b/src/clientP2P/ClientDownloadPartUDP.java index 4ec0391..30c373c 100644 --- a/src/clientP2P/ClientDownloadPartUDP.java +++ b/src/clientP2P/ClientDownloadPartUDP.java @@ -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; } + } } diff --git a/src/clientP2P/ClientDownloadUDP.java b/src/clientP2P/ClientDownloadUDP.java index 134ab2f..2bd2f29 100644 --- a/src/clientP2P/ClientDownloadUDP.java +++ b/src/clientP2P/ClientDownloadUDP.java @@ -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 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(); + try { + synchronized(this) { + this.wait(); + List 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(); + } } - } - 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(); } } diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 606f2cd..a1aa92a 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -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 + "/"); diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 9b068c1..98b7b23 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -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 -- + * */ + 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);