Fix #87 (#117)
All checks were successful
flavien's git/Projet_JAVA_P2P_STRI2A/pipeline/head This commit looks good

Fix #87
This commit is contained in:
Louis Royer 2020-04-09 16:10:25 +02:00
parent 389547ff20
commit 7a263fc9d8
4 changed files with 40 additions and 13 deletions

View File

@ -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 * @throws InternalError
*/ */
protected void setSize() throws InternalError { protected void setSize() throws InternalError {
@ -429,9 +429,6 @@ public abstract class ClientDownload extends ServeErrors implements Runnable {
* @throws InternalError * @throws InternalError
*/ */
protected void init() throws InternalError { protected void init() throws InternalError {
// get size
setSize();
// get hostlist from tracker // get hostlist from tracker
try { try {
ProtocolP2PPacket<?> req = createProtocolP2PPacket(new DiscoverRequest(filename)); ProtocolP2PPacket<?> req = createProtocolP2PPacket(new DiscoverRequest(filename));
@ -450,6 +447,9 @@ public abstract class ClientDownload extends ServeErrors implements Runnable {
throw new InternalError(); throw new InternalError();
} }
// get size
setSize();
// get hashsum from 1st server in list // get hashsum from 1st server in list
hash512 = getHashSum512(hostList.get(0)); hash512 = getHashSum512(hostList.get(0));
if (hash512.length == 0) { if (hash512.length == 0) {

View File

@ -45,7 +45,7 @@ public class ClientInterfaceCLI extends ClientInterface {
/** Implementation of Runnable /** Implementation of Runnable
*/ */
public void run() { public void run() {
boolean isRunning = true; boolean isRunning = initHostList();
while (isRunning) { while (isRunning) {
try { try {
int i = 1; int i = 1;

View File

@ -94,7 +94,7 @@ public abstract class ClientManagement extends ServeErrors {
try { try {
d.sendRequest(getTrackerSocket()); d.sendRequest(getTrackerSocket());
Payload p = d.receiveResponse().getPayload(); 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)) { if (!(p instanceof DiscoverResponse)) {
throw new InternalError(); throw new InternalError();
} else { } else {
@ -155,14 +155,11 @@ public abstract class ClientManagement extends ServeErrors {
* @throws VersionRemoteError * @throws VersionRemoteError
*/ */
public String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, 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)); ProtocolP2PPacket<?> d = createProtocolP2PPacket(new Payload(RequestResponseCode.LIST_REQUEST));
try { try {
d.sendRequest(getHostItemSocket(hostList.get(0))); d.sendRequest(getTrackerSocket());
Payload p = d.receiveResponse().getPayload(); Payload p = d.receiveResponse().getPayload();
closeHostItemSocket(hostList.get(0)); closeTrackerSocket();
assert p instanceof FileList : "This payload must be instance of Filelist"; assert p instanceof FileList : "This payload must be instance of Filelist";
if (!(p instanceof FileList)) { if (!(p instanceof FileList)) {
throw new InternalError(); throw new InternalError();

View File

@ -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 /** Handle requests
* @throws LocalException * @throws LocalException
@ -352,8 +382,8 @@ public abstract class TrackerManagement extends ServeErrors implements Runnable
sendNotFound(pd); sendNotFound(pd);
break; break;
case LIST_REQUEST: case LIST_REQUEST:
writeLog("Received LIST_REQUEST from host " + pd.getHostItem() + ", sending EMPTY_DIRECTORY", LogLevel.Action); writeLog("Received LIST_REQUEST from host " + pd.getHostItem(), LogLevel.Action);
sendEmptyDirectory(pd); handleListRequest(pd);
break; break;
case HASH_REQUEST: case HASH_REQUEST:
writeLog("Received HASH_REQUEST from host " + pd.getHostItem() + ", sending NOT_FOUND", LogLevel.Action); writeLog("Received HASH_REQUEST from host " + pd.getHostItem() + ", sending NOT_FOUND", LogLevel.Action);