Changed the simpleRegistry file

Edited the GetPersonIDDetails method , and refactored the basic methods of upload data to BulletinBoardServer
vote-registry
Vladimir Eliezer Tokarev 2016-01-16 06:00:07 -08:00
parent 7734ba8c91
commit 109135ae1b
3 changed files with 149 additions and 143 deletions

View File

@ -20,7 +20,6 @@ import java.util.*;
/** /**
* Created by Vladimir Eliezer Tokarev on 1/8/2016. * Created by Vladimir Eliezer Tokarev on 1/8/2016.
* Gives the ability to manage voters information * Gives the ability to manage voters information
* This object is synchronous (which meaning its method blocking)
*/ */
public class SimpleRegistry { public class SimpleRegistry {
@ -28,123 +27,27 @@ public class SimpleRegistry {
protected SimpleBulletinBoardClient communicator; protected SimpleBulletinBoardClient communicator;
/**
* @param signatory implements the DigitalSignature interface
* @param communicator implements the BulletinBoardClient interface
*/
public SimpleRegistry(Encryption signatory, SimpleBulletinBoardClient communicator) { public SimpleRegistry(Encryption signatory, SimpleBulletinBoardClient communicator) {
this.signatory = signatory; this.signatory = signatory;
this.communicator = communicator; this.communicator = communicator;
} }
/** private Tag.Builder[] GetTagsArrayInLength(int length) {
* Adds new voter to the bulletin-board Tag.Builder[] tags = new Tag.Builder[length];
* @param voterID
* @param personalData for example residence location
* @throws throws CommunicationException
* @return void
*/
public void AddVoter(String voterID, String personalData) throws CommunicationException, IOException {
Tag.Builder idTag = Tag.newBuilder();
idTag.setContent(Tags.ID_TAG + " " + voterID);
Tag.Builder voterEntryTag = Tag.newBuilder(); for (int i = 0; i < length; i++) {
voterEntryTag.setContent(Tags.VOTER_ENTRY_TAG.toString()); tags[i] = Tag.newBuilder();
}
Tag.Builder timestampTag = Tag.newBuilder(); return tags;
timestampTag.setContent(Tags.ACTION_TIMESTAMP_TAG + " "
+ AccurateTimestamp.GetCurrentTimestampString());
BasicMessage.Builder basicMessage =
BasicMessage.newBuilder().addTag(idTag).addTag(voterEntryTag).addTag(timestampTag);
basicMessage.setData(ByteString.copyFrom(personalData.getBytes()));
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
} }
/** /**
* Adding given voter to given group * Creates BulletinBoardMessage with signed basicMessage and UnsignedBulletinBoardMessage that contains the basic message
* @param voterID *
* @param groupID
* @throws CommunicationException
* @return true if the adding action succeeded else return false
*/
public void AddToGroup(String voterID, String groupID) throws CommunicationException, IOException {
Tag.Builder idTag = Tag.newBuilder();
idTag.setContent(Tags.ID_TAG + " " + voterID);
Tag.Builder groupIDTag = Tag.newBuilder();
groupIDTag.setContent(Tags.GROUP_ID_TAG + " " + groupID);
Tag.Builder actionTag = Tag.newBuilder();
actionTag.setContent(Tags.GROUP_ACTION_TAG + " " + Tags.ADD_TO_GROUP_TAG);
Tag.Builder timestampTag = Tag.newBuilder();
timestampTag.setContent(Tags.ACTION_TIMESTAMP_TAG + " "
+AccurateTimestamp.GetCurrentTimestampString());
BasicMessage.Builder basicMessage =
BasicMessage.newBuilder().addTag(idTag).addTag(groupIDTag).addTag(actionTag).addTag(timestampTag);
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
}
/**
* Removes given voter from given group
* @param voterID
* @param groupID
* @return true if the removing action succeeded else return false
* @throws CommunicationException
*/
public void RemoveFromGroup(String voterID, String groupID) throws CommunicationException, IOException {
Tag.Builder idTag = Tag.newBuilder();
idTag.setContent(Tags.ID_TAG + " " + voterID);
Tag.Builder groupIDTag = Tag.newBuilder();
groupIDTag.setContent(Tags.GROUP_ID_TAG + " " + groupID);
Tag.Builder actionTag = Tag.newBuilder();
actionTag.setContent(Tags.GROUP_ACTION_TAG + " " + Tags.REMOVE_FROM_GROUP_TAG);
Tag.Builder timestampTag = Tag.newBuilder();
timestampTag.setContent(Tags.ACTION_TIMESTAMP_TAG + " "
+AccurateTimestamp.GetCurrentTimestampString());
BasicMessage.Builder basicMessage =
BasicMessage.newBuilder().addTag(idTag).addTag(groupIDTag).addTag(actionTag).addTag(timestampTag);
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
}
/**
* Sets that the voter have voted
* @param id
* @return true if the set voted succeded else false
* @throws CommunicationException
*/
public void SetVoted(String id) throws CommunicationException, IOException {
Tag.Builder idTag = Tag.newBuilder();
idTag.setContent(Tags.ID_TAG + " " + id);
Tag.Builder voteAction = Tag.newBuilder();
voteAction.setContent(Tags.VOTE_ACTION_TAG.toString());
Tag.Builder timestampTag = Tag.newBuilder();
timestampTag.setContent(Tags.ACTION_TIMESTAMP_TAG + " "
+AccurateTimestamp.GetCurrentTimestampString());
BasicMessage.Builder basicMessage = BasicMessage.newBuilder().addTag(idTag).addTag(voteAction).addTag(timestampTag);
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
}
/**
* Creates bulletin board message from basic message (with the signatures parts)
* @param basicMessage BasicMessage * @param basicMessage BasicMessage
* @return BulletinBoardAPI.BulletinBoardMessage * @return BulletinBoardAPI.BulletinBoardMessage
*/ */
private BulletinBoardAPI.BulletinBoardMessage CreateBulletinBoardMessage(BasicMessage basicMessage) throws IOException { private BulletinBoardAPI.BulletinBoardMessage CreateBulletinBoardMessage(BasicMessage basicMessage) throws IOException {
BulletinBoardAPI.BulletinBoardMessage.Builder bulletinBoardMessage = BulletinBoardAPI.BulletinBoardMessage.Builder bulletinBoardMessage =
BulletinBoardAPI.BulletinBoardMessage.newBuilder(); BulletinBoardAPI.BulletinBoardMessage.newBuilder();
@ -167,62 +70,162 @@ public class SimpleRegistry {
/** /**
* Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG * Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG
* @param id *
* @param tags the tags based on which the messages will be filtered
* @return List<BulletinBoardAPI.BulletinBoardMessage> * @return List<BulletinBoardAPI.BulletinBoardMessage>
*/ */
private List<BulletinBoardAPI.BulletinBoardMessage> GetRelevantMessages(String id) { private List<BulletinBoardAPI.BulletinBoardMessage> GetRelevantMessages(List<Tag> tags) {
BulletinBoardAPI.MessageFilterList.Builder filters = BulletinBoardAPI.MessageFilterList.Builder filters =
BulletinBoardAPI.MessageFilterList.newBuilder(); BulletinBoardAPI.MessageFilterList.newBuilder();
for (Tag tag : tags) {
BulletinBoardAPI.MessageFilter.Builder idFilter = BulletinBoardAPI.MessageFilter.Builder idFilter =
BulletinBoardAPI.MessageFilter.newBuilder().setTag(Tags.ID_TAG + " " + id) BulletinBoardAPI.MessageFilter.newBuilder().setTag(tag.getContent());
.setTag(Tags.GROUP_ACTION_TAG + " " + Tags.ADD_TO_GROUP_TAG)
.setTag(Tags.GROUP_ACTION_TAG + " " + Tags.REMOVE_FROM_GROUP_TAG);
filters.addFilter(idFilter); filters.addFilter(idFilter);
}
return communicator.readMessages(filters.build()); return communicator.readMessages(filters.build());
} }
/**
* Gets Relevant bulletinBoard messages from communicator than converts them to VoterRegistryMessages
*
* @param tags list of tags that will be used as filters
* @return List<VoterRegistryMessage>
*/
private List<VoterRegistryMessage> GetRelevantVoterRegistryMessages(List<Tag> tags) throws InvalidProtocolBufferException {
List<BulletinBoardAPI.BulletinBoardMessage> relevantMessages = GetRelevantMessages(tags);
List<BasicMessage> messages = CollectionMessagesUtils.GetBasicMessagesFromBulletinBoardMessages(relevantMessages);
List<VoterRegistryMessage> voterRegistryMessages = CollectionMessagesUtils.ConvertToVoterRegistryMessages(messages);
return voterRegistryMessages;
}
/**
* Adds new voter to the bulletin-board
*
* @param voterID
* @param personalData for example residence location
* @return void
* @throws throws CommunicationException
*/
public void SetVoted(String voterID, String personalData) throws CommunicationException, IOException {
Tag.Builder[] tags = GetTagsArrayInLength(3);
tags[0].setContent(Tags.ID_TAG + " " + voterID);
tags[1].setContent(Tags.VOTER_ENTRY_TAG.toString());
tags[2].setContent(Tags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
BasicMessage.Builder basicMessage = BasicMessage.newBuilder().addTag(tags[0]).addTag(tags[1]).addTag(tags[1]);
basicMessage.setData(ByteString.copyFrom(personalData.getBytes()));
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
}
/**
* Adding given voter to given group
*
* @param voterID
* @param groupID
* @return true if the adding action succeeded else return false
* @throws CommunicationException
*/
public void AddToGroup(String voterID, String groupID) throws CommunicationException, IOException {
Tag.Builder[] tags = GetTagsArrayInLength(4);
tags[0].setContent(Tags.ID_TAG + " " + voterID);
tags[1].setContent(Tags.GROUP_ID_TAG + " " + groupID);
tags[2].setContent(Tags.GROUP_ACTION_TAG + " " + Tags.ADD_TO_GROUP_TAG);
tags[3].setContent(Tags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
BasicMessage.Builder basicMessage =
BasicMessage.newBuilder().addTag(tags[0]).addTag(tags[1]).addTag(tags[2]).addTag(tags[3]);
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
}
/**
* Removes given voter from given group
*
* @param voterID
* @param groupID
* @return true if the removing action succeeded else return false
* @throws CommunicationException
*/
public void RemoveFromGroup(String voterID, String groupID) throws CommunicationException, IOException {
Tag.Builder[] tags = GetTagsArrayInLength(4);
tags[0].setContent(Tags.ID_TAG + " " + voterID);
tags[1].setContent(Tags.GROUP_ID_TAG + " " + groupID);
tags[2].setContent(Tags.GROUP_ACTION_TAG + " " + Tags.REMOVE_FROM_GROUP_TAG);
tags[3].setContent(Tags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
BasicMessage.Builder basicMessage =
BasicMessage.newBuilder().addTag(tags[0]).addTag(tags[1]).addTag(tags[2]).addTag(tags[3]);
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
}
/**
* Sets that the voter have voted
*
* @param id id tag string
* @return true if the set voted succeded else false
* @throws CommunicationException
*/
public void SetVoted(String id) throws CommunicationException, IOException {
Tag.Builder[] tags = GetTagsArrayInLength(3);
tags[0].setContent(Tags.ID_TAG + " " + id);
tags[1].setContent(Tags.VOTE_ACTION_TAG.toString());
tags[2].setContent(Tags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
BasicMessage.Builder basicMessage = BasicMessage.newBuilder().addTag(tags[0]).addTag(tags[1]).addTag(tags[2]);
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
}
/** /**
* Requests all the groups that the given id voter is in * Requests all the groups that the given id voter is in
* @param id *
* @param id id tag string
* @return list of groups ids (or names), if the method fails its empty * @return list of groups ids (or names), if the method fails its empty
* @throws CommunicationException * @throws CommunicationException, InvalidProtocolBufferException
*/ */
public List<String> GetGroups(String id) throws CommunicationException, InvalidProtocolBufferException { public List<String> GetGroups(String id) throws CommunicationException, InvalidProtocolBufferException {
List<BulletinBoardAPI.BulletinBoardMessage> relevantMessages = GetRelevantMessages(id); List<Tag> GroupsActionsTags = new ArrayList<Tag>();
GroupsActionsTags.add(Tag.newBuilder().setContent(Tags.ID_TAG + " " + id).build());
GroupsActionsTags.add(Tag.newBuilder().setContent(Tags.GROUP_ACTION_TAG + " " + Tags.REMOVE_FROM_GROUP_TAG).build());
GroupsActionsTags.add(Tag.newBuilder().setContent(Tags.GROUP_ACTION_TAG + " " + Tags.ADD_TO_GROUP_TAG).build());
List<BasicMessage> messages = List<VoterRegistryMessage> voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
CollectionMessagesUtils.GetBasicMessagesFromBulletinBoardMessages(relevantMessages);
List<VoterRegistryMessage> voterRegistryMessages =
CollectionMessagesUtils.ConvertToVoterRegistryMessages(messages);
try { try {
Map<String, VoterRegistryMessage> groupIdToMessage = Map<String, VoterRegistryMessage> groupIdToMessage = CollectionMessagesUtils.GetLatestGroupsActions(voterRegistryMessages);
CollectionMessagesUtils.GetLatestGroupsActions(voterRegistryMessages);
return CollectionMessagesUtils.GetListOfGroupIds(groupIdToMessage); return CollectionMessagesUtils.GetListOfGroupIds(groupIdToMessage);
} } catch (ParseException e) {
catch (ParseException e)
{
// write log
return null; return null;
} }
} }
/** /**
* Retrieves list of strings that represents voter * Retrieves list of strings that represents voter
* @param id *
* @param id id tag string
* @return list of strings (empty list if the lookup failed) * @return list of strings (empty list if the lookup failed)
* @throws CommunicationException * @throws CommunicationException
*/ */
public List<String> GetPersonIDDetails(String id) throws CommunicationException { public List<String> GetPersonIDDetails(String id) throws CommunicationException, InvalidProtocolBufferException, ParseException {
/** List<Tag> GroupsActionsTags = new ArrayList<Tag>();
* Retrieve all List<BulletinBoardMessage> that contains this id GroupsActionsTags.add(Tag.newBuilder().setContent(Tags.ID_TAG + " " + id).build());
* search for message with the wanted personal data GroupsActionsTags.add(Tag.newBuilder().setContent(Tags.VOTER_ENTRY_TAG.toString()).build());
* Create list with personal data
* return the list List<VoterRegistryMessage> voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
*/ VoterRegistryMessage LatestMessage = voterRegistryMessages.get(0);
for (VoterRegistryMessage message : voterRegistryMessages) {
if (LatestMessage.GetBasicMessageActionTimestamp().before(message.GetBasicMessageActionTimestamp()) {
LatestMessage = message;
}
}
return Arrays.asList(LatestMessage.GetWantedTagFromBasicMessage(Tags.ID_TAG), LatestMessage.base.getData().toString());
} }
} }

View File

@ -12,7 +12,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* Created by Dasha on 1/15/2016. * Created by Vladimir Eliezer Tokarev on 1/15/2016.
* adds extra functionality to Messages collections
*/ */
public abstract class CollectionMessagesUtils { public abstract class CollectionMessagesUtils {
@ -47,7 +48,7 @@ public abstract class CollectionMessagesUtils {
if (temp != null) { if (temp != null) {
if (temp != messages.get(i)) { if (temp != messages.get(i)) {
if (temp.GetBasicMessageActionTimestamp().compareTo(messages.get(i).GetBasicMessageActionTimestamp()) < 0) { if (temp.GetBasicMessageActionTimestamp().before(messages.get(i).GetBasicMessageActionTimestamp())) {
groupIdToMessage.put(groupId, messages.get(i)); groupIdToMessage.put(groupId, messages.get(i));
} }
} }

View File

@ -27,3 +27,5 @@ public enum Tags {
} }