Changed the usage of timestamp to miliseconds and removed the aqurate timestamp

Voter-Registry
Vladimir Eliezer Tokarev 2016-02-27 03:00:14 -08:00
parent 097aad4e7b
commit 5169c935bf
9 changed files with 52 additions and 79 deletions

Binary file not shown.

View File

@ -20,4 +20,9 @@ message GroupID{
message VoterGroup{ message VoterGroup{
VoterID voterId = 1; VoterID voterId = 1;
GroupID groupId = 2; GroupID groupId = 2;
}
message VoterRegistryMessage{
bytes data = 1;
uint64 CreationMiliseconds = 2;
} }

View File

@ -1,35 +0,0 @@
package meerkat.Registry;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by Vladimir Eliezer Tokarev on 1/15/2016.
* converts time stamps to strings and the other way
*/
public abstract class AccurateTimestamp {
private static final String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss.SSS";
/**
* Converts current timestamp to string
* @return
*/
public static String GetCurrentTimestampString(){
return new SimpleDateFormat(DATE_FORMAT).format(new java.util.Date());
}
/**
* Convets string timesta,p tp java.sql.timestamp
* @param timestamp string
* @return
* @throws ParseException
*/
public static java.sql.Timestamp GetTimestampFromString(String timestamp) throws ParseException {
Date date = new SimpleDateFormat(DATE_FORMAT).parse(timestamp);
return new Timestamp(date.getTime());
}
}

View File

@ -1,5 +1,6 @@
package meerkat.Registry; package meerkat.Registry;
import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.VoterRegistryMessage; import meerkat.VoterRegistryMessage;
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
import meerkat.protobuf.BulletinBoardAPI.FilterType; import meerkat.protobuf.BulletinBoardAPI.FilterType;
@ -45,7 +46,8 @@ public abstract class CollectionMessagesUtils {
* @return Map{String:VoterRegistryMessage} * @return Map{String:VoterRegistryMessage}
* @throws ParseException * @throws ParseException
*/ */
public static Map<String, VoterRegistryMessage> GetLatestGroupsActions(List<VoterRegistryMessage> messages) throws ParseException { public static Map<String, VoterRegistryMessage> GetLatestGroupsActions(List<VoterRegistryMessage> messages)
throws ParseException, InvalidProtocolBufferException {
Map<String, VoterRegistryMessage> groupIdToMessage = new HashMap<>(); Map<String, VoterRegistryMessage> groupIdToMessage = new HashMap<>();
// 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
@ -55,7 +57,7 @@ public abstract class CollectionMessagesUtils {
VoterRegistryMessage temp = groupIdToMessage.get(groupId); VoterRegistryMessage temp = groupIdToMessage.get(groupId);
if (temp != null && temp != message) { if (temp != null && temp != message) {
if (temp.GetBasicMessageActionTimestamp().before(message.GetBasicMessageActionTimestamp())) { if (temp.GetCreationTime() < message.GetCreationTime()) {
groupIdToMessage.put(groupId, message); groupIdToMessage.put(groupId, message);
} }
} }
@ -75,7 +77,7 @@ public abstract class CollectionMessagesUtils {
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(); VoterRegistryMessage message = (VoterRegistryMessage) tuple.getValue();
String groupId = message.GetWantedTagFromBasicMessage(RegistryTags.GROUP_ID_TAG.toString()); String groupId = message.GetWantedTagFromBasicMessage(RegistryTags.GROUP_ID_TAG);
groupsIds.add(groupId); groupsIds.add(groupId);
} }
return groupsIds; return groupsIds;
@ -88,15 +90,16 @@ public abstract class CollectionMessagesUtils {
* @throws ParseException * @throws ParseException
* @throws EmptyListException * @throws EmptyListException
*/ */
public static VoterRegistryMessage GetLatestMessage(List<VoterRegistryMessage> messages) throws ParseException, EmptyListException { public static VoterRegistryMessage GetLatestMessage(List<VoterRegistryMessage> messages)
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); VoterRegistryMessage 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); VoterRegistryMessage message = messages.get(i);
if (message.GetBasicMessageActionTimestamp().before(LatestMessage.GetBasicMessageActionTimestamp())) { if (message.GetCreationTime() < LatestMessage.GetCreationTime()) {
LatestMessage = message; LatestMessage = message;
} }
} }

View File

@ -6,12 +6,12 @@ package meerkat.Registry;
* Have the tags for the registry messages * Have the tags for the registry messages
*/ */
public abstract class RegistryTags { public abstract class RegistryTags {
public final static String ID_TAG = "ID:"; public final static String ID_TAG = "ID: ";
public final static String VOTER_ENTRY_TAG = "VoterEntry:"; public final static String VOTER_ENTRY_TAG = "VoterEntry: ";
public final static String GROUP_ID_TAG = "GroupID:"; public final static String VOTER_DATA_TAG = "Data: ";
public final static String ADD_TO_GROUP_TAG = "AddToGroup:"; public final static String GROUP_ID_TAG = "GroupID: ";
public final static String ACTION_TIMESTAMP_TAG = "ActionTimestamp: "; public final static String ADD_TO_GROUP_TAG = "AddToGroup: ";
public final static String VOTE_ACTION_TAG = "VoteAction:"; public final static String VOTE_ACTION_TAG = "VoteAction: ";
} }

View File

@ -1,5 +1,6 @@
package meerkat.Registry; package meerkat.Registry;
import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.MessageValidator; import meerkat.MessageValidator;
import meerkat.VoterRegistry.RegistryCallBack; import meerkat.VoterRegistry.RegistryCallBack;
import meerkat.VoterRegistryMessage; import meerkat.VoterRegistryMessage;
@ -88,7 +89,8 @@ public class RelevantDataCallBack implements ClientCallback<List<BulletinBoardMe
else { else {
callback.HandleResult(GetLatestMessage(messages)); callback.HandleResult(GetLatestMessage(messages));
} }
} catch (ValidationError | ParseException | EmptyListException validationError) { } catch (ValidationError | ParseException | EmptyListException
| InvalidProtocolBufferException validationError) {
callback.HandleResult(null); callback.HandleResult(null);
validationError.printStackTrace(); validationError.printStackTrace();
} }

