Changed the usage of timestamp to miliseconds and removed the aqurate timestamp
parent
097aad4e7b
commit
5169c935bf
Binary file not shown.
|
@ -20,4 +20,9 @@ message GroupID{
|
|||
message VoterGroup{
|
||||
VoterID voterId = 1;
|
||||
GroupID groupId = 2;
|
||||
}
|
||||
|
||||
message VoterRegistryMessage{
|
||||
bytes data = 1;
|
||||
uint64 CreationMiliseconds = 2;
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package meerkat.Registry;
|
||||
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import meerkat.VoterRegistryMessage;
|
||||
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
|
||||
import meerkat.protobuf.BulletinBoardAPI.FilterType;
|
||||
|
@ -45,7 +46,8 @@ public abstract class CollectionMessagesUtils {
|
|||
* @return Map{String:VoterRegistryMessage}
|
||||
* @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<>();
|
||||
|
||||
// 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);
|
||||
|
||||
if (temp != null && temp != message) {
|
||||
if (temp.GetBasicMessageActionTimestamp().before(message.GetBasicMessageActionTimestamp())) {
|
||||
if (temp.GetCreationTime() < message.GetCreationTime()) {
|
||||
groupIdToMessage.put(groupId, message);
|
||||
}
|
||||
}
|
||||
|
@ -75,7 +77,7 @@ public abstract class CollectionMessagesUtils {
|
|||
while (entries.hasNext()) {
|
||||
Map.Entry tuple = (Map.Entry) entries.next();
|
||||
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);
|
||||
}
|
||||
return groupsIds;
|
||||
|
@ -88,15 +90,16 @@ public abstract class CollectionMessagesUtils {
|
|||
* @throws ParseException
|
||||
* @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 ){
|
||||
throw new EmptyListException("The list of messages passed to GetLatestMessage is empty.");
|
||||
}
|
||||
VoterRegistryMessage LatestMessage = messages.get(0);
|
||||
|
||||
for (int i = 0 ; i < messages.size() ; i++) {
|
||||
VoterRegistryMessage message = messages.get(i);
|
||||
if (message.GetBasicMessageActionTimestamp().before(LatestMessage.GetBasicMessageActionTimestamp())) {
|
||||
if (message.GetCreationTime() < LatestMessage.GetCreationTime()) {
|
||||
LatestMessage = message;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,12 +6,12 @@ package meerkat.Registry;
|
|||
* Have the tags for the registry messages
|
||||
*/
|
||||
public abstract class RegistryTags {
|
||||
public final static String ID_TAG = "ID:";
|
||||
public final static String VOTER_ENTRY_TAG = "VoterEntry:";
|
||||
public final static String GROUP_ID_TAG = "GroupID:";
|
||||
public final static String ADD_TO_GROUP_TAG = "AddToGroup:";
|
||||
public final static String ACTION_TIMESTAMP_TAG = "ActionTimestamp: ";
|
||||
public final static String VOTE_ACTION_TAG = "VoteAction:";
|
||||
public final static String ID_TAG = "ID: ";
|
||||
public final static String VOTER_ENTRY_TAG = "VoterEntry: ";
|
||||
public final static String VOTER_DATA_TAG = "Data: ";
|
||||
public final static String GROUP_ID_TAG = "GroupID: ";
|
||||
public final static String ADD_TO_GROUP_TAG = "AddToGroup: ";
|
||||
public final static String VOTE_ACTION_TAG = "VoteAction: ";
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package meerkat.Registry;
|
||||
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import meerkat.MessageValidator;
|
||||
import meerkat.VoterRegistry.RegistryCallBack;
|
||||
import meerkat.VoterRegistryMessage;
|
||||
|
@ -88,7 +89,8 @@ public class RelevantDataCallBack implements ClientCallback<List<BulletinBoardMe
|
|||
else {
|
||||
callback.HandleResult(GetLatestMessage(messages));
|
||||
}
|
||||
} catch (ValidationError | ParseException | EmptyListException validationError) {
|
||||
} catch (ValidationError | ParseException | EmptyListException
|
||||
| InvalidProtocolBufferException validationError) {
|
||||
callback.HandleResult(null);
|
||||
validationError.printStackTrace();
|
||||
}
|
||||
|
|
|
@ -6,8 +6,11 @@ import meerkat.crypto.DigitalSignature;
|
|||
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
|
||||
import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage;
|
||||
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.security.SignatureException;
|
||||
|
@ -60,33 +63,39 @@ public class SimpleRegistry implements VoterRegistry{
|
|||
}
|
||||
|
||||
public void AddVoter(VoterInfo voterInfo, RegistryCallBack callback) {
|
||||
VoterRegistryMessage.Builder wrapper = VoterRegistryMessage.newBuilder().
|
||||
setData(voterInfo.toByteString()).setCreationMiliseconds(System.currentTimeMillis());
|
||||
UnsignedBulletinBoardMessage.Builder basicMessage =
|
||||
UnsignedBulletinBoardMessage.newBuilder().
|
||||
addTag(RegistryTags.ID_TAG + voterInfo.getId().getId())
|
||||
.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));
|
||||
}
|
||||
|
||||
public void AddToGroup(VoterGroup voterGroup, RegistryCallBack callback) {
|
||||
VoterRegistryMessage.Builder wrapper = VoterRegistryMessage.newBuilder().
|
||||
setData(voterGroup.toByteString()).setCreationMiliseconds(System.currentTimeMillis());
|
||||
UnsignedBulletinBoardMessage.Builder basicMessage =
|
||||
UnsignedBulletinBoardMessage.newBuilder()
|
||||
.addTag(RegistryTags.ID_TAG + voterGroup.getVoterId().getId())
|
||||
.addTag(RegistryTags.GROUP_ID_TAG + voterGroup.getGroupId().getId())
|
||||
.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));
|
||||
}
|
||||
|
||||
public void SetVoted(VoterID voterId, RegistryCallBack callback) {
|
||||
VoterRegistryMessage.Builder wrapper = VoterRegistryMessage.newBuilder().
|
||||
setData(voterId.toByteString()).setCreationMiliseconds(System.currentTimeMillis());
|
||||
UnsignedBulletinBoardMessage.Builder basicMessage =
|
||||
UnsignedBulletinBoardMessage.newBuilder()
|
||||
.addTag(RegistryTags.ID_TAG + voterId.getId())
|
||||
.addTag(RegistryTags.VOTE_ACTION_TAG)
|
||||
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + AccurateTimestamp.GetCurrentTimestampString());
|
||||
.setData(wrapper.build().toByteString());
|
||||
|
||||
bulletinBoardClient.postMessage(CreateBulletinBoardMessage(basicMessage.build()), new BooleanCallBack(callback));
|
||||
}
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
package meerkat;
|
||||
|
||||
import meerkat.Registry.AccurateTimestamp;
|
||||
import meerkat.Registry.RegistryTags;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
|
||||
import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage;
|
||||
import meerkat.protobuf.VoterRegistry;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
|
@ -47,23 +45,15 @@ public class VoterRegistryMessage {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets the timestamp of the tag adding
|
||||
*
|
||||
* @return Timestamp
|
||||
* @throws ParseException
|
||||
* Gets the creation time milliseconds of the tag adding
|
||||
* @return long that represent the creation time
|
||||
*/
|
||||
public Timestamp GetBasicMessageActionTimestamp() throws ParseException {
|
||||
List<String> tags = base.getTagList();
|
||||
for (int i = 0 ; i < tags.size() ; i++) {
|
||||
String tag = tags.get(i);
|
||||
if (tag.contains(RegistryTags.ACTION_TIMESTAMP_TAG)) {
|
||||
String[] tagParts = tag.split(" ");
|
||||
|
||||
String timestamp = tagParts[tagParts.length - 2] + " " + tagParts[tagParts.length - 1];
|
||||
return AccurateTimestamp.GetTimestampFromString(timestamp);
|
||||
}
|
||||
public long GetCreationTime() throws InvalidProtocolBufferException {
|
||||
try {
|
||||
VoterRegistry.VoterRegistryMessage wrapper = VoterRegistry.VoterRegistryMessage.parseFrom(base.getData());
|
||||
return wrapper.getCreationMiliseconds();
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
throw e;
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,6 +76,7 @@ public class SimpleRegistryTest extends TestCase {
|
|||
|
||||
@Override
|
||||
public void handleFailure(Throwable t){
|
||||
System.out.println(t);
|
||||
messages = null;
|
||||
jobSemaphore.release();
|
||||
}
|
||||
|
@ -88,7 +89,7 @@ public class SimpleRegistryTest extends TestCase {
|
|||
|
||||
private void CommunicatorSetup() {
|
||||
bulletinBoardClient = new ThreadedBulletinBoardClient();
|
||||
String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081";
|
||||
String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081/";
|
||||
bulletinBoardClient.init(Voting.BulletinBoardClientParams.newBuilder()
|
||||
.addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS)
|
||||
.setMinRedundancy((float) 1.0)
|
||||
|
@ -169,8 +170,7 @@ public class SimpleRegistryTest extends TestCase {
|
|||
|
||||
String id = new BigInteger(130, random).toString(32);
|
||||
String data = new BigInteger(130, random).toString(32);
|
||||
VoterInfo voterInfo = VoterInfo.newBuilder().
|
||||
setId(VoterID.newBuilder().setId(id)).setInfo(data).build();
|
||||
VoterInfo voterInfo = VoterInfo.newBuilder().setId(VoterID.newBuilder().setId(id)).setInfo(data).build();
|
||||
|
||||
SimpleRegistry registry = new SimpleRegistry(signer, bulletinBoardClient, certStream);
|
||||
registry.AddVoter(voterInfo, handler);
|
||||
|
@ -301,11 +301,10 @@ public class SimpleRegistryTest extends TestCase {
|
|||
DummyRegistryCallBackHandler<VoterRegistryMessage> personalHandler = new DummyRegistryCallBackHandler<>();
|
||||
registry.GetPersonIDDetails(VoterID.newBuilder().setId(id).build(), personalHandler);
|
||||
|
||||
|
||||
jobSemaphore.acquire(1);
|
||||
assertEquals(RegistryTags.ID_TAG + id,
|
||||
personalHandler.data.GetWantedTagFromBasicMessage(RegistryTags.ID_TAG));
|
||||
assertEquals(data,personalHandler.data.base.getData().toStringUtf8());
|
||||
assertTrue(personalHandler.data.GetWantedTagFromBasicMessage(RegistryTags.VOTER_DATA_TAG).contains(data));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue