Re-organized the voter-registry

Since there no need for basicMessage i have removed it,
and changed few methods to be in language level 7
Voter-Registry
Vladimir Eliezer Tokarev 2016-01-22 10:22:36 -08:00
parent 89bac8d346
commit 01604d1acb
3 changed files with 183 additions and 89 deletions

View File

@ -0,0 +1,47 @@
package meerkat;
import java.util.List;
/**
* Created by Vladimir Eliezer Tokarev on 1/22/2016.
* Those methods will be called when their parallel methods ends running
* parallel means for instance first some registry will post voter then call HandleVoterPosted
*/
public interface RegistryCallBack {
/**
* Runs logic after voter have been posted
* @param succeeded shows if the adding of the voter succeeded
*/
void HandleVoterAdded(boolean succeeded);
/**
* Runs logic after voter have been removed form group
* @param succeeded shows if the removing of the voter succeeded
*/
void HandleVoterAddedToGroup(boolean succeeded);
/**
* Runs logic after voter have been added form group
* @param succeeded shows if the adding of the voter succeeded
*/
void HandleVoterRemovedFromGroup(boolean succeeded);
/**
* Runs logic after voter have voted
* @param succeeded shows if the voter voting have been documented successfully
*/
void HandleSetVoted(boolean succeeded);
/**
* Handles the voter information
* @param voterInformation list of information of the voter (its id, and personal data)
*/
void HandleVoterInfo(List<String> voterInformation);
/**
* Handles the groups the voter in
* @param groupsVoterIn list of groups ids of the voter
*/
void HandleVoterGroups(List<String> groupsVoterIn);
}

View File

@ -0,0 +1,65 @@
package meerkat;
/**
* Created by Vladimir Eliezer Tokarev on 1/22/2016.
* provides voters management options
*/
public abstract class RegistryInstance {
/**
* Adds new voter to the bulletin-board
*
* @param voterID id of the new user
* @param personalData for example residence location
* @param callBack when the adding voter done callBack.HandleVoterAdded will be called
* @return void
*/
abstract void AddVoter(String voterID, String personalData, RegistryCallBack callBack);
/**
* Adding given voter to given group
*
* @param voterID the voter id
* @param groupID the group id to which we want add the voter
* @param callBack when the adding voter done callBack.HandleVoterAddedToGroup will be called
* @return true if the adding action succeeded else return false
*/
abstract void AddToGroup(String voterID, String groupID, RegistryCallBack callBack);
/**
* Removes given voter from given group
*
* @param voterID the voter id
* @param groupID the group from which we want to remove the user
* @param callBack when the adding voter done callBack.HandleVoterRemovedFromGroup will be called
* @return true if the removing action succeeded else return false
*/
abstract void RemoveFromGroup(String voterID, String groupID, RegistryCallBack callBack);
/**
* Sets that the voter have voted
*
* @param id id tag string
* @param callBack when the adding voter done callBack.HandleSetVoted will be called
* @return true if the set voted succeed else false
*/
abstract void SetVoted(String id, RegistryCallBack callBack);
/**
* Requests all the groups that the given id voter is in
*
* @param id id tag string
* @param callBack when the adding voter done callBack.HandleVoterGroups will be called
* @return list of groups ids (or names), if the method fails its empty
*/
abstract void GetGroups(String id, RegistryCallBack callBack);
/**
* Retrieves list of strings that represents voter
*
* @param id id tag string
* @param callBack when the adding voter done callBack.HandleVoterInfo will be called
* @return list of strings (empty list if the lookup failed)
*/
abstract void GetPersonIDDetails(String id, RegistryCallBack callBack);
}

View File

