diff --git a/src/clientP2P/ClientDownload.java b/src/clientP2P/ClientDownload.java index 3847cab..2f039eb 100644 --- a/src/clientP2P/ClientDownload.java +++ b/src/clientP2P/ClientDownload.java @@ -329,7 +329,7 @@ public abstract class ClientDownload extends ServeErrors implements Runnable { } - /** Set size of file to download. Also download first file part. + /** Set size of file to download. * @throws InternalError */ protected void setSize() throws InternalError { @@ -429,9 +429,6 @@ public abstract class ClientDownload extends ServeErrors implements Runnable { * @throws InternalError */ protected void init() throws InternalError { - // get size - setSize(); - // get hostlist from tracker try { ProtocolP2PPacket req = createProtocolP2PPacket(new DiscoverRequest(filename)); @@ -450,6 +447,9 @@ public abstract class ClientDownload extends ServeErrors implements Runnable { throw new InternalError(); } + // get size + setSize(); + // get hashsum from 1st server in list hash512 = getHashSum512(hostList.get(0)); if (hash512.length == 0) { diff --git a/src/clientP2P/ClientInterfaceCLI.java b/src/clientP2P/ClientInterfaceCLI.java index 842c50e..4361a10 100644 --- a/src/clientP2P/ClientInterfaceCLI.java +++ b/src/clientP2P/ClientInterfaceCLI.java @@ -45,7 +45,7 @@ public class ClientInterfaceCLI extends ClientInterface { /** Implementation of Runnable */ public void run() { - boolean isRunning = true; + boolean isRunning = initHostList(); while (isRunning) { try { int i = 1; diff --git a/src/clientP2P/ClientManagement.java b/src/clientP2P/ClientManagement.java index f928506..fdc7311 100644 --- a/src/clientP2P/ClientManagement.java +++ b/src/clientP2P/ClientManagement.java @@ -94,7 +94,7 @@ public abstract class ClientManagement extends ServeErrors { try { d.sendRequest(getTrackerSocket()); Payload p = d.receiveResponse().getPayload(); - assert p instanceof DiscoverResponse : "This payload must be instance of Filelist"; + assert p instanceof DiscoverResponse : "This payload must be instance of DiscoverResponse"; if (!(p instanceof DiscoverResponse)) { throw new InternalError(); } else { @@ -155,14 +155,11 @@ public abstract class ClientManagement extends ServeErrors { * @throws VersionRemoteError */ public String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { - if (hostList.size() == 0) { - return new String[0]; - } ProtocolP2PPacket d = createProtocolP2PPacket(new Payload(RequestResponseCode.LIST_REQUEST)); try { - d.sendRequest(getHostItemSocket(hostList.get(0))); + d.sendRequest(getTrackerSocket()); Payload p = d.receiveResponse().getPayload(); - closeHostItemSocket(hostList.get(0)); + closeTrackerSocket(); assert p instanceof FileList : "This payload must be instance of Filelist"; if (!(p instanceof FileList)) { throw new InternalError(); diff --git a/src/tracker/TrackerManagement.java b/src/tracker/TrackerManagement.java index c91a855..1b90610 100644 --- a/src/tracker/TrackerManagement.java +++ b/src/tracker/TrackerManagement.java @@ -340,6 +340,36 @@ public abstract class TrackerManagement extends ServeErrors implements Runnable } } + /** Handle List request + * @param pd Received request + * @throws InternalError + */ + protected < T extends ProtocolP2PPacket > void handleListRequest(T pd) throws InternalError { + try { + String[] l; + synchronized (this) { + while(writeLock.get()) { + this.wait(); + } + readLock.incrementAndGet(); + l = fileList.keySet().toArray(new String[0]); + readLock.decrementAndGet(); + this.notify(); + } + if (l.length == 0) { + writeLog("Sending EMPTY_DIRECTORY to " + pd.getHostItem(), LogLevel.Action); + sendEmptyDirectory(pd); + } else { + writeLog("Sending FILELIST to " + pd.getHostItem(), LogLevel.Action); + pd.sendResponse(createProtocolP2PPacket(new FileList(l))); + } + } catch (InterruptedException e) { + throw new InternalError(); + } catch (Exception e) { + writeLog(e, LogLevel.Error); + } + } + /** Handle requests * @throws LocalException @@ -352,8 +382,8 @@ public abstract class TrackerManagement extends ServeErrors implements Runnable sendNotFound(pd); break; case LIST_REQUEST: - writeLog("Received LIST_REQUEST from host " + pd.getHostItem() + ", sending EMPTY_DIRECTORY", LogLevel.Action); - sendEmptyDirectory(pd); + writeLog("Received LIST_REQUEST from host " + pd.getHostItem(), LogLevel.Action); + handleListRequest(pd); break; case HASH_REQUEST: writeLog("Received HASH_REQUEST from host " + pd.getHostItem() + ", sending NOT_FOUND", LogLevel.Action);