Update tracker: 100%

pull/86/head
Louis Royer 4 years ago
parent a5b1abc9a8
commit c60fdedf80

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

@ -17,6 +17,7 @@ import protocolP2P.Register;
import protocolP2P.RequestResponseCode; import protocolP2P.RequestResponseCode;
import protocolP2P.RatioRequest; import protocolP2P.RatioRequest;
import protocolP2P.RatioResponse; import protocolP2P.RatioResponse;
import protocolP2P.UpdateRatio;
import localException.InternalError; import localException.InternalError;
import remoteException.EmptyDirectory; import remoteException.EmptyDirectory;
import exception.LocalException; import exception.LocalException;
@ -154,6 +155,29 @@ public abstract class TrackerManagement extends ServeErrors implements Runnable
*/ */
protected abstract void closeHostItemSocket(HostItem hostItem); 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 /** Handle Registering
* @param pd Received request * @param pd Received request
* @throws InternalError * @throws InternalError
@ -171,12 +195,9 @@ public abstract class TrackerManagement extends ServeErrors implements Runnable
} }
// initialize ratios if this is a new host // initialize ratios if this is a new host
if (!ratioUp.containsKey(host)) { ratioUp.putIfAbsent(host, Long.valueOf(0));
ratioUp.put(host, Long.valueOf(0)); ratioDown.putIfAbsent(host, Long.valueOf(0));
}
if (!ratioDown.containsKey(host)) {
ratioDown.put(host, Long.valueOf(0));
}
// send a list request // send a list request
try { try {
ProtocolP2PPacket<?> pLReq = createProtocolP2PPacket(new Payload(RequestResponseCode.LIST_REQUEST)); 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); writeLog("Received RATIO REQUEST from host " + pd.getHostItem(), LogLevel.Action);
handleRatio(pd); handleRatio(pd);
break; break;
case UPDATE_RATIO:
writeLog("Received UPDATE RATIO from host " + pd.getHostItem(), LogLevel.Action);
handleUpdateRatio(pd);
break;
default: default:
writeLog("Received grabbage from host " + pd.getHostItem(), LogLevel.Action); writeLog("Received grabbage from host " + pd.getHostItem(), LogLevel.Action);
sendInternalError(pd); sendInternalError(pd);

Loading…
Cancel
Save