From c30ea072f21539e5d6f2913269cfcae311f77a90 Mon Sep 17 00:00:00 2001 From: Vladimir Eliezer Tokarev Date: Sat, 23 Jan 2016 03:31:04 -0800 Subject: [PATCH] Changed the SimpleRegistry The new Simple Registry works according to the RegistryInstance interface --- .../src/main/java/meerkat/SimpleRegistry.java | 33 +++++++------------ .../java/meerkat/VoterRegistryMessage.java | 29 +++++++++++----- .../java/util/CollectionMessagesUtils.java | 26 ++++++++++----- 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/voter-registry/src/main/java/meerkat/SimpleRegistry.java b/voter-registry/src/main/java/meerkat/SimpleRegistry.java index 551d43d..9a7962b 100644 --- a/voter-registry/src/main/java/meerkat/SimpleRegistry.java +++ b/voter-registry/src/main/java/meerkat/SimpleRegistry.java @@ -8,18 +8,19 @@ import meerkat.crypto.Encryption; import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.Crypto; import util.AccurateTimestamp; -import util.CollectionMessagesUtils; import util.RegistryTags; import java.io.IOException; import java.text.ParseException; import java.util.*; +import static util.CollectionMessagesUtils.*; + /** * Created by Vladimir Eliezer Tokarev on 1/8/2016. * Gives the ability to synchronously manage voters information */ -public class SimpleRegistry extends RegistryInstance{ +public class SimpleRegistry implements RegistryInstance{ protected Encryption signatory; @@ -83,8 +84,8 @@ public class SimpleRegistry extends RegistryInstance{ private List GetRelevantVoterRegistryMessages(List tags) throws InvalidProtocolBufferException { List relevantMessages = GetRelevantMessages(tags); List messages = - CollectionMessagesUtils.GetUnsignedBulletinBoardMessages(relevantMessages); - return CollectionMessagesUtils.ConvertToVoterRegistryMessages(messages); + GetUnsignedBulletinBoardMessages(relevantMessages); + return ConvertToVoterRegistryMessages(messages); } /** @@ -153,12 +154,11 @@ public class SimpleRegistry extends RegistryInstance{ add(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.REMOVE_FROM_GROUP_TAG); add(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.ADD_TO_GROUP_TAG); }}; + List voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags); - List voterRegistryMessages = null; - voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags); + Map groupIdToMessage = GetLatestGroupsActions(voterRegistryMessages); - Map groupIdToMessage = CollectionMessagesUtils.GetLatestGroupsActions(voterRegistryMessages); - callback.HandleVoterGroups(CollectionMessagesUtils.GetListOfGroupIds(groupIdToMessage)); + callback.HandleVoterGroups(GetListOfGroupIds(groupIdToMessage)); } catch (ParseException | InvalidProtocolBufferException e) { callback.HandleVoterGroups(null); } @@ -170,22 +170,11 @@ public class SimpleRegistry extends RegistryInstance{ add(RegistryTags.ID_TAG + " " + id); add(RegistryTags.VOTER_ENTRY_TAG.toString()); }}; + List voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags); - List voterRegistryMessages = - GetRelevantVoterRegistryMessages(GroupsActionsTags); - VoterRegistryMessage LatestMessage = voterRegistryMessages.get(0); + VoterRegistryMessage LatestMessage = GetLatestMessage(voterRegistryMessages); - - // create FindLatestMessage - for (VoterRegistryMessage message : voterRegistryMessages) { - if (message.GetBasicMessageActionTimestamp().before(LatestMessage.GetBasicMessageActionTimestamp())) { - LatestMessage = message; - } - } - // convert VoterRegistryMessage to list of strings - callback.HandleVoterInfo(Arrays.asList( - LatestMessage.GetWantedTagFromBasicMessage(RegistryTags.ID_TAG), - LatestMessage.base.getData().toString())); + callback.HandleVoterInfo(LatestMessage.tagsToStringList()); } catch (InvalidProtocolBufferException | ParseException e) { callback.HandleVoterInfo(null); } diff --git a/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java b/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java index 40d770d..a67bfee 100644 --- a/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java +++ b/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java @@ -6,6 +6,7 @@ import util.RegistryTags; import java.sql.Timestamp; import java.text.ParseException; +import java.util.List; /** * Created by Vladimir Eliezer Tokarev on 1/15.2016 @@ -15,18 +16,19 @@ public class VoterRegistryMessage { public BulletinBoardAPI.UnsignedBulletinBoardMessage base; - public VoterRegistryMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage message){ + public VoterRegistryMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage message) { base = BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder().addAllTag(message.getTagList()).build(); } /** * Gets the wanted tag from given basic message + * * @param tagName the name of the tag * @return string */ - public String GetWantedTagFromBasicMessage(RegistryTags tagName){ - for (String tag : base.getTagList()) { - if ( tag.contains(tagName.toString())) { + public String GetWantedTagFromBasicMessage(RegistryTags tagName) { + for (String tag : base.getTagList()) { + if (tag.contains(tagName.toString())) { return tag; } } @@ -35,12 +37,13 @@ public class VoterRegistryMessage { /** * Gets the timestamp of the tag adding + * * @return Timestamp * @throws ParseException */ - public Timestamp GetBasicMessageActionTimestamp() throws ParseException { + public Timestamp GetBasicMessageActionTimestamp() throws ParseException { for (String tag : base.getTagList()) { - if ( tag.contains(RegistryTags.ACTION_TIMESTAMP_TAG.toString())) { + if (tag.contains(RegistryTags.ACTION_TIMESTAMP_TAG.toString())) { String[] tagParts = tag.split(" "); String timestamp = tagParts[tagParts.length - 1]; return AccurateTimestamp.GetTimestampFromString(timestamp); @@ -52,14 +55,24 @@ public class VoterRegistryMessage { /** * Checks if the given message have the ADD_TO_GROUP_TAG + * * @return true when ADD_TO_GROUP_TAG exist else false */ - public boolean IsGroupAdding() { + public boolean IsGroupAdding() { for (String tag : base.getTagList()) { - if ( tag.contains(RegistryTags.ADD_TO_GROUP_TAG.toString())) { + if (tag.contains(RegistryTags.ADD_TO_GROUP_TAG.toString())) { return true; } } return false; } + + /** + * converts the messages tags to list of strings + * + * @return List of strings + */ + public List tagsToStringList(){ + return base.getTagList(); + } } diff --git a/voter-registry/src/main/java/util/CollectionMessagesUtils.java b/voter-registry/src/main/java/util/CollectionMessagesUtils.java index 3c8ebd0..3f1b496 100644 --- a/voter-registry/src/main/java/util/CollectionMessagesUtils.java +++ b/voter-registry/src/main/java/util/CollectionMessagesUtils.java @@ -25,7 +25,7 @@ public abstract class CollectionMessagesUtils { public static List ConvertToVoterRegistryMessages(List messages){ return messages.stream().map(VoterRegistryMessage::new).collect(Collectors.toList()); } - + /** * Gets map of GroupId to basicMessage, where the basicMessages are the last actions for those groups * @param messages List @@ -33,8 +33,7 @@ public abstract class CollectionMessagesUtils { * @throws ParseException */ public static Map GetLatestGroupsActions(List messages) throws ParseException { - - Map groupIdToMessage = new HashMap<>(); + Map groupIdToMessage = new HashMap<>(); // iterate trough all the messages and put into the map the last updated groups actions @@ -42,15 +41,12 @@ public abstract class CollectionMessagesUtils { String groupId = message.GetWantedTagFromBasicMessage(RegistryTags.GROUP_ID_TAG); VoterRegistryMessage temp = groupIdToMessage.get(groupId); - if (temp != null) { - if (temp != message) { - if (temp.GetBasicMessageActionTimestamp().before(message.GetBasicMessageActionTimestamp())) { - groupIdToMessage.put(groupId, message); - } + if (temp != null && temp != message) { + if (temp.GetBasicMessageActionTimestamp().before(message.GetBasicMessageActionTimestamp())) { + groupIdToMessage.put(groupId, message); } } } - return groupIdToMessage; } @@ -79,4 +75,16 @@ public abstract class CollectionMessagesUtils { List listOfMessages) throws InvalidProtocolBufferException { return listOfMessages.stream().map(BulletinBoardAPI.BulletinBoardMessage::getMsg).collect(Collectors.toList()); } + + public static VoterRegistryMessage GetLatestMessage(List messages) throws ParseException { + VoterRegistryMessage LatestMessage = messages.get(0); + + for (VoterRegistryMessage message : messages) { + if (message.GetBasicMessageActionTimestamp().before(LatestMessage.GetBasicMessageActionTimestamp())) { + LatestMessage = message; + } + } + return LatestMessage; + } + }