diff --git a/meerkat-common/src/main/java/meerkat/util/BulletinBoardUtils.java b/meerkat-common/src/main/java/meerkat/util/BulletinBoardUtils.java index 9ba29da..ca7e0d0 100644 --- a/meerkat-common/src/main/java/meerkat/util/BulletinBoardUtils.java +++ b/meerkat-common/src/main/java/meerkat/util/BulletinBoardUtils.java @@ -6,10 +6,7 @@ import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage; import meerkat.protobuf.Crypto; import java.security.SignatureException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; +import java.util.*; /** * Created by Arbel Deutsch Peled on 16-Feb-16. @@ -36,6 +33,18 @@ public class BulletinBoardUtils { return bulletinBoardMessage.build(); } + /** + * Gets the latest message from collection of messages (BulletinBoardMessage) based on timestamp + * @param messages List of BulletinBoardMessage + * @return BulletinBoardMessage + */ + public static BulletinBoardMessage getLatestMessage(Collection messages){ + return Collections.max(messages, (first, second) -> { + TimestampComparator comparator = new TimestampComparator(); + return comparator.compare(first.getMsg().getTimestamp(), second.getMsg().getTimestamp()); + }); + } + /** * Searches the tags in the message for one that begins with given prefix * @param message is the message to search diff --git a/voter-registry/src/main/java/meerkat/AsyncRegistry.java b/voter-registry/src/main/java/meerkat/AsyncRegistry.java index ee42877..aebe79c 100644 --- a/voter-registry/src/main/java/meerkat/AsyncRegistry.java +++ b/voter-registry/src/main/java/meerkat/AsyncRegistry.java @@ -8,7 +8,9 @@ import meerkat.protobuf.VoterRegistry.GroupID; import meerkat.protobuf.VoterRegistry.VoterID; import meerkat.protobuf.VoterRegistry.VoterInfo; import meerkat.protobuf.VoterRegistry.VoterRegistryMessage; -import meerkat.registry.LatestMessagesCallBack; +import meerkat.registry.AsyncRegistryCallbacks.GetGroupsCallback; +import meerkat.registry.AsyncRegistryCallbacks.GetVoterCallback; +import meerkat.registry.AsyncRegistryCallbacks.HasVotedCallback; import meerkat.registry.MessageCollectionUtils; import meerkat.registry.RegistryTags; import meerkat.util.BulletinBoardUtils; @@ -20,9 +22,8 @@ import java.util.List; import static meerkat.util.BulletinBoardUtils.signBulletinBoardMessage; -/** - * TODO : add ability to use DB of certificates - */ + +// TODO : add ability to use DB of certificates and add logging /** * Created by Vladimir Eliezer Tokarev on 1/8/2016. @@ -89,7 +90,7 @@ public class AsyncRegistry implements VoterRegistry{ }}; bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(GroupsActionsTags), - new LatestMessagesCallBack(callback, signers, "getGroups")); + new GetGroupsCallback(callback)); } @Override @@ -100,7 +101,7 @@ public class AsyncRegistry implements VoterRegistry{ }}; bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(addVoterTags), - new LatestMessagesCallBack(callback, signers, "getVoter")); + new GetVoterCallback(callback)); } @Override @@ -111,6 +112,6 @@ public class AsyncRegistry implements VoterRegistry{ }}; bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(setVotedTags), - new LatestMessagesCallBack(callBack, signers, "hasVoted")); + new HasVotedCallback(callBack)); } } diff --git a/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/GetGroupsCallback.java b/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/GetGroupsCallback.java new file mode 100644 index 0000000..9acd17b --- /dev/null +++ b/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/GetGroupsCallback.java @@ -0,0 +1,33 @@ +package meerkat.registry.AsyncRegistryCallbacks; + +import com.google.common.util.concurrent.FutureCallback; +import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; +import meerkat.registry.RegistryTags; + +import java.util.List; + +import static meerkat.util.BulletinBoardUtils.GetListOfTags; + +// TODO : add logging + +/** + * Created by Vladimir Eliezer on 3/12/2016. + * AsyncRegistry getGroups creates this callback to list of groups the voter in + */ +public class GetGroupsCallback implements FutureCallback> { + protected FutureCallback> callback; + + public GetGroupsCallback(FutureCallback> callback){ + this.callback = callback; + } + + @Override + public void onSuccess(List messages) { + callback.onSuccess(GetListOfTags(messages, RegistryTags.GROUP_ID_TAG)); + } + + @Override + public void onFailure(Throwable t) { + this.callback.onFailure(t); + } +} diff --git a/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/HasVotedCallback.java b/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/HasVotedCallback.java new file mode 100644 index 0000000..b768a21 --- /dev/null +++ b/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/HasVotedCallback.java @@ -0,0 +1,35 @@ +package meerkat.registry.AsyncRegistryCallbacks; + +import com.google.common.util.concurrent.FutureCallback; +import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; +import meerkat.registry.RegistryTags; + +import java.util.List; + +import static meerkat.util.BulletinBoardUtils.findTagWithPrefix; +import static meerkat.util.BulletinBoardUtils.getLatestMessage; + +// TODO : add logging + +/** + * Created by Vladimir Eliezer Tokarev on 3/12/2016. + * AsyncRegistry hasVoted creates this callback to check if wanted user has voted + */ +public class HasVotedCallback implements FutureCallback>{ + protected FutureCallback callback; + + public HasVotedCallback(FutureCallback callback){ + this.callback = callback; + } + + @Override + public void onSuccess(List messages) { + BulletinBoardMessage message = getLatestMessage(messages); + this.callback.onSuccess(findTagWithPrefix(message, RegistryTags.VOTE_ACTION_TAG) != null); + } + + @Override + public void onFailure(Throwable t) { + this.callback.onFailure(t); + } +} diff --git a/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/getVoterCallback.java b/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/getVoterCallback.java new file mode 100644 index 0000000..4c371f7 --- /dev/null +++ b/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/getVoterCallback.java @@ -0,0 +1,42 @@ +package meerkat.registry.AsyncRegistryCallbacks; + +import com.google.common.util.concurrent.FutureCallback; +import meerkat.protobuf.BulletinBoardAPI.*; +import meerkat.protobuf.VoterRegistry; +import meerkat.registry.RegistryTags; + +import java.util.List; + +import static meerkat.util.BulletinBoardUtils.findTagWithPrefix; +import static meerkat.util.BulletinBoardUtils.getLatestMessage; + +// TODO : add logging + +/** + * Created by Vladimir Eliezer on 3/12/2016. + * AsyncRegistry getVoter creates this callback to get voterInfo protobuff + */ +public class GetVoterCallback implements FutureCallback> { + protected FutureCallback callback; + + public GetVoterCallback(FutureCallback callback){ + this.callback = callback; + } + + @Override + public void onSuccess(List messages) { + BulletinBoardMessage latestMessage = getLatestMessage(messages); + + VoterRegistry.VoterInfo info = VoterRegistry.VoterInfo.newBuilder() + .setId(VoterRegistry.VoterID.newBuilder() + .setId(findTagWithPrefix(latestMessage, RegistryTags.ID_TAG))) + .setInfo(latestMessage.getMsg().getData().toStringUtf8()).build(); + + callback.onSuccess(info); + } + + @Override + public void onFailure(Throwable t) { + callback.onFailure(t); + } +} diff --git a/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java b/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java deleted file mode 100644 index 6b993be..0000000 --- a/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java +++ /dev/null @@ -1,84 +0,0 @@ -package meerkat.registry; - -import com.google.common.util.concurrent.FutureCallback; -import meerkat.crypto.DigitalSignature; -import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; -import meerkat.protobuf.VoterRegistry; -import meerkat.util.TimestampComparator; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -import static meerkat.util.BulletinBoardUtils.GetListOfTags; -import static meerkat.util.BulletinBoardUtils.findTagWithPrefix; - -/** - * TODO : add logging and verification of the messages by Arbel Peled - */ - -/** - * Created by Vladimir Eliezer Tokarev on 2/19/2016. - * Gets latest data from given List - */ -public class LatestMessagesCallBack implements FutureCallback> { - public FutureCallback callback; - protected Collection validators; - protected String type; - - /** - * init MessagesCallBack - * @param callback voter registry callback object - * @param validators DigitalSignature object - */ - public LatestMessagesCallBack(FutureCallback callback, - Collection validators, String type) { - this.callback = callback; - this.validators = validators; - this.type = type; - } - - /** - * Checks if the list of messages is of type GROUP_ID_TAG if it is then calls - * handleResult with mapping of the latest groups, else calls to handleResult with - * the latest tag from this list (in case of personal data) - * - * @param messages List - */ - @Override - public void onSuccess(List messages) { - BulletinBoardMessage lastAddedMessage = Collections.max(messages, (first, second) -> { - TimestampComparator comparator = new TimestampComparator(); - return comparator.compare(first.getMsg().getTimestamp(), second.getMsg().getTimestamp()); - }); - - List lastMessageList = new ArrayList(){{add(lastAddedMessage);}}; - switch (type){ - case "getGroups" : - callback.onSuccess(GetListOfTags(lastMessageList, RegistryTags.GROUP_ID_TAG)); - break; - case "getVoter" : - VoterRegistry.VoterInfo info = VoterRegistry.VoterInfo.newBuilder() - .setId(VoterRegistry.VoterID.newBuilder() - .setId(findTagWithPrefix(lastAddedMessage, RegistryTags.ID_TAG))) - .setInfo(lastAddedMessage.getMsg().getData().toStringUtf8()).build(); - - callback.onSuccess(info); - break; - case "hasVoted" : - callback.onSuccess(findTagWithPrefix(lastAddedMessage, - RegistryTags.VOTE_ACTION_TAG) != null); - break; - } - } - - /** - * Calls the callback handleResult method with false because the post method failed - * @param t the exception data that have been thrown during the failure of the post method - */ - @Override - public void onFailure(Throwable t) { - callback.onFailure(t); - } -} diff --git a/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java b/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java index 3345798..fa4c301 100644 --- a/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java +++ b/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java @@ -6,9 +6,9 @@ import meerkat.protobuf.BulletinBoardAPI.MessageFilterList; import java.util.List; -/** - * TODO: add logging to this utils - */ + +// TODO: add logging + /** * Created by Vladimir Eliezer Tokarev on 1/15/2016.