Changed the MessageCollectionUtils file and the files that affacted by this change.

Voter-Registry
Vladimir Eliezer Tokarev 2016-03-04 08:26:55 -08:00
parent 9828832553
commit 2d72822405
9 changed files with 88 additions and 196 deletions

View File

@ -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<String>
*/
public static List<String> GetListOfTags(List<BulletinBoardMessage> messages, String tagName) {
List<String> 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

View File

@ -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"

View File

@ -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<String> GroupsActionsTags = new ArrayList<String>(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));
}
}

View File

@ -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<VoterRegistryMessage>
* @return List<VoterRegistryMessage>
*/
public static List<UnsignedBulletinBoardMessage> ConvertToVoterRegistryMessages(List<BulletinBoardMessage> messages){
List<UnsignedBulletinBoardMessage> 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<VoterRegistryMessage>
* @return Map{String:VoterRegistryMessage}
* @throws ParseException
*/
public static Map<String, BulletinBoardMessage> GetLatestGroupsActions(List<BulletinBoardMessage> messages)
throws ParseException, InvalidProtocolBufferException {
Map<String, BulletinBoardMessage> 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<String, VoterRegistryMessage>
* @return List<String>
*/
public static List<String> GetListOfGroupIds(Map<String, BulletinBoardMessage> groupIdToMessage) {
List<String> 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<VoterRegistryMessage>
* @return VoterRegistryMessage
* @throws ParseException
* @throws EmptyListException
*/
public static BulletinBoardMessage GetLatestMessage(List<BulletinBoardMessage> 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<String> 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<String> 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();
}
}

View File

@ -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<List<BulletinBoard
*/
@Override
public void onSuccess(List<BulletinBoardMessage> msg) {
try {
if(isAddToGroupsList(msg)) {
Map<String, BulletinBoardMessage> map = GetLatestGroupsActions(msg);
List<String> 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<String> 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<List<BulletinBoard
*/
@Override
public List<BulletinBoardMessage> validate(List<BulletinBoardMessage> object) throws ValidationError {
List<BulletinBoardMessage> verifiedMessages = new ArrayList<>();
List<BulletinBoardMessage> verifiedMessages = new ArrayList<>(object.size());
for (int i = 0 ; i < object.size() ; i++)
{
BulletinBoardMessage message = object.get(i);

View File

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

View File

@ -1,4 +1,4 @@
package meerkat.RegistryUtils;
package meerkat.registry;
import com.google.common.util.concurrent.FutureCallback;
import meerkat.VoterRegistry;

View File

@ -1,4 +1,4 @@
package meerkat.RegistryUtils;
package meerkat.registry;
/**

View File

@ -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<String> tags = new ArrayList<String>(){{ 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<String> tags = new ArrayList<String>(){{ 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<String> tags = new ArrayList<String>(){{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));
}
}