diff --git a/polling-station/src/main/java/meerkat/pollingstation/PollingStationClientToyRun.java b/polling-station/src/main/java/meerkat/pollingstation/PollingStationClientToyRun.java index b3f25b3..01f5e66 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/PollingStationClientToyRun.java +++ b/polling-station/src/main/java/meerkat/pollingstation/PollingStationClientToyRun.java @@ -3,6 +3,8 @@ package meerkat.pollingstation; import com.google.protobuf.ByteString; import meerkat.protobuf.PollingStation; +import java.nio.ByteBuffer; + import static meerkat.pollingstation.PollingStationConstants.POLLING_STATION_WEB_SCANNER_SCAN_PATH; /** @@ -15,12 +17,19 @@ public class PollingStationClientToyRun { private static final int PORT = 8080; public static void main(String [] args) { - byte[] data = {(byte) 1, (byte) 2}; + int channel = 10; + byte[] data = ByteBuffer.allocate(4).putInt(channel).array(); +// byte[] data = {(byte) 1, (byte) 2}; PollingStation.ScannedData scannedData = PollingStation.ScannedData.newBuilder() .setChannel(ByteString.copyFrom(data)) .build(); +// int retrieved = ByteBuffer.wrap(scannedData.getChannel().toByteArray()).getInt(); +// System.out.println(retrieved); +// System.out.println("Channel int initial:"+channel); +// System.out.println("Channel int retrieved:"+retrieved); + ScannerClientAPI scannerClient = new ScannerClientAPI(ADDRESS, SUB_ADDRESS, PORT, POLLING_STATION_WEB_SCANNER_SCAN_PATH); scannerClient.sendScan(scannedData); } diff --git a/polling-station/src/main/java/meerkat/pollingstation/PollingStationMainController.java b/polling-station/src/main/java/meerkat/pollingstation/PollingStationMainController.java index 4e02ed9..9adb79b 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/PollingStationMainController.java +++ b/polling-station/src/main/java/meerkat/pollingstation/PollingStationMainController.java @@ -18,20 +18,32 @@ public class PollingStationMainController { private LinkedBlockingQueue queue; private volatile boolean shutDownHasBeenCalled; + private static ReceiverScanHandler server; + private static final String ADDRESS = "http://localhost"; + private static final String SUB_ADDRESS = ""; + private static final int PORT = 8080; + public PollingStationMainController() { - queue = new LinkedBlockingQueue<>(); + queue = new LinkedBlockingQueue(); shutDownHasBeenCalled = false; } public void start() throws Exception { - while (! wasShutDownCalled()) { - try { - PollingStationCommand command = queue.take(); - handleSingleCommand(command); - } - catch (InterruptedException e) { - System.err.println("Interrupted while reading from command queue " + e); + server = new ReceiverScanHandler(PORT, SUB_ADDRESS); + server.setControllerQueue(queue); + try { + server.start(); + while (! wasShutDownCalled()) { + try { + PollingStationCommand command = queue.take(); + handleSingleCommand(command); + } + catch (InterruptedException e) { + System.err.println("Interrupted while reading from command queue " + e); + } } + } catch (Exception e) { + System.err.println("Could not start server: " + e.getMessage()); } } diff --git a/polling-station/src/main/java/meerkat/pollingstation/PollingStationToyRun.java b/polling-station/src/main/java/meerkat/pollingstation/PollingStationToyRun.java index ca630dc..2c6112a 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/PollingStationToyRun.java +++ b/polling-station/src/main/java/meerkat/pollingstation/PollingStationToyRun.java @@ -18,6 +18,7 @@ public class PollingStationToyRun { try { scanner.start(); } catch (Exception e) { + e.printStackTrace(); System.err.println("Could not start server: " + e.getMessage()); } diff --git a/polling-station/src/main/java/meerkat/pollingstation/ReceiverScanHandler.java b/polling-station/src/main/java/meerkat/pollingstation/ReceiverScanHandler.java index d4c987f..0608676 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/ReceiverScanHandler.java +++ b/polling-station/src/main/java/meerkat/pollingstation/ReceiverScanHandler.java @@ -3,6 +3,9 @@ package meerkat.pollingstation; import com.google.common.util.concurrent.FutureCallback; import meerkat.pollingstation.controller.callbacks.ScanCallback; import meerkat.pollingstation.controller.callbacks.ScanDataCallback; +import meerkat.pollingstation.controller.callbacks.ScanErrorCallback; +import meerkat.pollingstation.controller.commands.PollingStationCommand; +import meerkat.pollingstation.controller.commands.ReceivedScanCommand; import meerkat.protobuf.PollingStation; import meerkat.rest.ProtobufMessageBodyReader; import meerkat.rest.ProtobufMessageBodyWriter; @@ -12,6 +15,7 @@ import org.eclipse.jetty.servlet.ServletHolder; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.servlet.ServletContainer; +import java.nio.ByteBuffer; import java.util.LinkedList; import java.util.List; import java.util.concurrent.LinkedBlockingQueue; @@ -27,6 +31,7 @@ public class ReceiverScanHandler implements PollingStationScanner.Consumer{ private final List> callbacks; private LinkedBlockingQueue queue; + private LinkedBlockingQueue controllerQueue; private volatile boolean shutDownHasBeenCalled; public ReceiverScanHandler(int port, String subAddress) { @@ -53,6 +58,10 @@ public class ReceiverScanHandler implements PollingStationScanner.Consumer{ } + public void setControllerQueue(LinkedBlockingQueue q) { + this.controllerQueue = q; + } + private boolean wasShutDownCalled () { return shutDownHasBeenCalled; } @@ -65,9 +74,7 @@ public class ReceiverScanHandler implements PollingStationScanner.Consumer{ while (! wasShutDownCalled()) { try { - ScanDataCallback callback = (ScanDataCallback) queue.take(); -// handleSingleCommand(Command); - callback.doNothing(); + handleSingleCallback(queue.take()); } catch (InterruptedException e) { System.err.println("Interrupted while reading from command queue " + e); @@ -85,4 +92,35 @@ public class ReceiverScanHandler implements PollingStationScanner.Consumer{ callbacks.add(scanCallback); } + + private void handleSingleCallback(ScanCallback callback) { + System.out.println(callback.toString()); + if (callback instanceof ScanDataCallback) { + doScannedData((ScanDataCallback) callback); + } + else if (callback instanceof ScanErrorCallback) { + doScanError((ScanErrorCallback) callback); + } + else { + System.out.println("in the else"); +// logger.error("handleSingleCommand: unknown type of PollingStationCommand received: " + command.getClass().getName()); +// doReportErrorAndForceRestart(systemMessages.get(StorageManager.SOMETHING_WRONG_MESSAGE)); +// doReportErrorAndForceRestart("error message to define"); + } + } + + // TODO: parse fields of protobuf 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)); + } + + private void doScanError(ScanErrorCallback cb) { + + } + } \ No newline at end of file diff --git a/polling-station/src/main/java/meerkat/pollingstation/controller/callbacks/ScanDataCallback.java b/polling-station/src/main/java/meerkat/pollingstation/controller/callbacks/ScanDataCallback.java index 4ad7877..8ce3119 100644 --- a/polling-station/src/main/java/meerkat/pollingstation/controller/callbacks/ScanDataCallback.java +++ b/polling-station/src/main/java/meerkat/pollingstation/controller/callbacks/ScanDataCallback.java @@ -9,18 +9,15 @@ import meerkat.protobuf.PollingStation; public class ScanDataCallback extends ScanCallback implements FutureCallback { private final PollingStation.ScannedData expectedData; - private final PollingStation.ErrorMsg errorMsg; private boolean dataIsAsExpected; private Throwable thrown; public ScanDataCallback(PollingStation.ScannedData expectedData) { this.expectedData = expectedData; - this.errorMsg = null; } - public ScanDataCallback(PollingStation.ErrorMsg errorMsg) { - this.expectedData = null; - this.errorMsg = errorMsg; + public PollingStation.ScannedData getScannedData() { + return this.expectedData; } @Override