From 25781f40f72330f5f06fbbef35a7b8ca42f0682f Mon Sep 17 00:00:00 2001 From: Vladimir Eliezer Tokarev Date: Fri, 1 Apr 2016 02:20:47 -0700 Subject: [PATCH] Changed AsyncVoterRegistry to use CachedBulletinBoardClient Instead of using ThreadedBulletinBoardClient. The test have been changed to use LocalBulletinBoardClient. --- .../src/main/java/meerkat/AsyncRegistry.java | 20 ++-- .../src/main/java/meerkat/VoterRegistry.java | 4 +- .../src/test/java/SimpleRegistryTest.java | 97 ++++++++++++++----- 3 files changed, 87 insertions(+), 34 deletions(-) diff --git a/voter-registry/src/main/java/meerkat/AsyncRegistry.java b/voter-registry/src/main/java/meerkat/AsyncRegistry.java index f0905b5..ed77573 100644 --- a/voter-registry/src/main/java/meerkat/AsyncRegistry.java +++ b/voter-registry/src/main/java/meerkat/AsyncRegistry.java @@ -1,7 +1,7 @@ package meerkat; import com.google.common.util.concurrent.FutureCallback; -import meerkat.bulletinboard.AsyncBulletinBoardClient; +import meerkat.bulletinboard.SubscriptionAsyncBulletinBoardClient; import meerkat.crypto.DigitalSignature; import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage; import meerkat.protobuf.VoterRegistry.VoterID; @@ -31,12 +31,12 @@ import static meerkat.util.BulletinBoardUtils.signBulletinBoardMessage; public class AsyncRegistry implements VoterRegistry{ protected DigitalSignature signer; - protected AsyncBulletinBoardClient bulletinBoardClient ; + protected SubscriptionAsyncBulletinBoardClient cachedBulletinBoardClient; @Override - public void init(DigitalSignature signer, AsyncBulletinBoardClient communicator) { + public void init(DigitalSignature signer, SubscriptionAsyncBulletinBoardClient communicator) { this.signer = signer; - this.bulletinBoardClient = communicator; + this.cachedBulletinBoardClient = communicator; } @Override @@ -50,7 +50,7 @@ public class AsyncRegistry implements VoterRegistry{ .build(); - bulletinBoardClient.postMessage(signBulletinBoardMessage(basicMessage, signer), callback); + cachedBulletinBoardClient.postMessage(signBulletinBoardMessage(basicMessage, signer), callback); } @Override @@ -62,7 +62,7 @@ public class AsyncRegistry implements VoterRegistry{ .setData(voterRegistryMessage.toByteString()) .setTimestamp(BulletinBoardUtils.getCurrentTimestampProto()); - bulletinBoardClient.postMessage(signBulletinBoardMessage(basicMessage.build(), signer), callback); + cachedBulletinBoardClient.postMessage(signBulletinBoardMessage(basicMessage.build(), signer), callback); } @Override @@ -74,7 +74,7 @@ public class AsyncRegistry implements VoterRegistry{ .setTimestamp(BulletinBoardUtils.getCurrentTimestampProto()) .build(); - bulletinBoardClient.postMessage(signBulletinBoardMessage(basicMessage, signer), callback); + cachedBulletinBoardClient.postMessage(signBulletinBoardMessage(basicMessage, signer), callback); } @Override @@ -84,7 +84,7 @@ public class AsyncRegistry implements VoterRegistry{ add(RegistryTags.ID_TAG + voterID.getId()); }}; - bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(GroupsActionsTags), + cachedBulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(GroupsActionsTags), new GetGroupsCallback(callback)); } @@ -95,7 +95,7 @@ public class AsyncRegistry implements VoterRegistry{ add(RegistryTags.VOTER_ENTRY_TAG); }}; - bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(addVoterTags), + cachedBulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(addVoterTags), new GetVoterCallback(callback)); } @@ -106,7 +106,7 @@ public class AsyncRegistry implements VoterRegistry{ add(RegistryTags.VOTE_ACTION_TAG); }}; - bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(setVotedTags), + cachedBulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(setVotedTags), new HasVotedCallback(callBack)); } } diff --git a/voter-registry/src/main/java/meerkat/VoterRegistry.java b/voter-registry/src/main/java/meerkat/VoterRegistry.java index 3a7794a..677046c 100644 --- a/voter-registry/src/main/java/meerkat/VoterRegistry.java +++ b/voter-registry/src/main/java/meerkat/VoterRegistry.java @@ -1,7 +1,7 @@ package meerkat; import com.google.common.util.concurrent.FutureCallback; -import meerkat.bulletinboard.AsyncBulletinBoardClient; +import meerkat.bulletinboard.SubscriptionAsyncBulletinBoardClient; import meerkat.crypto.DigitalSignature; import meerkat.protobuf.VoterRegistry.VoterID; import meerkat.protobuf.VoterRegistry.VoterInfo; @@ -22,7 +22,7 @@ public interface VoterRegistry { * @param signer object which sign the outputed message * @param communicator the object which communicates with the BulletinBoardServer */ - void init(DigitalSignature signer, AsyncBulletinBoardClient communicator); + void init(DigitalSignature signer, SubscriptionAsyncBulletinBoardClient communicator); /** * Adds new voter to the bulletin-board diff --git a/voter-registry/src/test/java/SimpleRegistryTest.java b/voter-registry/src/test/java/SimpleRegistryTest.java index 42e6ef4..8b89abe 100644 --- a/voter-registry/src/test/java/SimpleRegistryTest.java +++ b/voter-registry/src/test/java/SimpleRegistryTest.java @@ -1,7 +1,11 @@ import com.google.common.util.concurrent.FutureCallback; import meerkat.AsyncRegistry; -import meerkat.bulletinboard.AsyncBulletinBoardClient; -import meerkat.bulletinboard.ThreadedBulletinBoardClient; +import meerkat.bulletinboard.BulletinBoardServer; +import meerkat.bulletinboard.CachedBulletinBoardClient; +import meerkat.bulletinboard.LocalBulletinBoardClient; +import meerkat.bulletinboard.sqlserver.BulletinBoardSQLServer; +import meerkat.bulletinboard.sqlserver.H2QueryProvider; +import meerkat.comm.CommunicationException; import meerkat.crypto.DigitalSignature; import meerkat.crypto.concrete.ECDSASignature; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; @@ -22,6 +26,9 @@ import java.math.BigInteger; import java.security.KeyStore; import java.security.SecureRandom; import java.security.SignatureException; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Semaphore; @@ -44,14 +51,18 @@ import static meerkat.util.BulletinBoardUtils.getLatestMessage; public class SimpleRegistryTest { private DigitalSignature signer; - private AsyncBulletinBoardClient bulletinBoardClient; - private InputStream certStream; + private CachedBulletinBoardClient cachedBulletinBoardClient; private SecureRandom random = new SecureRandom(); + Semaphore jobSemaphore; public static String KEYFILE_EXAMPLE = "/certs/enduser-certs/user1-key-with-password-secret.p12"; public static String CERT1_PEM_EXAMPLE = "/certs/enduser-certs/user1.crt"; public static String KEYFILE_PASSWORD = "secret"; - Semaphore jobSemaphore; + private static final String DB_NAME = "TestDB"; + private static final String LOCALHOST = "127.0.0.1:8000"; + private static final int SUBSCRIPTION_DELAY = 3000; + private static final int THREAD_NUM = 3; + private static final int FAIL_DELAY = 6000; class DummyRegistryCallBackHandler implements FutureCallback{ public int counter; @@ -71,7 +82,7 @@ public class SimpleRegistryTest { @Override public void onFailure(Throwable throwable) { - System.out.print(throwable); + System.out.print(throwable.toString()); } } @@ -92,16 +103,52 @@ public class SimpleRegistryTest { } } + /** + * Creates LocalBulletinBoardClient which connects to TestDB + * @return LocalBulletinBoardClient object + * @throws CommunicationException + */ + private LocalBulletinBoardClient CreateLocalBulletinBoardClient() throws CommunicationException, SQLException { + H2QueryProvider queryProvider = new H2QueryProvider(DB_NAME) ; - private void CommunicatorSetup() { - bulletinBoardClient = new ThreadedBulletinBoardClient(); - String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081/"; - bulletinBoardClient.init(Voting.BulletinBoardClientParams.newBuilder() - .addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS) - .setMinRedundancy((float) 1.0) + Connection conn = queryProvider.getDataSource().getConnection(); + Statement stmt = conn.createStatement(); + + List deletionQueries = queryProvider.getSchemaDeletionCommands(); + + for (String deletionQuery : deletionQueries) { + stmt.execute(deletionQuery); + } + + BulletinBoardServer server = new BulletinBoardSQLServer(queryProvider); + server.init(DB_NAME); + + return new LocalBulletinBoardClient(server, THREAD_NUM, SUBSCRIPTION_DELAY); + } + + /** + * Initialize CachedBulletinBoardClient client that communicates with LocalBulletinBoardClient + * @throws InstantiationException + * @throws IllegalAccessException + * @throws CommunicationException + * @throws SQLException + */ + private void CommunicatorSetup() throws InstantiationException, IllegalAccessException, CommunicationException, SQLException { + LocalBulletinBoardClient localClient = CreateLocalBulletinBoardClient(); + cachedBulletinBoardClient = new CachedBulletinBoardClient(localClient, + THREAD_NUM, + FAIL_DELAY, + SUBSCRIPTION_DELAY); + + cachedBulletinBoardClient.init(Voting.BulletinBoardClientParams.newBuilder() + .setBulletinBoardAddress(0, LOCALHOST) + .setMinRedundancy(1) .build()); } + /** + * Sets the signer to be instance of ECDSASignature + */ private void SetSigner(){ try { ECDSASignature signer = new ECDSASignature(); @@ -121,10 +168,14 @@ public class SimpleRegistryTest { } /** - * Initialize registry object + * Initializes the needed params for all the tests + * @throws SQLException + * @throws InstantiationException + * @throws CommunicationException + * @throws IllegalAccessException */ @Before - public void setUp() { + public void setUp() throws SQLException, InstantiationException, CommunicationException, IllegalAccessException { SetSigner(); CommunicatorSetup(); jobSemaphore = new Semaphore(0); @@ -133,10 +184,14 @@ public class SimpleRegistryTest { private AsyncRegistry GetRegistry() { AsyncRegistry registry = new AsyncRegistry(); - registry.init(signer, bulletinBoardClient); + registry.init(signer, cachedBulletinBoardClient); return registry; } + /** + * Generates random string 32 chars length + * @return String object + */ private String generateString() { return new BigInteger(130, random).toString(32); @@ -148,7 +203,7 @@ public class SimpleRegistryTest { @Test public void simpleRegistryCreation() { try { - AsyncRegistry registry = GetRegistry(); + GetRegistry(); } catch (Exception e) { assert false : "While creating the registry exception have been thrown " + e; } @@ -182,7 +237,7 @@ public class SimpleRegistryTest { } /** - * Reads messages from bulletinBoardClient by given tags and return the callback handler + * Reads messages from cachedBulletinBoardClient by given tags and return the callback handler * object * * @param tags list of strings that represents tags @@ -192,7 +247,7 @@ public class SimpleRegistryTest { { MessageFilterList filters = MessageCollectionUtils.generateFiltersFromTags(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); - bulletinBoardClient.readMessages(filters, bulletinHandler); + cachedBulletinBoardClient.readMessages(filters, bulletinHandler); return bulletinHandler; } @@ -307,8 +362,6 @@ public class SimpleRegistryTest { .addGroupID(GroupID.newBuilder().setId(groupId1)) .addGroupID(GroupID.newBuilder().setId(groupId2)).build(); - this.certStream = getClass().getResourceAsStream(CERT1_PEM_EXAMPLE); - AsyncRegistry registry = GetRegistry(); registry.setVoterGroups(voterInfo, handler); @@ -329,8 +382,8 @@ public class SimpleRegistryTest { } /** - * Test that the personal data outputted about the user is right - */ + * Test that the personal data outputted about the user is right + */ @Test public void testGetVoter() throws InterruptedException, SignatureException { DummyRegistryCallBackHandler handler =