View File

@ -6,8 +6,11 @@ import meerkat.crypto.DigitalSignature;
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage;
import meerkat.protobuf.Crypto; import meerkat.protobuf.Crypto;
import meerkat.protobuf.VoterRegistry.*; import meerkat.protobuf.VoterRegistry.GroupID;
import meerkat.protobuf.VoterRegistry.VoterGroup;
import meerkat.protobuf.VoterRegistry.VoterID;
import meerkat.protobuf.VoterRegistry.VoterInfo;
import meerkat.protobuf.VoterRegistry.VoterRegistryMessage;
import java.io.InputStream; import java.io.InputStream;
import java.security.SignatureException; import java.security.SignatureException;
@ -60,33 +63,39 @@ public class SimpleRegistry implements VoterRegistry{
} }
public void AddVoter(VoterInfo voterInfo, RegistryCallBack callback) { public void AddVoter(VoterInfo voterInfo, RegistryCallBack callback) {
VoterRegistryMessage.Builder wrapper = VoterRegistryMessage.newBuilder().
setData(voterInfo.toByteString()).setCreationMiliseconds(System.currentTimeMillis());
UnsignedBulletinBoardMessage.Builder basicMessage = UnsignedBulletinBoardMessage.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder(). UnsignedBulletinBoardMessage.newBuilder().
addTag(RegistryTags.ID_TAG + voterInfo.getId().getId()) addTag(RegistryTags.ID_TAG + voterInfo.getId().getId())
.addTag(RegistryTags.VOTER_ENTRY_TAG) .addTag(RegistryTags.VOTER_ENTRY_TAG)
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + AccurateTimestamp.GetCurrentTimestampString()); .addTag(RegistryTags.VOTER_DATA_TAG + voterInfo.getInfo())
.setData(wrapper.build().toByteString());
basicMessage.setData(voterInfo.getInfoBytes());
bulletinBoardClient.postMessage(CreateBulletinBoardMessage(basicMessage.build()), new BooleanCallBack(callback)); bulletinBoardClient.postMessage(CreateBulletinBoardMessage(basicMessage.build()), new BooleanCallBack(callback));
} }
public void AddToGroup(VoterGroup voterGroup, RegistryCallBack callback) { public void AddToGroup(VoterGroup voterGroup, RegistryCallBack callback) {
VoterRegistryMessage.Builder wrapper = VoterRegistryMessage.newBuilder().
setData(voterGroup.toByteString()).setCreationMiliseconds(System.currentTimeMillis());
UnsignedBulletinBoardMessage.Builder basicMessage = UnsignedBulletinBoardMessage.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder() UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + voterGroup.getVoterId().getId()) .addTag(RegistryTags.ID_TAG + voterGroup.getVoterId().getId())
.addTag(RegistryTags.GROUP_ID_TAG + voterGroup.getGroupId().getId()) .addTag(RegistryTags.GROUP_ID_TAG + voterGroup.getGroupId().getId())
.addTag(RegistryTags.ADD_TO_GROUP_TAG) .addTag(RegistryTags.ADD_TO_GROUP_TAG)
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + AccurateTimestamp.GetCurrentTimestampString()); .setData(wrapper.build().toByteString());
bulletinBoardClient.postMessage(CreateBulletinBoardMessage(basicMessage.build()), new BooleanCallBack(callback)); bulletinBoardClient.postMessage(CreateBulletinBoardMessage(basicMessage.build()), new BooleanCallBack(callback));
} }
public void SetVoted(VoterID voterId, RegistryCallBack callback) { public void SetVoted(VoterID voterId, RegistryCallBack callback) {
VoterRegistryMessage.Builder wrapper = VoterRegistryMessage.newBuilder().
setData(voterId.toByteString()).setCreationMiliseconds(System.currentTimeMillis());
UnsignedBulletinBoardMessage.Builder basicMessage = UnsignedBulletinBoardMessage.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder() UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + voterId.getId()) .addTag(RegistryTags.ID_TAG + voterId.getId())
.addTag(RegistryTags.VOTE_ACTION_TAG) .addTag(RegistryTags.VOTE_ACTION_TAG)
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + AccurateTimestamp.GetCurrentTimestampString()); .setData(wrapper.build().toByteString());
bulletinBoardClient.postMessage(CreateBulletinBoardMessage(basicMessage.build()), new BooleanCallBack(callback)); bulletinBoardClient.postMessage(CreateBulletinBoardMessage(basicMessage.build()), new BooleanCallBack(callback));
} }

