From 72d96c4d787d546dd9b2f801adca361f76c043eb Mon Sep 17 00:00:00 2001 From: Laura Radaelli Date: Thu, 12 Jan 2017 18:03:40 +0200 Subject: [PATCH] virtual printer (javaFX window) added --- voting-booth/build.gradle | 3 + .../voting/VotingBoothToyGraphicalRun.java | 3 +- .../meerkat/voting/gui/ui/GraphicalUI.java | 32 +---- .../java/meerkat/voting/gui/ui/MainFX.java | 25 ++++ .../controllersFX/CastOrAuditController.java | 2 + .../ui/controllersFX/LoadingController.java | 25 ++++ .../ui/controllersFX/PrinterController.java | 42 ++++++ .../ui/controllersFX/Vista1Controller.java | 23 --- .../ui/controllersFX/Vista2Controller.java | 24 ---- .../gui/ui/controllersFX/VistaNavigator.java | 68 ++++++++- .../voting/output/FXWindowOutputDevice.java | 131 ++++++++++++++++++ .../src/main/resources/views/castOrAudit.fxml | 5 +- .../src/main/resources/views/loading.fxml | 5 +- .../src/main/resources/views/main.fxml | 17 ++- .../src/main/resources/views/printer.fxml | 21 +++ .../src/main/resources/views/vista1.fxml | 11 -- .../src/main/resources/views/vista2.fxml | 11 -- 17 files changed, 337 insertions(+), 111 deletions(-) create mode 100644 voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/LoadingController.java create mode 100644 voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/PrinterController.java delete mode 100644 voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/Vista1Controller.java delete mode 100644 voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/Vista2Controller.java create mode 100644 voting-booth/src/main/java/meerkat/voting/output/FXWindowOutputDevice.java create mode 100644 voting-booth/src/main/resources/views/printer.fxml delete mode 100644 voting-booth/src/main/resources/views/vista1.fxml delete mode 100644 voting-booth/src/main/resources/views/vista2.fxml diff --git a/voting-booth/build.gradle b/voting-booth/build.gradle index 0920359..23f2dec 100644 --- a/voting-booth/build.gradle +++ b/voting-booth/build.gradle @@ -54,6 +54,9 @@ dependencies { runtime 'ch.qos.logback:logback-classic:1.1.2' runtime 'ch.qos.logback:logback-core:1.1.2' + // Jar that creates barcodes + compile group: 'net.sourceforge.barbecue', name: 'barbecue', version: '1.5-beta1' + // Google protobufs compile 'com.google.protobuf:protobuf-java:3.+' diff --git a/voting-booth/src/main/java/meerkat/voting/VotingBoothToyGraphicalRun.java b/voting-booth/src/main/java/meerkat/voting/VotingBoothToyGraphicalRun.java index 6d72d6f..1879b9e 100644 --- a/voting-booth/src/main/java/meerkat/voting/VotingBoothToyGraphicalRun.java +++ b/voting-booth/src/main/java/meerkat/voting/VotingBoothToyGraphicalRun.java @@ -8,6 +8,7 @@ import meerkat.voting.controller.VotingBoothImpl; import meerkat.voting.encryptor.VBCryptoManager; import meerkat.voting.encryptor.VBCryptoManagerImpl; import meerkat.voting.gui.ui.GraphicalUI; +import meerkat.voting.output.FXWindowOutputDevice; import meerkat.voting.output.SystemConsoleOutputDevice; import meerkat.voting.storage.StorageManager; import meerkat.voting.storage.StorageManagerMockup; @@ -41,7 +42,7 @@ public class VotingBoothToyGraphicalRun { DigitalSignature sig = new ToySignature("MY_SIGNER_ID"); StorageManager storageManager = new StorageManagerMockup(); - SystemConsoleOutputDevice outputDevice = new SystemConsoleOutputDevice(); + FXWindowOutputDevice outputDevice = new FXWindowOutputDevice(); VBCryptoManager cryptoManager = new VBCryptoManagerImpl(rand, enc, sig); // SystemConsoleUI ui = new SystemConsoleUI (); GraphicalUI ui = new GraphicalUI(); diff --git a/voting-booth/src/main/java/meerkat/voting/gui/ui/GraphicalUI.java b/voting-booth/src/main/java/meerkat/voting/gui/ui/GraphicalUI.java index 71c13da..f9eb1dc 100644 --- a/voting-booth/src/main/java/meerkat/voting/gui/ui/GraphicalUI.java +++ b/voting-booth/src/main/java/meerkat/voting/gui/ui/GraphicalUI.java @@ -162,9 +162,6 @@ public class GraphicalUI implements VotingBoothUI, Runnable { logger.debug("UI entered doShowWelcomeScreen"); VistaNavigator.setCurrentCommand(command); VistaNavigator.loadVista(VistaNavigator.WELCOME_SCREEN); -// waitForEnter(null); -// ControllerCallback callback = command.getCallback(); -// callback.onSuccess(null); } public void answerWelcomeScreen(StartSessionUICommand command) { @@ -278,30 +275,8 @@ public class GraphicalUI implements VotingBoothUI, Runnable { */ private void doCastOrAudit(CastOrAuditUICommand command) { logger.debug("UI entered doCastOrAudit"); - System.out.println ("Finalizing your vote. Do you wish to (C)ast or (A)udit?"); - - FinalizeBallotChoice fChoice; - - try { - String s = readInputLine(); - if (s.equals("cast") || s.equals("c")) { - fChoice = FinalizeBallotChoice.CAST; - } - else if (s.equals("audit") || s.equals("a")) { - fChoice = FinalizeBallotChoice.AUDIT; - } - else { - throw new IllegalArgumentException("UI could not understand the answer for cast/audit question '" + s + "'"); - } - ControllerCallback callback = command.getCallback(); - assert (callback instanceof CastOrAuditCallback); - ((CastOrAuditCallback)callback).onSuccess(fChoice); - } - catch (IllegalArgumentException|IOException e) { - String errorMessage = "doCastOrAudit: some error with reading input from console. details: " + e; - logger.error(errorMessage); - command.getCallback().onFailure(e); - } + VistaNavigator.setCurrentCommand(command); + VistaNavigator.loadVista(VistaNavigator.CAST_OR_AUDIT); } @@ -332,8 +307,7 @@ public class GraphicalUI implements VotingBoothUI, Runnable { } else { messageString = UIUtils.bytesToString(message.getData()); } - System.out.println(messageString); - System.out.print ("Waiting : ."); + VistaNavigator.loadLoadingVista(messageString); } /** diff --git a/voting-booth/src/main/java/meerkat/voting/gui/ui/MainFX.java b/voting-booth/src/main/java/meerkat/voting/gui/ui/MainFX.java index a43e127..f4d10f5 100644 --- a/voting-booth/src/main/java/meerkat/voting/gui/ui/MainFX.java +++ b/voting-booth/src/main/java/meerkat/voting/gui/ui/MainFX.java @@ -5,10 +5,13 @@ package meerkat.voting.gui.ui; */ import javafx.application.Application; +import javafx.geometry.Rectangle2D; import javafx.scene.Scene; import javafx.scene.layout.Pane; +import javafx.stage.Screen; import javafx.stage.Stage; import meerkat.voting.gui.ui.controllersFX.MainController; +import meerkat.voting.gui.ui.controllersFX.PrinterController; import meerkat.voting.gui.ui.controllersFX.VistaNavigator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,6 +22,7 @@ import java.io.IOException; public class MainFX extends Application { private static final Logger logger = LoggerFactory.getLogger(GraphicalUI.class); + private Stage printing; // @Override // public void init() throws Exception { @@ -57,6 +61,9 @@ public class MainFX extends Application { ) ); stage.show(); + createPrinter(stage.getX(), stage.getY()); + stage.requestFocus(); + notifyLaunched(); } @@ -97,4 +104,22 @@ public class MainFX extends Application { return scene; } + private void createPrinter(double mainX, double mainY) throws IOException { + printing = new Stage(); + FXMLLoader loader = new FXMLLoader(getClass().getResource(VistaNavigator.PRINTER)); + Pane mainPane = null; + mainPane = (Pane) loader.load(); + PrinterController printerController = loader.getController(); + VistaNavigator.setPrinterController(printerController); + + printing.setX(mainX - printing.getWidth()); + printing.setY(mainY - printing.getHeight()); + + Scene scene = new Scene(mainPane); + printing.setScene(scene); + printing.show(); + } + + + } diff --git a/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/CastOrAuditController.java b/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/CastOrAuditController.java index e98d0f0..4d7c9da 100644 --- a/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/CastOrAuditController.java +++ b/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/CastOrAuditController.java @@ -33,11 +33,13 @@ public class CastOrAuditController { @FXML private void cast() { fChoice = VotingBoothUI.FinalizeBallotChoice.CAST; + nextPane(); } @FXML private void audit() { fChoice = VotingBoothUI.FinalizeBallotChoice.AUDIT; + nextPane(); } private void nextPane() { diff --git a/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/LoadingController.java b/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/LoadingController.java new file mode 100644 index 0000000..53af0f9 --- /dev/null +++ b/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/LoadingController.java @@ -0,0 +1,25 @@ +package meerkat.voting.gui.ui.controllersFX; + +/** + * Created by Laura on 01/12/2017. + */ +import javafx.fxml.FXML; +import javafx.scene.Node; +import javafx.scene.layout.StackPane; +import javafx.scene.text.Text; + +/** + * Main controller class for the entire layout. + */ +public class LoadingController { + + /** Holder of a switchable vista. */ + @FXML + private Text loading_text; + + public void setContent(String s) { + loading_text.setText(s); + } + + +} \ No newline at end of file diff --git a/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/PrinterController.java b/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/PrinterController.java new file mode 100644 index 0000000..61bb7de --- /dev/null +++ b/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/PrinterController.java @@ -0,0 +1,42 @@ +package meerkat.voting.gui.ui.controllersFX; + +/** + * Created by Laura on 01/12/2017. + */ +import javafx.fxml.FXML; +import javafx.scene.Node; +import javafx.scene.layout.StackPane; +import javafx.scene.text.Text; +import meerkat.voting.gui.ui.CommandPend; +import meerkat.voting.gui.ui.uicommands.UICommand; + +/** + * Main controller class for the entire layout. + */ +public class PrinterController { + + /** Holder of a switchable vista. */ + @FXML + private StackPane printerHolder; + @FXML + private Text content; + + /** + * Replaces the vista displayed in the vista holder with a new vista. + * + * @param node the vista node to be swapped in. + */ + public void setVista(Node node) { + printerHolder.getChildren().setAll(node); + } + + public void setContent(String s) { + content.setText(s); + } + + public void addContent(String s) { + String old_content = content.getText(); + content.setText(old_content+"\n"+s); + } + +} \ No newline at end of file diff --git a/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/Vista1Controller.java b/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/Vista1Controller.java deleted file mode 100644 index 99f0924..0000000 --- a/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/Vista1Controller.java +++ /dev/null @@ -1,23 +0,0 @@ -package meerkat.voting.gui.ui.controllersFX; - -/** - * Created by Laura on 12/16/2016. - */ -import javafx.event.ActionEvent; -import javafx.fxml.FXML; - -/** - * Controller class for the first vista. - */ -public class Vista1Controller { - - /** - * Event handler fired when the user requests a new vista. - * - */ - @FXML - void nextPane() { - VistaNavigator.loadVista(VistaNavigator.VISTA_2); - } - -} \ No newline at end of file diff --git a/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/Vista2Controller.java b/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/Vista2Controller.java deleted file mode 100644 index b6dce36..0000000 --- a/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/Vista2Controller.java +++ /dev/null @@ -1,24 +0,0 @@ -package meerkat.voting.gui.ui.controllersFX; - -/** - * Created by Laura on 12/16/2016. - */ -import javafx.event.ActionEvent; -import javafx.fxml.FXML; - -/** - * Controller class for the second vista. - */ -public class Vista2Controller { - - /** - * Event handler fired when the user requests a previous vista. - * - * @param event the event that triggered the handler. - */ - @FXML - void previousPane(ActionEvent event) { - VistaNavigator.loadVista(VistaNavigator.EMPTY_START); - } - -} \ No newline at end of file diff --git a/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/VistaNavigator.java b/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/VistaNavigator.java index 2874eb7..d2568dd 100644 --- a/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/VistaNavigator.java +++ b/voting-booth/src/main/java/meerkat/voting/gui/ui/controllersFX/VistaNavigator.java @@ -10,8 +10,6 @@ import meerkat.voting.gui.ui.GraphicalUI; import meerkat.voting.gui.ui.uicommands.UICommand; import java.io.IOException; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.BlockingQueue; /** * Utility class for controlling navigation between vistas. @@ -25,15 +23,17 @@ public class VistaNavigator { * Convenience constants for fxml layouts managed by the navigator. */ public static final String MAIN = "/views/main.fxml"; + public static final String PRINTER = "/views/printer.fxml"; public static final String LOADING = "/views/loading.fxml"; public static final String EMPTY_START = "/views/empty_start.fxml"; public static final String WELCOME_SCREEN = "/views/welcome_screen.fxml"; - public static final String VISTA_2 = "/views/vista2.fxml"; public static final String CHANNEL_CHOICE = "/views/channel_choice.fxml"; public static final String VOTING = "/views/voting.fxml"; + public static final String CAST_OR_AUDIT = "/views/castOrAudit.fxml"; /** The main application layout controller. */ private static MainController mainController; + private static PrinterController printerController; public static GraphicalUI uiThread; @@ -50,6 +50,19 @@ public class VistaNavigator { return VistaNavigator.mainController; } + /** + * Stores the printer controller for later use in printing tasks. + * + * @param printerController the printer application layout controller. + */ + public static void setPrinterController(PrinterController printerController) { + VistaNavigator.printerController = printerController; + } + + public static PrinterController getPrinterController() { + return VistaNavigator.printerController; + } + /** * Stores the thread that controls the UI. * @@ -111,4 +124,53 @@ public class VistaNavigator { } } + + /** + * Prints the content into the printer application layout. + * + * @param content a String with the content that should be printed. + * @param mode if value 'a' means append, any other character will erase current content of printer + */ + public static void printFX(String content, char mode) { + Platform.runLater(new Runnable() { + @Override + public void run() { + if (mode=='a') { + printerController.addContent(content); + } else { + printerController.setContent(content); + } + } + } + ); + } + + /** + * Loads the 'LOADING' vista + * vistaHolder pane of the main application layout. + * + * The parameter allows to set some special text to show in the new vista. + * + * @param text the extra text to show instead of 'loading...' + */ + public static void loadLoadingVista(String text) { + try { + FXMLLoader loader = new FXMLLoader(VistaNavigator.class.getResource(VistaNavigator.LOADING)); + Node node = (Node) loader.load(); + LoadingController loadingController = loader.getController(); + loadingController.setContent(text); + Platform.runLater(new Runnable() { + @Override + public void run() { + mainController.setVista(node); + } + } + ); + + + } catch (IOException e) { + e.printStackTrace(); + } + } + } \ No newline at end of file diff --git a/voting-booth/src/main/java/meerkat/voting/output/FXWindowOutputDevice.java b/voting-booth/src/main/java/meerkat/voting/output/FXWindowOutputDevice.java new file mode 100644 index 0000000..64ba1c5 --- /dev/null +++ b/voting-booth/src/main/java/meerkat/voting/output/FXWindowOutputDevice.java @@ -0,0 +1,131 @@ +package meerkat.voting.output; + +import com.google.protobuf.ByteString; +import meerkat.protobuf.Crypto.EncryptionRandomness; +import meerkat.protobuf.Crypto.RandomnessGenerationProof; +import meerkat.protobuf.Voting.BallotSecrets; +import meerkat.protobuf.Voting.PlaintextBallot; +import meerkat.protobuf.Voting.SignedEncryptedBallot; +import meerkat.voting.gui.ui.controllersFX.VistaNavigator; +import meerkat.voting.output.outputcommands.AuditOutputCommand; +import meerkat.voting.output.outputcommands.CancelOutputCommand; +import meerkat.voting.output.outputcommands.CastOutputCommand; +import meerkat.voting.output.outputcommands.CommitOutputCommand; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A toy OutputDevice class + * outputs everything simply to the System console + */ +public class FXWindowOutputDevice extends AsyncRunnableOutputDevice { + + private static final Logger logger = LoggerFactory.getLogger(FXWindowOutputDevice.class); + + public FXWindowOutputDevice() { + super(); + logger.info("A FXWindowOutputDevice is constructed"); + } + + /** + * Committing to the ballot. + * Simply prints to the output stream all the details in the CommitOutputCommand. + * @param command details to commit to, and the callback to call when finished + */ + public void doCommitToBallot(CommitOutputCommand command) { + logger.debug("entered method doCommitToBallot"); + PlaintextBallot plaintextBallot = command.getPlaintext(); + long plaintextSerialNumber = plaintextBallot.getSerialNumber(); + String toPrint = ""; + + toPrint+="Commitment of Ballot #" + plaintextSerialNumber; + toPrint+="\n"; + toPrint+="(channel): "; + toPrint+="\n"; + toPrint+=bytesToString(command.getChannelIdentifierByteString()); + toPrint+="\n"; + toPrint+="(plaintext): "; + toPrint+="\n"; + toPrint+=plaintextBallot; + SignedEncryptedBallot signedEncryptedBallot = command.getSignedEncryptedBallot(); + long encryptedSerialNumber = signedEncryptedBallot.getEncryptedBallot().getSerialNumber(); + toPrint+="Commitment of Ballot #" + encryptedSerialNumber + " (ciphertext):"; + toPrint+="\n"; + if (plaintextSerialNumber != encryptedSerialNumber) { + logger.error("plaintext and encryption serial numbers do not match!! plaintext# = " + + plaintextSerialNumber + ", ciphertext# = " + encryptedSerialNumber); + } + ByteString encryptedData = signedEncryptedBallot.getEncryptedBallot().getData().getData(); + toPrint+=bytesToString(encryptedData); + toPrint+="\n"; + VistaNavigator.printFX(toPrint, 'w'); + command.getCallback().onSuccess(null); + } + + + /** + * auditing the ballot. + * prints to the output stream the ballot secrets (the encryption randomness and its proof of random generation) + * @param command An auditing command with the callback to finally call + */ + public void doAudit(AuditOutputCommand command) { + logger.debug("entered method doAudit"); + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + String msg = "Auditing"; + VistaNavigator.printFX(msg, 'a'); + BallotSecrets ballotSecrets = command.getBallotSecrets(); + printEncryptionRandomness(ballotSecrets.getEncryptionRandomness()); + printRandomnessGenerationProof (ballotSecrets.getProof()); + command.getCallback().onSuccess(null); + } + + /** + * Casting the ballot (actually does nothing new) + * @param command a CastOutputCommand with the details and the callback + */ + public void doCastBallot(CastOutputCommand command) { + logger.debug("entered method doCastBallot"); + String msg = "Ballot finalized for casting!"; + VistaNavigator.printFX(msg, 'a'); + command.getCallback().onSuccess(null); + } + + + /** + * Canceling the ballot (actually does nothing new) + * @param command a CancelOutputCommand with the details and the callback + */ + public void doCancel(CancelOutputCommand command) { + logger.debug("entered method doCancel"); + System.out.println("Ballot cancelled!"); + command.getCallback().onSuccess(null); + } + + + private void printEncryptionRandomness (EncryptionRandomness encryptionRandomness) { + String msg = "Encryption Randomness = "; + ByteString data = encryptionRandomness.getData(); + msg+=bytesToString(data); + VistaNavigator.printFX(msg, 'a'); + } + + private void printRandomnessGenerationProof (RandomnessGenerationProof proof) { + String msg = "Proof of randomness generation:"; + ByteString data = proof.getData(); + msg+=bytesToString(data); + VistaNavigator.printFX(msg, 'a'); + } + + + /* + * Returns the UTF8 decoding of byte-string data + */ + private static String bytesToString(ByteString data) { + return data.toStringUtf8(); + } + +} diff --git a/voting-booth/src/main/resources/views/castOrAudit.fxml b/voting-booth/src/main/resources/views/castOrAudit.fxml index eeaf57c..b8e1362 100644 --- a/voting-booth/src/main/resources/views/castOrAudit.fxml +++ b/voting-booth/src/main/resources/views/castOrAudit.fxml @@ -7,8 +7,7 @@ - -