polling station server and client communicate (reading scannedData works)
parent
ed7b50de23
commit
29f6b244d7
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -18,20 +18,32 @@ public class PollingStationMainController {
|
|||
private LinkedBlockingQueue<PollingStationCommand> 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<PollingStationCommand>();
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ public class PollingStationToyRun {
|
|||
try {
|
||||
scanner.start();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Could not start server: " + e.getMessage());
|
||||
}
|
||||
|
||||
|
|
|
@ -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<FutureCallback<PollingStation.ScannedData>> callbacks;
|
||||
|
||||
private LinkedBlockingQueue<ScanCallback> queue;
|
||||
private LinkedBlockingQueue<PollingStationCommand> 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<PollingStationCommand> 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) {
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -9,18 +9,15 @@ import meerkat.protobuf.PollingStation;
|
|||
public class ScanDataCallback extends ScanCallback implements FutureCallback<PollingStation.ScannedData> {
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue