Changed the SimpleRegistry

The new Simple Registry works according to the RegistryInstance interface
Voter-Registry
Vladimir Eliezer Tokarev 2016-01-23 03:31:04 -08:00
parent 7a167639db
commit c30ea072f2
3 changed files with 49 additions and 39 deletions

View File

@ -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<VoterRegistryMessage> GetRelevantVoterRegistryMessages(List<String> tags) throws InvalidProtocolBufferException {
List<BulletinBoardAPI.BulletinBoardMessage> relevantMessages = GetRelevantMessages(tags);
List<BulletinBoardAPI.UnsignedBulletinBoardMessage> 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<VoterRegistryMessage> voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
List<VoterRegistryMessage> voterRegistryMessages = null;
voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
Map<String, VoterRegistryMessage> groupIdToMessage = GetLatestGroupsActions(voterRegistryMessages);
Map<String, VoterRegistryMessage> 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<VoterRegistryMessage> voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
List<VoterRegistryMessage> 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);
}

View File

@ -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<String> tagsToStringList(){
return base.getTagList();
}
}

View File

@ -25,7 +25,7 @@ public abstract class CollectionMessagesUtils {
public static List<VoterRegistryMessage> ConvertToVoterRegistryMessages(List<BulletinBoardAPI.UnsignedBulletinBoardMessage> 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<VoterRegistryMessage>
@ -33,8 +33,7 @@ public abstract class CollectionMessagesUtils {
* @throws ParseException
*/
public static Map<String, VoterRegistryMessage> GetLatestGroupsActions(List<VoterRegistryMessage> messages) throws ParseException {
Map<String, VoterRegistryMessage> groupIdToMessage = new HashMap<>();
Map<String, VoterRegistryMessage> 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<BulletinBoardAPI.BulletinBoardMessage> listOfMessages) throws InvalidProtocolBufferException {
return listOfMessages.stream().map(BulletinBoardAPI.BulletinBoardMessage::getMsg).collect(Collectors.toList());
}
public static VoterRegistryMessage GetLatestMessage(List<VoterRegistryMessage> messages) throws ParseException {
VoterRegistryMessage LatestMessage = messages.get(0);
for (VoterRegistryMessage message : messages) {
if (message.GetBasicMessageActionTimestamp().before(LatestMessage.GetBasicMessageActionTimestamp())) {
LatestMessage = message;
}
}
return LatestMessage;
}
}