From ecc39e1157dbefe3f7c8dd31588eb610f12dbea1 Mon Sep 17 00:00:00 2001 From: Dennis Eisold <de@itstall.de> Date: Fri, 29 Nov 2019 03:35:23 +0100 Subject: [PATCH] =?UTF-8?q?Ein=20paar=20=C3=84nderungen.=20Verschl=C3=BCss?= =?UTF-8?q?elung=20funktioniert=20nun=20in=20beide=20Richtungen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/enigma/App.java | 24 - src/main/java/enigma/GuiController.java | 1099 +++++++++-------- .../java/enigma/database/DatenbankSqlite.java | 44 +- src/main/java/enigma/model/Codebuch.java | 59 +- src/main/java/enigma/model/Codierer.java | 229 +++- src/main/java/enigma/model/Funkraum.java | 64 +- src/main/java/enigma/model/Morsecode.java | 3 +- src/main/java/enigma/model/Walze.java | 28 +- 8 files changed, 884 insertions(+), 666 deletions(-) diff --git a/src/main/java/enigma/App.java b/src/main/java/enigma/App.java index dfb8ce5..7dd22d6 100644 --- a/src/main/java/enigma/App.java +++ b/src/main/java/enigma/App.java @@ -63,30 +63,6 @@ public class App extends Application { scene = new Scene(loadFXML("gui")); stage.setScene(scene); stage.show(); - - this.c = new Codierer(); - - /** - * TODO: Nur zum Testen muss später wieder gelöscht werden. - */ - //region Test - c.setKenngruppe("deisold"); - String nachricht = "Hello world."; - - for (char buchstabe : nachricht.toCharArray()) { - if (buchstabe > 0) { - System.out.print(buchstabe + ":"); - System.out.print(c.getHardware().codiere(buchstabe)); - System.out.println(" W1:" + c.getHardware().getWalzen()[0].getPosition() + " W2:" + c.getHardware().getWalzen()[1].getPosition() + " W3:" + c.getHardware().getWalzen()[2].getPosition()); - } - } - try { - System.out.println(c.getNachricht()); - c.sendeNachricht(); - } catch (HttpException e) { - e.printStackTrace(); - } - //endregion } } \ No newline at end of file diff --git a/src/main/java/enigma/GuiController.java b/src/main/java/enigma/GuiController.java index e42ef95..377cab1 100644 --- a/src/main/java/enigma/GuiController.java +++ b/src/main/java/enigma/GuiController.java @@ -1,609 +1,690 @@ package enigma; +import enigma.model.Codierer; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.scene.shape.Circle; +import org.apache.http.HttpException; +import java.io.IOException; import java.util.ArrayList; public class GuiController { - private ArrayList<MenuItem> mItem; - private final static String[] str= {"I", "II", "III", "IV", "V"}; - private final static String[] num ={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26"}; - private final static String[] bs ={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; - private String textEingabe=""; + private final static String[] str = {"I", "II", "III", "IV", "V"}; + private final static String[] num = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"}; + private final static String[] bs = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; + private ArrayList<MenuItem> mItem; + private String textEingabe; + private String textCodiert; + private boolean resetDisplay; + private Codierer codierer; - @FXML - private Button btnL; + @FXML + private Button btnL; - @FXML - private Button btnP; + @FXML + private Button btnP; - @FXML - private Button btnY; + @FXML + private Button btnY; - @FXML - private Button btnX; + @FXML + private Button btnX; - @FXML - private Button btnC; + @FXML + private Button btnC; - @FXML - private Button btnV; + @FXML + private Button btnV; - @FXML - private Button btnB; + @FXML + private Button btnB; - @FXML - private Button btnN; + @FXML + private Button btnN; - @FXML - private Button btnM; + @FXML + private Button btnM; - @FXML - private Button btnA; + @FXML + private Button btnA; - @FXML - private Button btnS; + @FXML + private Button btnS; - @FXML - private Button btnD; + @FXML + private Button btnD; - @FXML - private Button btnF; + @FXML + private Button btnF; - @FXML - private Button btnG; + @FXML + private Button btnG; - @FXML - private Button btnH; + @FXML + private Button btnH; - @FXML - private Button btnJ; + @FXML + private Button btnJ; - @FXML - private Button btnK; + @FXML + private Button btnK; - @FXML - private Button btnQ; + @FXML + private Button btnQ; - @FXML - private Button btnW; + @FXML + private Button btnW; - @FXML - private Button btnE; + @FXML + private Button btnE; - @FXML - private Button btnR; + @FXML + private Button btnR; - @FXML - private Button btnT; + @FXML + private Button btnT; - @FXML - private Button btnZ; + @FXML + private Button btnZ; - @FXML - private Button btnU; + @FXML + private Button btnU; - @FXML - private Button btnI; + @FXML + private Button btnI; - @FXML - private Button btnO; + @FXML + private Button btnO; + @FXML + private MenuButton mBtnWalzPos1; - @FXML - private MenuButton mBtnWalzPos1; + @FXML + private MenuButton mBtnWalzPos3; - @FXML - private MenuButton mBtnWalzPos3; + @FXML + private MenuButton mBtnWalzPos2; - @FXML - private MenuButton mBtnWalzPos2; + @FXML + private MenuButton mBtnNotchPos3; - @FXML - private MenuButton mBtnNotchPos3; + @FXML + private MenuButton mBtnNotchPos2; - @FXML - private MenuButton mBtnNotchPos2; + @FXML + private MenuButton mBtnStartPos1; - @FXML - private MenuButton mBtnStartPos1; + @FXML + private MenuButton mBtnStartPos3; - @FXML - private MenuButton mBtnStartPos3; + @FXML + private MenuButton mBtnStartPos2; - @FXML - private MenuButton mBtnStartPos2; + @FXML + private TextField tfrein; - @FXML - private TextField tfrein; + @FXML + private TextField tfcodiert; - @FXML - private TextField tfcodiert; + @FXML + private MenuButton mBtnNotchPos1; + @FXML + private TextField tfStecker1; - @FXML - private MenuButton mBtnNotchPos1; + @FXML + private TextField tfStecker2; - @FXML - private TextField tfStecker1; + @FXML + private TextField tfStecker3; - @FXML - private TextField tfStecker2; + @FXML + private TextField tfStecker4; - @FXML - private TextField tfStecker3; + @FXML + private TextField tfStecker5; - @FXML - private TextField tfStecker4; + @FXML + private TextField tfStecker6; - @FXML - private TextField tfStecker5; + @FXML + private TextField tfStecker7; - @FXML - private TextField tfStecker6; + @FXML + private TextField tfStecker8; - @FXML - private TextField tfStecker7; + @FXML + private TextField tfStecker9; - @FXML - private TextField tfStecker8; + @FXML + private TextField tfStecker10; - @FXML - private TextField tfStecker9; + @FXML + private Label lblQ; - @FXML - private TextField tfStecker10; - @FXML - private Label lblQ; + @FXML + private Label lblE; - @FXML - private Label lblE; + @FXML + private Label lblW; - @FXML - private Label lblW; + @FXML + private Label lblR; - @FXML - private Label lblR; + @FXML + private Label lblT; - @FXML - private Label lblT; + @FXML + private Label lblZ; - @FXML - private Label lblZ; + @FXML + private Label lblO; - @FXML - private Label lblO; + @FXML + private Label lblI; - @FXML - private Label lblI; + @FXML + private Label lblU; - @FXML - private Label lblU; + @FXML + private Label lblA; - @FXML - private Label lblA; + @FXML + private Label lblD; - @FXML - private Label lblD; + @FXML + private Label lblS; - @FXML - private Label lblS; + @FXML + private Label lblF; - @FXML - private Label lblF; + @FXML + private Label lblG; - @FXML - private Label lblG; + @FXML + private Label lblH; - @FXML - private Label lblH; + @FXML + private Label lblK; - @FXML - private Label lblK; + @FXML + private Label lblJ; - @FXML - private Label lblJ; + @FXML + private Label lblP; - @FXML - private Label lblP; + @FXML + private Label lblX; - @FXML - private Label lblX; + @FXML + private Label lblY; - @FXML - private Label lblY; + @FXML + private Label lblC; - @FXML - private Label lblC; + @FXML + private Label lblV; - @FXML - private Label lblV; + @FXML + private Label lblB; - @FXML - private Label lblB; + @FXML + private Label lblL; - @FXML - private Label lblL; + @FXML + private Label lblM; - @FXML - private Label lblM; + @FXML + private Label lblN; - @FXML - private Label lblN; + @FXML + private Circle circE; - @FXML - private Circle circE; + @FXML + private Circle circR; - @FXML - private Circle circR; + @FXML + private Circle circT; - @FXML - private Circle circT; + @FXML + private Circle circZ; - @FXML - private Circle circZ; + @FXML + private Circle circU; - @FXML - private Circle circU; + @FXML + private Circle circI; - @FXML - private Circle circI; + @FXML + private Circle circO; - @FXML - private Circle circO; + @FXML + private Circle circA; - @FXML - private Circle circA; + @FXML + private Circle circS; - @FXML - private Circle circS; + @FXML + private Circle circD; - @FXML - private Circle circD; + @FXML + private Circle circF; - @FXML - private Circle circF; - - @FXML - private Circle circG; - - @FXML - private Circle circH; - - @FXML - private Circle circJ; - - @FXML - private Circle circK; - - @FXML - private Circle circP; - - @FXML - private Circle circY; - - @FXML - private Circle circX; - - @FXML - private Circle circC; - - @FXML - private Circle circV; - - @FXML - private Circle circB; - - @FXML - private Circle circN; - - @FXML - private Circle circM; - - @FXML - private Circle circL; - - @FXML - private Circle circW; - - @FXML - private Circle circQ; - - @FXML - void initialize() { - //Einträge für Walzen - menu(mBtnWalzPos1, str); - menu(mBtnWalzPos2, str); - menu(mBtnWalzPos3, str); - //Einträge für die Ringe - menu(mBtnNotchPos1, num); - menu(mBtnNotchPos2, num); - menu(mBtnNotchPos3, num); - //Einträge für die Startpositionen - menu(mBtnStartPos1, bs); - menu(mBtnStartPos3, bs); - menu(mBtnStartPos2, bs); - - assert tfStecker1 != null : "fx:id=\"tfStecker1\" was not injected: check your FXML file 'gui.fxml'."; - assert tfStecker2 != null : "fx:id=\"tfStecker2\" was not injected: check your FXML file 'gui.fxml'."; - assert tfStecker3 != null : "fx:id=\"tfStecker3\" was not injected: check your FXML file 'gui.fxml'."; - assert tfStecker4 != null : "fx:id=\"tfStecker4\" was not injected: check your FXML file 'gui.fxml'."; - assert tfStecker5 != null : "fx:id=\"tfStecker5\" was not injected: check your FXML file 'gui.fxml'."; - assert tfStecker6 != null : "fx:id=\"tfStecker6\" was not injected: check your FXML file 'gui.fxml'."; - assert tfStecker7 != null : "fx:id=\"tfStecker7\" was not injected: check your FXML file 'gui.fxml'."; - assert tfStecker8 != null : "fx:id=\"tfStecker8\" was not injected: check your FXML file 'gui.fxml'."; - assert tfStecker9 != null : "fx:id=\"tfStecker9\" was not injected: check your FXML file 'gui.fxml'."; - assert tfStecker10 != null : "fx:id=\"tfStecker10\" was not injected: check your FXML file 'gui.fxml'."; - assert btnP != null : "fx:id=\"btnP\" was not injected: check your FXML file 'gui.fxml'."; - assert btnY != null : "fx:id=\"btnY\" was not injected: check your FXML file 'gui.fxml'."; - assert btnX != null : "fx:id=\"btnX\" was not injected: check your FXML file 'gui.fxml'."; - assert btnC != null : "fx:id=\"btnC\" was not injected: check your FXML file 'gui.fxml'."; - assert btnV != null : "fx:id=\"btnV\" was not injected: check your FXML file 'gui.fxml'."; - assert btnB != null : "fx:id=\"btnB\" was not injected: check your FXML file 'gui.fxml'."; - assert btnN != null : "fx:id=\"btnN\" was not injected: check your FXML file 'gui.fxml'."; - assert btnM != null : "fx:id=\"btnM\" was not injected: check your FXML file 'gui.fxml'."; - assert btnL != null : "fx:id=\"btnL\" was not injected: check your FXML file 'gui.fxml'."; - assert btnA != null : "fx:id=\"btnA\" was not injected: check your FXML file 'gui.fxml'."; - assert btnS != null : "fx:id=\"btnS\" was not injected: check your FXML file 'gui.fxml'."; - assert btnD != null : "fx:id=\"btnD\" was not injected: check your FXML file 'gui.fxml'."; - assert btnF != null : "fx:id=\"btnF\" was not injected: check your FXML file 'gui.fxml'."; - assert btnG != null : "fx:id=\"btnG\" was not injected: check your FXML file 'gui.fxml'."; - assert btnH != null : "fx:id=\"btnH\" was not injected: check your FXML file 'gui.fxml'."; - assert btnJ != null : "fx:id=\"btnJ\" was not injected: check your FXML file 'gui.fxml'."; - assert btnK != null : "fx:id=\"btnK\" was not injected: check your FXML file 'gui.fxml'."; - assert btnQ != null : "fx:id=\"btnQ\" was not injected: check your FXML file 'gui.fxml'."; - assert btnW != null : "fx:id=\"btnW\" was not injected: check your FXML file 'gui.fxml'."; - assert btnE != null : "fx:id=\"btnE\" was not injected: check your FXML file 'gui.fxml'."; - assert btnR != null : "fx:id=\"btnR\" was not injected: check your FXML file 'gui.fxml'."; - assert btnT != null : "fx:id=\"btnT\" was not injected: check your FXML file 'gui.fxml'."; - assert btnZ != null : "fx:id=\"btnZ\" was not injected: check your FXML file 'gui.fxml'."; - assert btnU != null : "fx:id=\"btnU\" was not injected: check your FXML file 'gui.fxml'."; - assert btnI != null : "fx:id=\"btnI\" was not injected: check your FXML file 'gui.fxml'."; - assert btnO != null : "fx:id=\"btnO\" was not injected: check your FXML file 'gui.fxml'."; - assert mBtnWalzPos1 != null : "fx:id=\"mBtnWalzPos1\" was not injected: check your FXML file 'gui.fxml'."; - assert mBtnWalzPos3 != null : "fx:id=\"mBtnWalzPos3\" was not injected: check your FXML file 'gui.fxml'."; - assert mBtnWalzPos2 != null : "fx:id=\"mBtnWalzPos2\" was not injected: check your FXML file 'gui.fxml'."; - assert mBtnNotchPos3 != null : "fx:id=\"mBtnNotchPos3\" was not injected: check your FXML file 'gui.fxml'."; - assert mBtnNotchPos2 != null : "fx:id=\"mBtnNotchPos2\" was not injected: check your FXML file 'gui.fxml'."; - assert mBtnStartPos1 != null : "fx:id=\"mBtnStartPos1\" was not injected: check your FXML file 'gui.fxml'."; - assert mBtnStartPos3 != null : "fx:id=\"mBtnStartPos3\" was not injected: check your FXML file 'gui.fxml'."; - assert mBtnStartPos2 != null : "fx:id=\"mBtnStartPos2\" was not injected: check your FXML file 'gui.fxml'."; - assert tfrein != null : "fx:id=\"tfrein\" was not injected: check your FXML file 'gui.fxml'."; - assert tfcodiert != null : "fx:id=\"tfcodiert\" was not injected: check your FXML file 'gui.fxml'."; - assert mBtnNotchPos1 != null : "fx:id=\"mBtnNotchPos1\" was not injected: check your FXML file 'gui.fxml'."; - assert lblQ != null : "fx:id=\"lblQ\" was not injected: check your FXML file 'gui.fxml'."; - assert lblE != null : "fx:id=\"lblE\" was not injected: check your FXML file 'gui.fxml'."; - assert lblW != null : "fx:id=\"lblW\" was not injected: check your FXML file 'gui.fxml'."; - assert lblR != null : "fx:id=\"lblR\" was not injected: check your FXML file 'gui.fxml'."; - assert lblT != null : "fx:id=\"lblT\" was not injected: check your FXML file 'gui.fxml'."; - assert lblZ != null : "fx:id=\"lblZ\" was not injected: check your FXML file 'gui.fxml'."; - assert lblO != null : "fx:id=\"lblO\" was not injected: check your FXML file 'gui.fxml'."; - assert lblI != null : "fx:id=\"lblI\" was not injected: check your FXML file 'gui.fxml'."; - assert lblU != null : "fx:id=\"lblU\" was not injected: check your FXML file 'gui.fxml'."; - assert lblA != null : "fx:id=\"lblA\" was not injected: check your FXML file 'gui.fxml'."; - assert lblD != null : "fx:id=\"lblD\" was not injected: check your FXML file 'gui.fxml'."; - assert lblS != null : "fx:id=\"lblS\" was not injected: check your FXML file 'gui.fxml'."; - assert lblF != null : "fx:id=\"lblF\" was not injected: check your FXML file 'gui.fxml'."; - assert lblG != null : "fx:id=\"lblG\" was not injected: check your FXML file 'gui.fxml'."; - assert lblH != null : "fx:id=\"lblH\" was not injected: check your FXML file 'gui.fxml'."; - assert lblK != null : "fx:id=\"lblK\" was not injected: check your FXML file 'gui.fxml'."; - assert lblJ != null : "fx:id=\"lblJ\" was not injected: check your FXML file 'gui.fxml'."; - assert lblP != null : "fx:id=\"lblP\" was not injected: check your FXML file 'gui.fxml'."; - assert lblX != null : "fx:id=\"lblX\" was not injected: check your FXML file 'gui.fxml'."; - assert lblY != null : "fx:id=\"lblY\" was not injected: check your FXML file 'gui.fxml'."; - assert lblC != null : "fx:id=\"lblC\" was not injected: check your FXML file 'gui.fxml'."; - assert lblV != null : "fx:id=\"lblV\" was not injected: check your FXML file 'gui.fxml'."; - assert lblB != null : "fx:id=\"lblB\" was not injected: check your FXML file 'gui.fxml'."; - assert lblL != null : "fx:id=\"lblL\" was not injected: check your FXML file 'gui.fxml'."; - assert lblM != null : "fx:id=\"lblM\" was not injected: check your FXML file 'gui.fxml'."; - assert lblN != null : "fx:id=\"lblN\" was not injected: check your FXML file 'gui.fxml'."; - - leuchten(btnA, circA); - leuchten(btnB, circB); - leuchten(btnC,circC); - leuchten(btnD, circD); - leuchten(btnE, circE); - leuchten(btnF, circF); - leuchten(btnG, circG); - leuchten(btnH, circH); - leuchten(btnI, circI); - leuchten(btnJ,circJ); - leuchten(btnK, circK); - leuchten(btnL, circL); - leuchten(btnM, circM); - leuchten(btnN, circN); - leuchten(btnO, circO); - leuchten(btnP, circP); - leuchten(btnQ,circQ); - leuchten(btnR, circR); - leuchten(btnS, circS); - leuchten(btnT, circT); - leuchten(btnU, circU); - leuchten(btnV, circV); - leuchten(btnW, circW); - leuchten(btnX,circX); - leuchten(btnY, circY); - leuchten(btnZ, circZ); - //TODO: Leuchten und Tasten ordnen - } - - @FXML - void löschen() { - textEingabe=textEingabe.substring(0,textEingabe.length()-1); - tfrein.setText(textEingabe); - //TODO: anpassen, wenn kein Text da ist - } - - @FXML - void btnempfangen() { - } - - @FXML - void btnsenden() { - } - - /** - * Methode zum setzen des menuButton Textes auf das ausgewählte MenuItem - * @param mb : MenuButton der Walze - * @param mi : MenuItem - */ - private void listener(MenuButton mb, MenuItem mi){ - mb.setText(mi.getText()); - } - /** - * Methode zur Erstellung der Einträge und Action Listener für die einzelnen Menüs - * @param button : Button für die die Einträge erstellt werden sollen - */ - private void menu(MenuButton button, String[] str) { - mItem = new ArrayList<>(); - for (int i = 0; i < str.length; i++) { - //Eintrag aus str der Arraylist mi hinzufügen - mItem.add(new MenuItem(str[i])); - //MenuItem mi dem Button button hinzufügen - button.getItems().add(mItem.get(i)); - int finalI = i; - //Listener für die einzelnen Einträge - ArrayList<MenuItem> finalMi = mItem; - mItem.get(i).setOnAction(e -> { - listener(button, finalMi.get(finalI)); - }); - } - } - - /** - * Auswertung welcher Button gedrückt wurde - * @param e => ActionEvent des auslösenden Button - */ - @FXML - private void gedrueckteTaste(ActionEvent e) { - - //TODO Anbindung an Klasse Hardware - if (e.getSource() == btnA) setText(btnA); - if (e.getSource() == btnB) setText(btnB); - if (e.getSource() == btnC) setText(btnC); - if (e.getSource() == btnD) setText(btnD); - if (e.getSource() == btnE) setText(btnE); - if (e.getSource() == btnF) setText(btnF); - if (e.getSource() == btnG) setText(btnG); - if (e.getSource() == btnH) setText(btnH); - if (e.getSource() == btnI) setText(btnI); - if (e.getSource() == btnJ) setText(btnJ); - if (e.getSource() == btnK) setText(btnK); - if (e.getSource() == btnL) setText(btnL); - if (e.getSource() == btnM) setText(btnM); - if (e.getSource() == btnN) setText(btnN); - if (e.getSource() == btnO) setText(btnO); - if (e.getSource() == btnP) setText(btnP); - if (e.getSource() == btnQ) setText(btnQ); - if (e.getSource() == btnR) setText(btnR); - if (e.getSource() == btnS) setText(btnS); - if (e.getSource() == btnT) setText(btnT); - if (e.getSource() == btnU) setText(btnU); - if (e.getSource() == btnV) setText(btnV); - if (e.getSource() == btnW) setText(btnW); - if (e.getSource() == btnX) setText(btnX); - if (e.getSource() == btnY) setText(btnY); - if (e.getSource() == btnZ) setText(btnZ); - } - /** - * schreibt Buchstaben des gedrückten Knopfes in Textfeld tfrein - * @param b : gedrückter Knopf - */ - private void setText(Button b){ - if (textEingabe.length()<250) { - textEingabe += b.getText(); - tfrein.setText(textEingabe); - } - } - private void leuchten(Button taste, Circle leuchte){ - - taste.setOnMousePressed(mouseEvent -> {leuchte.setStyle("-fx-fill: #FFA500");}); - taste.setOnMouseReleased(mouseEvent -> { - leuchte.setStyle("-fx-background-color: "+ taste.getBackground()); - leuchte.setStyle("-fx-border-color: #000000"); - }); - } - /** - * Setzt die Anzeige des entsprechende Gui-Element auf die entsprechende Walze - * ->d.h. welche Walze sitzt auf welcher Position - * - * @param walze => gibt die Walzennummer an (entspricht - * @param position => gibt die Position der Walze (entspricht - */ - public void setzeWalze(int walze, int position){ - switch (position){ - case 1: - mBtnWalzPos1.setText(str[position]); - break; - case 2: - mBtnWalzPos2.setText(str[position]); - break; - case 3: - mBtnWalzPos3.setText(str[position]); - break; - default: - break; - } - } - - /** - * Setzt den Notch (position)=> die Mitnahmeposition der Walze (walze) fest - * Mitnahmeposition meint => die nächste Walze wird bei erreichen dieser Position (notch) - * um eine Stelle versetzt - * - * @param walze => gibt die walze an - * @param position => gibt den notch der Walze (walze) an - */ - public void setzeRing(int walze, int position){ - switch (walze){ - case 1: - mBtnNotchPos1.setText(num[position]); - break; - case 2: - mBtnNotchPos2.setText(num[position]); - break; - case 3: - mBtnNotchPos3.setText(num[position]); - break; - default: - break; - } - } - - /** - * Setzt die Startposition/ aktuelle Position der Walze - * @param walze => gibt die walze an - * @param buchstabe gibt den Startwert bzw die aktuelle Position der Walze (walze) an - */ - public void setzePosition(int walze, char buchstabe){ - switch (walze){ - case 1: - mBtnStartPos1.setText(String.valueOf(buchstabe)); - break; - case 2: - mBtnStartPos2.setText(buchstabe+""); - break; - case 3: - mBtnStartPos3.setText(buchstabe+""); - break; - default: - break; - } - } - - /** - * Setzt den aktuellen TagesSchluessel - * @param tagesSchluessel - */ - public void setzeTagesSchluessel(String tagesSchluessel){ - //TODO Benötige TestString aus der DB Codebuch - } -} + @FXML + private Circle circG; + @FXML + private Circle circH; + @FXML + private Circle circJ; + @FXML + private Circle circK; + @FXML + private Circle circP; + + @FXML + private Circle circY; + + @FXML + private Circle circX; + + @FXML + private Circle circC; + + @FXML + private Circle circV; + + @FXML + private Circle circB; + + @FXML + private Circle circN; + + @FXML + private Circle circM; + + @FXML + private Circle circL; + + @FXML + private Circle circW; + + @FXML + private Circle circQ; + + @FXML + void initialize() { + + // Variablen setzen + textEingabe = ""; + textCodiert = ""; + resetDisplay = true; + + //Einträge für Walzen + menu(mBtnWalzPos1, str); + menu(mBtnWalzPos2, str); + menu(mBtnWalzPos3, str); + + //Einträge für die Ringe + menu(mBtnNotchPos1, num); + menu(mBtnNotchPos2, num); + menu(mBtnNotchPos3, num); + + //Einträge für die Startpositionen + menu(mBtnStartPos1, bs); + menu(mBtnStartPos3, bs); + menu(mBtnStartPos2, bs); + + assert tfStecker1 != null : "fx:id=\"tfStecker1\" was not injected: check your FXML file 'gui.fxml'."; + assert tfStecker2 != null : "fx:id=\"tfStecker2\" was not injected: check your FXML file 'gui.fxml'."; + assert tfStecker3 != null : "fx:id=\"tfStecker3\" was not injected: check your FXML file 'gui.fxml'."; + assert tfStecker4 != null : "fx:id=\"tfStecker4\" was not injected: check your FXML file 'gui.fxml'."; + assert tfStecker5 != null : "fx:id=\"tfStecker5\" was not injected: check your FXML file 'gui.fxml'."; + assert tfStecker6 != null : "fx:id=\"tfStecker6\" was not injected: check your FXML file 'gui.fxml'."; + assert tfStecker7 != null : "fx:id=\"tfStecker7\" was not injected: check your FXML file 'gui.fxml'."; + assert tfStecker8 != null : "fx:id=\"tfStecker8\" was not injected: check your FXML file 'gui.fxml'."; + assert tfStecker9 != null : "fx:id=\"tfStecker9\" was not injected: check your FXML file 'gui.fxml'."; + assert tfStecker10 != null : "fx:id=\"tfStecker10\" was not injected: check your FXML file 'gui.fxml'."; + assert btnP != null : "fx:id=\"btnP\" was not injected: check your FXML file 'gui.fxml'."; + assert btnY != null : "fx:id=\"btnY\" was not injected: check your FXML file 'gui.fxml'."; + assert btnX != null : "fx:id=\"btnX\" was not injected: check your FXML file 'gui.fxml'."; + assert btnC != null : "fx:id=\"btnC\" was not injected: check your FXML file 'gui.fxml'."; + assert btnV != null : "fx:id=\"btnV\" was not injected: check your FXML file 'gui.fxml'."; + assert btnB != null : "fx:id=\"btnB\" was not injected: check your FXML file 'gui.fxml'."; + assert btnN != null : "fx:id=\"btnN\" was not injected: check your FXML file 'gui.fxml'."; + assert btnM != null : "fx:id=\"btnM\" was not injected: check your FXML file 'gui.fxml'."; + assert btnL != null : "fx:id=\"btnL\" was not injected: check your FXML file 'gui.fxml'."; + assert btnA != null : "fx:id=\"btnA\" was not injected: check your FXML file 'gui.fxml'."; + assert btnS != null : "fx:id=\"btnS\" was not injected: check your FXML file 'gui.fxml'."; + assert btnD != null : "fx:id=\"btnD\" was not injected: check your FXML file 'gui.fxml'."; + assert btnF != null : "fx:id=\"btnF\" was not injected: check your FXML file 'gui.fxml'."; + assert btnG != null : "fx:id=\"btnG\" was not injected: check your FXML file 'gui.fxml'."; + assert btnH != null : "fx:id=\"btnH\" was not injected: check your FXML file 'gui.fxml'."; + assert btnJ != null : "fx:id=\"btnJ\" was not injected: check your FXML file 'gui.fxml'."; + assert btnK != null : "fx:id=\"btnK\" was not injected: check your FXML file 'gui.fxml'."; + assert btnQ != null : "fx:id=\"btnQ\" was not injected: check your FXML file 'gui.fxml'."; + assert btnW != null : "fx:id=\"btnW\" was not injected: check your FXML file 'gui.fxml'."; + assert btnE != null : "fx:id=\"btnE\" was not injected: check your FXML file 'gui.fxml'."; + assert btnR != null : "fx:id=\"btnR\" was not injected: check your FXML file 'gui.fxml'."; + assert btnT != null : "fx:id=\"btnT\" was not injected: check your FXML file 'gui.fxml'."; + assert btnZ != null : "fx:id=\"btnZ\" was not injected: check your FXML file 'gui.fxml'."; + assert btnU != null : "fx:id=\"btnU\" was not injected: check your FXML file 'gui.fxml'."; + assert btnI != null : "fx:id=\"btnI\" was not injected: check your FXML file 'gui.fxml'."; + assert btnO != null : "fx:id=\"btnO\" was not injected: check your FXML file 'gui.fxml'."; + assert mBtnWalzPos1 != null : "fx:id=\"mBtnWalzPos1\" was not injected: check your FXML file 'gui.fxml'."; + assert mBtnWalzPos3 != null : "fx:id=\"mBtnWalzPos3\" was not injected: check your FXML file 'gui.fxml'."; + assert mBtnWalzPos2 != null : "fx:id=\"mBtnWalzPos2\" was not injected: check your FXML file 'gui.fxml'."; + assert mBtnNotchPos3 != null : "fx:id=\"mBtnNotchPos3\" was not injected: check your FXML file 'gui.fxml'."; + assert mBtnNotchPos2 != null : "fx:id=\"mBtnNotchPos2\" was not injected: check your FXML file 'gui.fxml'."; + assert mBtnStartPos1 != null : "fx:id=\"mBtnStartPos1\" was not injected: check your FXML file 'gui.fxml'."; + assert mBtnStartPos3 != null : "fx:id=\"mBtnStartPos3\" was not injected: check your FXML file 'gui.fxml'."; + assert mBtnStartPos2 != null : "fx:id=\"mBtnStartPos2\" was not injected: check your FXML file 'gui.fxml'."; + assert tfrein != null : "fx:id=\"tfrein\" was not injected: check your FXML file 'gui.fxml'."; + assert tfcodiert != null : "fx:id=\"tfcodiert\" was not injected: check your FXML file 'gui.fxml'."; + assert mBtnNotchPos1 != null : "fx:id=\"mBtnNotchPos1\" was not injected: check your FXML file 'gui.fxml'."; + assert lblQ != null : "fx:id=\"lblQ\" was not injected: check your FXML file 'gui.fxml'."; + assert lblE != null : "fx:id=\"lblE\" was not injected: check your FXML file 'gui.fxml'."; + assert lblW != null : "fx:id=\"lblW\" was not injected: check your FXML file 'gui.fxml'."; + assert lblR != null : "fx:id=\"lblR\" was not injected: check your FXML file 'gui.fxml'."; + assert lblT != null : "fx:id=\"lblT\" was not injected: check your FXML file 'gui.fxml'."; + assert lblZ != null : "fx:id=\"lblZ\" was not injected: check your FXML file 'gui.fxml'."; + assert lblO != null : "fx:id=\"lblO\" was not injected: check your FXML file 'gui.fxml'."; + assert lblI != null : "fx:id=\"lblI\" was not injected: check your FXML file 'gui.fxml'."; + assert lblU != null : "fx:id=\"lblU\" was not injected: check your FXML file 'gui.fxml'."; + assert lblA != null : "fx:id=\"lblA\" was not injected: check your FXML file 'gui.fxml'."; + assert lblD != null : "fx:id=\"lblD\" was not injected: check your FXML file 'gui.fxml'."; + assert lblS != null : "fx:id=\"lblS\" was not injected: check your FXML file 'gui.fxml'."; + assert lblF != null : "fx:id=\"lblF\" was not injected: check your FXML file 'gui.fxml'."; + assert lblG != null : "fx:id=\"lblG\" was not injected: check your FXML file 'gui.fxml'."; + assert lblH != null : "fx:id=\"lblH\" was not injected: check your FXML file 'gui.fxml'."; + assert lblK != null : "fx:id=\"lblK\" was not injected: check your FXML file 'gui.fxml'."; + assert lblJ != null : "fx:id=\"lblJ\" was not injected: check your FXML file 'gui.fxml'."; + assert lblP != null : "fx:id=\"lblP\" was not injected: check your FXML file 'gui.fxml'."; + assert lblX != null : "fx:id=\"lblX\" was not injected: check your FXML file 'gui.fxml'."; + assert lblY != null : "fx:id=\"lblY\" was not injected: check your FXML file 'gui.fxml'."; + assert lblC != null : "fx:id=\"lblC\" was not injected: check your FXML file 'gui.fxml'."; + assert lblV != null : "fx:id=\"lblV\" was not injected: check your FXML file 'gui.fxml'."; + assert lblB != null : "fx:id=\"lblB\" was not injected: check your FXML file 'gui.fxml'."; + assert lblL != null : "fx:id=\"lblL\" was not injected: check your FXML file 'gui.fxml'."; + assert lblM != null : "fx:id=\"lblM\" was not injected: check your FXML file 'gui.fxml'."; + assert lblN != null : "fx:id=\"lblN\" was not injected: check your FXML file 'gui.fxml'."; + + leuchten(btnA, circA); + leuchten(btnB, circB); + leuchten(btnC, circC); + leuchten(btnD, circD); + leuchten(btnE, circE); + leuchten(btnF, circF); + leuchten(btnG, circG); + leuchten(btnH, circH); + leuchten(btnI, circI); + leuchten(btnJ, circJ); + leuchten(btnK, circK); + leuchten(btnL, circL); + leuchten(btnM, circM); + leuchten(btnN, circN); + leuchten(btnO, circO); + leuchten(btnP, circP); + leuchten(btnQ, circQ); + leuchten(btnR, circR); + leuchten(btnS, circS); + leuchten(btnT, circT); + leuchten(btnU, circU); + leuchten(btnV, circV); + leuchten(btnW, circW); + leuchten(btnX, circX); + leuchten(btnY, circY); + leuchten(btnZ, circZ); + //TODO: Leuchten und Tasten ordnen + + this.codierer = new Codierer(); + this.codierer.setKenngruppe("enigma"); + + // Walzen auf ihre Position setzen. Der Spruchschlüssel ist hier bereits berücksichtigt. + mBtnStartPos1.setText(String.valueOf(this.codierer.getWalzen()[0])); + mBtnStartPos2.setText(String.valueOf(this.codierer.getWalzen()[1])); + mBtnStartPos3.setText(String.valueOf(this.codierer.getWalzen()[2])); + } + + @FXML + void löschen() { + textEingabe = textEingabe.substring(0, textEingabe.length() - 1); + tfrein.setText(textEingabe); + //TODO: anpassen, wenn kein Text da ist + } + + @FXML + void btnempfangen() { + String[] empfangeneNachricht = this.codierer.empfangeNachricht(); + if (empfangeneNachricht[0] != null) { + this.tfrein.setText(empfangeneNachricht[2]); + this.tfcodiert.setText(empfangeneNachricht[1]); + this.resetDisplay = true; + + mBtnStartPos1.setText(String.valueOf(this.codierer.getWalzen()[0])); + mBtnStartPos2.setText(String.valueOf(this.codierer.getWalzen()[1])); + mBtnStartPos3.setText(String.valueOf(this.codierer.getWalzen()[2])); + } + } + + @FXML + void btnsenden() { + this.tfrein.setText(""); + this.tfcodiert.setText(""); + + try { + this.codierer.sendeNachricht(); + } catch (HttpException | IOException e) { + e.printStackTrace(); + } + + } + + /** + * Methode zum setzen des menuButton Textes auf das ausgewählte MenuItem + * + * @param mb : MenuButton der Walze + * @param mi : MenuItem + */ + private void listener(MenuButton mb, MenuItem mi) { + mb.setText(mi.getText()); + } + + /** + * Methode zur Erstellung der Einträge und Action Listener für die einzelnen Menüs + * + * @param button : Button für die die Einträge erstellt werden sollen + */ + private void menu(MenuButton button, String[] str) { + mItem = new ArrayList<>(); + for (int i = 0; i < str.length; i++) { + //Eintrag aus str der Arraylist mi hinzufügen + mItem.add(new MenuItem(str[i])); + //MenuItem mi dem Button button hinzufügen + button.getItems().add(mItem.get(i)); + int finalI = i; + //Listener für die einzelnen Einträge + ArrayList<MenuItem> finalMi = mItem; + mItem.get(i).setOnAction(e -> { + listener(button, finalMi.get(finalI)); + }); + } + } + + /** + * Auswertung welcher Button gedrückt wurde + * + * @param e => ActionEvent des auslösenden Button + */ + @FXML + private void gedrueckteTaste(ActionEvent e) { + + //TODO Anbindung an Klasse Hardware + if (e.getSource() == btnA) setText(btnA); + if (e.getSource() == btnB) setText(btnB); + if (e.getSource() == btnC) setText(btnC); + if (e.getSource() == btnD) setText(btnD); + if (e.getSource() == btnE) setText(btnE); + if (e.getSource() == btnF) setText(btnF); + if (e.getSource() == btnG) setText(btnG); + if (e.getSource() == btnH) setText(btnH); + if (e.getSource() == btnI) setText(btnI); + if (e.getSource() == btnJ) setText(btnJ); + if (e.getSource() == btnK) setText(btnK); + if (e.getSource() == btnL) setText(btnL); + if (e.getSource() == btnM) setText(btnM); + if (e.getSource() == btnN) setText(btnN); + if (e.getSource() == btnO) setText(btnO); + if (e.getSource() == btnP) setText(btnP); + if (e.getSource() == btnQ) setText(btnQ); + if (e.getSource() == btnR) setText(btnR); + if (e.getSource() == btnS) setText(btnS); + if (e.getSource() == btnT) setText(btnT); + if (e.getSource() == btnU) setText(btnU); + if (e.getSource() == btnV) setText(btnV); + if (e.getSource() == btnW) setText(btnW); + if (e.getSource() == btnX) setText(btnX); + if (e.getSource() == btnY) setText(btnY); + if (e.getSource() == btnZ) setText(btnZ); + } + + /** + * schreibt Buchstaben des gedrückten Knopfes in Textfeld tfrein + * + * @param b : gedrückter Knopf + */ + private void setText(Button b) { + if (textEingabe.length() < 250) { + if(this.resetDisplay) { + this.tfcodiert.setText(""); + this.tfrein.setText(""); + this.resetDisplay = false; + this.codierer.resetHardware(); + textCodiert = ""; + textEingabe = ""; + // Spruchschlüssel generieren und codieren + this.codierer.generateSpruchschluessel(); + } + textEingabe += b.getText(); + textCodiert += this.codierer.codiere(b.getText().charAt(0), true); + + tfrein.setText(textEingabe); + tfcodiert.setText(textCodiert); + + mBtnStartPos1.setText(String.valueOf(this.codierer.getWalzen()[0])); + mBtnStartPos2.setText(String.valueOf(this.codierer.getWalzen()[1])); + mBtnStartPos3.setText(String.valueOf(this.codierer.getWalzen()[2])); + } + } + + private void leuchten(Button taste, Circle leuchte) { + + taste.setOnMousePressed(mouseEvent -> { + leuchte.setStyle("-fx-fill: #FFA500"); + + }); + taste.setOnMouseReleased(mouseEvent -> { + leuchte.setStyle("-fx-background-color: " + taste.getBackground()); + leuchte.setStyle("-fx-border-color: #000000"); + }); + } + + /** + * Setzt die Anzeige des entsprechende Gui-Element auf die entsprechende Walze + * ->d.h. welche Walze sitzt auf welcher Position + * + * @param walze => gibt die Walzennummer an (entspricht + * @param position => gibt die Position der Walze (entspricht + */ + public void setzeWalze(int walze, int position) { + switch (position) { + case 1: + mBtnWalzPos1.setText(str[position]); + break; + case 2: + mBtnWalzPos2.setText(str[position]); + break; + case 3: + mBtnWalzPos3.setText(str[position]); + break; + default: + break; + } + } + + /** + * Setzt den Notch (position)=> die Mitnahmeposition der Walze (walze) fest + * Mitnahmeposition meint => die nächste Walze wird bei erreichen dieser Position (notch) + * um eine Stelle versetzt + * + * @param walze => gibt die walze an + * @param position => gibt den notch der Walze (walze) an + */ + public void setzeRing(int walze, int position) { + switch (walze) { + case 1: + mBtnNotchPos1.setText(num[position]); + break; + case 2: + mBtnNotchPos2.setText(num[position]); + break; + case 3: + mBtnNotchPos3.setText(num[position]); + break; + default: + break; + } + } + + /** + * Setzt die Startposition/ aktuelle Position der Walze + * + * @param walze => gibt die walze an + * @param buchstabe gibt den Startwert bzw die aktuelle Position der Walze (walze) an + */ + public void setzePosition(int walze, char buchstabe) { + switch (walze) { + case 1: + mBtnStartPos1.setText(String.valueOf(buchstabe)); + break; + case 2: + mBtnStartPos2.setText(buchstabe + ""); + break; + case 3: + mBtnStartPos3.setText(buchstabe + ""); + break; + default: + break; + } + } + + /** + * Setzt den aktuellen TagesSchluessel + * + * @param tagesSchluessel + */ + public void setzeTagesSchluessel(String tagesSchluessel) { + /** + * TODO Benötige TestString aus der DB Codebuch + * + * Den Tagesschlüssel bekommst du wie folgt: + * <p> + * // Aktuelle Position der Walzen + * codierer.codebuch.getWalzenlage(); + * + * // Position der Ringe + * codierer.codebuch.getRingstellung(); + * + * //Position der Steckverbindungen + * codierer.codebuch.getSteckverbindung(); + * </p> + */ + + } +} \ No newline at end of file diff --git a/src/main/java/enigma/database/DatenbankSqlite.java b/src/main/java/enigma/database/DatenbankSqlite.java index 2030867..00e88ea 100644 --- a/src/main/java/enigma/database/DatenbankSqlite.java +++ b/src/main/java/enigma/database/DatenbankSqlite.java @@ -1,7 +1,5 @@ package enigma.database; -import enigma.model.Codebuch; - import java.sql.*; /** @@ -42,36 +40,32 @@ public class DatenbankSqlite { * Anschließend wir eine Verbindung zur SQLite Datenbank aufgebaut und das Codebuch angefragt. * Dieses wird in ein Object vom Type Codebuch gepackt und zurück gegeben. * + * TODO: Es wird kein Codebuch Objekt mehr erzeugt sondern ein String[] zurück gegeben + * Hole den heutigen Eintrag aus der SQLite Datenbank und erstelle daraus ein Codebuch Objekt + * * @param tag : int : Tag für welchen ein Codebuch benötigt wird. * @return Codebuch : Codebuch Object mit dem Codebuch des angefragten Tages */ - public Codebuch getCodebuch(int tag) throws SQLException { - Codebuch cb = new Codebuch(); - - Connection conn = this.connect(); - PreparedStatement pstm = conn.prepareStatement("SELECT * FROM table_codebuch WHERE Tag=? LIMIT 1"); - pstm.setInt(1, tag); - ResultSet rs = pstm.executeQuery(); - - String[] walzenlage, ringstellung, steckverbindung; + public String[] getCodebuch(int tag) { - while (rs.next()) { - cb.setTag(rs.getInt("Tag")); + String[] codebuch = new String[4]; - walzenlage = rs.getString("Walzenlage").split(","); - for (int i = 0; i < 3; i++) { - cb.setWalzenlage(i, Integer.parseInt(walzenlage[i])); - } - - ringstellung = rs.getString("Ringstellung").split(","); - for (int i = 0; i < 3; i++) { - cb.setRingstellung(i, Integer.parseInt(ringstellung[i])); + try { + Connection conn = this.connect(); + PreparedStatement pstm = conn.prepareStatement("SELECT * FROM table_codebuch WHERE Tag=? LIMIT 1"); + pstm.setInt(1, tag); + ResultSet rs = pstm.executeQuery(); + + while (rs.next()) { + codebuch[0] = rs.getString("Tag"); + codebuch[1] = rs.getString("Walzenlage"); + codebuch[2] = rs.getString("Ringstellung"); + codebuch[3] = rs.getString("Steckverbindung"); } - - cb.setSteckverbindung(rs.getString("Steckverbindung").split(",")); + } catch (SQLException ignored) { } - return cb; + return codebuch; } //region Random New Codebuch generieren @@ -138,7 +132,7 @@ public class DatenbankSqlite { /** * Generiert einen String aus 10 * 2 unterschiedlichen Großbuchstaben - * @return String => 10 Buchstabenkombinationen mit Komma getrennt + * @return String => 10 Buchstabenkombinationen mit Komma getrennt *//* private static String steckverbindung(){ Random rnd = new Random(); diff --git a/src/main/java/enigma/model/Codebuch.java b/src/main/java/enigma/model/Codebuch.java index 8b2c0ee..020b070 100644 --- a/src/main/java/enigma/model/Codebuch.java +++ b/src/main/java/enigma/model/Codebuch.java @@ -1,5 +1,10 @@ package enigma.model; +import enigma.database.DatenbankSqlite; + +import java.time.LocalDate; +import java.time.ZoneId; + /** * Das Codebuch war eines der essentiel wichtigen Sachen bei der Enigma. * Jeder Enigma gehörte auch ein Codebuch bei, welches notwenidg war um die Nachrichten zu ver/entschlüsseln. @@ -46,6 +51,31 @@ public class Codebuch { } } + public void getTagesschluessel(int... tag) { + String[] walzenlage, ringstellung, db; + + if(tag.length > 0) { + db = new DatenbankSqlite().getCodebuch(tag[0]); + } else { + db = new DatenbankSqlite().getCodebuch(LocalDate.now(ZoneId.of("Europe/Berlin")).getDayOfMonth()); + } + + this.setTag(Integer.parseInt(db[0])); + + walzenlage = db[1].split(","); + for (int i = 0; i < 3; i++) { + this.setWalzenlage(i, Integer.parseInt(walzenlage[i])); + } + + ringstellung = db[2].split(","); + for (int i = 0; i < 3; i++) { + this.setRingstellung(i, Integer.parseInt(ringstellung[i])); + } + + this.setSteckverbindung(db[3].split(",")); + + } + /** * Gibt die aktuelle Instanz des Codebuches als String zurück. * Hierbei werden der Tag die Walzenlagen, Ring/Grundstellung sowie die Steckverbindungen zurück gegeben. @@ -55,39 +85,36 @@ public class Codebuch { @Override public String toString() { StringBuilder sb = new StringBuilder(); - boolean wlLoop = false; - boolean rsLoop = false; - boolean svLoop = false; + // Tag des aktuellen Codebuch Eintrags sb.append("Tag: ").append(this.getTag()).append("\n"); + // In welchem Slot steckt die Walze? sb.append("Walzenlage: "); for (int wl : this.getWalzenlage()) { sb.append(wl).append(","); - wlLoop = true; } - // TODO if (this.getWalzenlage().length > 0) { sb.setLength(sb.length() - 1); } sb.append("\n"); + // Die Ringstellung an den Walzen sb.append("Ringstellung: "); for (int rs : this.getRingstellung()) { sb.append(rs).append(","); - rsLoop = true; } - if (rsLoop) { + if (this.getRingstellung().length > 0) { sb.setLength(sb.length() - 1); } sb.append("\n"); + // Das Steckbrett und wie die Kabel angeschlossen sind sb.append("Steckverbindung: "); for (char[] verbindung : this.getSteckverbindung()) { sb.append(verbindung[0]).append(":").append(verbindung[1]).append(","); - svLoop = true; } - if (svLoop) { + if (this.getSteckverbindung().length > 0) { sb.setLength(sb.length() - 1); } sb.append("\n"); @@ -109,7 +136,7 @@ public class Codebuch { * * @param tag : int */ - public void setTag(int tag) { + private void setTag(int tag) { this.tag = tag; } @@ -145,11 +172,11 @@ public class Codebuch { * Setzt die Steckverbindung dieser Instanz * * @param kabel : String[] : Die Steckverbindungen die gesteckt werden sollen. - * Jeder Eintrag im Array ist ein String mit zwei Zeichen. - * Diese werden in zwei char zerlegt und anschließend im globalen - * Array gespeichert. + * Jeder Eintrag im Array ist ein String mit zwei Zeichen. + * Diese werden in zwei char zerlegt und anschließend im globalen + * Array gespeichert. */ - public void setSteckverbindung(String[] kabel) { + private void setSteckverbindung(String[] kabel) { int counter = 0; for (String stecker : kabel) { @@ -166,7 +193,7 @@ public class Codebuch { * @param position : int : Position der Walze * @param walzenlage : int : Welche Walze wurde eingesetzt */ - public void setWalzenlage(int position, int walzenlage) { + private void setWalzenlage(int position, int walzenlage) { this.walzenlage[position] = walzenlage; } @@ -176,7 +203,7 @@ public class Codebuch { * @param position : int : Auf welche Walze wird der Ring aufgesetzt? * @param ringstellung : char : An dieser Position soll die nächste Walze weiter gedreht werden. */ - public void setRingstellung(int position, int ringstellung) { + private void setRingstellung(int position, int ringstellung) { this.ringstellung[position] = ringstellung; } } diff --git a/src/main/java/enigma/model/Codierer.java b/src/main/java/enigma/model/Codierer.java index 54b3744..0f8aa88 100644 --- a/src/main/java/enigma/model/Codierer.java +++ b/src/main/java/enigma/model/Codierer.java @@ -1,12 +1,9 @@ package enigma.model; -import enigma.database.DatenbankSqlite; import org.apache.http.HttpException; import java.io.IOException; -import java.sql.SQLException; -import java.time.LocalDate; -import java.time.ZoneId; +import java.util.Calendar; import java.util.Random; /** @@ -18,7 +15,8 @@ public class Codierer { * TODO Dokumentieren */ private String spruchschluessel; - private String kenngruppe; + private String spruchschluesselCodiert; + private String kenngruppe, kenngruppeCodiert; private String nachricht; private Hardware hardware; private Codebuch codebuch; @@ -28,7 +26,12 @@ public class Codierer { */ public Codierer() { this.nachricht = ""; - initialisiereHardware(); + this.spruchschluessel = ""; + this.spruchschluesselCodiert = ""; + this.kenngruppe = ""; + this.kenngruppeCodiert = ""; + this.codebuch = new Codebuch(); + this.codebuch.getTagesschluessel(); } /** @@ -44,15 +47,10 @@ public class Codierer { */ private void initialisiereHardware() { - // Hole den heutigen Eintrag aus der SQLite Datenbank und erstelle daraus ein Codebuch Objekt - try { - this.codebuch = new DatenbankSqlite().getCodebuch(LocalDate.now(ZoneId.of("Europe/Berlin")).getDayOfMonth()); - } catch (SQLException ignored) { - } - // Das Steckbrett initialisieren Steckbrett sb = new Steckbrett(); - char[][] verbinder = codebuch.getSteckverbindung(); + char[][] verbinder = this.codebuch.getSteckverbindung(); + // Für jedes Kabel eine Verbindung auf dem Steckbrett setzen for (char[] kabel : verbinder) { sb.setzeVertauschung(kabel[0], kabel[1]); @@ -62,15 +60,16 @@ public class Codierer { this.hardware = new Hardware(); // Den Ring an der Walze anbringen und die Walze dann in die Hardware einsetzen - hardware.setWalzen(0, codebuch.getWalzenlage()[0], codebuch.getRingstellung()[0]); - hardware.setWalzen(1, codebuch.getWalzenlage()[1], codebuch.getRingstellung()[1]); - hardware.setWalzen(2, codebuch.getWalzenlage()[2], codebuch.getRingstellung()[2]); + this.hardware.setWalzen(0, this.codebuch.getWalzenlage()[0], this.codebuch.getRingstellung()[0]); + this.hardware.setWalzen(1, this.codebuch.getWalzenlage()[1], this.codebuch.getRingstellung()[1]); + this.hardware.setWalzen(2, this.codebuch.getWalzenlage()[2], this.codebuch.getRingstellung()[2]); // Der Hardware das gesetzte Steckbrett zuweisen - hardware.setSteckbrett(sb); + this.hardware.setSteckbrett(sb); // Ein Reflektor Objekt erstellen und der Hardware bekannt geben - hardware.setReflektor(new Reflektor()); + this.hardware.setReflektor(new Reflektor()); + } /** @@ -83,14 +82,69 @@ public class Codierer { * Wenn nicht, wird der Buchstabe dem Spruchschlüssel hinzugefügt. * <p> * Dies wir nun so lange gemacht bis der Spruchschlüssel eine länge von drei Zeichen hat. + * <p> + * TODO: Doku aktuallisieren */ - public void spruchSchluessel() { - while (this.spruchschluessel.length() < 3) { + public void generateSpruchschluessel() { + + String klartext = ""; + + while (klartext.length() < 3) { String temp = this.randomBuchstabe(); - if (!this.spruchschluessel.contains(temp)) { - this.spruchschluessel += temp; + if (!klartext.contains(temp)) { + klartext += temp; } } + + this.spruchschluessel = klartext; + this.spruchschluesselCodiert = this.codiere(klartext + klartext, false); + + // Walzen auf den Spruchschlüssel stellen + this.hardware.setzePosition(0, this.spruchschluessel.charAt(0)); + this.hardware.setzePosition(1, this.spruchschluessel.charAt(1)); + this.hardware.setzePosition(2, this.spruchschluessel.charAt(2)); + + // Die Kenngruppe codieren + this.kenngruppeCodiert = this.codiere(this.kenngruppe, true); + } + + /** + * TODO Dokumentieren + * + * @return String : Der codierte Spruchschlüssel + */ + public String getSpruchschluesselCodiert() { + return this.spruchschluesselCodiert; + } + + /** + * TODO Dokumentieren + * + * @return String : Der klartext Spruchschlüssel + */ + public String getSpruchschluessel() { + return this.spruchschluessel; + } + + /** + * TODO Dokumentieren + */ + private String generateKopf() { + Calendar cal = Calendar.getInstance(); + + // Uhrzeit an den Kopf hängen + // Todo: Da gab es doch noch eine besser Lösung für den Korrekturfaktor der Uhrzeit? + + return String.format("%02d%02d", cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE)) + " " + + + // Zeichen Anzahl der Nachricht + this.nachricht.length() + " " + + + // Spruchschlüssel anhängen + this.spruchschluesselCodiert.substring(0, 3) + " " + this.spruchschluesselCodiert.substring(3, 6) + " "; + + // Kenngruppe an das Ende setzen + //this.kenngruppeCodiert; } /** @@ -111,7 +165,9 @@ public class Codierer { * @throws HttpException : Die Nachricht konnte nicht abgesendet werden */ public void sendeNachricht() throws IOException, HttpException { - new Funkraum().sendeFunkspruch(new Morsecode().convertBuchstabeToMorsecode(this.nachricht), this.kenngruppe); + String kopf = this.generateKopf(); + new Funkraum().sendeFunkspruch(new Morsecode().convertBuchstabeToMorsecode(kopf + this.nachricht), this.kenngruppe); + this.nachricht = ""; } /** @@ -122,24 +178,19 @@ public class Codierer { * String[2] = Nachricht im Klartext */ public String[] empfangeNachricht() { - String[] codierteNachricht = new String[3]; - Morsecode mc = new Morsecode(); - - try { - codierteNachricht = new Funkraum().empfangeFunkspruch(this.kenngruppe); - String morsecode = mc.convertMorsecodeToBuchstabe(codierteNachricht[1]); - StringBuilder decodiert = new StringBuilder(); - for (char buchstabe : morsecode.toCharArray()) { - decodiert.append(this.hardware.codiere(buchstabe)); - } - - codierteNachricht[2] = decodiert.toString(); + this.nachricht = ""; + Morsecode mc = new Morsecode(); + String[] nachricht = new String[3]; + String[] codierteNachricht = new Funkraum().empfangeFunkspruch(this.kenngruppe); - } catch (IOException e) { - e.printStackTrace(); + if (codierteNachricht[1] != null) { + nachricht[0] = codierteNachricht[0]; + nachricht[1] = mc.convertMorsecodeToBuchstabe(codierteNachricht[1]); + nachricht[2] = this.decodiere(nachricht[1], Integer.parseInt(nachricht[0])); } - return codierteNachricht; + + return nachricht; } /** @@ -158,7 +209,10 @@ public class Codierer { * @param kenngruppe : String : Kenngruppe welche die Enigma gerade benutzt */ public void setKenngruppe(String kenngruppe) { + this.kenngruppe = kenngruppe; + this.initialisiereHardware(); + } /** @@ -171,12 +225,17 @@ public class Codierer { } /** - * Gibt das Hardware Objekt zurück + * Gibt die aktuellen Buchstaben auf den Walzen zurück * - * @return Hardware + * @return */ - public Hardware getHardware() { - return hardware; + public char[] getWalzen() { + char[] walzen = new char[3]; + walzen[0] = this.hardware.getWalzen()[0].getAnzeige(); + walzen[1] = this.hardware.getWalzen()[1].getAnzeige(); + walzen[2] = this.hardware.getWalzen()[2].getAnzeige(); + + return walzen; } /** @@ -187,4 +246,90 @@ public class Codierer { public void setHardware(Hardware hardware) { this.hardware = hardware; } + + /** + * TODO Dokumentieren + * + * @param buchstabe + * @return + */ + public char codiere(char buchstabe, boolean save) { + char codiert = this.hardware.codiere(buchstabe); + if(save) { + this.nachricht += codiert; + } + + return codiert; + } + + public String codiere(String klartext, boolean save) { + + StringBuilder sb = new StringBuilder(); + + for (char buchstabe : klartext.toCharArray()) { + sb.append(this.codiere(buchstabe, save)); + } + + return sb.toString(); + } + + /** + * TODO Dokumentieren + * + * @param codierteNachricht : String : Enigma codierte Nachricht + * @param tag : int : Tag der Nachricht + * @return String : decodierte Nachricht + */ + private String decodiere(String codierteNachricht, int tag) { + + // Hardware reseten und Tageseinstellungen aus dem Codebuch laden + this.initialisiereHardware(); + this.codebuch.getTagesschluessel(tag); + + String[] nachricht = codierteNachricht.split(" "); + StringBuilder sb = new StringBuilder(); + + // Uhrzeit + sb.append(nachricht[0]).append(" "); + + // Zeichen Anzahl der Nachricht + sb.append(nachricht[1]).append(" "); + + // Spruchschlüssel + String spruchschluessel = this.decodiereString(nachricht[2]); + sb.append(spruchschluessel).append(" "); + sb.append(this.decodiereString(nachricht[3])).append(" "); + + // Walzen neu einstellen mit dem Spruchschlüssel + this.hardware.setzePosition(0, spruchschluessel.charAt(0)); + this.hardware.setzePosition(1, spruchschluessel.charAt(1)); + this.hardware.setzePosition(2, spruchschluessel.charAt(2)); + + // Nachricht decodieren + sb.append(this.decodiereString(nachricht[4])); + + return sb.toString(); + } + + public String decodiereString(String nachricht) { + + StringBuilder sb = new StringBuilder(); + + for (char buchstabe : nachricht.toCharArray()) { + if (buchstabe > 0) { + sb.append(this.hardware.codiere(buchstabe)); + } + } + + return sb.toString(); + } + + /** + * Todo Dokumentieren + */ + public void resetHardware() { + + this.initialisiereHardware(); + } + } diff --git a/src/main/java/enigma/model/Funkraum.java b/src/main/java/enigma/model/Funkraum.java index 720157a..d3fe288 100644 --- a/src/main/java/enigma/model/Funkraum.java +++ b/src/main/java/enigma/model/Funkraum.java @@ -10,9 +10,11 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.json.JSONObject; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; @@ -24,39 +26,46 @@ import java.util.List; * (Benutzername) übergeben und falls eine Nachricht gesendet werden soll, zusätzlich ein m mit der Nachricht. * Beides wird als String gesendet. */ -public class Funkraum { +class Funkraum { /** * Zum Empfangen liefert der Server, anstatt der Kenngruppe (k) den Empfangstag der Nachricht mit damit man - * die Grundeinstellungen aus dem Codebuch, für diesen Tag, raussuchen kann. - * {'m': 'Hello world', 't': '26'} + * die Grundeinstellungen aus dem Codebuch, für diesen Tag, raussuchen kann. + * {'m': 'Hello world', 't': '26'} * * @param kenngruppe : String : Kenngruppe dieser Engiam * @return String[] : String Array wobei Index 0 dem Empfangstag entspricht und index 1 der empfangenen Nachricht - * @throws IOException : Exception : wenn keine Verbindung zum Funker aufgebaut werden konnte. */ - public String[] empfangeFunkspruch(String kenngruppe) throws IOException { + String[] empfangeFunkspruch(String kenngruppe) { + String[] funkspruch = new String[2]; HttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost("https://enigma.itstall.de/"); - // Anfrage Parameter und Encoding setzen - List<NameValuePair> params = new ArrayList<>(2); - params.add(new BasicNameValuePair("k", kenngruppe)); - httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); + try { + // Anfrage Parameter und Encoding setzen + List<NameValuePair> params = new ArrayList<>(2); + params.add(new BasicNameValuePair("k", kenngruppe)); + httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); - // POST Request ausführen und Rückgabe verarbeiten - HttpResponse response = httpclient.execute(httppost); - HttpEntity entity = response.getEntity(); + // POST Request ausführen und Rückgabe verarbeiten + HttpResponse response = httpclient.execute(httppost); + HttpEntity entity = response.getEntity(); - if (entity != null) { - BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent())); - JSONObject result = new JSONObject(reader.readLine()); + if (entity != null) { + BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent())); + JSONObject result = new JSONObject(reader.readLine()); - if(!result.getString("m").isEmpty()) { - funkspruch[0] = result.getString("t"); - funkspruch[1] = result.getString("m"); + if (!result.getString("m").isEmpty()) { + funkspruch[0] = result.getString("t"); + funkspruch[1] = result.getString("m"); + } } + } catch (UnsupportedEncodingException e) { + System.out.println("Encoding wird nicht unterstützt."); + } catch (IOException e) { + System.out.println("HTTP Post war nicht erfolgreich.\nBitte wenden Sie sich an ihren Vorgesetzten."); + e.printStackTrace(); } return funkspruch; @@ -66,19 +75,20 @@ public class Funkraum { * Dem Service senden wir unsere Nachricht als POST Parameter (m) und unsere Kenngruppe (k). * Dieser nimmt die Daten entgegeb und hierlerlegt sie auf seinem Stapel (Datenbank) von dem die Nachrichten * irgendwann, auf Anfrage, wieder abgerufen werden können. - * - * Darauf antworter der Server dann mit einem JSON Object in dem das Ergebnis der Anfrage zu sehen ist. - * {'result': 200} - * - * Die 200 heißt hier das alles erfolgreich durchgeführt werden konnte. Steht hier etwas anderes, ist ein Fehler - * aufgetreten und die Anfrage war nicht erfolgreich. - * In letzterem Fall sollte eine Meldung an den Benutzer ausgegeben werden und ggF später erneut versucht. + * <p> + * Darauf antworter der Server dann mit einem JSON Object in dem das Ergebnis der Anfrage zu sehen ist. + * {'result': 200} + * <p> + * Die 200 heißt hier das alles erfolgreich durchgeführt werden konnte. Steht hier etwas anderes, ist ein Fehler + * aufgetreten und die Anfrage war nicht erfolgreich. + * In letzterem Fall sollte eine Meldung an den Benutzer ausgegeben werden und ggF später erneut versucht. * * @param funkspruch : String : Der zu sendede Funkspruch * @param kenngruppe : String : Die Kenngruppe dieser Engima * @throws IOException : Exception : Funkspruch konnte nicht gesendet werden */ - public void sendeFunkspruch(String funkspruch, String kenngruppe) throws HttpException, IOException { + void sendeFunkspruch(String funkspruch, String kenngruppe) throws HttpException, IOException { + HttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost("https://enigma.itstall.de/"); @@ -95,7 +105,7 @@ public class Funkraum { if (entity != null) { BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent())); JSONObject result = new JSONObject(reader.readLine()); - if(result.getInt("result") != 200) { + if (result.getInt("result") != 200) { throw new HttpException("Der andere Funker mag deine Nachricht nicht. Rüge ihn wenn du ihn wieder siehst..."); } } diff --git a/src/main/java/enigma/model/Morsecode.java b/src/main/java/enigma/model/Morsecode.java index fdcf5c1..01df55d 100644 --- a/src/main/java/enigma/model/Morsecode.java +++ b/src/main/java/enigma/model/Morsecode.java @@ -134,14 +134,13 @@ public class Morsecode { String[] morsecodes = input.split(" "); - // Durch alle char im übergebenen String loopen for(String morsecode : morsecodes) { // Wenn der char in der codeMap enthalten ist, an den Rückgabe String anheften, ansonsten nichts ignorieren. buchstabe = this.getBuchstabe(morsecode); if(buchstabe != null) { - sb.append(buchstabe).append(' '); + sb.append(buchstabe); } } diff --git a/src/main/java/enigma/model/Walze.java b/src/main/java/enigma/model/Walze.java index 3f94ead..d6be910 100644 --- a/src/main/java/enigma/model/Walze.java +++ b/src/main/java/enigma/model/Walze.java @@ -17,16 +17,10 @@ public class Walze { /** * Konstanten und Variablen der Klasse Walze * <p> - * buchstabe : Bestimmt den Buchstaben * ringstellung : Bestimmt den Umspringpunkt der Walze * walzennr : Bestimmt die Walzennr. (also wie der Buchstabe codiert wird) - * iPositionBuchstabe : gibt die Position des Buchstabens auf dem Array der Walze wieder. - * iPositionBuchstabeRe : gibt die Position des codierten Buchstabens im Alphabet wieder. - * private Character[] walze : Character-Array der Walze */ - /*Alphawalze*/ private Character[] walzeAlpha; - private Character buchstabe; private char ringstellung; private int walzennr; private Character[] alphabet; @@ -40,10 +34,13 @@ public class Walze { */ public Walze(int walzennr, int ringstellung) { this.alphabet = new Character[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; - this.walzennr = walzennr; - this.buchstabe = 'A'; - this.ringstellung = this.alphabet[ringstellung - 1]; this.walzeAlpha = new Character[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; + + // Setzen der Walzennummer + this.setWalzennr(walzennr); + + // Setzen der Ringstellung + this.setRingstellung(ringstellung); } @@ -184,15 +181,6 @@ public class Walze { return buchstabe; } - /** - * TODO Dokumentieren - * - * @return buchstabe : Character : gibt den neuen Buchstaben zurueck - */ - public Character getBuchstabe() { - return buchstabe; - } - /** * TODO Dokumentieren * @@ -246,13 +234,11 @@ public class Walze { */ public void setPosition(Character buchstabe) { if (Character.isLetter(buchstabe)) { - this.buchstabe = Character.toUpperCase(buchstabe); + buchstabe = Character.toUpperCase(buchstabe); System.arraycopy(this.alphabet, 0, this.walzeAlpha, 0, this.alphabet.length); if (buchstabe > 'A') { dreheWalze(this.getArrayIndex(buchstabe, this.alphabet)); } - } else { - this.buchstabe = 'A'; } } } -- GitLab