diff --git a/bulletin-board-client/src/main/java/meerkat/bulletinboard/SimpleBulletinBoardClient.java b/bulletin-board-client/src/main/java/meerkat/bulletinboard/SimpleBulletinBoardClient.java index 26b1e4e..74eacae 100644 --- a/bulletin-board-client/src/main/java/meerkat/bulletinboard/SimpleBulletinBoardClient.java +++ b/bulletin-board-client/src/main/java/meerkat/bulletinboard/SimpleBulletinBoardClient.java @@ -1,5 +1,6 @@ package meerkat.bulletinboard; +import com.google.protobuf.BoolValue; import com.google.protobuf.ByteString; import meerkat.comm.CommunicationException; import meerkat.crypto.Digest; @@ -70,7 +71,7 @@ public class SimpleBulletinBoardClient implements BulletinBoardClient{ // Only consider valid responses if (response.getStatusInfo() == Response.Status.OK || response.getStatusInfo() == Response.Status.CREATED) { - response.readEntity(BoolMsg.class).getValue(); + response.readEntity(BoolValue.class).getValue(); } } } catch (Exception e) { // Occurs only when server replies with valid status but invalid data diff --git a/bulletin-board-client/src/main/java/meerkat/bulletinboard/SingleServerBulletinBoardClient.java b/bulletin-board-client/src/main/java/meerkat/bulletinboard/SingleServerBulletinBoardClient.java index 34531cf..5edd079 100644 --- a/bulletin-board-client/src/main/java/meerkat/bulletinboard/SingleServerBulletinBoardClient.java +++ b/bulletin-board-client/src/main/java/meerkat/bulletinboard/SingleServerBulletinBoardClient.java @@ -7,12 +7,10 @@ import com.google.common.util.concurrent.MoreExecutors; import com.google.protobuf.ByteString; import meerkat.bulletinboard.workers.singleserver.*; import meerkat.comm.CommunicationException; -import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.BulletinBoardAPI.*; import meerkat.protobuf.Voting.BulletinBoardClientParams; import meerkat.util.BulletinBoardUtils; -import javax.ws.rs.NotFoundException; import java.util.Arrays; import java.util.Iterator; import java.util.LinkedList; diff --git a/bulletin-board-client/src/main/java/meerkat/bulletinboard/workers/singleserver/SingleServerGenericPostWorker.java b/bulletin-board-client/src/main/java/meerkat/bulletinboard/workers/singleserver/SingleServerGenericPostWorker.java index 2148801..08a66d1 100644 --- a/bulletin-board-client/src/main/java/meerkat/bulletinboard/workers/singleserver/SingleServerGenericPostWorker.java +++ b/bulletin-board-client/src/main/java/meerkat/bulletinboard/workers/singleserver/SingleServerGenericPostWorker.java @@ -1,5 +1,6 @@ package meerkat.bulletinboard.workers.singleserver; +import com.google.protobuf.BoolValue; import meerkat.bulletinboard.SingleServerWorker; import meerkat.comm.CommunicationException; import meerkat.protobuf.Comm.*; @@ -43,8 +44,8 @@ public class SingleServerGenericPostWorker extends SingleServerWorker lengthResult = jdbcTemplate.query(sql, namedParameters, new LongMapper()); if (lengthResult.get(0) != message.getBatchLength()) { - return BoolMsg.newBuilder().setValue(false).build(); + return BoolValue.newBuilder().setValue(false).build(); } // Get Tags and add them to CompleteBatch @@ -813,7 +813,7 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{ // TODO: Actual verification // //signer.verify(completeBatch); // } catch (CertificateException | InvalidKeyException | SignatureException e) { -// return BoolMsg.newBuilder().setValue(false).build(); +// return BoolValue.newBuilder().setValue(false).build(); // } // Batch verified: finalize it @@ -849,7 +849,7 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{ // Return TRUE - return BoolMsg.newBuilder().setValue(true).build(); + return BoolValue.newBuilder().setValue(true).build(); } diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java index f88d31f..aef820e 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java @@ -8,6 +8,7 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.StreamingOutput; +import com.google.protobuf.BoolValue; import meerkat.bulletinboard.BulletinBoardServer; import meerkat.bulletinboard.sqlserver.BulletinBoardSQLServer; import meerkat.bulletinboard.sqlserver.H2QueryProvider; @@ -88,7 +89,7 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL @Consumes(MEDIATYPE_PROTOBUF) @Produces(MEDIATYPE_PROTOBUF) @Override - public BoolMsg postMessage(BulletinBoardMessage msg) throws CommunicationException { + public BoolValue postMessage(BulletinBoardMessage msg) throws CommunicationException { init(); return bulletinBoard.postMessage(msg); } @@ -132,7 +133,7 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL @Consumes(MEDIATYPE_PROTOBUF) @Produces(MEDIATYPE_PROTOBUF) @Override - public BoolMsg beginBatch(BeginBatchMessage message) { + public BoolValue beginBatch(BeginBatchMessage message) { try { init(); return bulletinBoard.beginBatch(message); @@ -147,7 +148,7 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL @Consumes(MEDIATYPE_PROTOBUF) @Produces(MEDIATYPE_PROTOBUF) @Override - public BoolMsg postBatchMessage(BatchMessage batchMessage) { + public BoolValue postBatchMessage(BatchMessage batchMessage) { try { init(); return bulletinBoard.postBatchMessage(batchMessage); @@ -162,7 +163,7 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL @Consumes(MEDIATYPE_PROTOBUF) @Produces(MEDIATYPE_PROTOBUF) @Override - public BoolMsg closeBatchMessage(CloseBatchMessage message) { + public BoolValue closeBatchMessage(CloseBatchMessage message) { try { init(); return bulletinBoard.closeBatchMessage(message); diff --git a/bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardSQLServerIntegrationTest.java b/bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardSQLServerIntegrationTest.java index a9be646..844a050 100644 --- a/bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardSQLServerIntegrationTest.java +++ b/bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardSQLServerIntegrationTest.java @@ -1,6 +1,7 @@ package meerkat.bulletinboard; +import com.google.protobuf.BoolValue; import com.google.protobuf.ByteString; import com.google.protobuf.TextFormat; @@ -61,7 +62,7 @@ public class BulletinBoardSQLServerIntegrationTest { WebTarget webTarget; Response response; - BoolMsg bool; + BoolValue bool; BulletinBoardMessage msg; @@ -95,7 +96,7 @@ public class BulletinBoardSQLServerIntegrationTest { response = webTarget.request(Constants.MEDIATYPE_PROTOBUF).post(Entity.entity(msg, Constants.MEDIATYPE_PROTOBUF)); System.err.println(response); - bool = response.readEntity(BoolMsg.class); + bool = response.readEntity(BoolValue.class); assert bool.getValue(); msg = BulletinBoardMessage.newBuilder() @@ -114,7 +115,7 @@ public class BulletinBoardSQLServerIntegrationTest { response = webTarget.request(Constants.MEDIATYPE_PROTOBUF).post(Entity.entity(msg, Constants.MEDIATYPE_PROTOBUF)); System.err.println(response); - bool = response.readEntity(BoolMsg.class); + bool = response.readEntity(BoolValue.class); assert bool.getValue(); // Test reading mechanism diff --git a/bulletin-board-server/src/test/java/meerkat/bulletinboard/GenericBulletinBoardServerTest.java b/bulletin-board-server/src/test/java/meerkat/bulletinboard/GenericBulletinBoardServerTest.java index e09f051..49c3050 100644 --- a/bulletin-board-server/src/test/java/meerkat/bulletinboard/GenericBulletinBoardServerTest.java +++ b/bulletin-board-server/src/test/java/meerkat/bulletinboard/GenericBulletinBoardServerTest.java @@ -17,6 +17,7 @@ import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; import java.util.*; +import com.google.protobuf.BoolValue; import com.google.protobuf.ByteString; import com.google.protobuf.Timestamp; @@ -457,7 +458,7 @@ public class GenericBulletinBoardServerTest { .setSeconds(978325) .setNanos(8097234) .build()); - BoolMsg result; + BoolValue result; // Create data @@ -527,7 +528,7 @@ public class GenericBulletinBoardServerTest { .build()); int currentBatch = completeBatches.size(); - BoolMsg result; + BoolValue result; // Define batch data @@ -646,7 +647,7 @@ public class GenericBulletinBoardServerTest { BulletinBoardMessage newMessage = bulletinBoardMessageGenerator.generateRandomMessage(signers, timestamp, 10, 10); - BoolMsg result = bulletinBoardServer.postMessage(newMessage); + BoolValue result = bulletinBoardServer.postMessage(newMessage); assertThat("Failed to post message to BB Server", result.getValue(), is(true)); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); diff --git a/meerkat-common/src/main/java/meerkat/bulletinboard/BulletinBoardServer.java b/meerkat-common/src/main/java/meerkat/bulletinboard/BulletinBoardServer.java index abe4335..1507ab7 100644 --- a/meerkat-common/src/main/java/meerkat/bulletinboard/BulletinBoardServer.java +++ b/meerkat-common/src/main/java/meerkat/bulletinboard/BulletinBoardServer.java @@ -1,9 +1,9 @@ package meerkat.bulletinboard; +import com.google.protobuf.BoolValue; import meerkat.comm.CommunicationException; import meerkat.comm.MessageOutputStream; import meerkat.protobuf.BulletinBoardAPI.*; -import meerkat.protobuf.Comm.*; import java.util.Collection; @@ -30,7 +30,7 @@ public interface BulletinBoardServer{ * @return TRUE if the message has been authenticated and FALSE otherwise (in ProtoBuf form) * @throws CommunicationException on DB connection error */ - public BoolMsg postMessage(BulletinBoardMessage msg) throws CommunicationException; + public BoolValue postMessage(BulletinBoardMessage msg) throws CommunicationException; /** * Read all messages posted matching the given filter @@ -48,7 +48,7 @@ public interface BulletinBoardServer{ * However, if such a batch exists and is already closed: the value returned will be FALSE * @throws CommunicationException on DB connection error */ - public BoolMsg beginBatch(BeginBatchMessage message) throws CommunicationException; + public BoolValue beginBatch(BeginBatchMessage message) throws CommunicationException; /** * Posts a (part of a) batch message to the bulletin board @@ -60,7 +60,7 @@ public interface BulletinBoardServer{ * However, requiring to open a batch before insertion of messages is implementation-dependent * @throws CommunicationException on DB connection error */ - public BoolMsg postBatchMessage(BatchMessage batchMessage) throws CommunicationException; + public BoolValue postBatchMessage(BatchMessage batchMessage) throws CommunicationException; /** * Attempts to stop and finalize a batch message @@ -69,7 +69,7 @@ public interface BulletinBoardServer{ * Specifically, if the signature is invalid or if some of the batch parts have not yet been submitted: the value returned will be FALSE * @throws CommunicationException on DB connection error */ - public BoolMsg closeBatchMessage(CloseBatchMessage message) throws CommunicationException; + public BoolValue closeBatchMessage(CloseBatchMessage message) throws CommunicationException; /** * Reads a batch message from the server (starting with the supplied position) diff --git a/meerkat-common/src/main/java/meerkat/pollingstation/PollingStationScanner.java b/meerkat-common/src/main/java/meerkat/pollingstation/PollingStationScanner.java index 9adc616..290d0ee 100644 --- a/meerkat-common/src/main/java/meerkat/pollingstation/PollingStationScanner.java +++ b/meerkat-common/src/main/java/meerkat/pollingstation/PollingStationScanner.java @@ -1,7 +1,7 @@ package meerkat.pollingstation; import com.google.common.util.concurrent.FutureCallback; -import meerkat.protobuf.Comm.BoolMsg; +import com.google.protobuf.BoolValue; import meerkat.protobuf.PollingStation.*; /** * Created by Arbel on 05/05/2016. @@ -45,16 +45,16 @@ public interface PollingStationScanner { /** * Sends a scan to all subscribers * @param scannedData contains the scanned data - * @return a BoolMsg containing TRUE iff the scanned data has been sent to at least one subscriber + * @return a BoolValue containing TRUE iff the scanned data has been sent to at least one subscriber */ - public BoolMsg newScan(ScannedData scannedData); + public BoolValue newScan(ScannedData scannedData); /** * Notifies subscribers about an error that occurred during scan * @param errorMsg is the error that occurred - * @return a BoolMsg containing TRUE iff the error has been sent to at least one subscriber + * @return a BoolValue containing TRUE iff the error has been sent to at least one subscriber */ - public BoolMsg reportScanError(ErrorMsg errorMsg); + public BoolValue reportScanError(ErrorMsg errorMsg); } diff --git a/meerkat-common/src/main/proto/meerkat/PollingStation.proto b/meerkat-common/src/main/proto/meerkat/PollingStation.proto index 35dbd12..0cdc658 100644 --- a/meerkat-common/src/main/proto/meerkat/PollingStation.proto +++ b/meerkat-common/src/main/proto/meerkat/PollingStation.proto @@ -12,11 +12,4 @@ message ScannedData { // Container for error messages message ErrorMsg { string msg = 1; -} - -// Container for HTTP address -message HTTPAddress { - string hostname = 1; - int32 port = 2; - string address = 3; } \ No newline at end of file diff --git a/meerkat-common/src/main/proto/meerkat/comm.proto b/meerkat-common/src/main/proto/meerkat/comm.proto index 2f23678..6808288 100644 --- a/meerkat-common/src/main/proto/meerkat/comm.proto +++ b/meerkat-common/src/main/proto/meerkat/comm.proto @@ -10,12 +10,4 @@ message BroadcastMessage { bool is_private = 3; bytes payload = 5; -} - -message BoolMsg { - bool value = 1; -} - -message IntMsg { - int32 value = 1; -} +} \ No newline at end of file diff --git a/polling-station/src/main/java/meerkat/pollingstation/PollingStationScannerWebApp.java b/polling-station/src/main/java/meerkat/pollingstation/PollingStationScannerWebApp.java index edebe33..327c774 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/PollingStationScannerWebApp.java +++ b/polling-station/src/main/java/meerkat/pollingstation/PollingStationScannerWebApp.java @@ -5,7 +5,7 @@ package meerkat.pollingstation; */ import com.google.common.util.concurrent.FutureCallback; -import meerkat.protobuf.Comm; +import com.google.protobuf.BoolValue; import meerkat.protobuf.PollingStation; import javax.annotation.PostConstruct; @@ -16,7 +16,6 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import java.io.IOException; -import java.util.Iterator; import static meerkat.pollingstation.PollingStationConstants.POLLING_STATION_WEB_SCANNER_ERROR_PATH; import static meerkat.pollingstation.PollingStationConstants.POLLING_STATION_WEB_SCANNER_SCAN_PATH; @@ -32,14 +31,14 @@ public class PollingStationScannerWebApp implements PollingStationScanner.Produc @Context ServletContext servletContext; - Iterator> callbacks; + Iterable> callbacks; /** * This method is called by the Jetty engine when instantiating the servlet */ @PostConstruct public void init() throws Exception{ - callbacks = ((PollingStationWebScanner.CallbackAccessor) servletContext.getAttribute(PollingStationWebScanner.CALLBACKS_ATTRIBUTE_NAME)).getCallbackIterator(); + callbacks = (Iterable>) servletContext.getAttribute(PollingStationWebScanner.CALLBACKS_ATTRIBUTE_NAME); } @POST @@ -47,18 +46,18 @@ public class PollingStationScannerWebApp implements PollingStationScanner.Produc @Consumes(MEDIATYPE_PROTOBUF) @Produces(MEDIATYPE_PROTOBUF) @Override - public Comm.BoolMsg newScan(PollingStation.ScannedData scannedData) { + public BoolValue newScan(PollingStation.ScannedData scannedData) { boolean handled = false; - while (callbacks.hasNext()){ + for (FutureCallback callback : callbacks){ - callbacks.next().onSuccess(scannedData); + callback.onSuccess(scannedData); handled = true; } - return Comm.BoolMsg.newBuilder() + return BoolValue.newBuilder() .setValue(handled) .build(); @@ -69,18 +68,18 @@ public class PollingStationScannerWebApp implements PollingStationScanner.Produc @Consumes(MEDIATYPE_PROTOBUF) @Produces(MEDIATYPE_PROTOBUF) @Override - public Comm.BoolMsg reportScanError(PollingStation.ErrorMsg errorMsg) { + public BoolValue reportScanError(PollingStation.ErrorMsg errorMsg) { boolean handled = false; - while (callbacks.hasNext()){ + for (FutureCallback callback : callbacks){ - callbacks.next().onFailure(new IOException(errorMsg.getMsg())); + callback.onFailure(new IOException(errorMsg.getMsg())); handled = true; } - return Comm.BoolMsg.newBuilder() + return BoolValue.newBuilder() .setValue(handled) .build(); diff --git a/polling-station/src/main/java/meerkat/pollingstation/PollingStationWebScanner.java b/polling-station/src/main/java/meerkat/pollingstation/PollingStationWebScanner.java index d7fc6a4..a4e290a 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/PollingStationWebScanner.java +++ b/polling-station/src/main/java/meerkat/pollingstation/PollingStationWebScanner.java @@ -1,6 +1,5 @@ package meerkat.pollingstation; -import java.util.Iterator; import java.util.List; import java.util.LinkedList; @@ -8,7 +7,6 @@ import com.google.common.util.concurrent.FutureCallback; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.*; -import meerkat.protobuf.PollingStation.*; import org.glassfish.jersey.servlet.ServletContainer; import org.glassfish.jersey.server.ResourceConfig; @@ -26,14 +24,14 @@ public class PollingStationWebScanner implements PollingStationScanner.Consumer{ private final Server server; private final List> callbacks; - public PollingStationWebScanner(HTTPAddress address) { + public PollingStationWebScanner(int port, String subAddress) { callbacks = new LinkedList<>(); - server = new Server(address.getPort()); + server = new Server(port); - ServletContextHandler servletContextHandler = new ServletContextHandler(server, address.getAddress()); - servletContextHandler.setAttribute(CALLBACKS_ATTRIBUTE_NAME, new CallbackAccessor()); + ServletContextHandler servletContextHandler = new ServletContextHandler(server, subAddress); + servletContextHandler.setAttribute(CALLBACKS_ATTRIBUTE_NAME, (Iterable>) callbacks); ResourceConfig resourceConfig = new ResourceConfig(PollingStationScannerWebApp.class); resourceConfig.register(ProtobufMessageBodyReader.class); @@ -59,10 +57,4 @@ public class PollingStationWebScanner implements PollingStationScanner.Consumer{ callbacks.add(scanCallback); } - public class CallbackAccessor { - public Iterator> getCallbackIterator() { - return callbacks.iterator(); - } - } - } diff --git a/polling-station/src/test/java/meerkat/pollingstation/PollingStationWebScannerTest.java b/polling-station/src/test/java/meerkat/pollingstation/PollingStationWebScannerTest.java index 63cd8dd..7da106c 100644 --- a/polling-station/src/test/java/meerkat/pollingstation/PollingStationWebScannerTest.java +++ b/polling-station/src/test/java/meerkat/pollingstation/PollingStationWebScannerTest.java @@ -2,8 +2,6 @@ package meerkat.pollingstation; import com.google.common.util.concurrent.FutureCallback; import com.google.protobuf.ByteString; -import com.sun.org.apache.regexp.internal.RE; -import meerkat.protobuf.PollingStation; import meerkat.protobuf.PollingStation.*; import meerkat.rest.Constants; @@ -18,12 +16,10 @@ import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Response; -import java.io.IOException; import java.util.concurrent.Semaphore; import static meerkat.pollingstation.PollingStationConstants.*; -import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat; /** @@ -32,7 +28,9 @@ import static org.hamcrest.MatcherAssert.assertThat; public class PollingStationWebScannerTest { private PollingStationScanner.Consumer scanner; - private HTTPAddress httpAddress; + private static final String ADDRESS = "http://localhost"; + private static final String SUB_ADDRESS = ""; + private static final int PORT = 8080; private Semaphore semaphore; private Throwable thrown; @@ -86,12 +84,7 @@ public class PollingStationWebScannerTest { System.err.println("Setting up Scanner WebApp!"); - httpAddress = HTTPAddress.newBuilder() - .setPort(8080) - .setHostname("http://localhost") - .build(); - - scanner = new PollingStationWebScanner(httpAddress); + scanner = new PollingStationWebScanner(PORT, SUB_ADDRESS); semaphore = new Semaphore(0); thrown = null; @@ -110,8 +103,8 @@ public class PollingStationWebScannerTest { Client client = ClientBuilder.newClient(); client.register(ProtobufMessageBodyReader.class); client.register(ProtobufMessageBodyWriter.class); - WebTarget webTarget = client.target(httpAddress.getHostname() + ":" + httpAddress.getPort()) - .path(httpAddress.getAddress()).path(POLLING_STATION_WEB_SCANNER_SCAN_PATH); + WebTarget webTarget = client.target(ADDRESS + ":" + PORT) + .path(SUB_ADDRESS).path(POLLING_STATION_WEB_SCANNER_SCAN_PATH); byte[] data = {(byte) 1, (byte) 2}; @@ -136,8 +129,8 @@ public class PollingStationWebScannerTest { Client client = ClientBuilder.newClient(); client.register(ProtobufMessageBodyReader.class); client.register(ProtobufMessageBodyWriter.class); - WebTarget webTarget = client.target(httpAddress.getHostname() + ":" + httpAddress.getPort()) - .path(httpAddress.getAddress()).path(POLLING_STATION_WEB_SCANNER_ERROR_PATH); + WebTarget webTarget = client.target(ADDRESS + ":" + PORT) + .path(SUB_ADDRESS).path(POLLING_STATION_WEB_SCANNER_ERROR_PATH); ErrorMsg errorMsg = ErrorMsg.newBuilder() .setMsg("!Error Message!")