From 2d72822405bf3f76a92cfdabe32a2c849d1c1884 Mon Sep 17 00:00:00 2001 From: Vladimir Eliezer Tokarev Date: Fri, 4 Mar 2016 08:26:55 -0800 Subject: [PATCH] Changed the MessageCollectionUtils file and the files that affacted by this change. --- .../java/meerkat/util/BulletinBoardUtils.java | 15 ++ voter-registry/build.gradle | 2 +- .../src/main/java/meerkat/Registry.java | 12 +- .../CollectionMessagesUtils.java | 157 ------------------ .../LatestMessagesCallBack.java | 34 ++-- .../registry/MessageCollectionUtils.java | 38 +++++ .../MessagesCallBack.java | 2 +- .../RegistryTags.java | 2 +- .../src/test/java/SimpleRegistryTest.java | 22 +-- 9 files changed, 88 insertions(+), 196 deletions(-) delete mode 100644 voter-registry/src/main/java/meerkat/RegistryUtils/CollectionMessagesUtils.java rename voter-registry/src/main/java/meerkat/{RegistryUtils => registry}/LatestMessagesCallBack.java (83%) create mode 100644 voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java rename voter-registry/src/main/java/meerkat/{RegistryUtils => registry}/MessagesCallBack.java (97%) rename voter-registry/src/main/java/meerkat/{RegistryUtils => registry}/RegistryTags.java (94%) diff --git a/meerkat-common/src/main/java/meerkat/util/BulletinBoardUtils.java b/meerkat-common/src/main/java/meerkat/util/BulletinBoardUtils.java index 8793b2d..68867ff 100644 --- a/meerkat-common/src/main/java/meerkat/util/BulletinBoardUtils.java +++ b/meerkat-common/src/main/java/meerkat/util/BulletinBoardUtils.java @@ -2,6 +2,7 @@ package meerkat.util; import meerkat.protobuf.BulletinBoardAPI.*; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -28,6 +29,20 @@ public class BulletinBoardUtils { } + /** + * Gets list of tags values from given messages (tagName values) + * @param messages list of messages from which we want to retrieve specific tag values + * @return List + */ + public static List GetListOfTags(List messages, String tagName) { + List tagsValues = new ArrayList<>(messages.size()); + for ( int i = 0 ; i < messages.size() ; i++ ){ + BulletinBoardMessage message = messages.get(i); + tagsValues.add(findTagWithPrefix(message, tagName)); + } + return tagsValues; + } + /** * Searches the tags in a message for tags that do not contain a given list of prefixes * @param message is the message to search diff --git a/voter-registry/build.gradle b/voter-registry/build.gradle index 8472906..6c68706 100644 --- a/voter-registry/build.gradle +++ b/voter-registry/build.gradle @@ -30,7 +30,7 @@ ext { nexusPassword = project.hasProperty('nexusPassword') ? project.property('nexusPassword') : "" } -description = "Meerkat Voter RegistryUtils application" +description = "Meerkat Voter registry application" // Your project version version = "0.0" diff --git a/voter-registry/src/main/java/meerkat/Registry.java b/voter-registry/src/main/java/meerkat/Registry.java index 5a64595..9920167 100644 --- a/voter-registry/src/main/java/meerkat/Registry.java +++ b/voter-registry/src/main/java/meerkat/Registry.java @@ -1,10 +1,10 @@ package meerkat; import com.google.protobuf.Timestamp; -import meerkat.RegistryUtils.MessagesCallBack; -import meerkat.RegistryUtils.CollectionMessagesUtils; -import meerkat.RegistryUtils.RegistryTags; -import meerkat.RegistryUtils.LatestMessagesCallBack; +import meerkat.registry.MessagesCallBack; +import meerkat.registry.MessageCollectionUtils; +import meerkat.registry.RegistryTags; +import meerkat.registry.LatestMessagesCallBack; import meerkat.bulletinboard.AsyncBulletinBoardClient; import meerkat.crypto.DigitalSignature; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; @@ -102,7 +102,7 @@ public class Registry implements VoterRegistry{ List GroupsActionsTags = new ArrayList(2) {{ add(RegistryTags.GROUP_ID_TAG + groupID.getId()); }}; - bulletinBoardClient.readMessages(CollectionMessagesUtils.GenerateFiltersFromTags(GroupsActionsTags), + bulletinBoardClient.readMessages(MessageCollectionUtils.GenerateFiltersFromTags(GroupsActionsTags), new LatestMessagesCallBack(callback, signer, signature, certificateStream)); } @@ -111,7 +111,7 @@ public class Registry implements VoterRegistry{ add(RegistryTags.ID_TAG + voterID.getId()); add(RegistryTags.VOTER_ENTRY_TAG); }}; - bulletinBoardClient.readMessages(CollectionMessagesUtils.GenerateFiltersFromTags(GroupsActionsTags), + bulletinBoardClient.readMessages(MessageCollectionUtils.GenerateFiltersFromTags(GroupsActionsTags), new LatestMessagesCallBack(callback, signer, signature, certificateStream)); } } diff --git a/voter-registry/src/main/java/meerkat/RegistryUtils/CollectionMessagesUtils.java b/voter-registry/src/main/java/meerkat/RegistryUtils/CollectionMessagesUtils.java deleted file mode 100644 index ea304a9..0000000 --- a/voter-registry/src/main/java/meerkat/RegistryUtils/CollectionMessagesUtils.java +++ /dev/null @@ -1,157 +0,0 @@ -package meerkat.RegistryUtils; - -import com.google.protobuf.InvalidProtocolBufferException; -import meerkat.protobuf.BulletinBoardAPI.*; - -import java.text.ParseException; -import java.util.*; - -/** - * TODO: add logging to this utils - */ - -/** - * Created by Vladimir Eliezer Tokarev on 1/15/2016. - * adds extra functionality to Messages collections - */ -public abstract class CollectionMessagesUtils { - - public static class EmptyListException extends Exception { - public EmptyListException(String message) { - super(message); - } - } - - /** - * Converts lost of UnsignedBulletinBoardMessage to VoterRegistryMessages - * @param messages list - * @return List - */ - public static List ConvertToVoterRegistryMessages(List messages){ - List voterMessages = new ArrayList<>(); - for (int i = 0 ; i < messages.size() ; i++) - { - voterMessages.add(messages.get(i).getMsg()); - } - return voterMessages; - } - - /** - * Return true if message1 was created before message 2 - * @param message1 BulletinBoardMessage object - * @param message2 BulletinBoardMessage object - * @return boolean - * @throws InvalidProtocolBufferException - */ - private static boolean FirstBeforeSecond(BulletinBoardMessage message1, BulletinBoardMessage message2) - throws InvalidProtocolBufferException { - int firstCreationTime = message1.getMsg().getTimestamp().getNanos(); - long secondCreationTime = message2.getMsg().getTimestamp().getNanos(); - return firstCreationTime < secondCreationTime; - } - - /** - * Gets map of GroupId to basicMessage, where the basicMessages are the last actions for those groups - * @param messages List - * @return Map{String:VoterRegistryMessage} - * @throws ParseException - */ - public static Map GetLatestGroupsActions(List messages) - throws ParseException, InvalidProtocolBufferException { - Map groupIdToMessage = new HashMap<>(messages.size()); - - // iterate trough all the messages and put into the map the last updated groups actions - for (int i = 0 ; i < messages.size() ; i++) { - BulletinBoardMessage message = messages.get(i); - String groupId = GetTagByName(message.getMsg().getTagList(), RegistryTags.GROUP_ID_TAG); - BulletinBoardMessage temp = groupIdToMessage.get(groupId); - - if (temp != null && temp != message) { - if (FirstBeforeSecond(temp, message)) { - groupIdToMessage.put(groupId, message); - } - } - groupIdToMessage.put(groupId, message); - } - return groupIdToMessage; - } - - /** - * Gets list of groups ids of the basicMessages that carried the adding to group tag - * @param groupIdToMessage Map - * @return List - */ - public static List GetListOfGroupIds(Map groupIdToMessage) { - List groupsIds = new ArrayList<>(groupIdToMessage.size()); - Iterator entries = groupIdToMessage.entrySet().iterator(); - while (entries.hasNext()) { - Map.Entry tuple = (Map.Entry) entries.next(); - BulletinBoardMessage message = (BulletinBoardMessage) tuple.getValue(); - String groupId = GetTagByName(message.getMsg().getTagList(), RegistryTags.GROUP_ID_TAG); - groupId.replace(RegistryTags.GROUP_ID_TAG, ""); - groupsIds.add(groupId); - } - return groupsIds; - } - - /** - * Gets the message with the latest timestamp from messages - * @param messages List - * @return VoterRegistryMessage - * @throws ParseException - * @throws EmptyListException - */ - public static BulletinBoardMessage GetLatestMessage(List messages) - throws ParseException, EmptyListException, InvalidProtocolBufferException { - if (messages.size() == 0 ){ - throw new EmptyListException("The list of messages passed to GetLatestMessage is empty."); - } - BulletinBoardMessage LatestMessage = messages.get(0); - for (int i = 0 ; i < messages.size() ; i++) { - BulletinBoardMessage message = messages.get(i); - if (FirstBeforeSecond(message, LatestMessage)) { - LatestMessage = message; - } - } - return LatestMessage; - } - - /** - * Gets the wanted tag from given basic message - * - * @param content the name of the tag - * @param tags the list of tags - * @return string - */ - public static String GetTagByName(List tags, String content) { - for (int i = 0 ; i < tags.size() ; i++) { - String tag = tags.get(i); - if (tag.startsWith(content) || tag.endsWith(content)) { - return tag; - } - } - return null; - } - - - /** - * Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG - * - * @param tags the tags based on which the messages will be filtered - * @return MessageFilterList. - */ - public static MessageFilterList GenerateFiltersFromTags(List tags) { - MessageFilterList.Builder filters = MessageFilterList.newBuilder(); - - if (tags.isEmpty()){ - return filters.build(); - } - - for (int i = 0 ;i < tags.size() ; i++) { - String tag = tags.get(i); - MessageFilter.Builder filter = MessageFilter.newBuilder().setTag(tag).setType(FilterType.TAG); - filters.addFilter(filter); - } - return filters.build(); - } -} diff --git a/voter-registry/src/main/java/meerkat/RegistryUtils/LatestMessagesCallBack.java b/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java similarity index 83% rename from voter-registry/src/main/java/meerkat/RegistryUtils/LatestMessagesCallBack.java rename to voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java index b12934a..5a6e044 100644 --- a/voter-registry/src/main/java/meerkat/RegistryUtils/LatestMessagesCallBack.java +++ b/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java @@ -1,27 +1,26 @@ -package meerkat.RegistryUtils; +package meerkat.registry; import com.google.common.util.concurrent.FutureCallback; -import com.google.protobuf.InvalidProtocolBufferException; import meerkat.MessageValidator; import meerkat.VoterRegistry.RegistryCallBack; import meerkat.crypto.DigitalSignature; import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.Crypto; +import meerkat.util.TimestampComparator; import java.io.InputStream; import java.security.InvalidKeyException; import java.security.SignatureException; import java.security.cert.CertificateException; -import java.text.ParseException; import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import java.util.Map; -import static meerkat.RegistryUtils.CollectionMessagesUtils.*; +import static meerkat.util.BulletinBoardUtils.GetListOfTags; /** - * TODO : add logging + * TODO : add logging and verification of the messages */ /** @@ -76,18 +75,15 @@ public class LatestMessagesCallBack implements FutureCallback msg) { - try { - if(isAddToGroupsList(msg)) { - Map map = GetLatestGroupsActions(msg); - List groupsOfUser = GetListOfGroupIds(map); - callback.HandleResult(groupsOfUser); - } - else { - callback.HandleResult(GetLatestMessage(msg)); - } - } catch (ParseException | EmptyListException | InvalidProtocolBufferException validationError) { - callback.HandleFailure(validationError); - validationError.printStackTrace(); + if (isAddToGroupsList(msg)) { + List groupsOfUser = GetListOfTags(msg, RegistryTags.GROUP_ID_TAG); + callback.HandleResult(groupsOfUser); + } + else { + callback.HandleResult(Collections.max(msg, (first, second) -> { + TimestampComparator comparator = new TimestampComparator(); + return comparator.compare(first.getMsg().getTimestamp(), second.getMsg().getTimestamp()); + })); } } @@ -126,7 +122,7 @@ public class LatestMessagesCallBack implements FutureCallback validate(List object) throws ValidationError { - List verifiedMessages = new ArrayList<>(); + List verifiedMessages = new ArrayList<>(object.size()); for (int i = 0 ; i < object.size() ; i++) { BulletinBoardMessage message = object.get(i); diff --git a/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java b/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java new file mode 100644 index 0000000..c011e38 --- /dev/null +++ b/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java @@ -0,0 +1,38 @@ +package meerkat.registry; + +import meerkat.protobuf.BulletinBoardAPI.FilterType; +import meerkat.protobuf.BulletinBoardAPI.MessageFilter; +import meerkat.protobuf.BulletinBoardAPI.MessageFilterList; + +import java.util.List; + +/** + * TODO: add logging to this utils + */ + +/** + * Created by Vladimir Eliezer Tokarev on 1/15/2016. + * adds extra functionality to Messages collections + */ +public abstract class MessageCollectionUtils { + /** + * Creates list of filters based on given list of strings (that are tags) + * + * @param tags the tags based on which the messages will be filtered + * @return MessageFilterList. + */ + public static MessageFilterList GenerateFiltersFromTags(List tags) { + MessageFilterList.Builder filters = MessageFilterList.newBuilder(); + + if (tags.isEmpty()){ + return filters.build(); + } + + for (int i = 0 ;i < tags.size() ; i++) { + String tag = tags.get(i); + MessageFilter.Builder filter = MessageFilter.newBuilder().setTag(tag).setType(FilterType.TAG); + filters.addFilter(filter); + } + return filters.build(); + } +} diff --git a/voter-registry/src/main/java/meerkat/RegistryUtils/MessagesCallBack.java b/voter-registry/src/main/java/meerkat/registry/MessagesCallBack.java similarity index 97% rename from voter-registry/src/main/java/meerkat/RegistryUtils/MessagesCallBack.java rename to voter-registry/src/main/java/meerkat/registry/MessagesCallBack.java index 525f2b3..a8fe184 100644 --- a/voter-registry/src/main/java/meerkat/RegistryUtils/MessagesCallBack.java +++ b/voter-registry/src/main/java/meerkat/registry/MessagesCallBack.java @@ -1,4 +1,4 @@ -package meerkat.RegistryUtils; +package meerkat.registry; import com.google.common.util.concurrent.FutureCallback; import meerkat.VoterRegistry; diff --git a/voter-registry/src/main/java/meerkat/RegistryUtils/RegistryTags.java b/voter-registry/src/main/java/meerkat/registry/RegistryTags.java similarity index 94% rename from voter-registry/src/main/java/meerkat/RegistryUtils/RegistryTags.java rename to voter-registry/src/main/java/meerkat/registry/RegistryTags.java index e0960cf..dc09ad4 100644 --- a/voter-registry/src/main/java/meerkat/RegistryUtils/RegistryTags.java +++ b/voter-registry/src/main/java/meerkat/registry/RegistryTags.java @@ -1,4 +1,4 @@ -package meerkat.RegistryUtils; +package meerkat.registry; /** diff --git a/voter-registry/src/test/java/SimpleRegistryTest.java b/voter-registry/src/test/java/SimpleRegistryTest.java index beb4d62..352b75c 100644 --- a/voter-registry/src/test/java/SimpleRegistryTest.java +++ b/voter-registry/src/test/java/SimpleRegistryTest.java @@ -2,8 +2,8 @@ import com.google.common.util.concurrent.FutureCallback; import com.google.protobuf.InvalidProtocolBufferException; import junit.framework.TestCase; import meerkat.Registry; -import meerkat.RegistryUtils.CollectionMessagesUtils; -import meerkat.RegistryUtils.RegistryTags; +import meerkat.registry.MessageCollectionUtils; +import meerkat.registry.RegistryTags; import meerkat.bulletinboard.AsyncBulletinBoardClient; import meerkat.bulletinboard.ThreadedBulletinBoardClient; import meerkat.crypto.concrete.ECDSASignature; @@ -29,7 +29,7 @@ import java.util.concurrent.Semaphore; /** * Created by Vladimir Eliezer Tokarev on 1/16/2016. - * Tests the Simple RegistryUtils contents + * Tests the Simple registry contents * NOTE: for most of this tests to pass there should run BulletinBoardServer * that should be reachable on BULLETIN_BOARD_SERVER_ADDRESS */ @@ -114,7 +114,7 @@ public class SimpleRegistryTest extends TestCase { } /** - * Initialize RegistryUtils object + * Initialize registry object */ public void setUp() { SetSigner(); @@ -130,7 +130,7 @@ public class SimpleRegistryTest extends TestCase { try { new Registry(signer, bulletinBoardClient, certStream); } catch (Exception e) { - assert false : "While creating the RegistryUtils exception have been thrown " + e; + assert false : "While creating the registry exception have been thrown " + e; } } @@ -150,7 +150,7 @@ public class SimpleRegistryTest extends TestCase { for (int j = 0 ;j < tags.size() ; j++) { String tag = tags.get(j); - if(CollectionMessagesUtils.GetTagByName(message.getMsg().getTagList(), tag)!=null){ + if(MessageCollectionUtils.GetTagByName(message.getMsg().getTagList(), tag)!=null){ wantedTagsCounter++; } } @@ -180,7 +180,7 @@ public class SimpleRegistryTest extends TestCase { assertEquals(1, handler.counter ); List tags = new ArrayList(){{ add(RegistryTags.VOTER_ENTRY_TAG);}}; - MessageFilterList filters = CollectionMessagesUtils.GenerateFiltersFromTags(tags); + MessageFilterList filters = MessageCollectionUtils.GenerateFiltersFromTags(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); bulletinBoardClient.readMessages(filters, bulletinHandler); @@ -208,7 +208,7 @@ public class SimpleRegistryTest extends TestCase { assertEquals(1, handler.counter ); List tags = new ArrayList(){{ add(RegistryTags.VOTE_ACTION_TAG);}}; - MessageFilterList filters = CollectionMessagesUtils.GenerateFiltersFromTags(tags); + MessageFilterList filters = MessageCollectionUtils.GenerateFiltersFromTags(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); bulletinBoardClient.readMessages(filters, bulletinHandler); @@ -238,7 +238,7 @@ public class SimpleRegistryTest extends TestCase { assertEquals(1, handler.counter); List tags = new ArrayList(){{add(RegistryTags.ADD_TO_GROUP_TAG);}}; - MessageFilterList filters = CollectionMessagesUtils.GenerateFiltersFromTags(tags); + MessageFilterList filters = MessageCollectionUtils.GenerateFiltersFromTags(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); bulletinBoardClient.readMessages(filters, bulletinHandler); @@ -303,9 +303,9 @@ public class SimpleRegistryTest extends TestCase { registry.GetPersonIDDetails(VoterID.newBuilder().setId(id).build(), personalHandler); jobSemaphore.acquire(1); - assertEquals(RegistryTags.ID_TAG + id, CollectionMessagesUtils.GetTagByName(personalHandler + assertEquals(RegistryTags.ID_TAG + id, MessageCollectionUtils.GetTagByName(personalHandler .data.getMsg().getTagList(), RegistryTags.ID_TAG)); - assertTrue(CollectionMessagesUtils.GetTagByName(personalHandler.data.getMsg().getTagList(), + assertTrue(MessageCollectionUtils.GetTagByName(personalHandler.data.getMsg().getTagList(), RegistryTags.VOTER_DATA_TAG).contains(data)); } }