Fixed all of Tal's remarks.
Switched to using the predefined BoolValue Protobuf.PollingStation-ScannerWebApp
parent
347e826f73
commit
ffac7c1e34
|
@ -1,5 +1,6 @@
|
||||||
package meerkat.bulletinboard;
|
package meerkat.bulletinboard;
|
||||||
|
|
||||||
|
import com.google.protobuf.BoolValue;
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
import meerkat.comm.CommunicationException;
|
import meerkat.comm.CommunicationException;
|
||||||
import meerkat.crypto.Digest;
|
import meerkat.crypto.Digest;
|
||||||
|
@ -70,7 +71,7 @@ public class SimpleBulletinBoardClient implements BulletinBoardClient{
|
||||||
// Only consider valid responses
|
// Only consider valid responses
|
||||||
if (response.getStatusInfo() == Response.Status.OK
|
if (response.getStatusInfo() == Response.Status.OK
|
||||||
|| response.getStatusInfo() == Response.Status.CREATED) {
|
|| 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
|
} catch (Exception e) { // Occurs only when server replies with valid status but invalid data
|
||||||
|
|
|
@ -7,12 +7,10 @@ import com.google.common.util.concurrent.MoreExecutors;
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
import meerkat.bulletinboard.workers.singleserver.*;
|
import meerkat.bulletinboard.workers.singleserver.*;
|
||||||
import meerkat.comm.CommunicationException;
|
import meerkat.comm.CommunicationException;
|
||||||
import meerkat.protobuf.BulletinBoardAPI;
|
|
||||||
import meerkat.protobuf.BulletinBoardAPI.*;
|
import meerkat.protobuf.BulletinBoardAPI.*;
|
||||||
import meerkat.protobuf.Voting.BulletinBoardClientParams;
|
import meerkat.protobuf.Voting.BulletinBoardClientParams;
|
||||||
import meerkat.util.BulletinBoardUtils;
|
import meerkat.util.BulletinBoardUtils;
|
||||||
|
|
||||||
import javax.ws.rs.NotFoundException;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package meerkat.bulletinboard.workers.singleserver;
|
package meerkat.bulletinboard.workers.singleserver;
|
||||||
|
|
||||||
|
import com.google.protobuf.BoolValue;
|
||||||
import meerkat.bulletinboard.SingleServerWorker;
|
import meerkat.bulletinboard.SingleServerWorker;
|
||||||
import meerkat.comm.CommunicationException;
|
import meerkat.comm.CommunicationException;
|
||||||
import meerkat.protobuf.Comm.*;
|
import meerkat.protobuf.Comm.*;
|
||||||
|
@ -43,8 +44,8 @@ public class SingleServerGenericPostWorker<T> extends SingleServerWorker<T, Bool
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// If a BoolMsg entity is returned: the post was successful
|
// If a BoolValue entity is returned: the post was successful
|
||||||
response.readEntity(BoolMsg.class);
|
response.readEntity(BoolValue.class);
|
||||||
return Boolean.TRUE;
|
return Boolean.TRUE;
|
||||||
|
|
||||||
} catch (ProcessingException | IllegalStateException e) {
|
} catch (ProcessingException | IllegalStateException e) {
|
||||||
|
|
|
@ -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.
|
* @param b is the boolean to convert.
|
||||||
* @return a ProtoBuf message with boolean payload.
|
* @return a ProtoBuf message with boolean payload.
|
||||||
*/
|
*/
|
||||||
private BoolMsg boolToBoolMsg(boolean b){
|
private BoolValue boolToBoolValue(boolean b){
|
||||||
return BoolMsg.newBuilder()
|
return BoolValue.newBuilder()
|
||||||
.setValue(b)
|
.setValue(b)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
@ -419,10 +419,10 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
|
||||||
* @return TRUE if the post is successful and FALSE otherwise
|
* @return TRUE if the post is successful and FALSE otherwise
|
||||||
* @throws CommunicationException
|
* @throws CommunicationException
|
||||||
*/
|
*/
|
||||||
public BoolMsg postMessage(BulletinBoardMessage msg, boolean checkSignature) throws CommunicationException{
|
public BoolValue postMessage(BulletinBoardMessage msg, boolean checkSignature) throws CommunicationException{
|
||||||
|
|
||||||
if (checkSignature && !verifyMessage(msg)) {
|
if (checkSignature && !verifyMessage(msg)) {
|
||||||
return boolToBoolMsg(false);
|
return boolToBoolValue(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
String sql;
|
String sql;
|
||||||
|
@ -520,12 +520,12 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
|
||||||
|
|
||||||
jdbcTemplate.batchUpdate(sql,namedParameterArray);
|
jdbcTemplate.batchUpdate(sql,namedParameterArray);
|
||||||
|
|
||||||
return boolToBoolMsg(true);
|
return boolToBoolValue(true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
return postMessage(msg, true); // Perform a post and check the signature for authenticity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -691,11 +691,11 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BoolMsg beginBatch(BeginBatchMessage message) throws CommunicationException {
|
public BoolValue beginBatch(BeginBatchMessage message) throws CommunicationException {
|
||||||
|
|
||||||
// Check if batch is closed
|
// Check if batch is closed
|
||||||
if (isBatchClosed(message.getSignerId(), message.getBatchId())) {
|
if (isBatchClosed(message.getSignerId(), message.getBatchId())) {
|
||||||
return BoolMsg.newBuilder().setValue(false).build();
|
return BoolValue.newBuilder().setValue(false).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add new tags to table
|
// Add new tags to table
|
||||||
|
@ -721,17 +721,17 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
|
||||||
|
|
||||||
jdbcTemplate.batchUpdate(sql,namedParameters);
|
jdbcTemplate.batchUpdate(sql,namedParameters);
|
||||||
|
|
||||||
return BoolMsg.newBuilder().setValue(true).build();
|
return BoolValue.newBuilder().setValue(true).build();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BoolMsg postBatchMessage(BatchMessage batchMessage) throws CommunicationException{
|
public BoolValue postBatchMessage(BatchMessage batchMessage) throws CommunicationException{
|
||||||
|
|
||||||
// Check if batch is closed
|
// Check if batch is closed
|
||||||
if (isBatchClosed(batchMessage.getSignerId(), batchMessage.getBatchId())) {
|
if (isBatchClosed(batchMessage.getSignerId(), batchMessage.getBatchId())) {
|
||||||
return BoolMsg.newBuilder().setValue(false).build();
|
return BoolValue.newBuilder().setValue(false).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add data
|
// Add data
|
||||||
|
@ -745,13 +745,13 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
|
||||||
|
|
||||||
jdbcTemplate.update(sql, namedParameters);
|
jdbcTemplate.update(sql, namedParameters);
|
||||||
|
|
||||||
return BoolMsg.newBuilder().setValue(true).build();
|
return BoolValue.newBuilder().setValue(true).build();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BoolMsg closeBatchMessage(CloseBatchMessage message) throws CommunicationException {
|
public BoolValue closeBatchMessage(CloseBatchMessage message) throws CommunicationException {
|
||||||
|
|
||||||
ByteString signerId = message.getSig().getSignerId();
|
ByteString signerId = message.getSig().getSignerId();
|
||||||
int batchId = message.getBatchId();
|
int batchId = message.getBatchId();
|
||||||
|
@ -770,7 +770,7 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
|
||||||
List<Long> lengthResult = jdbcTemplate.query(sql, namedParameters, new LongMapper());
|
List<Long> lengthResult = jdbcTemplate.query(sql, namedParameters, new LongMapper());
|
||||||
|
|
||||||
if (lengthResult.get(0) != message.getBatchLength()) {
|
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
|
// Get Tags and add them to CompleteBatch
|
||||||
|
@ -813,7 +813,7 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
|
||||||
// TODO: Actual verification
|
// TODO: Actual verification
|
||||||
// //signer.verify(completeBatch);
|
// //signer.verify(completeBatch);
|
||||||
// } catch (CertificateException | InvalidKeyException | SignatureException e) {
|
// } catch (CertificateException | InvalidKeyException | SignatureException e) {
|
||||||
// return BoolMsg.newBuilder().setValue(false).build();
|
// return BoolValue.newBuilder().setValue(false).build();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Batch verified: finalize it
|
// Batch verified: finalize it
|
||||||
|
@ -849,7 +849,7 @@ public class BulletinBoardSQLServer implements BulletinBoardServer{
|
||||||
|
|
||||||
// Return TRUE
|
// Return TRUE
|
||||||
|
|
||||||
return BoolMsg.newBuilder().setValue(true).build();
|
return BoolValue.newBuilder().setValue(true).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import javax.ws.rs.core.Context;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.StreamingOutput;
|
import javax.ws.rs.core.StreamingOutput;
|
||||||
|
|
||||||
|
import com.google.protobuf.BoolValue;
|
||||||
import meerkat.bulletinboard.BulletinBoardServer;
|
import meerkat.bulletinboard.BulletinBoardServer;
|
||||||
import meerkat.bulletinboard.sqlserver.BulletinBoardSQLServer;
|
import meerkat.bulletinboard.sqlserver.BulletinBoardSQLServer;
|
||||||
import meerkat.bulletinboard.sqlserver.H2QueryProvider;
|
import meerkat.bulletinboard.sqlserver.H2QueryProvider;
|
||||||
|
@ -88,7 +89,7 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL
|
||||||
@Consumes(MEDIATYPE_PROTOBUF)
|
@Consumes(MEDIATYPE_PROTOBUF)
|
||||||
@Produces(MEDIATYPE_PROTOBUF)
|
@Produces(MEDIATYPE_PROTOBUF)
|
||||||
@Override
|
@Override
|
||||||
public BoolMsg postMessage(BulletinBoardMessage msg) throws CommunicationException {
|
public BoolValue postMessage(BulletinBoardMessage msg) throws CommunicationException {
|
||||||
init();
|
init();
|
||||||
return bulletinBoard.postMessage(msg);
|
return bulletinBoard.postMessage(msg);
|
||||||
}
|
}
|
||||||
|
@ -132,7 +133,7 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL
|
||||||
@Consumes(MEDIATYPE_PROTOBUF)
|
@Consumes(MEDIATYPE_PROTOBUF)
|
||||||
@Produces(MEDIATYPE_PROTOBUF)
|
@Produces(MEDIATYPE_PROTOBUF)
|
||||||
@Override
|
@Override
|
||||||
public BoolMsg beginBatch(BeginBatchMessage message) {
|
public BoolValue beginBatch(BeginBatchMessage message) {
|
||||||
try {
|
try {
|
||||||
init();
|
init();
|
||||||
return bulletinBoard.beginBatch(message);
|
return bulletinBoard.beginBatch(message);
|
||||||
|
@ -147,7 +148,7 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL
|
||||||
@Consumes(MEDIATYPE_PROTOBUF)
|
@Consumes(MEDIATYPE_PROTOBUF)
|
||||||
@Produces(MEDIATYPE_PROTOBUF)
|
@Produces(MEDIATYPE_PROTOBUF)
|
||||||
@Override
|
@Override
|
||||||
public BoolMsg postBatchMessage(BatchMessage batchMessage) {
|
public BoolValue postBatchMessage(BatchMessage batchMessage) {
|
||||||
try {
|
try {
|
||||||
init();
|
init();
|
||||||
return bulletinBoard.postBatchMessage(batchMessage);
|
return bulletinBoard.postBatchMessage(batchMessage);
|
||||||
|
@ -162,7 +163,7 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL
|
||||||
@Consumes(MEDIATYPE_PROTOBUF)
|
@Consumes(MEDIATYPE_PROTOBUF)
|
||||||
@Produces(MEDIATYPE_PROTOBUF)
|
@Produces(MEDIATYPE_PROTOBUF)
|
||||||
@Override
|
@Override
|
||||||
public BoolMsg closeBatchMessage(CloseBatchMessage message) {
|
public BoolValue closeBatchMessage(CloseBatchMessage message) {
|
||||||
try {
|
try {
|
||||||
init();
|
init();
|
||||||
return bulletinBoard.closeBatchMessage(message);
|
return bulletinBoard.closeBatchMessage(message);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package meerkat.bulletinboard;
|
package meerkat.bulletinboard;
|
||||||
|
|
||||||
|
|
||||||
|
import com.google.protobuf.BoolValue;
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
import com.google.protobuf.TextFormat;
|
import com.google.protobuf.TextFormat;
|
||||||
|
|
||||||
|
@ -61,7 +62,7 @@ public class BulletinBoardSQLServerIntegrationTest {
|
||||||
|
|
||||||
WebTarget webTarget;
|
WebTarget webTarget;
|
||||||
Response response;
|
Response response;
|
||||||
BoolMsg bool;
|
BoolValue bool;
|
||||||
|
|
||||||
BulletinBoardMessage msg;
|
BulletinBoardMessage msg;
|
||||||
|
|
||||||
|
@ -95,7 +96,7 @@ public class BulletinBoardSQLServerIntegrationTest {
|
||||||
|
|
||||||
response = webTarget.request(Constants.MEDIATYPE_PROTOBUF).post(Entity.entity(msg, Constants.MEDIATYPE_PROTOBUF));
|
response = webTarget.request(Constants.MEDIATYPE_PROTOBUF).post(Entity.entity(msg, Constants.MEDIATYPE_PROTOBUF));
|
||||||
System.err.println(response);
|
System.err.println(response);
|
||||||
bool = response.readEntity(BoolMsg.class);
|
bool = response.readEntity(BoolValue.class);
|
||||||
assert bool.getValue();
|
assert bool.getValue();
|
||||||
|
|
||||||
msg = BulletinBoardMessage.newBuilder()
|
msg = BulletinBoardMessage.newBuilder()
|
||||||
|
@ -114,7 +115,7 @@ public class BulletinBoardSQLServerIntegrationTest {
|
||||||
|
|
||||||
response = webTarget.request(Constants.MEDIATYPE_PROTOBUF).post(Entity.entity(msg, Constants.MEDIATYPE_PROTOBUF));
|
response = webTarget.request(Constants.MEDIATYPE_PROTOBUF).post(Entity.entity(msg, Constants.MEDIATYPE_PROTOBUF));
|
||||||
System.err.println(response);
|
System.err.println(response);
|
||||||
bool = response.readEntity(BoolMsg.class);
|
bool = response.readEntity(BoolValue.class);
|
||||||
assert bool.getValue();
|
assert bool.getValue();
|
||||||
|
|
||||||
// Test reading mechanism
|
// Test reading mechanism
|
||||||
|
|
|
@ -17,6 +17,7 @@ import java.security.UnrecoverableKeyException;
|
||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import com.google.protobuf.BoolValue;
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
|
|
||||||
import com.google.protobuf.Timestamp;
|
import com.google.protobuf.Timestamp;
|
||||||
|
@ -457,7 +458,7 @@ public class GenericBulletinBoardServerTest {
|
||||||
.setSeconds(978325)
|
.setSeconds(978325)
|
||||||
.setNanos(8097234)
|
.setNanos(8097234)
|
||||||
.build());
|
.build());
|
||||||
BoolMsg result;
|
BoolValue result;
|
||||||
|
|
||||||
// Create data
|
// Create data
|
||||||
|
|
||||||
|
@ -527,7 +528,7 @@ public class GenericBulletinBoardServerTest {
|
||||||
.build());
|
.build());
|
||||||
int currentBatch = completeBatches.size();
|
int currentBatch = completeBatches.size();
|
||||||
|
|
||||||
BoolMsg result;
|
BoolValue result;
|
||||||
|
|
||||||
// Define batch data
|
// Define batch data
|
||||||
|
|
||||||
|
@ -646,7 +647,7 @@ public class GenericBulletinBoardServerTest {
|
||||||
|
|
||||||
BulletinBoardMessage newMessage = bulletinBoardMessageGenerator.generateRandomMessage(signers, timestamp, 10, 10);
|
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));
|
assertThat("Failed to post message to BB Server", result.getValue(), is(true));
|
||||||
|
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package meerkat.bulletinboard;
|
package meerkat.bulletinboard;
|
||||||
|
|
||||||
|
import com.google.protobuf.BoolValue;
|
||||||
import meerkat.comm.CommunicationException;
|
import meerkat.comm.CommunicationException;
|
||||||
import meerkat.comm.MessageOutputStream;
|
import meerkat.comm.MessageOutputStream;
|
||||||
import meerkat.protobuf.BulletinBoardAPI.*;
|
import meerkat.protobuf.BulletinBoardAPI.*;
|
||||||
import meerkat.protobuf.Comm.*;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
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)
|
* @return TRUE if the message has been authenticated and FALSE otherwise (in ProtoBuf form)
|
||||||
* @throws CommunicationException on DB connection error
|
* @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
|
* 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
|
* However, if such a batch exists and is already closed: the value returned will be FALSE
|
||||||
* @throws CommunicationException on DB connection error
|
* @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
|
* 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
|
* However, requiring to open a batch before insertion of messages is implementation-dependent
|
||||||
* @throws CommunicationException on DB connection error
|
* @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
|
* 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
|
* 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
|
* @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)
|
* Reads a batch message from the server (starting with the supplied position)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package meerkat.pollingstation;
|
package meerkat.pollingstation;
|
||||||
|
|
||||||
import com.google.common.util.concurrent.FutureCallback;
|
import com.google.common.util.concurrent.FutureCallback;
|
||||||
import meerkat.protobuf.Comm.BoolMsg;
|
import com.google.protobuf.BoolValue;
|
||||||
import meerkat.protobuf.PollingStation.*;
|
import meerkat.protobuf.PollingStation.*;
|
||||||
/**
|
/**
|
||||||
* Created by Arbel on 05/05/2016.
|
* Created by Arbel on 05/05/2016.
|
||||||
|
@ -45,16 +45,16 @@ public interface PollingStationScanner {
|
||||||
/**
|
/**
|
||||||
* Sends a scan to all subscribers
|
* Sends a scan to all subscribers
|
||||||
* @param scannedData contains the scanned data
|
* @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
|
* Notifies subscribers about an error that occurred during scan
|
||||||
* @param errorMsg is the error that occurred
|
* @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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,11 +12,4 @@ message ScannedData {
|
||||||
// Container for error messages
|
// Container for error messages
|
||||||
message ErrorMsg {
|
message ErrorMsg {
|
||||||
string msg = 1;
|
string msg = 1;
|
||||||
}
|
|
||||||
|
|
||||||
// Container for HTTP address
|
|
||||||
message HTTPAddress {
|
|
||||||
string hostname = 1;
|
|
||||||
int32 port = 2;
|
|
||||||
string address = 3;
|
|
||||||
}
|
}
|
|
@ -10,12 +10,4 @@ message BroadcastMessage {
|
||||||
bool is_private = 3;
|
bool is_private = 3;
|
||||||
|
|
||||||
bytes payload = 5;
|
bytes payload = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
message BoolMsg {
|
|
||||||
bool value = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
message IntMsg {
|
|
||||||
int32 value = 1;
|
|
||||||
}
|
|
|
@ -5,7 +5,7 @@ package meerkat.pollingstation;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import com.google.common.util.concurrent.FutureCallback;
|
import com.google.common.util.concurrent.FutureCallback;
|
||||||
import meerkat.protobuf.Comm;
|
import com.google.protobuf.BoolValue;
|
||||||
import meerkat.protobuf.PollingStation;
|
import meerkat.protobuf.PollingStation;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
@ -16,7 +16,6 @@ import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
import java.io.IOException;
|
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_ERROR_PATH;
|
||||||
import static meerkat.pollingstation.PollingStationConstants.POLLING_STATION_WEB_SCANNER_SCAN_PATH;
|
import static meerkat.pollingstation.PollingStationConstants.POLLING_STATION_WEB_SCANNER_SCAN_PATH;
|
||||||
|
@ -32,14 +31,14 @@ public class PollingStationScannerWebApp implements PollingStationScanner.Produc
|
||||||
@Context
|
@Context
|
||||||
ServletContext servletContext;
|
ServletContext servletContext;
|
||||||
|
|
||||||
Iterator<FutureCallback<PollingStation.ScannedData>> callbacks;
|
Iterable<FutureCallback<PollingStation.ScannedData>> callbacks;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is called by the Jetty engine when instantiating the servlet
|
* This method is called by the Jetty engine when instantiating the servlet
|
||||||
*/
|
*/
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() throws Exception{
|
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
|
@POST
|
||||||
|
@ -47,18 +46,18 @@ public class PollingStationScannerWebApp implements PollingStationScanner.Produc
|
||||||
@Consumes(MEDIATYPE_PROTOBUF)
|
@Consumes(MEDIATYPE_PROTOBUF)
|
||||||
@Produces(MEDIATYPE_PROTOBUF)
|
@Produces(MEDIATYPE_PROTOBUF)
|
||||||
@Override
|
@Override
|
||||||
public Comm.BoolMsg newScan(PollingStation.ScannedData scannedData) {
|
public BoolValue newScan(PollingStation.ScannedData scannedData) {
|
||||||
|
|
||||||
boolean handled = false;
|
boolean handled = false;
|
||||||
|
|
||||||
while (callbacks.hasNext()){
|
for (FutureCallback<PollingStation.ScannedData> callback : callbacks){
|
||||||
|
|
||||||
callbacks.next().onSuccess(scannedData);
|
callback.onSuccess(scannedData);
|
||||||
handled = true;
|
handled = true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Comm.BoolMsg.newBuilder()
|
return BoolValue.newBuilder()
|
||||||
.setValue(handled)
|
.setValue(handled)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
@ -69,18 +68,18 @@ public class PollingStationScannerWebApp implements PollingStationScanner.Produc
|
||||||
@Consumes(MEDIATYPE_PROTOBUF)
|
@Consumes(MEDIATYPE_PROTOBUF)
|
||||||
@Produces(MEDIATYPE_PROTOBUF)
|
@Produces(MEDIATYPE_PROTOBUF)
|
||||||
@Override
|
@Override
|
||||||
public Comm.BoolMsg reportScanError(PollingStation.ErrorMsg errorMsg) {
|
public BoolValue reportScanError(PollingStation.ErrorMsg errorMsg) {
|
||||||
|
|
||||||
boolean handled = false;
|
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;
|
handled = true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Comm.BoolMsg.newBuilder()
|
return BoolValue.newBuilder()
|
||||||
.setValue(handled)
|
.setValue(handled)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package meerkat.pollingstation;
|
package meerkat.pollingstation;
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.LinkedList;
|
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.server.Server;
|
||||||
import org.eclipse.jetty.servlet.*;
|
import org.eclipse.jetty.servlet.*;
|
||||||
|
|
||||||
import meerkat.protobuf.PollingStation.*;
|
|
||||||
import org.glassfish.jersey.servlet.ServletContainer;
|
import org.glassfish.jersey.servlet.ServletContainer;
|
||||||
import org.glassfish.jersey.server.ResourceConfig;
|
import org.glassfish.jersey.server.ResourceConfig;
|
||||||
|
|
||||||
|
@ -26,14 +24,14 @@ public class PollingStationWebScanner implements PollingStationScanner.Consumer{
|
||||||
private final Server server;
|
private final Server server;
|
||||||
private final List<FutureCallback<ScannedData>> callbacks;
|
private final List<FutureCallback<ScannedData>> callbacks;
|
||||||
|
|
||||||
public PollingStationWebScanner(HTTPAddress address) {
|
public PollingStationWebScanner(int port, String subAddress) {
|
||||||
|
|
||||||
callbacks = new LinkedList<>();
|
callbacks = new LinkedList<>();
|
||||||
|
|
||||||
server = new Server(address.getPort());
|
server = new Server(port);
|
||||||
|
|
||||||
ServletContextHandler servletContextHandler = new ServletContextHandler(server, address.getAddress());
|
ServletContextHandler servletContextHandler = new ServletContextHandler(server, subAddress);
|
||||||
servletContextHandler.setAttribute(CALLBACKS_ATTRIBUTE_NAME, new CallbackAccessor());
|
servletContextHandler.setAttribute(CALLBACKS_ATTRIBUTE_NAME, (Iterable<FutureCallback<ScannedData>>) callbacks);
|
||||||
|
|
||||||
ResourceConfig resourceConfig = new ResourceConfig(PollingStationScannerWebApp.class);
|
ResourceConfig resourceConfig = new ResourceConfig(PollingStationScannerWebApp.class);
|
||||||
resourceConfig.register(ProtobufMessageBodyReader.class);
|
resourceConfig.register(ProtobufMessageBodyReader.class);
|
||||||
|
@ -59,10 +57,4 @@ public class PollingStationWebScanner implements PollingStationScanner.Consumer{
|
||||||
callbacks.add(scanCallback);
|
callbacks.add(scanCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class CallbackAccessor {
|
|
||||||
public Iterator<FutureCallback<ScannedData>> getCallbackIterator() {
|
|
||||||
return callbacks.iterator();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,6 @@ package meerkat.pollingstation;
|
||||||
|
|
||||||
import com.google.common.util.concurrent.FutureCallback;
|
import com.google.common.util.concurrent.FutureCallback;
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
import com.sun.org.apache.regexp.internal.RE;
|
|
||||||
import meerkat.protobuf.PollingStation;
|
|
||||||
import meerkat.protobuf.PollingStation.*;
|
import meerkat.protobuf.PollingStation.*;
|
||||||
import meerkat.rest.Constants;
|
import meerkat.rest.Constants;
|
||||||
|
|
||||||
|
@ -18,12 +16,10 @@ import javax.ws.rs.client.Entity;
|
||||||
import javax.ws.rs.client.WebTarget;
|
import javax.ws.rs.client.WebTarget;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.concurrent.Semaphore;
|
import java.util.concurrent.Semaphore;
|
||||||
|
|
||||||
import static meerkat.pollingstation.PollingStationConstants.*;
|
import static meerkat.pollingstation.PollingStationConstants.*;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.*;
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -32,7 +28,9 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
public class PollingStationWebScannerTest {
|
public class PollingStationWebScannerTest {
|
||||||
|
|
||||||
private PollingStationScanner.Consumer scanner;
|
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 Semaphore semaphore;
|
||||||
private Throwable thrown;
|
private Throwable thrown;
|
||||||
|
@ -86,12 +84,7 @@ public class PollingStationWebScannerTest {
|
||||||
|
|
||||||
System.err.println("Setting up Scanner WebApp!");
|
System.err.println("Setting up Scanner WebApp!");
|
||||||
|
|
||||||
httpAddress = HTTPAddress.newBuilder()
|
scanner = new PollingStationWebScanner(PORT, SUB_ADDRESS);
|
||||||
.setPort(8080)
|
|
||||||
.setHostname("http://localhost")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
scanner = new PollingStationWebScanner(httpAddress);
|
|
||||||
|
|
||||||
semaphore = new Semaphore(0);
|
semaphore = new Semaphore(0);
|
||||||
thrown = null;
|
thrown = null;
|
||||||
|
@ -110,8 +103,8 @@ public class PollingStationWebScannerTest {
|
||||||
Client client = ClientBuilder.newClient();
|
Client client = ClientBuilder.newClient();
|
||||||
client.register(ProtobufMessageBodyReader.class);
|
client.register(ProtobufMessageBodyReader.class);
|
||||||
client.register(ProtobufMessageBodyWriter.class);
|
client.register(ProtobufMessageBodyWriter.class);
|
||||||
WebTarget webTarget = client.target(httpAddress.getHostname() + ":" + httpAddress.getPort())
|
WebTarget webTarget = client.target(ADDRESS + ":" + PORT)
|
||||||
.path(httpAddress.getAddress()).path(POLLING_STATION_WEB_SCANNER_SCAN_PATH);
|
.path(SUB_ADDRESS).path(POLLING_STATION_WEB_SCANNER_SCAN_PATH);
|
||||||
|
|
||||||
byte[] data = {(byte) 1, (byte) 2};
|
byte[] data = {(byte) 1, (byte) 2};
|
||||||
|
|
||||||
|
@ -136,8 +129,8 @@ public class PollingStationWebScannerTest {
|
||||||
Client client = ClientBuilder.newClient();
|
Client client = ClientBuilder.newClient();
|
||||||
client.register(ProtobufMessageBodyReader.class);
|
client.register(ProtobufMessageBodyReader.class);
|
||||||
client.register(ProtobufMessageBodyWriter.class);
|
client.register(ProtobufMessageBodyWriter.class);
|
||||||
WebTarget webTarget = client.target(httpAddress.getHostname() + ":" + httpAddress.getPort())
|
WebTarget webTarget = client.target(ADDRESS + ":" + PORT)
|
||||||
.path(httpAddress.getAddress()).path(POLLING_STATION_WEB_SCANNER_ERROR_PATH);
|
.path(SUB_ADDRESS).path(POLLING_STATION_WEB_SCANNER_ERROR_PATH);
|
||||||
|
|
||||||
ErrorMsg errorMsg = ErrorMsg.newBuilder()
|
ErrorMsg errorMsg = ErrorMsg.newBuilder()
|
||||||
.setMsg("!Error Message!")
|
.setMsg("!Error Message!")
|
||||||
|
|
Loading…
Reference in New Issue