Changed the SimpleRegistry
The new Simple Registry works according to the RegistryInstance interfaceVoter-Registry
parent
7a167639db
commit
c30ea072f2
|
@ -8,18 +8,19 @@ import meerkat.crypto.Encryption;
|
||||||
import meerkat.protobuf.BulletinBoardAPI;
|
import meerkat.protobuf.BulletinBoardAPI;
|
||||||
import meerkat.protobuf.Crypto;
|
import meerkat.protobuf.Crypto;
|
||||||
import util.AccurateTimestamp;
|
import util.AccurateTimestamp;
|
||||||
import util.CollectionMessagesUtils;
|
|
||||||
import util.RegistryTags;
|
import util.RegistryTags;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import static util.CollectionMessagesUtils.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Vladimir Eliezer Tokarev on 1/8/2016.
|
* Created by Vladimir Eliezer Tokarev on 1/8/2016.
|
||||||
* Gives the ability to synchronously manage voters information
|
* Gives the ability to synchronously manage voters information
|
||||||
*/
|
*/
|
||||||
public class SimpleRegistry extends RegistryInstance{
|
public class SimpleRegistry implements RegistryInstance{
|
||||||
|
|
||||||
protected Encryption signatory;
|
protected Encryption signatory;
|
||||||
|
|
||||||
|
@ -83,8 +84,8 @@ public class SimpleRegistry extends RegistryInstance{
|
||||||
private List<VoterRegistryMessage> GetRelevantVoterRegistryMessages(List<String> tags) throws InvalidProtocolBufferException {
|
private List<VoterRegistryMessage> GetRelevantVoterRegistryMessages(List<String> tags) throws InvalidProtocolBufferException {
|
||||||
List<BulletinBoardAPI.BulletinBoardMessage> relevantMessages = GetRelevantMessages(tags);
|
List<BulletinBoardAPI.BulletinBoardMessage> relevantMessages = GetRelevantMessages(tags);
|
||||||
List<BulletinBoardAPI.UnsignedBulletinBoardMessage> messages =
|
List<BulletinBoardAPI.UnsignedBulletinBoardMessage> messages =
|
||||||
CollectionMessagesUtils.GetUnsignedBulletinBoardMessages(relevantMessages);
|
GetUnsignedBulletinBoardMessages(relevantMessages);
|
||||||
return CollectionMessagesUtils.ConvertToVoterRegistryMessages(messages);
|
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.REMOVE_FROM_GROUP_TAG);
|
||||||
add(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.ADD_TO_GROUP_TAG);
|
add(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.ADD_TO_GROUP_TAG);
|
||||||
}};
|
}};
|
||||||
|
List<VoterRegistryMessage> voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
|
||||||
|
|
||||||
List<VoterRegistryMessage> voterRegistryMessages = null;
|
Map<String, VoterRegistryMessage> groupIdToMessage = GetLatestGroupsActions(voterRegistryMessages);
|
||||||
voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
|
|
||||||
|
|
||||||
Map<String, VoterRegistryMessage> groupIdToMessage = CollectionMessagesUtils.GetLatestGroupsActions(voterRegistryMessages);
|
callback.HandleVoterGroups(GetListOfGroupIds(groupIdToMessage));
|
||||||
callback.HandleVoterGroups(CollectionMessagesUtils.GetListOfGroupIds(groupIdToMessage));
|
|
||||||
} catch (ParseException | InvalidProtocolBufferException e) {
|
} catch (ParseException | InvalidProtocolBufferException e) {
|
||||||
callback.HandleVoterGroups(null);
|
callback.HandleVoterGroups(null);
|
||||||
}
|
}
|
||||||
|
@ -170,22 +170,11 @@ public class SimpleRegistry extends RegistryInstance{
|
||||||
add(RegistryTags.ID_TAG + " " + id);
|
add(RegistryTags.ID_TAG + " " + id);
|
||||||
add(RegistryTags.VOTER_ENTRY_TAG.toString());
|
add(RegistryTags.VOTER_ENTRY_TAG.toString());
|
||||||
}};
|
}};
|
||||||
|
List<VoterRegistryMessage> voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
|
||||||
|
|
||||||
List<VoterRegistryMessage> voterRegistryMessages =
|
VoterRegistryMessage LatestMessage = GetLatestMessage(voterRegistryMessages);
|
||||||
GetRelevantVoterRegistryMessages(GroupsActionsTags);
|
|
||||||
VoterRegistryMessage LatestMessage = voterRegistryMessages.get(0);
|
|
||||||
|
|
||||||
|
callback.HandleVoterInfo(LatestMessage.tagsToStringList());
|
||||||
// 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()));
|
|
||||||
} catch (InvalidProtocolBufferException | ParseException e) {
|
} catch (InvalidProtocolBufferException | ParseException e) {
|
||||||
callback.HandleVoterInfo(null);
|
callback.HandleVoterInfo(null);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import util.RegistryTags;
|
||||||
|
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Vladimir Eliezer Tokarev on 1/15.2016
|
* Created by Vladimir Eliezer Tokarev on 1/15.2016
|
||||||
|
@ -15,18 +16,19 @@ public class VoterRegistryMessage {
|
||||||
|
|
||||||
public BulletinBoardAPI.UnsignedBulletinBoardMessage base;
|
public BulletinBoardAPI.UnsignedBulletinBoardMessage base;
|
||||||
|
|
||||||
public VoterRegistryMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage message){
|
public VoterRegistryMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage message) {
|
||||||
base = BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder().addAllTag(message.getTagList()).build();
|
base = BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder().addAllTag(message.getTagList()).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the wanted tag from given basic message
|
* Gets the wanted tag from given basic message
|
||||||
|
*
|
||||||
* @param tagName the name of the tag
|
* @param tagName the name of the tag
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public String GetWantedTagFromBasicMessage(RegistryTags tagName){
|
public String GetWantedTagFromBasicMessage(RegistryTags tagName) {
|
||||||
for (String tag : base.getTagList()) {
|
for (String tag : base.getTagList()) {
|
||||||
if ( tag.contains(tagName.toString())) {
|
if (tag.contains(tagName.toString())) {
|
||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,12 +37,13 @@ public class VoterRegistryMessage {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the timestamp of the tag adding
|
* Gets the timestamp of the tag adding
|
||||||
|
*
|
||||||
* @return Timestamp
|
* @return Timestamp
|
||||||
* @throws ParseException
|
* @throws ParseException
|
||||||
*/
|
*/
|
||||||
public Timestamp GetBasicMessageActionTimestamp() throws ParseException {
|
public Timestamp GetBasicMessageActionTimestamp() throws ParseException {
|
||||||
for (String tag : base.getTagList()) {
|
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[] tagParts = tag.split(" ");
|
||||||
String timestamp = tagParts[tagParts.length - 1];
|
String timestamp = tagParts[tagParts.length - 1];
|
||||||
return AccurateTimestamp.GetTimestampFromString(timestamp);
|
return AccurateTimestamp.GetTimestampFromString(timestamp);
|
||||||
|
@ -52,14 +55,24 @@ public class VoterRegistryMessage {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the given message have the ADD_TO_GROUP_TAG
|
* Checks if the given message have the ADD_TO_GROUP_TAG
|
||||||
|
*
|
||||||
* @return true when ADD_TO_GROUP_TAG exist else false
|
* @return true when ADD_TO_GROUP_TAG exist else false
|
||||||
*/
|
*/
|
||||||
public boolean IsGroupAdding() {
|
public boolean IsGroupAdding() {
|
||||||
for (String tag : base.getTagList()) {
|
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 true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* converts the messages tags to list of strings
|
||||||
|
*
|
||||||
|
* @return List of strings
|
||||||
|
*/
|
||||||
|
public List<String> tagsToStringList(){
|
||||||
|
return base.getTagList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,7 @@ public abstract class CollectionMessagesUtils {
|
||||||
* @throws ParseException
|
* @throws ParseException
|
||||||
*/
|
*/
|
||||||
public static Map<String, VoterRegistryMessage> GetLatestGroupsActions(List<VoterRegistryMessage> messages) 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
|
// 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);
|
String groupId = message.GetWantedTagFromBasicMessage(RegistryTags.GROUP_ID_TAG);
|
||||||
VoterRegistryMessage temp = groupIdToMessage.get(groupId);
|
VoterRegistryMessage temp = groupIdToMessage.get(groupId);
|
||||||
|
|
||||||
if (temp != null) {
|
if (temp != null && temp != message) {
|
||||||
if (temp != message) {
|
if (temp.GetBasicMessageActionTimestamp().before(message.GetBasicMessageActionTimestamp())) {
|
||||||
if (temp.GetBasicMessageActionTimestamp().before(message.GetBasicMessageActionTimestamp())) {
|
groupIdToMessage.put(groupId, message);
|
||||||
groupIdToMessage.put(groupId, message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return groupIdToMessage;
|
return groupIdToMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,4 +75,16 @@ public abstract class CollectionMessagesUtils {
|
||||||
List<BulletinBoardAPI.BulletinBoardMessage> listOfMessages) throws InvalidProtocolBufferException {
|
List<BulletinBoardAPI.BulletinBoardMessage> listOfMessages) throws InvalidProtocolBufferException {
|
||||||
return listOfMessages.stream().map(BulletinBoardAPI.BulletinBoardMessage::getMsg).collect(Collectors.toList());
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue