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

Binary file not shown.

View File

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