Ended the refactoring of the code acording to Arbel Peled first CR commit

Voter-Registry
Vladimir Eliezer Tokarev 2016-02-25 21:45:52 -08:00
parent 6ec0d4a668
commit 20d2b3e68c
3 changed files with 59 additions and 46 deletions

View File

@ -2,13 +2,11 @@ package meerkat;
import meerkat.bulletinboard.BulletinBoardClient; import meerkat.bulletinboard.BulletinBoardClient;
import meerkat.crypto.DigitalSignature; import meerkat.crypto.DigitalSignature;
import meerkat.protobuf.BulletinBoardAPI;
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
import meerkat.protobuf.BulletinBoardAPI.MessageFilter;
import meerkat.protobuf.BulletinBoardAPI.MessageFilterList;
import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage;
import meerkat.protobuf.Crypto; import meerkat.protobuf.Crypto;
import util.AccurateTimestamp; import util.AccurateTimestamp;
import util.CollectionMessagesUtils;
import util.RegistryTags; import util.RegistryTags;
import java.security.SignatureException; import java.security.SignatureException;
@ -55,29 +53,6 @@ public class SimpleRegistry implements VoterRegistry{
} }
} }
/**
* Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG
*
* @param tags the tags based on which the messages will be filtered
* @return MessageFilterList.
*/
public MessageFilterList GetRelevantMessagesFilters(List<String> tags) {
MessageFilterList.Builder filters = MessageFilterList.newBuilder();
if (tags == null){
return filters.build();
}
for (String tag : tags) {
MessageFilter.Builder filter =
MessageFilter.newBuilder().setTag(tag)
.setType(BulletinBoardAPI.FilterType.TAG);
filters.addFilter(filter);
}
return filters.build();
}
/** /**
* Tries to post basicMessage and return true if its been successfully posted * Tries to post basicMessage and return true if its been successfully posted
* *
@ -125,7 +100,7 @@ public class SimpleRegistry implements VoterRegistry{
List<String> GroupsActionsTags = new ArrayList<String>() {{ List<String> GroupsActionsTags = new ArrayList<String>() {{
add(RegistryTags.GROUP_ID_TAG + groupID.getId()); add(RegistryTags.GROUP_ID_TAG + groupID.getId());
}}; }};
bulletinBoardClient.readMessages(GetRelevantMessagesFilters(GroupsActionsTags), bulletinBoardClient.readMessages(CollectionMessagesUtils.GetRelevantMessagesFilters(GroupsActionsTags),
new RelevantDataCallBack(callback)); new RelevantDataCallBack(callback));
} }
@ -134,7 +109,7 @@ public class SimpleRegistry implements VoterRegistry{
add(RegistryTags.ID_TAG + voterID.getId()); add(RegistryTags.ID_TAG + voterID.getId());
add(RegistryTags.VOTER_ENTRY_TAG.toString()); add(RegistryTags.VOTER_ENTRY_TAG.toString());
}}; }};
bulletinBoardClient.readMessages(GetRelevantMessagesFilters(GroupsActionsTags), bulletinBoardClient.readMessages(CollectionMessagesUtils.GetRelevantMessagesFilters(GroupsActionsTags),
new RelevantDataCallBack(callback)); new RelevantDataCallBack(callback));
} }
} }

View File

@ -10,12 +10,20 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* Created by Vladimir Eliezer Tokarev on 1/15/2016. * Created by Vladimir Eliezer Tokarev on 1/15/2016.
* adds extra functionality to Messages collections * adds extra functionality to Messages collections
*/ */
public abstract class CollectionMessagesUtils { public abstract class CollectionMessagesUtils {
static class EmptyListException extends Exception {
public EmptyListException(String message) {
super(message);
}
}
/** /**
* Converts lost of UnsignedBulletinBoardMessage to VoterRegistryMessages * Converts lost of UnsignedBulletinBoardMessage to VoterRegistryMessages
* @param messages list<VoterRegistryMessage> * @param messages list<VoterRegistryMessage>
@ -65,9 +73,16 @@ public abstract class CollectionMessagesUtils {
return groupsIds; return groupsIds;
} }
public static VoterRegistryMessage GetLatestMessage(List<VoterRegistryMessage> messages) throws ParseException { /**
* Gets the message with the latest timestamp from messages
* @param messages List<VoterRegistryMessage>
* @return VoterRegistryMessage
* @throws ParseException
* @throws EmptyListException
*/
public static VoterRegistryMessage GetLatestMessage(List<VoterRegistryMessage> messages) throws ParseException, EmptyListException {
if (messages.size() == 0 ){ if (messages.size() == 0 ){
return null; throw new EmptyListException("The list of messages passed to GetLatestMessage is empty.");
} }
VoterRegistryMessage LatestMessage = messages.get(0); VoterRegistryMessage LatestMessage = messages.get(0);
@ -79,4 +94,26 @@ public abstract class CollectionMessagesUtils {
return LatestMessage; return LatestMessage;
} }
/**
* Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG
*
* @param tags the tags based on which the messages will be filtered
* @return MessageFilterList.
*/
public static BulletinBoardAPI.MessageFilterList GetRelevantMessagesFilters(List<String> tags) {
BulletinBoardAPI.MessageFilterList.Builder filters = BulletinBoardAPI.MessageFilterList.newBuilder();
if (tags.isEmpty()){
return filters.build();
}
for (String tag : tags) {
BulletinBoardAPI.MessageFilter.Builder filter =
BulletinBoardAPI.MessageFilter.newBuilder().setTag(tag)
.setType(BulletinBoardAPI.FilterType.TAG);
filters.addFilter(filter);
}
return filters.build();
}
} }