@ -17,9 +17,9 @@ import java.util.*;
/**
* Created by Vladimir Eliezer Tokarev on 1/8/2016.
* Gives the ability to manage voters information
* Gives the ability to synchronously manage voters information
*/
public class SimpleRegistry {
public class SimpleRegistry extends RegistryInstance{
protected Encryption signatory;
@ -35,21 +35,24 @@ public class SimpleRegistry {
* @param basicMessage BasicMessage
* @return BulletinBoardAPI.BulletinBoardMessage
*/
private BulletinBoardAPI.BulletinBoardMessage CreateBulletinBoardMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage basicMessage) throws IOException {
BulletinBoardAPI.BulletinBoardMessage.Builder bulletinBoardMessage =
BulletinBoardAPI.BulletinBoardMessage.newBuilder();
private BulletinBoardAPI.BulletinBoardMessage CreateBulletinBoardMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage basicMessage) {
try {
BulletinBoardAPI.BulletinBoardMessage.Builder bulletinBoardMessage =
BulletinBoardAPI.BulletinBoardMessage.newBuilder();
Crypto.RerandomizableEncryptedMessage encryptedMessage =
signatory.encrypt(basicMessage, signatory.generateRandomness(new Random()));
Crypto.RerandomizableEncryptedMessage encryptedMessage =
signatory.encrypt(basicMessage, signatory.generateRandomness(new Random()));
Crypto.Signature.Builder messageSignature = Crypto.Signature.newBuilder();
messageSignature.mergeFrom(encryptedMessage);
Crypto.Signature.Builder messageSignature = Crypto.Signature.newBuilder();
messageSignature.mergeFrom(encryptedMessage);
bulletinBoardMessage.setMsg(basicMessage);
bulletinBoardMessage.setMsg(basicMessage);
bulletinBoardMessage.addSig(messageSignature);
bulletinBoardMessage.addSig(messageSignature);
return bulletinBoardMessage.build();
return bulletinBoardMessage.build();
} catch (IOException e) {
return null;
}
}
/**
@ -84,15 +87,22 @@ public class SimpleRegistry {
return CollectionMessagesUtils.ConvertToVoterRegistryMessages(messages);
}
/**
* Adds new voter to the bulletin-board
* @param voterID id of the new user
* @param personalData for example residence location
* @return void
* @throws CommunicationException, IOException
* Tries to post basicMessage and return true if its been successfully posted
*
* @param Message the massage to post
* @return true when the post was successful else false
*/
public void AddVoter(String voterID, String personalData) throws CommunicationException, IOException {
private boolean SafePost(BulletinBoardAPI.BulletinBoardMessage Message){
try {
communicator.postMessage(Message);
return true;
} catch (CommunicationException e) {
return false;
}
}
public void AddVoter(String voterID, String personalData, RegistryCallBack callback) {
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder().
addTag(RegistryTags.ID_TAG + " " + voterID)
@ -101,18 +111,10 @@ public class SimpleRegistry {
basicMessage.setData(ByteString.copyFrom(personalData.getBytes()));
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
callback.HandleVoterAdded(SafePost(CreateBulletinBoardMessage(basicMessage.build())));
}
/**
* Adding given voter to given group
*
* @param voterID the voter id
* @param groupID the group id to which we want add the voter
* @return true if the adding action succeeded else return false
* @throws CommunicationException
*/
public void AddToGroup(String voterID, String groupID) throws CommunicationException, IOException {
public void AddToGroup(String voterID, String groupID, RegistryCallBack callback) {
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + " " + voterID)
@ -120,18 +122,10 @@ public class SimpleRegistry {
.addTag(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.ADD_TO_GROUP_TAG)
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
callback.HandleVoterAddedToGroup(SafePost(CreateBulletinBoardMessage(basicMessage.build())));
}
/**
* Removes given voter from given group
*
* @param voterID the voter id
* @param groupID the group from which we want to remove the user
* @return true if the removing action succeeded else return false
* @throws CommunicationException
*/
public void RemoveFromGroup(String voterID, String groupID) throws CommunicationException, IOException {
public void RemoveFromGroup(String voterID, String groupID, RegistryCallBack callback) {
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + " " + voterID)
@ -139,73 +133,61 @@ public class SimpleRegistry {
.addTag(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.REMOVE_FROM_GROUP_TAG)
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
callback.HandleVoterRemovedFromGroup(SafePost(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 AddVoter(String id) throws CommunicationException, IOException {
public void SetVoted(String id, RegistryCallBack callback) {
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + " " + id)
.addTag(RegistryTags.VOTE_ACTION_TAG.toString())
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
callback.HandleVoterAddedToGroup(SafePost(CreateBulletinBoardMessage(basicMessage.build())));
}
/**
* Requests all the groups that the given id voter is in
*
* @param id id tag string
* @return list of groups ids (or names), if the method fails its empty
* @throws CommunicationException, InvalidProtocolBufferException
*/
public List<String> GetGroups(String id) throws CommunicationException, InvalidProtocolBufferException {
List<String> GroupsActionsTags = new ArrayList<String>(){{
add(RegistryTags.ID_TAG + " " + id);
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);
public void GetGroups(String id, RegistryCallBack callback) {
try {
List<String> GroupsActionsTags = new ArrayList<String>(){{
add(RegistryTags.ID_TAG + " " + id);
add(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.REMOVE_FROM_GROUP_TAG);
add(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.ADD_TO_GROUP_TAG);
}};
List<VoterRegistryMessage> voterRegistryMessages = null;
voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
Map<String, VoterRegistryMessage> groupIdToMessage = CollectionMessagesUtils.GetLatestGroupsActions(voterRegistryMessages);
return CollectionMessagesUtils.GetListOfGroupIds(groupIdToMessage);
} catch (ParseException e) {
return null;
callback.HandleVoterGroups(CollectionMessagesUtils.GetListOfGroupIds(groupIdToMessage));
} catch (ParseException | InvalidProtocolBufferException e) {
callback.HandleVoterGroups(null);
}
}
/**
* Retrieves list of strings that represents voter
*
* @param id id tag string
* @return list of strings (empty list if the lookup failed)
* @throws CommunicationException
*/
public List<String> GetPersonIDDetails(String id) throws CommunicationException, InvalidProtocolBufferException, ParseException {
List<String> GroupsActionsTags = new ArrayList<String>(){{
add(RegistryTags.ID_TAG + " " + id);
add(RegistryTags.VOTER_ENTRY_TAG.toString());
}};
public void GetPersonIDDetails(String id, RegistryCallBack callback) {
try {
List<String> GroupsActionsTags = new ArrayList<String>(){{
add(RegistryTags.ID_TAG + " " + id);
add(RegistryTags.VOTER_ENTRY_TAG.toString());
}};
List<VoterRegistryMessage> voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
VoterRegistryMessage LatestMessage = voterRegistryMessages.get(0);
List<VoterRegistryMessage> voterRegistryMessages =
GetRelevantVoterRegistryMessages(GroupsActionsTags);
VoterRegistryMessage LatestMessage = voterRegistryMessages.get(0);
for (VoterRegistryMessage message : voterRegistryMessages) {
if (message.GetBasicMessageActionTimestamp().before(LatestMessage.GetBasicMessageActionTimestamp())) {
LatestMessage = message;
// 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) {
callback.HandleVoterInfo(null);
}
return Arrays.asList(LatestMessage.GetWantedTagFromBasicMessage(RegistryTags.ID_TAG), LatestMessage.base.getData().toString());
}
}