Update tracker: 100%

This commit is contained in:
Louis Royer 2020-03-28 15:46:05 +01:00
parent a5b1abc9a8
commit c60fdedf80
2 changed files with 42 additions and 6 deletions

View File

@ -79,4 +79,15 @@ public abstract class ServeErrors {
}
}
/** Send an unknown host message.
* @param pd Request received
*/
protected < T extends ProtocolP2PPacket<?> > void sendUnknownHost(T pd) {
try {
pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.UNKNOWN_HOST)));
} catch (Exception e) {
writeLog(e, LogLevel.Error);
}
}
}

View File

@ -17,6 +17,7 @@ import protocolP2P.Register;
import protocolP2P.RequestResponseCode;
import protocolP2P.RatioRequest;
import protocolP2P.RatioResponse;
import protocolP2P.UpdateRatio;
import localException.InternalError;
import remoteException.EmptyDirectory;
import exception.LocalException;
@ -154,6 +155,29 @@ public abstract class TrackerManagement extends ServeErrors implements Runnable
*/
protected abstract void closeHostItemSocket(HostItem hostItem);
/** Handle Update Ratio
* @param pd Received request
* @throws InternalError
*/
protected < T extends ProtocolP2PPacket<?> > void handleUpdateRatio(T pd) throws InternalError {
Payload p = pd.getPayload();
assert p instanceof UpdateRatio : "payload must be an instance of UpdateRatio";
if (!(p instanceof UpdateRatio)) {
throw new InternalError();
}
UpdateRatio updateRatio = (UpdateRatio) p;
HostItem updateRatioServer = updateRatio.getServer();
HostItem updateRatioClient = updateRatio.getClient();
long ratioSize = updateRatio.getDataSize();
if (!ratioDown.containsKey(updateRatioClient) || ! ratioUp.containsKey(updateRatioServer)) {
sendUnknownHost(pd);
} else {
ratioDown.put(updateRatioClient, Long.valueOf(ratioDown.get(updateRatioClient).longValue() + ratioSize));
ratioUp.put(updateRatioServer, Long.valueOf(ratioUp.get(updateRatioServer).longValue() + ratioSize));
}
}
/** Handle Registering
* @param pd Received request
* @throws InternalError
@ -171,12 +195,9 @@ public abstract class TrackerManagement extends ServeErrors implements Runnable
}
// initialize ratios if this is a new host
if (!ratioUp.containsKey(host)) {
ratioUp.put(host, Long.valueOf(0));
}
if (!ratioDown.containsKey(host)) {
ratioDown.put(host, Long.valueOf(0));
}
ratioUp.putIfAbsent(host, Long.valueOf(0));
ratioDown.putIfAbsent(host, Long.valueOf(0));
// send a list request
try {
ProtocolP2PPacket<?> pLReq = createProtocolP2PPacket(new Payload(RequestResponseCode.LIST_REQUEST));
@ -232,6 +253,10 @@ public abstract class TrackerManagement extends ServeErrors implements Runnable
writeLog("Received RATIO REQUEST from host " + pd.getHostItem(), LogLevel.Action);
handleRatio(pd);
break;
case UPDATE_RATIO:
writeLog("Received UPDATE RATIO from host " + pd.getHostItem(), LogLevel.Action);
handleUpdateRatio(pd);
break;
default:
writeLog("Received grabbage from host " + pd.getHostItem(), LogLevel.Action);
sendInternalError(pd);