From 51b9f9decdce07d93d25dc2ab684da0a7f9c8d62 Mon Sep 17 00:00:00 2001 From: Vladimir Eliezer Tokarev Date: Sat, 9 Jan 2016 03:52:42 -0800 Subject: [PATCH] Changed the SimpleRegistry file started to implement the different methods of this class (such as created basic messages) --- .../main/java/meerkat/RegistryTagTypes.java | 15 +- .../src/main/java/meerkat/SimpleRegistry.java | 256 ++++++++++++++---- 2 files changed, 209 insertions(+), 62 deletions(-) diff --git a/voter-registry/src/main/java/meerkat/RegistryTagTypes.java b/voter-registry/src/main/java/meerkat/RegistryTagTypes.java index a175970..9bedeff 100644 --- a/voter-registry/src/main/java/meerkat/RegistryTagTypes.java +++ b/voter-registry/src/main/java/meerkat/RegistryTagTypes.java @@ -8,19 +8,18 @@ public abstract class RegistryTagTypes { private RegistryTagTypes(){} - // The ID tag - public static final String ID_TYPE = "ID:"; + public static final String ID_TAG = "ID:"; - // The Voter Entry Tag - public static final String VOTER_ENTRY_TAG = "Voter Entry:"; + public static final String VOTER_ENTRY_TAG = "Voter Entry"; - // THE Group ID Tag public static final String GROUP_ID_TAG = "Group ID:"; - // The Group Action Tag public static final String GROUP_ACTION_TAG = "Group Action:"; - // The Vote Action Tag - public static final String VOTE_ACTION_TAG = "Vote Action:"; + public static final String REMOVE_FROM_GROUP_TAG = "Remove From Group"; + + public static final String ADD_TO_GROUP_TAG = "Add To Group"; + + public static final String VOTE_ACTION_TAG = "Vote Action"; } diff --git a/voter-registry/src/main/java/meerkat/SimpleRegistry.java b/voter-registry/src/main/java/meerkat/SimpleRegistry.java index c5317ae..a9c5e0d 100644 --- a/voter-registry/src/main/java/meerkat/SimpleRegistry.java +++ b/voter-registry/src/main/java/meerkat/SimpleRegistry.java @@ -1,8 +1,18 @@ package meerkat; -import meerkat.ProtobufsMessages.*; -import meerkat.comm.CommunicationException; +import com.google.protobuf.*; +import meerkat.ProtobufsMessages.BasicMessage; +import meerkat.ProtobufsMessages.BulletinBoardMessage; +import meerkat.ProtobufsMessages.Tag; +import meerkat.bulletinboard.BulletinBoardClient; +import meerkat.comm.CommunicationException; +import meerkat.crypto.DigitalSignature; +import meerkat.protobuf.BulletinBoardAPI; + +import java.io.IOException; +import java.io.OutputStream; import java.util.List; +import java.util.Map; /** * Created by Vladimir Eliezer Tokarev on 1/8/2016. @@ -11,92 +21,230 @@ import java.util.List; */ public class SimpleRegistry { - public SimpleRegistry(){ - // inits the encoding object - // inits the connection data + protected DigitalSignature signatory; + + protected BulletinBoardClient communicator; + + /** + * @param signatory implements the DigitalSignature interface + * @param communicator implements the BulletinBoardClient interface + */ + public SimpleRegistry(DigitalSignature signatory, BulletinBoardClient communicator){ + this.signatory = signatory; + this.communicator = communicator; } /** - * Tries to add new voter to the bulletin-board + * Adds new voter to the bulletin-board * @param voterID * @param personalData for example residence location * @throws throws CommunicationException - * @return true if the adding action succeeded else return false + * @return void */ - public boolean AddVoter(String voterID, String personalData) throws CommunicationException { - /** - * try: - * Create basic message for adding new voter - * Create bulletin board message using CreateBulletinBoardMessage - * use SimpleBulletinBoardClient.PostMessage to send the message - * return true - * catch exception: - * return false - */ + public void AddVoter(String voterID, String personalData) throws CommunicationException { + Tag.Builder idTag = Tag.newBuilder(); + idTag.setContent(RegistryTagTypes.ID_TAG + " " + voterID); + + Tag.Builder voterEntryTag = Tag.newBuilder(); + voterEntryTag.setContent(RegistryTagTypes.VOTER_ENTRY_TAG); + + BasicMessage.Builder basicMessage = BasicMessage.newBuilder(); + basicMessage.setData(ByteString.copyFrom(personalData.getBytes())); + basicMessage.addTag(idTag); + basicMessage.addTag(voterEntryTag); + + communicator.postMessage(CreateBulletinBoardMessage(basicMessage), null); } /** - * Tries adding given voter to given group + * Adding given voter to given group * @param voterID * @param groupID * @throws CommunicationException * @return true if the adding action succeeded else return false */ - public boolean AddToGroup(String voterID, String groupID) throws CommunicationException { - /** - * try: - * Create basic message for adding the given voter to the wanted group - * Create bulletin board message using CreateBulletinBoardMessage - * use SimpleBulletinBoardClient.PostMessage to send the message - * return true - * catch exception: - * return false - */ + public void AddToGroup(String voterID, String groupID) throws CommunicationException { + Tag.Builder idTag = Tag.newBuilder(); + idTag.setContent(RegistryTagTypes.ID_TAG + " " + voterID); + + Tag.Builder groupIDTag = Tag.newBuilder(); + groupIDTag.setContent(RegistryTagTypes.GROUP_ID_TAG + " " + groupID); + + Tag.Builder actionTag = Tag.newBuilder(); + actionTag.setContent(RegistryTagTypes.GROUP_ACTION_TAG + " " + RegistryTagTypes.ADD_TO_GROUP_TAG); + + BasicMessage.Builder basicMessage = BasicMessage.newBuilder(); + basicMessage.addTag(idTag); + basicMessage.addTag(groupIDTag); + basicMessage.addTag(actionTag); + + communicator.postMessage(CreateBulletinBoardMessage(basicMessage), null); } /** - * Tries remove given voter from given group + * Removes given voter from given group * @param voterID * @param groupID * @return true if the removing action succeeded else return false * @throws CommunicationException */ - public boolean RemoveFromGroup(String voterID, String groupID) throws CommunicationException { - /** - * try: - * Create basic message for removing the given voter from the wanted group - * Create bulletin board message using CreateBulletinBoardMessage - * use SimpleBulletinBoardClient.PostMessage to send the message - * return true - * catch exception: - * return false - */ + public void RemoveFromGroup(String voterID, String groupID) throws CommunicationException { + Tag.Builder idTag = Tag.newBuilder(); + idTag.setContent(RegistryTagTypes.ID_TAG + " " + voterID); + + Tag.Builder groupIDTag = Tag.newBuilder(); + groupIDTag.setContent(RegistryTagTypes.GROUP_ID_TAG + " " + groupID); + + Tag.Builder actionTag = Tag.newBuilder(); + actionTag.setContent(RegistryTagTypes.GROUP_ACTION_TAG + " " + RegistryTagTypes.REMOVE_FROM_GROUP_TAG); + + BasicMessage.Builder basicMessage = BasicMessage.newBuilder(); + basicMessage.addTag(idTag); + basicMessage.addTag(groupIDTag); + basicMessage.addTag(actionTag); + + communicator.postMessage(CreateBulletinBoardMessage(basicMessage), null); } /** - * Tries to set that the voter have voted + * Sets that the voter have voted * @param id * @return true if the set voted succeded else false * @throws CommunicationException */ - public boolean SetVoted(String id) throws CommunicationException { - /** - * try: - * Create basic message set voted - * Create bulletin board message using CreateBulletinBoardMessage - * use SimpleBulletinBoardClient.PostMessage to send the message - * return true - * catch exception: - * return false - */ + public void SetVoted(String id) throws CommunicationException { + Tag.Builder idTag = Tag.newBuilder(); + idTag.setContent(RegistryTagTypes.ID_TAG + " " + id); + + Tag.Builder voteAction = Tag.newBuilder(); + voteAction.setContent(RegistryTagTypes.VOTE_ACTION_TAG); + + BasicMessage.Builder basicMessage = BasicMessage.newBuilder(); + basicMessage.addTag(idTag); + basicMessage.addTag(voteAction); + + communicator.postMessage(CreateBulletinBoardMessage(basicMessage), null); } /** - * Craetes bulletin board message from basic message (with the signatures parts) - * @param basicMessage array of bytes - * @return + * Creates bulletin board message from basic message (with the signatures parts) + * @param basicMessage BasicMessage.Builder + * @return BulletinBoardAPI.BulletinBoardMessage */ - private byte[] CreateBulletinBoardMessage(byte[] basicMessage) { + private BulletinBoardAPI.BulletinBoardMessage CreateBulletinBoardMessage(BasicMessage.Builder basicMessage) { + BulletinBoardMessage.Builder bulletinBoardmessage = BulletinBoardMessage.newBuilder(); + Message encodableMessage = new Message() { + @Override + public Parser getParserForType() { + return null; + } + + @Override + public Builder newBuilderForType() { + return null; + } + + @Override + public Builder toBuilder() { + return null; + } + + @Override + public void writeTo(CodedOutputStream output) throws IOException { + + } + + @Override + public int getSerializedSize() { + return 0; + } + + @Override + public ByteString toByteString() { + return null; + } + + @Override + public byte[] toByteArray() { + return new byte[0]; + } + + @Override + public void writeTo(OutputStream output) throws IOException { + + } + + @Override + public void writeDelimitedTo(OutputStream output) throws IOException { + + } + + @Override + public Message getDefaultInstanceForType() { + return null; + } + + @Override + public List findInitializationErrors() { + return null; + } + + @Override + public String getInitializationErrorString() { + return null; + } + + @Override + public Descriptors.Descriptor getDescriptorForType() { + return null; + } + + @Override + public Map getAllFields() { + return null; + } + + @Override + public boolean hasOneof(Descriptors.OneofDescriptor oneof) { + return false; + } + + @Override + public Descriptors.FieldDescriptor getOneofFieldDescriptor(Descriptors.OneofDescriptor oneof) { + return null; + } + + @Override + public boolean hasField(Descriptors.FieldDescriptor field) { + return false; + } + + @Override + public Object getField(Descriptors.FieldDescriptor field) { + return null; + } + + @Override + public int getRepeatedFieldCount(Descriptors.FieldDescriptor field) { + return 0; + } + + @Override + public Object getRepeatedField(Descriptors.FieldDescriptor field, int index) { + return null; + } + + @Override + public UnknownFieldSet getUnknownFields() { + return null; + } + + @Override + public boolean isInitialized() { + return false; + } + } + /** * encode the basic message * create bulletin board message