From c60fdedf80c1ea963a53c9fcf4d587cd083e43a0 Mon Sep 17 00:00:00 2001 From: Louis Royer Date: Sat, 28 Mar 2020 15:46:05 +0100 Subject: [PATCH] Update tracker: 100% --- src/tools/ServeErrors.java | 11 +++++++++ src/tracker/TrackerManagement.java | 37 +++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/tools/ServeErrors.java b/src/tools/ServeErrors.java index ff083f9..4392aa6 100644 --- a/src/tools/ServeErrors.java +++ b/src/tools/ServeErrors.java @@ -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); + } + } + } diff --git a/src/tracker/TrackerManagement.java b/src/tracker/TrackerManagement.java index b4a51fc..617f101 100644 --- a/src/tracker/TrackerManagement.java +++ b/src/tracker/TrackerManagement.java @@ -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);