diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/PollingStationServerToyRun.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/PollingStationServerToyRun.java new file mode 100644 index 0000000..7dc37f0 --- /dev/null +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/PollingStationServerToyRun.java @@ -0,0 +1,88 @@ +package meerkat.voting.gui; + +import com.google.common.util.concurrent.FutureCallback; +import meerkat.pollingstation.PollingStationScanner; +import meerkat.pollingstation.PollingStationWebScanner; +import meerkat.protobuf.PollingStation; + +import java.util.concurrent.Semaphore; + +/** + * Created by Laura on 2/1/2017. + */ +public class PollingStationServerToyRun { + + private PollingStationScanner.Consumer scanner; + private static final String ADDRESS = "http://localhost"; + private static final String SUB_ADDRESS = ""; + private static final int PORT = 8080; + + private Semaphore semaphore; + private Throwable thrown; + private boolean dataIsAsExpected; + + private class ScanHandler implements FutureCallback { + + private final PollingStation.ScannedData expectedData; + + public ScanHandler(PollingStation.ScannedData expectedData) { + this.expectedData = expectedData; + } + + @Override + public void onSuccess(PollingStation.ScannedData result) { + dataIsAsExpected = result.getChannel().equals(expectedData.getChannel()); + semaphore.release(); + } + + @Override + public void onFailure(Throwable t) { + dataIsAsExpected = false; + thrown = t; + semaphore.release(); + } + } + + private class ErrorHandler implements FutureCallback { + + private final String expectedErrorMessage; + + public ErrorHandler(String expectedErrorMessage) { + this.expectedErrorMessage = expectedErrorMessage; + } + + @Override + public void onSuccess(PollingStation.ScannedData result) { + dataIsAsExpected = false; + semaphore.release(); + } + + @Override + public void onFailure(Throwable t) { + dataIsAsExpected = t.getMessage().equals(expectedErrorMessage); + semaphore.release(); + } + } + + public void init() { + + System.err.println("Setting up Scanner WebApp!"); + + scanner = new PollingStationWebScanner(PORT, SUB_ADDRESS); + + semaphore = new Semaphore(0); + thrown = null; + + try { + scanner.start(); + } catch (Exception e) { + System.out.println("Could not start server: " + e.getMessage()); + } + + } + + public static void main(String [] args) { + PollingStationServerToyRun run = new PollingStationServerToyRun(); + run.init(); + } +} \ No newline at end of file diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/VotingBoothToyGraphicalRun.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/VotingBoothToyGraphicalRun.java index 23881a6..f138908 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/VotingBoothToyGraphicalRun.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/VotingBoothToyGraphicalRun.java @@ -11,6 +11,7 @@ import meerkat.voting.encryptor.VBCryptoManager; import meerkat.voting.encryptor.VBCryptoManagerImpl; import meerkat.voting.gui.uiFX.GraphicalUI; import meerkat.voting.gui.output.FXWindowOutputDevice; +import meerkat.voting.output.NetworkVirtualPrinter; import meerkat.voting.storage.StorageManager; import meerkat.voting.storage.StorageManagerMockup; @@ -44,6 +45,7 @@ public class VotingBoothToyGraphicalRun { StorageManager storageManager = new StorageManagerMockup(); FXWindowOutputDevice outputDevice = new FXWindowOutputDevice(); +// NetworkVirtualPrinter outputDevice = new NetworkVirtualPrinter("http://localhost:8080"); VBCryptoManager cryptoManager = new VBCryptoManagerImpl(rand, enc, sig); // SystemConsoleUI ui = new SystemConsoleUI (); GraphicalUI ui = new GraphicalUI(); diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/output/FXWindowOutputDevice.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/output/FXWindowOutputDevice.java index 77ae2eb..429e602 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/output/FXWindowOutputDevice.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/output/FXWindowOutputDevice.java @@ -1,12 +1,16 @@ package meerkat.voting.gui.output; import com.google.protobuf.ByteString; +import com.google.protobuf.InvalidProtocolBufferException; import com.google.zxing.WriterException; import meerkat.protobuf.Crypto.EncryptionRandomness; import meerkat.protobuf.Crypto.RandomnessGenerationProof; +import meerkat.protobuf.PollingStation; +import meerkat.protobuf.PollingStation.ScannedData; import meerkat.protobuf.Voting.BallotSecrets; import meerkat.protobuf.Voting.PlaintextBallot; import meerkat.protobuf.Voting.SignedEncryptedBallot; +import meerkat.rest.Constants; import meerkat.voting.gui.controllersFX.VistaNavigator; import meerkat.voting.output.AsyncRunnableOutputDevice; import meerkat.voting.output.outputcommands.AuditOutputCommand; @@ -16,8 +20,10 @@ import meerkat.voting.output.outputcommands.CommitOutputCommand; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.ws.rs.client.Entity; import java.awt.image.BufferedImage; import java.io.IOException; +import java.util.Base64; /** * A toy OutputDevice class @@ -113,8 +119,22 @@ public class FXWindowOutputDevice extends AsyncRunnableOutputDevice { logger.error("plaintext and encryption serial numbers do not match!! plaintext# = " + plaintextSerialNumber + ", ciphertext# = " + encryptedSerialNumber); } - ByteString encryptedData = signedEncryptedBallot.getEncryptedBallot().getData().getData(); - encryptedText = encryptedSerialNumber+"|"+bytesToString(encryptedData); + + ScannedData scannedData = ScannedData.newBuilder() + .setChannel(command.getChannelIdentifierByteString()) + .setSignedEncryptedBallot(command.getSignedEncryptedBallot()) + .build(); + + System.out.println(scannedData.getSerializedSize()); + encryptedText = Base64.getEncoder().encodeToString(scannedData.toByteArray()); + + try { + ScannedData result = ScannedData.parseFrom(Base64.getDecoder().decode(encryptedText.getBytes())); + System.out.println(result.getChannel()); + System.out.println(command.getChannelIdentifierByteString()); + } catch (InvalidProtocolBufferException e) { + e.printStackTrace(); + } try { printQRCode(encryptedText, 250); diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/uiFX/GraphicalUI.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/uiFX/GraphicalUI.java index e51dfd0..a4cde32 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/uiFX/GraphicalUI.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/uiFX/GraphicalUI.java @@ -336,6 +336,7 @@ public class GraphicalUI implements VotingBoothUI, Runnable { cmdPend.trample(new FatalErrorUICommand(errorMessage, buttonLabels, (ControllerCallback)callback)); } + // TODO: make javafx interface for handling fatal error /** * show an error to the voter. let him press a (chosen) button for handling the error. * @param command a FatalErrorUICommand with the callback