Fix #87 #117

Merged
louis_royer merged 1 commits from fix87 into master 2020-04-09 16:10:26 +02:00
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
*/
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) {

View File

@ -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;

View File

@ -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();

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
* @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);