diff --git a/bulletin-board-server/meerkat b/bulletin-board-server/meerkat index 46153c6..af8adb6 100644 Binary files a/bulletin-board-server/meerkat and b/bulletin-board-server/meerkat differ diff --git a/voter-registry/src/main/java/meerkat/MessageValidator.java b/voter-registry/src/main/java/meerkat/MessageValidator.java index 5d99279..b1da884 100644 --- a/voter-registry/src/main/java/meerkat/MessageValidator.java +++ b/voter-registry/src/main/java/meerkat/MessageValidator.java @@ -11,7 +11,7 @@ public interface MessageValidator * @param object object which will be checked * @return T object */ - T validate(T object) throws ValidationError ; + T validate(T object) throws ValidationError; class ValidationError extends Exception { public ValidationError(String message) { diff --git a/voter-registry/src/main/java/meerkat/Registry/BooleanCallBack.java b/voter-registry/src/main/java/meerkat/Registry/BooleanCallBack.java index 680a652..073587c 100644 --- a/voter-registry/src/main/java/meerkat/Registry/BooleanCallBack.java +++ b/voter-registry/src/main/java/meerkat/Registry/BooleanCallBack.java @@ -38,13 +38,13 @@ public class BooleanCallBack implements ClientCallback, MessageValidato */ @Override public void handleFailure(Throwable t) { - callback.HandleResult(false); + callback.HandleFailure(t); } /** - * always return true because there no need to check boolean + * always return true because there no n * @param object object which will be checked - * @return + * @return alwyas true * @throws ValidationError */ @Override diff --git a/voter-registry/src/main/java/meerkat/Registry/CollectionMessagesUtils.java b/voter-registry/src/main/java/meerkat/Registry/CollectionMessagesUtils.java index c44f009..385df42 100644 --- a/voter-registry/src/main/java/meerkat/Registry/CollectionMessagesUtils.java +++ b/voter-registry/src/main/java/meerkat/Registry/CollectionMessagesUtils.java @@ -1,11 +1,8 @@ package meerkat.Registry; import com.google.protobuf.InvalidProtocolBufferException; -import meerkat.VoterRegistryMessage; -import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; -import meerkat.protobuf.BulletinBoardAPI.FilterType; -import meerkat.protobuf.BulletinBoardAPI.MessageFilter; -import meerkat.protobuf.BulletinBoardAPI.MessageFilterList; +import meerkat.protobuf.BulletinBoardAPI.*; +import meerkat.protobuf.VoterRegistry; import java.text.ParseException; import java.util.*; @@ -31,33 +28,54 @@ public abstract class CollectionMessagesUtils { * @param messages list * @return List */ - public static List ConvertToVoterRegistryMessages(List messages){ - List voterMessages = new ArrayList<>(); + public static List ConvertToVoterRegistryMessages(List messages){ + List voterMessages = new ArrayList<>(); for (int i = 0 ; i < messages.size() ; i++) { - voterMessages.add(new VoterRegistryMessage(messages.get(i))); + voterMessages.add(messages.get(i).getMsg()); } return voterMessages; } + /** + * Return true if message1 was created before message 2 + * @param message1 BulletinBoardMessage object + * @param message2 BulletinBoardMessage object + * @return boolean + * @throws InvalidProtocolBufferException + */ + private static boolean FirstBeforeSecond(BulletinBoardMessage message1, BulletinBoardMessage message2) + throws InvalidProtocolBufferException { + try { + long firstCreationTime = VoterRegistry.VoterRegistryMessage.newBuilder() + .mergeFrom(message1.getMsg().getData()).build().getCreationMiliseconds(); + long secondCreationTime = VoterRegistry.VoterRegistryMessage.newBuilder() + .mergeFrom(message2.getMsg().getData()).build().getCreationMiliseconds(); + return firstCreationTime < secondCreationTime; + } catch (InvalidProtocolBufferException e) { + e.printStackTrace(); + throw e; + } + } + /** * Gets map of GroupId to basicMessage, where the basicMessages are the last actions for those groups * @param messages List * @return Map{String:VoterRegistryMessage} * @throws ParseException */ - public static Map GetLatestGroupsActions(List messages) + public static Map GetLatestGroupsActions(List messages) throws ParseException, InvalidProtocolBufferException { - Map groupIdToMessage = new HashMap<>(); + Map groupIdToMessage = new HashMap<>(messages.size()); // iterate trough all the messages and put into the map the last updated groups actions for (int i = 0 ; i < messages.size() ; i++) { - VoterRegistryMessage message = messages.get(i); - String groupId = message.GetWantedTagFromBasicMessage(RegistryTags.GROUP_ID_TAG); - VoterRegistryMessage temp = groupIdToMessage.get(groupId); + BulletinBoardMessage message = messages.get(i); + String groupId = GetTagByName(message.getMsg().getTagList(), RegistryTags.GROUP_ID_TAG); + BulletinBoardMessage temp = groupIdToMessage.get(groupId); if (temp != null && temp != message) { - if (temp.GetCreationTime() < message.GetCreationTime()) { + if (FirstBeforeSecond(temp, message)) { groupIdToMessage.put(groupId, message); } } @@ -71,13 +89,14 @@ public abstract class CollectionMessagesUtils { * @param groupIdToMessage Map * @return List */ - public static List GetListOfGroupIds(Map groupIdToMessage) { - List groupsIds = new ArrayList<>(); + public static List GetListOfGroupIds(Map groupIdToMessage) { + List groupsIds = new ArrayList<>(groupIdToMessage.size()); Iterator entries = groupIdToMessage.entrySet().iterator(); while (entries.hasNext()) { Map.Entry tuple = (Map.Entry) entries.next(); - VoterRegistryMessage message = (VoterRegistryMessage) tuple.getValue(); - String groupId = message.GetWantedTagFromBasicMessage(RegistryTags.GROUP_ID_TAG); + BulletinBoardMessage message = (BulletinBoardMessage) tuple.getValue(); + String groupId = GetTagByName(message.getMsg().getTagList(), RegistryTags.GROUP_ID_TAG); + groupId.replace(RegistryTags.GROUP_ID_TAG, ""); groupsIds.add(groupId); } return groupsIds; @@ -90,22 +109,39 @@ public abstract class CollectionMessagesUtils { * @throws ParseException * @throws EmptyListException */ - public static VoterRegistryMessage GetLatestMessage(List messages) + public static BulletinBoardMessage GetLatestMessage(List messages) throws ParseException, EmptyListException, InvalidProtocolBufferException { - System.out.print("1"); if (messages.size() == 0 ){ throw new EmptyListException("The list of messages passed to GetLatestMessage is empty."); } - VoterRegistryMessage LatestMessage = messages.get(0); + BulletinBoardMessage LatestMessage = messages.get(0); for (int i = 0 ; i < messages.size() ; i++) { - VoterRegistryMessage message = messages.get(i); - if (message.GetCreationTime() < LatestMessage.GetCreationTime()) { + BulletinBoardMessage message = messages.get(i); + if (FirstBeforeSecond(message, LatestMessage)) { LatestMessage = message; } } return LatestMessage; } + /** + * Gets the wanted tag from given basic message + * + * @param content the name of the tag + * @param tags the list of tags + * @return string + */ + public static String GetTagByName(List tags, String content) { + for (int i = 0 ; i < tags.size() ; i++) { + String tag = tags.get(i); + if (tag.startsWith(content) || tag.endsWith(content)) { + return tag; + } + } + return null; + } + + /** * Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG * diff --git a/voter-registry/src/main/java/meerkat/Registry/RelevantDataCallBack.java b/voter-registry/src/main/java/meerkat/Registry/RelevantDataCallBack.java index 9eabb1b..f6a662d 100644 --- a/voter-registry/src/main/java/meerkat/Registry/RelevantDataCallBack.java +++ b/voter-registry/src/main/java/meerkat/Registry/RelevantDataCallBack.java @@ -3,7 +3,6 @@ package meerkat.Registry; import com.google.protobuf.InvalidProtocolBufferException; import meerkat.MessageValidator; import meerkat.VoterRegistry.RegistryCallBack; -import meerkat.VoterRegistryMessage; import meerkat.bulletinboard.BulletinBoardClient.ClientCallback; import meerkat.crypto.DigitalSignature; import meerkat.protobuf.BulletinBoardAPI; @@ -35,7 +34,6 @@ public class RelevantDataCallBack implements ClientCallback msg) { try { - List messages = ConvertToVoterRegistryMessages(validate(msg)); - if(isAddToGroupsList(msg)) { - Map map = GetLatestGroupsActions(messages); + Map map = GetLatestGroupsActions(msg); List groupsOfUser = GetListOfGroupIds(map); callback.HandleResult(groupsOfUser); } else { - callback.HandleResult(GetLatestMessage(messages)); + callback.HandleResult(GetLatestMessage(msg)); } - } catch (ValidationError | ParseException | EmptyListException - | InvalidProtocolBufferException validationError) { - callback.HandleResult(null); + } catch (ParseException | EmptyListException | InvalidProtocolBufferException validationError) { + callback.HandleFailure(validationError); validationError.printStackTrace(); } } @@ -102,7 +97,7 @@ public class RelevantDataCallBack implements ClientCallback { void HandleResult(T result); + void HandleFailure(Throwable throwable); } /** diff --git a/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java b/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java deleted file mode 100644 index d232b8e..0000000 --- a/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java +++ /dev/null @@ -1,59 +0,0 @@ -package meerkat; - -import com.google.protobuf.InvalidProtocolBufferException; -import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; -import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage; -import meerkat.protobuf.VoterRegistry; - -import java.util.List; - - -/** - * TODO: add logging - */ - -/** - * Created by Vladimir Eliezer Tokarev on 1/15.2016 - * this class wraps BasicMessage and gives the ability to find wanted tags - */ -public class VoterRegistryMessage { - - public UnsignedBulletinBoardMessage base; - - public VoterRegistryMessage(BulletinBoardMessage message) { - UnsignedBulletinBoardMessage.Builder unsignedBase = - UnsignedBulletinBoardMessage.newBuilder().addAllTag(message.getMsg().getTagList()); - unsignedBase.setData(message.getMsg().getData()); - base = unsignedBase.build(); - } - - /** - * Gets the wanted tag from given basic message - * - * @param tagName the name of the tag - * @return string - */ - public String GetWantedTagFromBasicMessage(String tagName) { - List tags = base.getTagList(); - for (int i = 0 ; i < tags.size() ; i++) { - String tag = tags.get(i); - if (tag.contains(tagName)) { - return tag; - } - } - return null; - } - - /** - * Gets the creation time milliseconds of the tag adding - * @return long that represent the creation time - */ - public long GetCreationTime() throws InvalidProtocolBufferException { - try { - VoterRegistry.VoterRegistryMessage wrapper = VoterRegistry.VoterRegistryMessage.parseFrom(base.getData()); - return wrapper.getCreationMiliseconds(); - } catch (InvalidProtocolBufferException e) { - throw e; - } - } -} diff --git a/voter-registry/src/test/java/SimpleRegistryTest.java b/voter-registry/src/test/java/SimpleRegistryTest.java index 69c3dc2..4a0e2d6 100644 --- a/voter-registry/src/test/java/SimpleRegistryTest.java +++ b/voter-registry/src/test/java/SimpleRegistryTest.java @@ -3,7 +3,6 @@ import junit.framework.TestCase; import meerkat.Registry.CollectionMessagesUtils; import meerkat.Registry.RegistryTags; import meerkat.SimpleRegistry; -import meerkat.VoterRegistryMessage; import meerkat.bulletinboard.BulletinBoardClient; import meerkat.bulletinboard.ThreadedBulletinBoardClient; import meerkat.crypto.concrete.ECDSASignature; @@ -23,8 +22,6 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.Semaphore; -import static meerkat.Registry.CollectionMessagesUtils.ConvertToVoterRegistryMessages; - /** * TODO: add logs prints for the tests to be clear what they are */ @@ -62,6 +59,10 @@ public class SimpleRegistryTest extends TestCase { data = result; jobSemaphore.release(); } + + @Override + public void HandleFailure(Throwable throwable) { + } } public class DummyBulletinBoardCallBackHandler implements BulletinBoardClient.ClientCallback> { @@ -76,7 +77,6 @@ public class SimpleRegistryTest extends TestCase { @Override public void handleFailure(Throwable t){ - System.out.println(t); messages = null; jobSemaphore.release(); } @@ -139,17 +139,17 @@ public class SimpleRegistryTest extends TestCase { * @param tags List * @return integer that represent the amount of messages with wanted tags */ - private int countMessagesWithTags(List messages, List tags) + private int countMessagesWithTags(List messages, List tags) { int counter = 0 ; for (int i = 0 ; i < messages.size() ; i++) { - VoterRegistryMessage message = messages.get(i); + BulletinBoardMessage message = messages.get(i); int wantedTagsCounter = 0; for (int j = 0 ;j < tags.size() ; j++) { String tag = tags.get(j); - if(message.GetWantedTagFromBasicMessage(tag)!=null){ + if(CollectionMessagesUtils.GetTagByName(message.getMsg().getTagList(), tag)!=null){ wantedTagsCounter++; } } @@ -187,7 +187,7 @@ public class SimpleRegistryTest extends TestCase { tags.add(id); - int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags); + int counter = countMessagesWithTags(bulletinHandler.messages, tags); assert counter == 1 : "The server don't have the new user data."; } @@ -214,7 +214,7 @@ public class SimpleRegistryTest extends TestCase { jobSemaphore.acquire(); tags.add(id); - int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags); + int counter = countMessagesWithTags(bulletinHandler.messages, tags); assert counter == 1 : "The server don't have the new user id."; } @@ -246,7 +246,7 @@ public class SimpleRegistryTest extends TestCase { tags.add(voterId); tags.add(groupId); - int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags); + int counter = countMessagesWithTags(bulletinHandler.messages, tags); assert counter == 1 : "The server don't have the new user added to group."; } @@ -298,13 +298,14 @@ public class SimpleRegistryTest extends TestCase { jobSemaphore.acquire(); assertEquals(1, handler.counter ); - DummyRegistryCallBackHandler personalHandler = new DummyRegistryCallBackHandler<>(); + DummyRegistryCallBackHandler personalHandler = new DummyRegistryCallBackHandler<>(); registry.GetPersonIDDetails(VoterID.newBuilder().setId(id).build(), personalHandler); jobSemaphore.acquire(1); - assertEquals(RegistryTags.ID_TAG + id, - personalHandler.data.GetWantedTagFromBasicMessage(RegistryTags.ID_TAG)); - assertTrue(personalHandler.data.GetWantedTagFromBasicMessage(RegistryTags.VOTER_DATA_TAG).contains(data)); + assertEquals(RegistryTags.ID_TAG + id, CollectionMessagesUtils.GetTagByName(personalHandler + .data.getMsg().getTagList(), RegistryTags.ID_TAG)); + assertTrue(CollectionMessagesUtils.GetTagByName(personalHandler.data.getMsg().getTagList(), + RegistryTags.VOTER_DATA_TAG).contains(data)); } }