From 4ba7fa9be088650dc32dbabaa1ef2ea47681ec81 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Thu, 9 Jan 2020 18:16:28 +0100 Subject: Test gossip initialization --- .../mimuw/cloudatlas/agent/modules/GossipGirl.java | 13 +---- .../cloudatlas/agent/modules/GossipGirlState.java | 55 ++++++++++++++++++++++ 2 files changed, 57 insertions(+), 11 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java index 01119bf..ccc7ee5 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirl.java @@ -69,8 +69,8 @@ public class GossipGirl extends Module { "", 0, state.gossipId, - getZoneTimestamps(message.getZMI()), - getQueryTimestamps(message.getQueries()) + state.getZoneTimestampsToSend(), + state.getQueryTimestampsToSend() ); UDUPMessage udupMessage = new UDUPMessage("", 0, state.theirContact, hejka); sendMessage(udupMessage); @@ -96,13 +96,4 @@ public class GossipGirl extends Module { System.out.println("ERROR: GossipGirl got state for a nonexistent gossip"); } } - - public Map getZoneTimestamps(ZMI root) { - return new HashMap(); - } - - public Map getQueryTimestamps(Map> queries) { - return new HashMap(); - } - } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirlState.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirlState.java index 726a9b2..8ec8ed2 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirlState.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/GossipGirlState.java @@ -1,5 +1,7 @@ package pl.edu.mimuw.cloudatlas.agent.modules; +import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -95,10 +97,63 @@ public class GossipGirlState { } } + public Map getZoneTimestampsToSend() { + Map timestamps = new HashMap(); + collectZoneTimestamps(timestamps, hierarchy, theirContact.getName()); + return timestamps; + } + + public Map getQueryTimestampsToSend() { + Map queryTimestamps= new HashMap(); + for (Entry> query : queries.entrySet()) { + queryTimestamps.put(query.getKey(), query.getValue().getValue()); + } + + return queryTimestamps; + } + public List getZMIsToSend() { return new LinkedList(); } + public void collectZoneTimestamps(Map timestamps, ZMI currentZMI, PathName recipientPath) { + for (ZMI zmi : currentZMI.getSons()) { + if (interestedIn(recipientPath, zmi.getPathName())) { + ValueTime timestamp = (ValueTime) zmi.getAttributes().getOrNull("timestamp"); + if (timestamp != null) { + timestamps.put(zmi.getPathName(), timestamp); + } else { + System.out.println("ERROR: collectZoneTimestamps encountered a zone with no timestamp"); + } + } else { + collectZoneTimestamps(timestamps, zmi, recipientPath); + } + } + } + + public boolean interestedIn(PathName recipientPath, PathName zmiPath) { + return isPrefix(zmiPath.levelUp(), recipientPath) && !isPrefix(zmiPath, recipientPath); + } + + public boolean isPrefix(PathName prefix, PathName path) { + List prefixComponents = prefix.getComponents(); + List pathComponents = path.getComponents(); + + if (prefixComponents.size() > pathComponents.size()) { + return false; + } + + Iterator prefixIterator = prefixComponents.iterator(); + Iterator pathIterator = pathComponents.iterator(); + + while (prefixIterator.hasNext()) { + if (!prefixIterator.next().equals(pathIterator.next())) { + return false; + } + } + return true; + } + public void sentInfo() { switch (state) { case SEND_INFO: -- cgit v1.2.3