Changed the SimpleRegistry to work with UnsignedBulletinBoardMessage

The basicMessage is not needed
Voter-Registry
Vladimir Eliezer Tokarev 2016-01-22 07:35:30 -08:00
parent 4324bdeecd
commit 0d8e522e93
3 changed files with 34 additions and 58 deletions

View File

@ -4,20 +4,18 @@ import meerkat.bulletinboard.BulletinBoardClient.ClientCallback;
import meerkat.bulletinboard.ThreadedBulletinBoardClient;
import meerkat.protobuf.BulletinBoardAPI.*;
import meerkat.protobuf.Crypto;
import meerkat.protobuf.Voting.*;
import meerkat.protobuf.Voting.BulletinBoardClientParams;
import meerkat.util.BulletinBoardMessageComparator;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.number.OrderingComparison.*;
import java.util.*;
import java.util.concurrent.Semaphore;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo;
import static org.junit.Assert.assertThat;
/**
* Created by Arbel Deutsch Peled on 05-Dec-15.
*/
@ -104,7 +102,7 @@ public class BulletinBoardClientIntegrationTest {
private ReadCallback readCallback;
private static String PROP_GETTY_URL = "gretty.httpBaseURI";
private static String DEFAULT_BASE_URL = "http://localhost:8081";
private static String DEFAULT_BASE_URL = "http://localhost:3306";
private static String BASE_URL = System.getProperty(PROP_GETTY_URL, DEFAULT_BASE_URL);
@Before
@ -116,7 +114,7 @@ public class BulletinBoardClientIntegrationTest {
testDB.add(BASE_URL);
bulletinBoardClient.init(BulletinBoardClientParams.newBuilder()
.addBulletinBoardAddress("http://localhost:8081")
.addBulletinBoardAddress("http://localhost:3306")
.setMinRedundancy((float) 1.0)
.build());
@ -165,6 +163,8 @@ public class BulletinBoardClientIntegrationTest {
messageID = bulletinBoardClient.postMessage(msg,postCallback);
System.out.println(messageID.toByteArray());
try {
jobSemaphore.acquire();
} catch (InterruptedException e) {
@ -206,6 +206,7 @@ public class BulletinBoardClientIntegrationTest {
System.err.println(t.getMessage());
}
if (thrown.size() > 0) {
System.out.println(thrown.size());
assert false;
}

Binary file not shown.

View File

@ -32,27 +32,12 @@ public class SimpleRegistry {
this.communicator = communicator;
}
/**
* Creates array of Tags in given length
* @param length the length og wanted array
* @return Tag.Builder[]
*/
private Tag.Builder[] GetTagsArrayInLength(int length) {
Tag.Builder[] tags = new Tag.Builder[length];
for (int i = 0; i < length; i++) {
tags[i] = Tag.newBuilder();
}
return tags;
}
/**
* Creates BulletinBoardMessage with signed basicMessage and UnsignedBulletinBoardMessage that contains the basic message
*
* @param basicMessage BasicMessage
* @return BulletinBoardAPI.BulletinBoardMessage
*/
private BulletinBoardAPI.BulletinBoardMessage CreateBulletinBoardMessage(BasicMessage basicMessage) throws IOException {
private BulletinBoardAPI.BulletinBoardMessage CreateBulletinBoardMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage basicMessage) throws IOException {
BulletinBoardAPI.BulletinBoardMessage.Builder bulletinBoardMessage =
BulletinBoardAPI.BulletinBoardMessage.newBuilder();
@ -62,14 +47,10 @@ public class SimpleRegistry {
Crypto.Signature.Builder messageSignature = Crypto.Signature.newBuilder();
messageSignature.mergeFrom(encryptedMessage);
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder unsignedBulletinBoardMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder();
unsignedBulletinBoardMessage.setData(basicMessage.toByteString());
bulletinBoardMessage.setMsg(basicMessage);
bulletinBoardMessage.addSig(messageSignature);
bulletinBoardMessage.setMsg(unsignedBulletinBoardMessage);
return bulletinBoardMessage.build();
}
@ -107,19 +88,18 @@ public class SimpleRegistry {
/**
* Adds new voter to the bulletin-board
*
* @param voterID id of the new user
* @param personalData for example residence location
* @return void
* @throws CommunicationException, IOException
*/
public void AddVoter(String voterID, String personalData) throws CommunicationException, IOException {
Tag.Builder[] tags = GetTagsArrayInLength(3);
tags[0].setContent(RegistryTags.ID_TAG + " " + voterID);
tags[1].setContent(RegistryTags.VOTER_ENTRY_TAG.toString());
tags[2].setContent(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder().
addTag(RegistryTags.ID_TAG + " " + voterID)
.addTag(RegistryTags.VOTER_ENTRY_TAG.toString())
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
BasicMessage.Builder basicMessage = BasicMessage.newBuilder().addTag(tags[0]).addTag(tags[1]).addTag(tags[1]);
basicMessage.setData(ByteString.copyFrom(personalData.getBytes()));
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
@ -134,14 +114,12 @@ public class SimpleRegistry {
* @throws CommunicationException
*/
public void AddToGroup(String voterID, String groupID) throws CommunicationException, IOException {
Tag.Builder[] tags = GetTagsArrayInLength(4);
tags[0].setContent(RegistryTags.ID_TAG + " " + voterID);
tags[1].setContent(RegistryTags.GROUP_ID_TAG + " " + groupID);
tags[2].setContent(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.ADD_TO_GROUP_TAG);
tags[3].setContent(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
BasicMessage.Builder basicMessage =
BasicMessage.newBuilder().addTag(tags[0]).addTag(tags[1]).addTag(tags[2]).addTag(tags[3]);
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + " " + voterID)
.addTag(RegistryTags.GROUP_ID_TAG + " " + groupID)
.addTag(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.ADD_TO_GROUP_TAG)
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
}
@ -155,14 +133,12 @@ public class SimpleRegistry {
* @throws CommunicationException
*/
public void RemoveFromGroup(String voterID, String groupID) throws CommunicationException, IOException {
Tag.Builder[] tags = GetTagsArrayInLength(4);
tags[0].setContent(RegistryTags.ID_TAG + " " + voterID);
tags[1].setContent(RegistryTags.GROUP_ID_TAG + " " + groupID);
tags[2].setContent(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.REMOVE_FROM_GROUP_TAG);
tags[3].setContent(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
BasicMessage.Builder basicMessage =
BasicMessage.newBuilder().addTag(tags[0]).addTag(tags[1]).addTag(tags[2]).addTag(tags[3]);
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + " " + voterID)
.addTag(RegistryTags.GROUP_ID_TAG + " " + groupID)
.addTag(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.REMOVE_FROM_GROUP_TAG)
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
}
@ -175,12 +151,11 @@ public class SimpleRegistry {
* @throws CommunicationException
*/
public void AddVoter(String id) throws CommunicationException, IOException {
Tag.Builder[] tags = GetTagsArrayInLength(3);
tags[0].setContent(RegistryTags.ID_TAG + " " + id);
tags[1].setContent(RegistryTags.VOTE_ACTION_TAG.toString());
tags[2].setContent(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
BasicMessage.Builder basicMessage = BasicMessage.newBuilder().addTag(tags[0]).addTag(tags[1]).addTag(tags[2]);
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + " " + id)
.addTag(RegistryTags.VOTE_ACTION_TAG.toString())
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
communicator.postMessage(CreateBulletinBoardMessage(basicMessage.build()));
}