From cc3e151a269ae6273c8cb2639fd66d81801269e0 Mon Sep 17 00:00:00 2001 From: Laura Radaelli Date: Sun, 16 Apr 2017 17:09:35 +0300 Subject: [PATCH] main that starts threads added --- .../PollingStationClientToyRun.java | 5 ++- .../PollingStationMainController.java | 31 +++++++++++-------- .../pollingstation/PollingStationToyRun.java | 25 ++++++++++----- .../pollingstation/ReceiverScanHandler.java | 20 +++++++----- .../pollingstation/ReceiverWebAPI.java | 15 +++++---- .../pollingstation/ScannerClientAPI.java | 18 +++++++++-- .../PollingStationControllerInterface.java | 22 +++++++++++++ .../rest/ProtobufMessageBodyWriter.java | 1 - 8 files changed, 98 insertions(+), 39 deletions(-) create mode 100644 polling-station/src/main/java/meerkat/pollingstation/controller/PollingStationControllerInterface.java diff --git a/polling-station/src/main/java/meerkat/pollingstation/PollingStationClientToyRun.java b/polling-station/src/main/java/meerkat/pollingstation/PollingStationClientToyRun.java index 01f5e66..7aa8a2c 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/PollingStationClientToyRun.java +++ b/polling-station/src/main/java/meerkat/pollingstation/PollingStationClientToyRun.java @@ -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); } } diff --git a/polling-station/src/main/java/meerkat/pollingstation/PollingStationMainController.java b/polling-station/src/main/java/meerkat/pollingstation/PollingStationMainController.java index 9adb79b..5990b5a 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/PollingStationMainController.java +++ b/polling-station/src/main/java/meerkat/pollingstation/PollingStationMainController.java @@ -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 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() { + + } } diff --git a/polling-station/src/main/java/meerkat/pollingstation/PollingStationToyRun.java b/polling-station/src/main/java/meerkat/pollingstation/PollingStationToyRun.java index 2c6112a..3f80ff5 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/PollingStationToyRun.java +++ b/polling-station/src/main/java/meerkat/pollingstation/PollingStationToyRun.java @@ -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(); } } diff --git a/polling-station/src/main/java/meerkat/pollingstation/ReceiverScanHandler.java b/polling-station/src/main/java/meerkat/pollingstation/ReceiverScanHandler.java index 0608676..55a694e 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/ReceiverScanHandler.java +++ b/polling-station/src/main/java/meerkat/pollingstation/ReceiverScanHandler.java @@ -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)); } diff --git a/polling-station/src/main/java/meerkat/pollingstation/ReceiverWebAPI.java b/polling-station/src/main/java/meerkat/pollingstation/ReceiverWebAPI.java index b396045..54381a0 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/ReceiverWebAPI.java +++ b/polling-station/src/main/java/meerkat/pollingstation/ReceiverWebAPI.java @@ -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 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(); } diff --git a/polling-station/src/main/java/meerkat/pollingstation/ScannerClientAPI.java b/polling-station/src/main/java/meerkat/pollingstation/ScannerClientAPI.java index ecf8f74..d692d77 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/ScannerClientAPI.java +++ b/polling-station/src/main/java/meerkat/pollingstation/ScannerClientAPI.java @@ -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(); } } diff --git a/polling-station/src/main/java/meerkat/pollingstation/controller/PollingStationControllerInterface.java b/polling-station/src/main/java/meerkat/pollingstation/controller/PollingStationControllerInterface.java new file mode 100644 index 0000000..343fafe --- /dev/null +++ b/polling-station/src/main/java/meerkat/pollingstation/controller/PollingStationControllerInterface.java @@ -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(); + + +} diff --git a/restful-api-common/src/main/java/meerkat/rest/ProtobufMessageBodyWriter.java b/restful-api-common/src/main/java/meerkat/rest/ProtobufMessageBodyWriter.java index 0a17ff6..b8ea503 100644 --- a/restful-api-common/src/main/java/meerkat/rest/ProtobufMessageBodyWriter.java +++ b/restful-api-common/src/main/java/meerkat/rest/ProtobufMessageBodyWriter.java @@ -34,7 +34,6 @@ public class ProtobufMessageBodyWriter implements MessageBodyWriter { public void writeTo(Message message, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { - System.out.println("protobufmessagebodywriter"); message.writeTo(entityStream); } }