View File

@ -1,12 +1,10 @@
package meerkat; package meerkat;
import meerkat.Registry.AccurateTimestamp; import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.Registry.RegistryTags;
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage;
import meerkat.protobuf.VoterRegistry;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.List; import java.util.List;
@ -47,23 +45,15 @@ public class VoterRegistryMessage {
} }
/** /**
* Gets the timestamp of the tag adding * Gets the creation time milliseconds of the tag adding
* * @return long that represent the creation time
* @return Timestamp
* @throws ParseException
*/ */
public Timestamp GetBasicMessageActionTimestamp() throws ParseException { public long GetCreationTime() throws InvalidProtocolBufferException {
List<String> tags = base.getTagList(); try {
for (int i = 0 ; i < tags.size() ; i++) { VoterRegistry.VoterRegistryMessage wrapper = VoterRegistry.VoterRegistryMessage.parseFrom(base.getData());
String tag = tags.get(i); return wrapper.getCreationMiliseconds();
if (tag.contains(RegistryTags.ACTION_TIMESTAMP_TAG)) { } catch (InvalidProtocolBufferException e) {
String[] tagParts = tag.split(" "); throw e;
String timestamp = tagParts[tagParts.length - 2] + " " + tagParts[tagParts.length - 1];
return AccurateTimestamp.GetTimestampFromString(timestamp);
}
} }
return null;
} }
} }

View File

@ -76,6 +76,7 @@ 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();
} }
@ -88,7 +89,7 @@ public class SimpleRegistryTest extends TestCase {
private void CommunicatorSetup() { private void CommunicatorSetup() {
bulletinBoardClient = new ThreadedBulletinBoardClient(); bulletinBoardClient = new ThreadedBulletinBoardClient();
String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081"; String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081/";
bulletinBoardClient.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)
@ -169,8 +170,7 @@ public class SimpleRegistryTest extends TestCase {
String id = new BigInteger(130, random).toString(32); String id = new BigInteger(130, random).toString(32);
String data = new BigInteger(130, random).toString(32); String data = new BigInteger(130, random).toString(32);
VoterInfo voterInfo = VoterInfo.newBuilder(). VoterInfo voterInfo = VoterInfo.newBuilder().setId(VoterID.newBuilder().setId(id)).setInfo(data).build();
setId(VoterID.newBuilder().setId(id)).setInfo(data).build();
SimpleRegistry registry = new SimpleRegistry(signer, bulletinBoardClient, certStream); SimpleRegistry registry = new SimpleRegistry(signer, bulletinBoardClient, certStream);
registry.AddVoter(voterInfo, handler); registry.AddVoter(voterInfo, handler);
@ -301,11 +301,10 @@ public class SimpleRegistryTest extends TestCase {
DummyRegistryCallBackHandler<VoterRegistryMessage> personalHandler = new DummyRegistryCallBackHandler<>(); DummyRegistryCallBackHandler<VoterRegistryMessage> 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,
personalHandler.data.GetWantedTagFromBasicMessage(RegistryTags.ID_TAG)); personalHandler.data.GetWantedTagFromBasicMessage(RegistryTags.ID_TAG));
assertEquals(data,personalHandler.data.base.getData().toStringUtf8()); assertTrue(personalHandler.data.GetWantedTagFromBasicMessage(RegistryTags.VOTER_DATA_TAG).contains(data));
} }
} }