View File

@ -9,6 +9,7 @@ import meerkat.bulletinboard.ThreadedBulletinBoardClient;
import meerkat.crypto.concrete.ECDSASignature; import meerkat.crypto.concrete.ECDSASignature;
import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.BulletinBoardAPI;
import meerkat.protobuf.Voting; import meerkat.protobuf.Voting;
import util.CollectionMessagesUtils;
import util.RegistryTags; import util.RegistryTags;
import java.io.InputStream; import java.io.InputStream;
@ -35,7 +36,7 @@ import static util.CollectionMessagesUtils.ConvertToVoterRegistryMessages;
public class SimpleRegistryTest extends TestCase { public class SimpleRegistryTest extends TestCase {
private ECDSASignature signer; private ECDSASignature signer;
private BulletinBoardClient communicator; private BulletinBoardClient bulletinBoardClient;
private SecureRandom random = new SecureRandom(); private SecureRandom random = new SecureRandom();
public static String KEYFILE_EXAMPLE = "/certs/enduser-certs/user1-key-with-password-secret.p12"; public static String KEYFILE_EXAMPLE = "/certs/enduser-certs/user1-key-with-password-secret.p12";
public static String KEYFILE_PASSWORD = "secret"; public static String KEYFILE_PASSWORD = "secret";
@ -80,9 +81,9 @@ public class SimpleRegistryTest extends TestCase {
* Creates the communication object (the bulletinBoardClient) * Creates the communication object (the bulletinBoardClient)
*/ */
private void CommunicatorSetup() { private void CommunicatorSetup() {
communicator = new ThreadedBulletinBoardClient(); bulletinBoardClient = new ThreadedBulletinBoardClient();
String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081"; String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081";
communicator.init(Voting.BulletinBoardClientParams.newBuilder() bulletinBoardClient.init(Voting.BulletinBoardClientParams.newBuilder()
.addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS) .addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS)
.setMinRedundancy((float) 1.0) .setMinRedundancy((float) 1.0)
.build()); .build());
@ -120,7 +121,7 @@ public class SimpleRegistryTest extends TestCase {
*/ */
public void testSimpleRegistryCreation() { public void testSimpleRegistryCreation() {
try { try {
new SimpleRegistry(signer, communicator); new SimpleRegistry(signer, bulletinBoardClient);
} catch (Exception e) { } catch (Exception e) {
assert false : "While creating the SimpleRegistry exception have been thrown " + e; assert false : "While creating the SimpleRegistry exception have been thrown " + e;
} }
@ -165,16 +166,16 @@ public class SimpleRegistryTest extends TestCase {
RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder(). RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder().
setId(RegistryMessages.VoterID.newBuilder().setId(id)).setInfo(data).build(); setId(RegistryMessages.VoterID.newBuilder().setId(id)).setInfo(data).build();
SimpleRegistry registry = new SimpleRegistry(signer, communicator); SimpleRegistry registry = new SimpleRegistry(signer, bulletinBoardClient);
registry.AddVoter(voterInfo, handler); registry.AddVoter(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
assertEquals(1, handler.counter ); assertEquals(1, handler.counter );
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTER_ENTRY_TAG.toString());}}; List<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTER_ENTRY_TAG.toString());}};
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); BulletinBoardAPI.MessageFilterList filters = CollectionMessagesUtils.GetRelevantMessagesFilters(tags);
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
communicator.readMessages(filters, bulletinHandler); bulletinBoardClient.readMessages(filters, bulletinHandler);
jobSemaphore.acquire(); jobSemaphore.acquire();
@ -193,16 +194,16 @@ public class SimpleRegistryTest extends TestCase {
String id = new BigInteger(130, random).toString(32); String id = new BigInteger(130, random).toString(32);
RegistryMessages.VoterID voterInfo = RegistryMessages.VoterID.newBuilder().setId(id).build(); RegistryMessages.VoterID voterInfo = RegistryMessages.VoterID.newBuilder().setId(id).build();
SimpleRegistry registry = new SimpleRegistry(signer, communicator); SimpleRegistry registry = new SimpleRegistry(signer, bulletinBoardClient);
registry.SetVoted(voterInfo, handler); registry.SetVoted(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
assertEquals(1, handler.counter ); assertEquals(1, handler.counter );
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTE_ACTION_TAG.toString());}}; List<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTE_ACTION_TAG.toString());}};
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); BulletinBoardAPI.MessageFilterList filters = CollectionMessagesUtils.GetRelevantMessagesFilters(tags);
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
communicator.readMessages(filters, bulletinHandler); bulletinBoardClient.readMessages(filters, bulletinHandler);
jobSemaphore.acquire(); jobSemaphore.acquire();
@ -223,7 +224,7 @@ public class SimpleRegistryTest extends TestCase {
.setVoterId(RegistryMessages.VoterID.newBuilder().setId(voterId)) .setVoterId(RegistryMessages.VoterID.newBuilder().setId(voterId))
.setGroupId(RegistryMessages.GroupID.newBuilder().setId(groupId)).build(); .setGroupId(RegistryMessages.GroupID.newBuilder().setId(groupId)).build();
SimpleRegistry registry = new SimpleRegistry(signer, communicator); SimpleRegistry registry = new SimpleRegistry(signer, bulletinBoardClient);
registry.AddToGroup(voterInfo, handler); registry.AddToGroup(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
@ -231,9 +232,9 @@ public class SimpleRegistryTest extends TestCase {
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.GROUP_ACTION_TAG .toString() List<String> tags = new ArrayList<String>(){{ add(RegistryTags.GROUP_ACTION_TAG .toString()
+ RegistryTags.ADD_TO_GROUP_TAG.toString());}}; + RegistryTags.ADD_TO_GROUP_TAG.toString());}};
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); BulletinBoardAPI.MessageFilterList filters = CollectionMessagesUtils.GetRelevantMessagesFilters(tags);
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
communicator.readMessages(filters, bulletinHandler); bulletinBoardClient.readMessages(filters, bulletinHandler);
jobSemaphore.acquire(); jobSemaphore.acquire();
@ -257,7 +258,7 @@ public class SimpleRegistryTest extends TestCase {
.setVoterId(RegistryMessages.VoterID.newBuilder().setId(voterId)) .setVoterId(RegistryMessages.VoterID.newBuilder().setId(voterId))
.setGroupId(RegistryMessages.GroupID.newBuilder().setId(groupId)).build(); .setGroupId(RegistryMessages.GroupID.newBuilder().setId(groupId)).build();
SimpleRegistry registry = new SimpleRegistry(signer, communicator); SimpleRegistry registry = new SimpleRegistry(signer, bulletinBoardClient);
registry.AddToGroup(voterInfo, handler); registry.AddToGroup(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
@ -284,7 +285,7 @@ public class SimpleRegistryTest extends TestCase {
RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder(). RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder().
setId(RegistryMessages.VoterID.newBuilder().setId(id)).setInfo(data).build(); setId(RegistryMessages.VoterID.newBuilder().setId(id)).setInfo(data).build();
SimpleRegistry registry = new SimpleRegistry(signer, communicator); SimpleRegistry registry = new SimpleRegistry(signer, bulletinBoardClient);
registry.AddVoter(voterInfo, handler); registry.AddVoter(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();