Removed the voterRegistryMessage and swaped the usage of it to BulletinBoardMessage

Voter-Registry
Vladimir Eliezer Tokarev 2016-02-27 03:46:36 -08:00
parent 5169c935bf
commit 87420a9d7d
8 changed files with 85 additions and 110 deletions

Binary file not shown.

View File

@ -38,13 +38,13 @@ public class BooleanCallBack implements ClientCallback<Boolean>, MessageValidato
*/ */
@Override @Override
public void handleFailure(Throwable t) { 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 * @param object object which will be checked
* @return * @return alwyas true
* @throws ValidationError * @throws ValidationError
*/ */
@Override @Override

View File

@ -1,11 +1,8 @@
package meerkat.Registry; package meerkat.Registry;
import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.VoterRegistryMessage; import meerkat.protobuf.BulletinBoardAPI.*;
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.VoterRegistry;
import meerkat.protobuf.BulletinBoardAPI.FilterType;
import meerkat.protobuf.BulletinBoardAPI.MessageFilter;
import meerkat.protobuf.BulletinBoardAPI.MessageFilterList;
import java.text.ParseException; import java.text.ParseException;
import java.util.*; import java.util.*;
@ -31,33 +28,54 @@ public abstract class CollectionMessagesUtils {
* @param messages list<VoterRegistryMessage> * @param messages list<VoterRegistryMessage>
* @return List<VoterRegistryMessage> * @return List<VoterRegistryMessage>
*/ */
public static List<VoterRegistryMessage> ConvertToVoterRegistryMessages(List<BulletinBoardMessage> messages){ public static List<UnsignedBulletinBoardMessage> ConvertToVoterRegistryMessages(List<BulletinBoardMessage> messages){
List<VoterRegistryMessage> voterMessages = new ArrayList<>(); List<UnsignedBulletinBoardMessage> voterMessages = new ArrayList<>();
for (int i = 0 ; i < messages.size() ; i++) for (int i = 0 ; i < messages.size() ; i++)
{ {
voterMessages.add(new VoterRegistryMessage(messages.get(i))); voterMessages.add(messages.get(i).getMsg());
} }
return voterMessages; 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 * Gets map of GroupId to basicMessage, where the basicMessages are the last actions for those groups
* @param messages List<VoterRegistryMessage> * @param messages List<VoterRegistryMessage>
* @return Map{String:VoterRegistryMessage} * @return Map{String:VoterRegistryMessage}
* @throws ParseException * @throws ParseException
*/ */
public static Map<String, VoterRegistryMessage> GetLatestGroupsActions(List<VoterRegistryMessage> messages) public static Map<String, BulletinBoardMessage> GetLatestGroupsActions(List<BulletinBoardMessage> messages)
throws ParseException, InvalidProtocolBufferException { throws ParseException, InvalidProtocolBufferException {
Map<String, VoterRegistryMessage> groupIdToMessage = new HashMap<>(); Map<String, BulletinBoardMessage> groupIdToMessage = new HashMap<>(messages.size());
// iterate trough all the messages and put into the map the last updated groups actions // iterate trough all the messages and put into the map the last updated groups actions
for (int i = 0 ; i < messages.size() ; i++) { for (int i = 0 ; i < messages.size() ; i++) {
VoterRegistryMessage message = messages.get(i); BulletinBoardMessage message = messages.get(i);
String groupId = message.GetWantedTagFromBasicMessage(RegistryTags.GROUP_ID_TAG); String groupId = GetTagByName(message.getMsg().getTagList(), RegistryTags.GROUP_ID_TAG);
VoterRegistryMessage temp = groupIdToMessage.get(groupId); BulletinBoardMessage temp = groupIdToMessage.get(groupId);
if (temp != null && temp != message) { if (temp != null && temp != message) {
if (temp.GetCreationTime() < message.GetCreationTime()) { if (FirstBeforeSecond(temp, message)) {
groupIdToMessage.put(groupId, message); groupIdToMessage.put(groupId, message);
} }
} }
@ -71,13 +89,14 @@ public abstract class CollectionMessagesUtils {
* @param groupIdToMessage Map<String, VoterRegistryMessage> * @param groupIdToMessage Map<String, VoterRegistryMessage>
* @return List<String> * @return List<String>
*/ */
public static List<String> GetListOfGroupIds(Map<String, VoterRegistryMessage> groupIdToMessage) { public static List<String> GetListOfGroupIds(Map<String, BulletinBoardMessage> groupIdToMessage) {
List<String> groupsIds = new ArrayList<>(); List<String> groupsIds = new ArrayList<>(groupIdToMessage.size());
Iterator entries = groupIdToMessage.entrySet().iterator(); Iterator entries = groupIdToMessage.entrySet().iterator();
while (entries.hasNext()) { while (entries.hasNext()) {
Map.Entry tuple = (Map.Entry) entries.next(); Map.Entry tuple = (Map.Entry) entries.next();
VoterRegistryMessage message = (VoterRegistryMessage) tuple.getValue(); BulletinBoardMessage message = (BulletinBoardMessage) tuple.getValue();
String groupId = message.GetWantedTagFromBasicMessage(RegistryTags.GROUP_ID_TAG); String groupId = GetTagByName(message.getMsg().getTagList(), RegistryTags.GROUP_ID_TAG);
groupId.replace(RegistryTags.GROUP_ID_TAG, "");
groupsIds.add(groupId); groupsIds.add(groupId);
} }
return groupsIds; return groupsIds;
@ -90,22 +109,39 @@ public abstract class CollectionMessagesUtils {
* @throws ParseException * @throws ParseException
* @throws EmptyListException * @throws EmptyListException
*/ */
public static VoterRegistryMessage GetLatestMessage(List<VoterRegistryMessage> messages) public static BulletinBoardMessage GetLatestMessage(List<BulletinBoardMessage> messages)
throws ParseException, EmptyListException, InvalidProtocolBufferException { throws ParseException, EmptyListException, InvalidProtocolBufferException {
System.out.print("1");
if (messages.size() == 0 ){ if (messages.size() == 0 ){
throw new EmptyListException("The list of messages passed to GetLatestMessage is empty."); 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++) { for (int i = 0 ; i < messages.size() ; i++) {
VoterRegistryMessage message = messages.get(i); BulletinBoardMessage message = messages.get(i);
if (message.GetCreationTime() < LatestMessage.GetCreationTime()) { if (FirstBeforeSecond(message, LatestMessage)) {
LatestMessage = message; LatestMessage = message;
} }
} }
return LatestMessage; 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<String> 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 * Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG
* *

View File

@ -3,7 +3,6 @@ package meerkat.Registry;
import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.MessageValidator; import meerkat.MessageValidator;
import meerkat.VoterRegistry.RegistryCallBack; import meerkat.VoterRegistry.RegistryCallBack;
import meerkat.VoterRegistryMessage;
import meerkat.bulletinboard.BulletinBoardClient.ClientCallback; import meerkat.bulletinboard.BulletinBoardClient.ClientCallback;
import meerkat.crypto.DigitalSignature; import meerkat.crypto.DigitalSignature;
import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.BulletinBoardAPI;
@ -35,7 +34,6 @@ public class RelevantDataCallBack implements ClientCallback<List<BulletinBoardMe
protected Crypto.Signature signature; protected Crypto.Signature signature;
protected InputStream certificateStream; protected InputStream certificateStream;
/** /**
* Init BooleanCallBack * Init BooleanCallBack
* @param callback voter registry callback object * @param callback voter registry callback object
@ -79,19 +77,16 @@ public class RelevantDataCallBack implements ClientCallback<List<BulletinBoardMe
@Override @Override
public void handleCallback(List<BulletinBoardMessage> msg) { public void handleCallback(List<BulletinBoardMessage> msg) {
try { try {
List<VoterRegistryMessage> messages = ConvertToVoterRegistryMessages(validate(msg));
if(isAddToGroupsList(msg)) { if(isAddToGroupsList(msg)) {
Map<String, VoterRegistryMessage> map = GetLatestGroupsActions(messages); Map<String, BulletinBoardMessage> map = GetLatestGroupsActions(msg);
List<String> groupsOfUser = GetListOfGroupIds(map); List<String> groupsOfUser = GetListOfGroupIds(map);
callback.HandleResult(groupsOfUser); callback.HandleResult(groupsOfUser);
} }
else { else {
callback.HandleResult(GetLatestMessage(messages)); callback.HandleResult(GetLatestMessage(msg));
} }
} catch (ValidationError | ParseException | EmptyListException } catch (ParseException | EmptyListException | InvalidProtocolBufferException validationError) {
| InvalidProtocolBufferException validationError) { callback.HandleFailure(validationError);
callback.HandleResult(null);
validationError.printStackTrace(); validationError.printStackTrace();
} }
} }
@ -102,7 +97,7 @@ public class RelevantDataCallBack implements ClientCallback<List<BulletinBoardMe
*/ */
@Override @Override
public void handleFailure(Throwable t) { public void handleFailure(Throwable t) {
callback.HandleResult(null); callback.HandleFailure(t);
} }
/** /**

View File

@ -11,9 +11,11 @@ public interface VoterRegistry {
/** /**
* Created by Vladimir Eliezer Tokarev on 1/22/2016. * Created by Vladimir Eliezer Tokarev on 1/22/2016.
* This interface will handle the end of methods of RegistryInstance * This interface will handle the end of methods of RegistryInstance
* in case of exception the handleFailure will be called
*/ */
interface RegistryCallBack<T> { interface RegistryCallBack<T> {
void HandleResult(T result); void HandleResult(T result);
void HandleFailure(Throwable throwable);
} }
/** /**

View File

@ -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<String> 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;
}
}
}

View File

@ -3,7 +3,6 @@ import junit.framework.TestCase;
import meerkat.Registry.CollectionMessagesUtils; import meerkat.Registry.CollectionMessagesUtils;
import meerkat.Registry.RegistryTags; import meerkat.Registry.RegistryTags;
import meerkat.SimpleRegistry; import meerkat.SimpleRegistry;
import meerkat.VoterRegistryMessage;
import meerkat.bulletinboard.BulletinBoardClient; import meerkat.bulletinboard.BulletinBoardClient;
import meerkat.bulletinboard.ThreadedBulletinBoardClient; import meerkat.bulletinboard.ThreadedBulletinBoardClient;
import meerkat.crypto.concrete.ECDSASignature; import meerkat.crypto.concrete.ECDSASignature;
@ -23,8 +22,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import static meerkat.Registry.CollectionMessagesUtils.ConvertToVoterRegistryMessages;
/** /**
* TODO: add logs prints for the tests to be clear what they are * TODO: add logs prints for the tests to be clear what they are
*/ */
@ -62,6 +59,10 @@ public class SimpleRegistryTest extends TestCase {
data = result; data = result;
jobSemaphore.release(); jobSemaphore.release();
} }
@Override
public void HandleFailure(Throwable throwable) {
}
} }
public class DummyBulletinBoardCallBackHandler implements BulletinBoardClient.ClientCallback<List<BulletinBoardMessage>> { public class DummyBulletinBoardCallBackHandler implements BulletinBoardClient.ClientCallback<List<BulletinBoardMessage>> {
@ -76,7 +77,6 @@ public class SimpleRegistryTest extends TestCase {
@Override @Override
public void handleFailure(Throwable t){ public void handleFailure(Throwable t){
System.out.println(t);
messages = null; messages = null;
jobSemaphore.release(); jobSemaphore.release();
} }
@ -139,17 +139,17 @@ public class SimpleRegistryTest extends TestCase {
* @param tags List<RegistryTags> * @param tags List<RegistryTags>
* @return integer that represent the amount of messages with wanted tags * @return integer that represent the amount of messages with wanted tags
*/ */
private int countMessagesWithTags(List<VoterRegistryMessage> messages, List<String> tags) private int countMessagesWithTags(List<BulletinBoardMessage> messages, List<String> tags)
{ {
int counter = 0 ; int counter = 0 ;
for (int i = 0 ; i < messages.size() ; i++) { for (int i = 0 ; i < messages.size() ; i++) {
VoterRegistryMessage message = messages.get(i); BulletinBoardMessage message = messages.get(i);
int wantedTagsCounter = 0; int wantedTagsCounter = 0;
for (int j = 0 ;j < tags.size() ; j++) { for (int j = 0 ;j < tags.size() ; j++) {
String tag = tags.get(j); String tag = tags.get(j);
if(message.GetWantedTagFromBasicMessage(tag)!=null){ if(CollectionMessagesUtils.GetTagByName(message.getMsg().getTagList(), tag)!=null){
wantedTagsCounter++; wantedTagsCounter++;
} }
} }
@ -187,7 +187,7 @@ public class SimpleRegistryTest extends TestCase {
tags.add(id); 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."; assert counter == 1 : "The server don't have the new user data.";
} }
@ -214,7 +214,7 @@ public class SimpleRegistryTest extends TestCase {
jobSemaphore.acquire(); jobSemaphore.acquire();
tags.add(id); 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."; 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(voterId);
tags.add(groupId); 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."; 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(); jobSemaphore.acquire();
assertEquals(1, handler.counter ); assertEquals(1, handler.counter );
DummyRegistryCallBackHandler<VoterRegistryMessage> personalHandler = new DummyRegistryCallBackHandler<>(); DummyRegistryCallBackHandler<BulletinBoardMessage> personalHandler = new DummyRegistryCallBackHandler<>();
registry.GetPersonIDDetails(VoterID.newBuilder().setId(id).build(), personalHandler); registry.GetPersonIDDetails(VoterID.newBuilder().setId(id).build(), personalHandler);
jobSemaphore.acquire(1); jobSemaphore.acquire(1);
assertEquals(RegistryTags.ID_TAG + id, assertEquals(RegistryTags.ID_TAG + id, CollectionMessagesUtils.GetTagByName(personalHandler
personalHandler.data.GetWantedTagFromBasicMessage(RegistryTags.ID_TAG)); .data.getMsg().getTagList(), RegistryTags.ID_TAG));
assertTrue(personalHandler.data.GetWantedTagFromBasicMessage(RegistryTags.VOTER_DATA_TAG).contains(data)); assertTrue(CollectionMessagesUtils.GetTagByName(personalHandler.data.getMsg().getTagList(),
RegistryTags.VOTER_DATA_TAG).contains(data));
} }
} }