|
|
|
@ -35,6 +35,7 @@ import tools.ServeErrors;
|
|
|
|
|
import tools.HostItem;
|
|
|
|
|
import tools.Logger;
|
|
|
|
|
import tools.LogLevel;
|
|
|
|
|
import java.net.SocketException;
|
|
|
|
|
|
|
|
|
|
/** Implementation of P2P-JAVA-PROJECT CLIENT
|
|
|
|
|
* @author Louis Royer
|
|
|
|
@ -77,8 +78,16 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Getter for tracker socket
|
|
|
|
|
* @return Tracker's socket
|
|
|
|
|
* @throws SocketException
|
|
|
|
|
* @throws UnknownHostException
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
protected abstract Object getTrackerSocket() throws SocketException, UnknownHostException, IOException;
|
|
|
|
|
|
|
|
|
|
/** Close Tracker socket
|
|
|
|
|
*/
|
|
|
|
|
protected abstract Object getTrackerSocket();
|
|
|
|
|
protected abstract void closeTrackerSocket();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Initialize hostList from tracker
|
|
|
|
@ -88,7 +97,29 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
|
|
|
|
|
private void initHostList() throws ProtocolError, InternalError {
|
|
|
|
|
ProtocolP2PPacket<?> d = createProtocolP2PPacket(new DiscoverRequest(null));
|
|
|
|
|
try {
|
|
|
|
|
d.sendRequest(getTrackerSocket());
|
|
|
|
|
boolean contacted = false;
|
|
|
|
|
boolean firstLoop = true;
|
|
|
|
|
boolean stop = false;
|
|
|
|
|
while (!contacted && !stop) {
|
|
|
|
|
try {
|
|
|
|
|
if (!firstLoop) {
|
|
|
|
|
writeLog("Cannot contact tracker. Try again [Y/n] ?", LogLevel.Error);
|
|
|
|
|
String tryAgain = scanner.nextLine();
|
|
|
|
|
if (tryAgain.equals("n") || tryAgain.equals("N")) {
|
|
|
|
|
stop = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
firstLoop = false;
|
|
|
|
|
d.sendRequest(getTrackerSocket());
|
|
|
|
|
contacted = true;
|
|
|
|
|
} catch (SocketException e) {
|
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (stop) {
|
|
|
|
|
System.exit(3);
|
|
|
|
|
}
|
|
|
|
|
Payload p = d.receiveResponse().getPayload();
|
|
|
|
|
assert p instanceof DiscoverResponse : "This payload must be instance of Filelist";
|
|
|
|
|
if (!(p instanceof DiscoverResponse)) {
|
|
|
|
@ -96,6 +127,7 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
|
|
|
|
|
} else {
|
|
|
|
|
hostList = ((DiscoverResponse)p).getHostList();
|
|
|
|
|
}
|
|
|
|
|
closeTrackerSocket();
|
|
|
|
|
} catch (SocketClosed e){
|
|
|
|
|
writeLog("listDirectory : SocketClosed", LogLevel.Error);
|
|
|
|
|
throw new ProtocolError();
|
|
|
|
@ -129,6 +161,12 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
|
|
|
|
|
*/
|
|
|
|
|
protected abstract Object getHostItemSocket(HostItem hostItem);
|
|
|
|
|
|
|
|
|
|
/** Close HostItem socket
|
|
|
|
|
* @param hostItem HostItem
|
|
|
|
|
*/
|
|
|
|
|
protected abstract void closeHostItemSocket(HostItem hostItem);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** list server’s directory content
|
|
|
|
|
* @return list of files
|
|
|
|
|
* @throws InternalError
|
|
|
|
@ -148,6 +186,7 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
|
|
|
|
|
try {
|
|
|
|
|
d.sendRequest(getHostItemSocket(hostList.get(0)));
|
|
|
|
|
Payload p = d.receiveResponse().getPayload();
|
|
|
|
|
closeHostItemSocket(hostList.get(0));
|
|
|
|
|
assert p instanceof FileList : "This payload must be instance of Filelist";
|
|
|
|
|
if (!(p instanceof FileList)) {
|
|
|
|
|
throw new InternalError();
|
|
|
|
@ -286,5 +325,6 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
|
|
|
|
|
writeLog("File is empty", LogLevel.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
writeLog("Exiting client", LogLevel.Info);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|