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
@ -21,6 +22,7 @@ public class VoterRegistryMessage {
/**
* Gets the wanted tag from given basic message
*
* @param tagName the name of the tag
* @return string
*/
@ -35,6 +37,7 @@ public class VoterRegistryMessage {
/**
* Gets the timestamp of the tag adding
*
* @return Timestamp
* @throws ParseException
*/
@ -52,6 +55,7 @@ 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() {
@ -62,4 +66,13 @@ public class VoterRegistryMessage {
}
return false;
}
/**
* converts the messages tags to list of strings
*
* @return List of strings
*/
public List<String> tagsToStringList(){
return base.getTagList();
}
}

View File

@ -33,7 +33,6 @@ public abstract class CollectionMessagesUtils {
* @throws ParseException
*/
public static Map<String, VoterRegistryMessage> GetLatestGroupsActions(List<VoterRegistryMessage> messages) throws ParseException {
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 != 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;
}
}