main that starts threads added
parent
29f6b244d7
commit
cc3e151a26
|
@ -18,6 +18,7 @@ public class PollingStationClientToyRun {
|
|||
|
||||
public static void main(String [] args) {
|
||||
int channel = 10;
|
||||
System.out.println("Message to send: "+channel);
|
||||
byte[] data = ByteBuffer.allocate(4).putInt(channel).array();
|
||||
// byte[] data = {(byte) 1, (byte) 2};
|
||||
|
||||
|
@ -31,6 +32,8 @@ public class PollingStationClientToyRun {
|
|||
// System.out.println("Channel int retrieved:"+retrieved);
|
||||
|
||||
ScannerClientAPI scannerClient = new ScannerClientAPI(ADDRESS, SUB_ADDRESS, PORT, POLLING_STATION_WEB_SCANNER_SCAN_PATH);
|
||||
scannerClient.sendScan(scannedData);
|
||||
boolean sent = scannerClient.sendScan(scannedData);
|
||||
|
||||
System.out.println("Message sent successfully: "+sent);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package meerkat.pollingstation;
|
||||
|
||||
import com.google.common.util.concurrent.FutureCallback;
|
||||
import meerkat.pollingstation.controller.PollingStationControllerInterface;
|
||||
import meerkat.pollingstation.controller.commands.PollingStationCommand;
|
||||
import meerkat.pollingstation.controller.commands.ReceivedScanCommand;
|
||||
import meerkat.protobuf.PollingStation;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -12,7 +11,7 @@ import java.util.concurrent.LinkedBlockingQueue;
|
|||
/**
|
||||
* Created by Laura on 3/20/2017.
|
||||
*/
|
||||
public class PollingStationMainController {
|
||||
public class PollingStationMainController implements PollingStationControllerInterface {
|
||||
private final Logger logger = LoggerFactory.getLogger(PollingStationMainController.class);
|
||||
|
||||
private LinkedBlockingQueue<PollingStationCommand> queue;
|
||||
|
@ -28,11 +27,13 @@ public class PollingStationMainController {
|
|||
shutDownHasBeenCalled = false;
|
||||
}
|
||||
|
||||
public void start() throws Exception {
|
||||
server = new ReceiverScanHandler(PORT, SUB_ADDRESS);
|
||||
@Override
|
||||
public void init(ReceiverScanHandler server) {
|
||||
server.setControllerQueue(queue);
|
||||
try {
|
||||
server.start();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
logger.info("Started controller");
|
||||
while (! wasShutDownCalled()) {
|
||||
try {
|
||||
PollingStationCommand command = queue.take();
|
||||
|
@ -42,9 +43,6 @@ public class PollingStationMainController {
|
|||
System.err.println("Interrupted while reading from command queue " + e);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Could not start server: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean wasShutDownCalled () {
|
||||
|
@ -68,7 +66,7 @@ public class PollingStationMainController {
|
|||
|
||||
// decide which method to run according to the command type
|
||||
if (command instanceof ReceivedScanCommand) {
|
||||
doProcessScan ();
|
||||
doProcessScan((ReceivedScanCommand) command);
|
||||
}
|
||||
// else if (command instanceof ChannelChoiceCommand) {
|
||||
// doChooseChannel();
|
||||
|
@ -80,8 +78,9 @@ public class PollingStationMainController {
|
|||
}
|
||||
}
|
||||
|
||||
private void doProcessScan() {
|
||||
|
||||
private void doProcessScan(ReceivedScanCommand command) {
|
||||
logger.info("The Polling Station starts processing the data...");
|
||||
logger.info("BallotID = "+command.getBallotSerialNumber());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -91,4 +90,10 @@ public class PollingStationMainController {
|
|||
private void doReportErrorAndForceRestart(String errorMessage) {
|
||||
queue.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
// TODO: 4/16/2017 Implemet the shutdown behavior
|
||||
public void callShutDown() {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,15 +13,26 @@ public class PollingStationToyRun {
|
|||
public static void main(String [] args) {
|
||||
System.err.println("Setting up Scanner WebApp!");
|
||||
|
||||
scanner = new ReceiverScanHandler(PORT, SUB_ADDRESS);
|
||||
// scanner = new ReceiverScanHandler(PORT, SUB_ADDRESS);
|
||||
//
|
||||
// try {
|
||||
// scanner.start();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// System.err.println("Could not start server: " + e.getMessage());
|
||||
// }
|
||||
|
||||
try {
|
||||
scanner.start();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Could not start server: " + e.getMessage());
|
||||
}
|
||||
ReceiverScanHandler serverController = new ReceiverScanHandler(PORT, SUB_ADDRESS);
|
||||
PollingStationMainController controller = new PollingStationMainController();
|
||||
controller.init(serverController);
|
||||
|
||||
Thread controllerThread = new Thread(controller);
|
||||
controllerThread.setName("Meerkat PS-Controller Thread");
|
||||
Thread serverThread = new Thread(serverController);
|
||||
serverThread.setName("Meerkat PS-ServerScanner Thread");
|
||||
|
||||
controllerThread.start();
|
||||
serverThread.start();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ import java.util.concurrent.LinkedBlockingQueue;
|
|||
/**
|
||||
* Created by Laura on 3/20/2017.
|
||||
*/
|
||||
public class ReceiverScanHandler implements PollingStationScanner.Consumer{
|
||||
public class ReceiverScanHandler implements PollingStationScanner.Consumer, Runnable{
|
||||
|
||||
public final static String CALLBACKS_ATTRIBUTE_NAME = "controller";
|
||||
|
||||
|
@ -66,12 +66,19 @@ public class ReceiverScanHandler implements PollingStationScanner.Consumer{
|
|||
return shutDownHasBeenCalled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
start();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Could not start server: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() throws Exception {
|
||||
server.start();
|
||||
|
||||
|
||||
|
||||
while (! wasShutDownCalled()) {
|
||||
try {
|
||||
handleSingleCallback(queue.take());
|
||||
|
@ -94,7 +101,6 @@ public class ReceiverScanHandler implements PollingStationScanner.Consumer{
|
|||
|
||||
|
||||
private void handleSingleCallback(ScanCallback callback) {
|
||||
System.out.println(callback.toString());
|
||||
if (callback instanceof ScanDataCallback) {
|
||||
doScannedData((ScanDataCallback) callback);
|
||||
}
|
||||
|
@ -109,13 +115,11 @@ public class ReceiverScanHandler implements PollingStationScanner.Consumer{
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: parse fields of protobuf properly
|
||||
// TODO: 4/16/2017 Parse protobuf fields properly
|
||||
private void doScannedData(ScanDataCallback cb) {
|
||||
PollingStation.ScannedData scan = cb.getScannedData();
|
||||
int channel = ByteBuffer.wrap(scan.getChannel().toByteArray()).getInt();
|
||||
long channelLong = 12345678910L;
|
||||
System.out.println("Channel int:"+channel);
|
||||
System.out.println("Channel int:"+channelLong);
|
||||
controllerQueue.add(new ReceivedScanCommand(channel, channelLong));
|
||||
}
|
||||
|
||||
|
|
|
@ -55,7 +55,7 @@ public class ReceiverWebAPI implements PollingStationScanner.Producer {
|
|||
@Override
|
||||
public BoolValue newScan(PollingStation.ScannedData scannedData) {
|
||||
|
||||
boolean handled = false;
|
||||
boolean sent = false;
|
||||
|
||||
// for (FutureCallback<PollingStation.ScannedData> callback : callbacks){
|
||||
//
|
||||
|
@ -63,14 +63,17 @@ public class ReceiverWebAPI implements PollingStationScanner.Producer {
|
|||
// handled = true;
|
||||
//
|
||||
// }
|
||||
|
||||
ScanDataCallback callback = new ScanDataCallback(scannedData);
|
||||
try {
|
||||
ScanDataCallback callback = new ScanDataCallback(scannedData);
|
||||
// scanner.subscribe(callback);
|
||||
callbacks.add(callback);
|
||||
|
||||
callbacks.add(callback);
|
||||
sent = true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return BoolValue.newBuilder()
|
||||
.setValue(handled)
|
||||
.setValue(sent)
|
||||
.build();
|
||||
|
||||
}
|
||||
|
|
|
@ -31,15 +31,27 @@ public class ScannerClientAPI {
|
|||
.path(sub_address).path(path);
|
||||
}
|
||||
|
||||
public void sendScan(PollingStation.ScannedData scannedData) {
|
||||
/**
|
||||
* Sends the scanned data to the polling station server
|
||||
* @param scannedData
|
||||
* @return boolean value: true if the data was sent successfully, false otherwise
|
||||
*/
|
||||
public boolean sendScan(PollingStation.ScannedData scannedData) {
|
||||
Response response = webTarget.request(Constants.MEDIATYPE_PROTOBUF).post(Entity.entity(scannedData, Constants.MEDIATYPE_PROTOBUF));
|
||||
BoolValue res = response.readEntity(BoolValue.class);
|
||||
System.out.println(res.toString());
|
||||
response.close();
|
||||
return res.getValue();
|
||||
}
|
||||
|
||||
public void sendError(PollingStation.ErrorMsg errorMsg) {
|
||||
/**
|
||||
* Sends the error message to the polling station server
|
||||
* @param errorMsg
|
||||
* @return boolean value: true if the error message was sent successfully, false otherwise
|
||||
*/
|
||||
public boolean sendError(PollingStation.ErrorMsg errorMsg) {
|
||||
Response response = webTarget.request(Constants.MEDIATYPE_PROTOBUF).post(Entity.entity(errorMsg, Constants.MEDIATYPE_PROTOBUF));
|
||||
BoolValue res = response.readEntity(BoolValue.class);
|
||||
response.close();
|
||||
return res.getValue();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package meerkat.pollingstation.controller;
|
||||
|
||||
import meerkat.pollingstation.ReceiverScanHandler;
|
||||
|
||||
/**
|
||||
* An interface for the controller component of the polling station
|
||||
*/
|
||||
public interface PollingStationControllerInterface extends Runnable {
|
||||
/**
|
||||
* initialize by setting all the different components of the Polling Station to be recognized by this controller
|
||||
* (see VotingBoothController)
|
||||
*/
|
||||
// TODO: 4/16/2017 complete with proper arguments and exceptions
|
||||
public void init(ReceiverScanHandler server);
|
||||
|
||||
/**
|
||||
* an asynchronous call from Admin Console (If there is such one implemented) to shut down the system
|
||||
*/
|
||||
public void callShutDown();
|
||||
|
||||
|
||||
}
|
|
@ -34,7 +34,6 @@ public class ProtobufMessageBodyWriter implements MessageBodyWriter<Message> {
|
|||
public void writeTo(Message message, Class<?> type, Type genericType, Annotation[] annotations,
|
||||
MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
|
||||
throws IOException, WebApplicationException {
|
||||
System.out.println("protobufmessagebodywriter");
|
||||
message.writeTo(entityStream);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue