Moved the AccurateTimeStamp and the RegistryTagTypes to package called util

This way the project files are more organized
vote-registry
Vladimir Eliezer Tokarev 2016-01-15 06:27:15 -08:00
parent 36d94b41ab
commit 65bc8bc160
3 changed files with 125 additions and 51 deletions

View File

@ -1,21 +1,19 @@
package meerkat; package meerkat;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import com.google.protobuf.Message; import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.ProtobufsMessages.BasicMessage; import meerkat.ProtobufsMessages.BasicMessage;
import meerkat.ProtobufsMessages.Tag; import meerkat.ProtobufsMessages.Tag;
import meerkat.bulletinboard.BulletinBoardClient;
import meerkat.bulletinboard.SimpleBulletinBoardClient; import meerkat.bulletinboard.SimpleBulletinBoardClient;
import meerkat.comm.CommunicationException; import meerkat.comm.CommunicationException;
import meerkat.crypto.Encryption; import meerkat.crypto.Encryption;
import meerkat.crypto.concrete.ECElGamalEncryption;
import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.BulletinBoardAPI;
import meerkat.protobuf.Crypto; import meerkat.protobuf.Crypto;
import meerkat.protobuf.ConcreteCrypto import util.AccurateTimestamp;
import util.RegistryTagTypes;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.*;
import java.util.Random;
/** /**
* Created by Vladimir Eliezer Tokarev on 1/8/2016. * Created by Vladimir Eliezer Tokarev on 1/8/2016.
@ -26,20 +24,15 @@ public class SimpleRegistry {
protected Encryption signatory; protected Encryption signatory;
protected BulletinBoardClient communicator; protected SimpleBulletinBoardClient communicator;
public SimpleRegistry() {
signatory = new ECElGamalEncryption();
communicator = new SimpleBulletinBoardClient();
}
/** /**
* @param signatory implements the DigitalSignature interface * @param signatory implements the DigitalSignature interface
* @param communicator implements the BulletinBoardClient interface * @param communicator implements the BulletinBoardClient interface
*/ */
public SimpleRegistry(Encryption signatory, BulletinBoardClient communicator){ public SimpleRegistry(Encryption signatory, SimpleBulletinBoardClient communicator){
signatory = signatory; this.signatory = signatory;
communicator = communicator; this.communicator = communicator;
} }
/** /**
@ -49,19 +42,22 @@ public class SimpleRegistry {
* @throws throws CommunicationException * @throws throws CommunicationException
* @return void * @return void
*/ */
public void AddVoter(String voterID, String personalData) throws CommunicationException { public void AddVoter(String voterID, String personalData) throws CommunicationException, IOException {
Tag.Builder idTag = Tag.newBuilder(); Tag.Builder idTag = Tag.newBuilder();
idTag.setContent(RegistryTagTypes.ID_TAG + " " + voterID); idTag.setContent(RegistryTagTypes.ID_TAG + " " + voterID);
Tag.Builder voterEntryTag = Tag.newBuilder(); Tag.Builder voterEntryTag = Tag.newBuilder();
voterEntryTag.setContent(RegistryTagTypes.VOTER_ENTRY_TAG); voterEntryTag.setContent(RegistryTagTypes.VOTER_ENTRY_TAG);
BasicMessage.Builder basicMessage = BasicMessage.newBuilder(); Tag.Builder timestampTag = Tag.newBuilder();
basicMessage.setData(ByteString.copyFrom(personalData.getBytes())); timestampTag.setContent(RegistryTagTypes.ACTION_TIMESTAMP_TAG + " "
basicMessage.addTag(idTag); + AccurateTimestamp.GetCurrentTimestampString());
basicMessage.addTag(voterEntryTag);
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()), null); BasicMessage.Builder basicMessage =
BasicMessage.newBuilder().addTag(idTag).addTag(voterEntryTag).addTag(timestampTag);
basicMessage.setData(ByteString.copyFrom(personalData.getBytes()));
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
} }
/** /**
@ -71,7 +67,7 @@ public class SimpleRegistry {
* @throws CommunicationException * @throws CommunicationException
* @return true if the adding action succeeded else return false * @return true if the adding action succeeded else return false
*/ */
public void AddToGroup(String voterID, String groupID) throws CommunicationException { public void AddToGroup(String voterID, String groupID) throws CommunicationException, IOException {
Tag.Builder idTag = Tag.newBuilder(); Tag.Builder idTag = Tag.newBuilder();
idTag.setContent(RegistryTagTypes.ID_TAG + " " + voterID); idTag.setContent(RegistryTagTypes.ID_TAG + " " + voterID);
@ -81,12 +77,14 @@ public class SimpleRegistry {
Tag.Builder actionTag = Tag.newBuilder(); Tag.Builder actionTag = Tag.newBuilder();
actionTag.setContent(RegistryTagTypes.GROUP_ACTION_TAG + " " + RegistryTagTypes.ADD_TO_GROUP_TAG); actionTag.setContent(RegistryTagTypes.GROUP_ACTION_TAG + " " + RegistryTagTypes.ADD_TO_GROUP_TAG);
BasicMessage.Builder basicMessage = BasicMessage.newBuilder(); Tag.Builder timestampTag = Tag.newBuilder();
basicMessage.addTag(idTag); timestampTag.setContent(RegistryTagTypes.ACTION_TIMESTAMP_TAG + " "
basicMessage.addTag(groupIDTag); +AccurateTimestamp.GetCurrentTimestampString());
basicMessage.addTag(actionTag);
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()), null); BasicMessage.Builder basicMessage =
BasicMessage.newBuilder().addTag(idTag).addTag(groupIDTag).addTag(actionTag).addTag(timestampTag);
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
} }
/** /**
@ -96,7 +94,7 @@ public class SimpleRegistry {
* @return true if the removing action succeeded else return false * @return true if the removing action succeeded else return false
* @throws CommunicationException * @throws CommunicationException
*/ */
public void RemoveFromGroup(String voterID, String groupID) throws CommunicationException { public void RemoveFromGroup(String voterID, String groupID) throws CommunicationException, IOException {
Tag.Builder idTag = Tag.newBuilder(); Tag.Builder idTag = Tag.newBuilder();
idTag.setContent(RegistryTagTypes.ID_TAG + " " + voterID); idTag.setContent(RegistryTagTypes.ID_TAG + " " + voterID);
@ -106,12 +104,14 @@ public class SimpleRegistry {
Tag.Builder actionTag = Tag.newBuilder(); Tag.Builder actionTag = Tag.newBuilder();
actionTag.setContent(RegistryTagTypes.GROUP_ACTION_TAG + " " + RegistryTagTypes.REMOVE_FROM_GROUP_TAG); actionTag.setContent(RegistryTagTypes.GROUP_ACTION_TAG + " " + RegistryTagTypes.REMOVE_FROM_GROUP_TAG);
BasicMessage.Builder basicMessage = BasicMessage.newBuilder(); Tag.Builder timestampTag = Tag.newBuilder();
basicMessage.addTag(idTag); timestampTag.setContent(RegistryTagTypes.ACTION_TIMESTAMP_TAG + " "
basicMessage.addTag(groupIDTag); +AccurateTimestamp.GetCurrentTimestampString());
basicMessage.addTag(actionTag);
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()), null); BasicMessage.Builder basicMessage =
BasicMessage.newBuilder().addTag(idTag).addTag(groupIDTag).addTag(actionTag).addTag(timestampTag);
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
} }
/** /**
@ -120,18 +120,20 @@ public class SimpleRegistry {
* @return true if the set voted succeded else false * @return true if the set voted succeded else false
* @throws CommunicationException * @throws CommunicationException
*/ */
public void SetVoted(String id) throws CommunicationException { public void SetVoted(String id) throws CommunicationException, IOException {
Tag.Builder idTag = Tag.newBuilder(); Tag.Builder idTag = Tag.newBuilder();
idTag.setContent(RegistryTagTypes.ID_TAG + " " + id); idTag.setContent(RegistryTagTypes.ID_TAG + " " + id);
Tag.Builder voteAction = Tag.newBuilder(); Tag.Builder voteAction = Tag.newBuilder();
voteAction.setContent(RegistryTagTypes.VOTE_ACTION_TAG); voteAction.setContent(RegistryTagTypes.VOTE_ACTION_TAG);
BasicMessage.Builder basicMessage = BasicMessage.newBuilder(); Tag.Builder timestampTag = Tag.newBuilder();
basicMessage.addTag(idTag); timestampTag.setContent(RegistryTagTypes.ACTION_TIMESTAMP_TAG + " "
basicMessage.addTag(voteAction); +AccurateTimestamp.GetCurrentTimestampString());
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()), null); BasicMessage.Builder basicMessage = BasicMessage.newBuilder().addTag(idTag).addTag(voteAction).addTag(timestampTag);
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
} }
/** /**
@ -140,25 +142,93 @@ public class SimpleRegistry {
* @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();
// signs the basic message
Crypto.RerandomizableEncryptedMessage encryptedMessage =
signatory.encrypt(basicMessage, signatory.generateRandomness(new Random())); signatory.encrypt(basicMessage, signatory.generateRandomness(new Random()));
/**
* encode the basic message // creates the signature of signed basic message
* create bulletin board message Crypto.Signature.Builder messageSignature = Crypto.Signature.newBuilder();
* put the basic message in bulletin board message messageSignature.mergeFrom(encryptedMessage);
* put the encoded messages signature into the bulletin board message
* return the bulletin board message // creates the unsigned basic message
*/ BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder unsignedBulletinBoardMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder();
unsignedBulletinBoardMessage.setData(basicMessage.toByteString());
// sets the signature and the byte array
bulletinBoardMessage.addSig(messageSignature);
bulletinBoardMessage.setMsg(unsignedBulletinBoardMessage);
return bulletinBoardMessage.build();
} }
/**
* Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG
* @param id
* @return List<BulletinBoardAPI.BulletinBoardMessage>
*/
private List<BulletinBoardAPI.BulletinBoardMessage> GetRelevantMessages(String id) {
BulletinBoardAPI.MessageFilterList.Builder filters =
BulletinBoardAPI.MessageFilterList.newBuilder();
BulletinBoardAPI.MessageFilter.Builder idFilter =
BulletinBoardAPI.MessageFilter.newBuilder().setTag(RegistryTagTypes.ID_TAG + " " + id)
.setTag(RegistryTagTypes.GROUP_ACTION_TAG + " " + RegistryTagTypes.ADD_TO_GROUP_TAG)
.setTag(RegistryTagTypes.GROUP_ACTION_TAG + " " + RegistryTagTypes.REMOVE_FROM_GROUP_TAG);
filters.addFilter(idFilter);
return communicator.readMessages(filters.build());
}
/**
* Gets all the basic messages from bulletin board messages
* @param listOfMessages
* @return List<BasicMessage> G
* @throws InvalidProtocolBufferException
*/
private List<BasicMessage> GetBasicMessagesFromBulletinBoardMessages(
List<BulletinBoardAPI.BulletinBoardMessage> listOfMessages) throws InvalidProtocolBufferException {
List<BasicMessage> basicMessages = new ArrayList<BasicMessage>();
for (BulletinBoardAPI.BulletinBoardMessage bulletinBoardMessage : listOfMessages){
BasicMessage.Builder basicMessage =
BasicMessage.newBuilder().mergeFrom(bulletinBoardMessage.getMsg().getData());
basicMessages.add(basicMessage.build());
}
return basicMessages;
}
private List<String> GetAllGroupsIdsUserIn(List<BasicMessage> messages){
Map<String, BasicMessage> groupIdToMessage = new HashMap<String, BasicMessage>();
List<String> latestStateOfGroup = new ArrayList<String>();
for (BasicMessage message : messages) {
BasicMessage temporary = groupIdToMessage.get(message.get)
}
}
/** /**
* 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
* @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
*/ */
public List<String> GetGroups(String ID) throws CommunicationException { public List<String> GetGroups(String id) throws CommunicationException {
List<BulletinBoardAPI.BulletinBoardMessage> relevantMessages = GetRelevantMessages(id);
List<String> groups = new ArrayList<String>();
/** /**
* Retrieve all List<BulletinBoardMessage> that contains this id * Retrieve all List<BulletinBoardMessage> that contains this id
* creates new list of strings with the names of the group * creates new list of strings with the names of the group

View File

@ -1,4 +1,4 @@
package meerkat; package util;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.ParseException; import java.text.ParseException;

View File

@ -1,4 +1,4 @@
package meerkat; package util;
/** /**
* Created by Vladimir Eliezer Tokarev on 1/9/2016. * Created by Vladimir Eliezer Tokarev on 1/9/2016.
@ -20,6 +20,10 @@ public abstract class RegistryTagTypes {
public static final String ADD_TO_GROUP_TAG = "Add To Group"; public static final String ADD_TO_GROUP_TAG = "Add To Group";
public static final String ACTION_TIMESTAMP_TAG = "Action timestamp: ";
public static final String VOTE_ACTION_TAG = "Vote Action"; public static final String VOTE_ACTION_TAG = "Vote Action";
} }