Fixed all of Tal's remarks.

Switched to using the predefined BoolValue Protobuf.
PollingStation-ScannerWebApp
arbel.peled 2016-06-02 14:48:48 +03:00
parent 347e826f73
commit ffac7c1e34
14 changed files with 69 additions and 97 deletions

View File

@ -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

View File

@ -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;

View File

@ -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<T> extends SingleServerWorker<T, Bool
try {
// If a BoolMsg entity is returned: the post was successful
response.readEntity(BoolMsg.class);
// If a BoolValue entity is returned: the post was successful
response.readEntity(BoolValue.class);
return Boolean.TRUE;
} catch (ProcessingException | IllegalStateException e) {

View File

@ -401,12 +401,12 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
}
/**
* This procedure is used to convert a boolean to a BoolMsg.
* This procedure is used to convert a boolean to a BoolValue.
* @param b is the boolean to convert.
* @return a ProtoBuf message with boolean payload.
*/
private BoolMsg boolToBoolMsg(boolean b){
return BoolMsg.newBuilder()
private BoolValue boolToBoolValue(boolean b){
return BoolValue.newBuilder()
.setValue(b)
.build();
}
@ -419,10 +419,10 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
* @return TRUE if the post is successful and FALSE otherwise
* @throws CommunicationException
*/
public BoolMsg postMessage(BulletinBoardMessage msg, boolean checkSignature) throws CommunicationException{
public BoolValue postMessage(BulletinBoardMessage msg, boolean checkSignature) throws CommunicationException{
if (checkSignature && !verifyMessage(msg)) {
return boolToBoolMsg(false);
return boolToBoolValue(false);
}
String sql;
@ -520,12 +520,12 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
jdbcTemplate.batchUpdate(sql,namedParameterArray);
return boolToBoolMsg(true);
return boolToBoolValue(true);
}
@Override
public BoolMsg postMessage(BulletinBoardMessage msg) throws CommunicationException {
public BoolValue postMessage(BulletinBoardMessage msg) throws CommunicationException {
return postMessage(msg, true); // Perform a post and check the signature for authenticity
}
@ -691,11 +691,11 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
@Override
public BoolMsg beginBatch(BeginBatchMessage message) throws CommunicationException {
public BoolValue beginBatch(BeginBatchMessage message) throws CommunicationException {
// Check if batch is closed
if (isBatchClosed(message.getSignerId(), message.getBatchId())) {
return BoolMsg.newBuilder().setValue(false).build();
return BoolValue.newBuilder().setValue(false).build();
}
// Add new tags to table
@ -721,17 +721,17 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
jdbcTemplate.batchUpdate(sql,namedParameters);
return BoolMsg.newBuilder().setValue(true).build();
return BoolValue.newBuilder().setValue(true).build();
}
@Override
public BoolMsg postBatchMessage(BatchMessage batchMessage) throws CommunicationException{
public BoolValue postBatchMessage(BatchMessage batchMessage) throws CommunicationException{
// Check if batch is closed
if (isBatchClosed(batchMessage.getSignerId(), batchMessage.getBatchId())) {
return BoolMsg.newBuilder().setValue(false).build();
return BoolValue.newBuilder().setValue(false).build();
}
// Add data
@ -745,13 +745,13 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
jdbcTemplate.update(sql, namedParameters);
return BoolMsg.newBuilder().setValue(true).build();
return BoolValue.newBuilder().setValue(true).build();
}
@Override
public BoolMsg closeBatchMessage(CloseBatchMessage message) throws CommunicationException {
public BoolValue closeBatchMessage(CloseBatchMessage message) throws CommunicationException {
ByteString signerId = message.getSig().getSignerId();
int batchId = message.getBatchId();
@ -770,7 +770,7 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
List<Long> 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();
}

View File

@ -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);

View File

@ -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

View File

@ -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();

View File

@ -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)

View File

@ -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);
}

View File

@ -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;
}

View File

@ -10,12 +10,4 @@ message BroadcastMessage {
bool is_private = 3;
bytes payload = 5;
}
message BoolMsg {
bool value = 1;
}
message IntMsg {
int32 value = 1;
}
}

View File

@ -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<FutureCallback<PollingStation.ScannedData>> callbacks;
Iterable<FutureCallback<PollingStation.ScannedData>> 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<FutureCallback<PollingStation.ScannedData>>) 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<PollingStation.ScannedData> 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<PollingStation.ScannedData> 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();

View File

@ -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<FutureCallback<ScannedData>> 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<FutureCallback<ScannedData>>) 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<FutureCallback<ScannedData>> getCallbackIterator() {
return callbacks.iterator();
}
}
}

View File

@ -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!")