Splited LatestMessagesCallBack into number of callbacks according to their destanation.
							parent
							
								
									a9d96e59b2
								
							
						
					
					
						commit
						b4661d0fed
					
				|  | @ -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<BulletinBoardMessage> 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 | ||||
|  |  | |||
|  | @ -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)); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -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<List<BulletinBoardMessage>> { | ||||
|     protected FutureCallback<List<String>> callback; | ||||
| 
 | ||||
|     public GetGroupsCallback(FutureCallback<List<String>> callback){ | ||||
|         this.callback = callback; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onSuccess(List<BulletinBoardMessage> messages) { | ||||
|         callback.onSuccess(GetListOfTags(messages, RegistryTags.GROUP_ID_TAG)); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onFailure(Throwable t) { | ||||
|         this.callback.onFailure(t); | ||||
|     } | ||||
| } | ||||
|  | @ -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<List<BulletinBoardMessage>>{ | ||||
|     protected FutureCallback<Boolean> callback; | ||||
| 
 | ||||
|     public  HasVotedCallback(FutureCallback<Boolean> callback){ | ||||
|         this.callback = callback; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onSuccess(List<BulletinBoardMessage> 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); | ||||
|     } | ||||
| } | ||||
|  | @ -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<List<BulletinBoardMessage>> { | ||||
|     protected FutureCallback<VoterRegistry.VoterInfo> callback; | ||||
| 
 | ||||
|     public GetVoterCallback(FutureCallback<VoterRegistry.VoterInfo> callback){ | ||||
|         this.callback = callback; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onSuccess(List<BulletinBoardMessage> 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); | ||||
|     } | ||||
| } | ||||
|  | @ -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<BulletinBoardMessage> | ||||
|  */ | ||||
| public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoardMessage>> { | ||||
|     public FutureCallback callback; | ||||
|     protected Collection<DigitalSignature> validators; | ||||
|     protected String type; | ||||
| 
 | ||||
|     /** | ||||
|      * init MessagesCallBack | ||||
|      * @param callback  voter registry callback object | ||||
|      * @param validators DigitalSignature object | ||||
|      */ | ||||
|     public LatestMessagesCallBack(FutureCallback callback, | ||||
|                                   Collection<DigitalSignature> 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<BulletinBoardAPI.BulletinBoardMessage> | ||||
|      */ | ||||
|     @Override | ||||
|     public void onSuccess(List<BulletinBoardMessage> messages) { | ||||
|         BulletinBoardMessage lastAddedMessage = Collections.max(messages, (first, second) -> { | ||||
|             TimestampComparator comparator = new TimestampComparator(); | ||||
|             return comparator.compare(first.getMsg().getTimestamp(), second.getMsg().getTimestamp()); | ||||
|         }); | ||||
| 
 | ||||
|         List<BulletinBoardMessage> lastMessageList = new ArrayList<BulletinBoardMessage>(){{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); | ||||
|     } | ||||
| } | ||||
|  | @ -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. | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue