...
 
Commits (4)
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/out/production/StefanMichelSimulator" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
......
......@@ -4,11 +4,13 @@ import controller.HamsterController;
import controller.IOController;
import controller.TerrainController;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.stage.Stage;
import model.DatabaseManager;
import model.Terrain;
import model.ThreadManager;
import network.NetworkManager;
import view.MainViewCreationFX;
import javax.sound.sampled.AudioInputStream;
......@@ -25,7 +27,8 @@ public class Main extends Application {
private DatabaseManager databaseManager;
public static final List<String> activePrograms = new ArrayList<>();
private IOController ioController;
ThreadManager threadManager;
private ThreadManager threadManager;
private NetworkManager networkManager = NetworkManager.getInstance();
public static void main(String[] args) {
launch(args);
......@@ -44,6 +47,7 @@ public class Main extends Application {
terrain.setHamsterName(name);
ioController.setUpHamster(name);
Stage newStage = new Stage();
newStage.setResizable(true);
Scene scene = new Scene(new MainViewCreationFX(terrain, terrainController, ioController, hamsterController,
threadManager, databaseManager), 1000, 500);
......@@ -80,9 +84,11 @@ public class Main extends Application {
primaryStage.setScene(scene);
primaryStage.setTitle(terrain.getHamsterName());
primaryStage.show();
primaryStage.setResizable(true);
primaryStage.setOnCloseRequest(event -> {
activePrograms.remove(terrain.getHamsterName());
ioController.saveFile(terrain.getHamsterName());
networkManager.unbind();
});
activePrograms.add(terrain.getHamsterName());
ioController.compile(terrain.getHamsterName());
......
......@@ -47,6 +47,7 @@ public class ChooseEntryDialogBox {
ComboBox<String> comboBox = new ComboBox<>(FXCollections.observableArrayList(exampleList));
comboBox.setPromptText(exampleList.get(0));
comboBox.setValue(exampleList.get(0)); // erstes element soll immer ausgewaehlt sein
grid.add(new Label("Tag:"), 0, 0);
grid.add(comboBox, 1, 0);
......
......@@ -78,7 +78,7 @@ public class DataBaseEntryDialogBox {
res = res.trim();
String[] tags = res.split(" ");
terrainController.parseXML(terrain.getName());
System.out.println(terrainController.loadXMLToString(terrain.getName()));
worked = databaseManager.insertExample(text.trim(), terrainController.loadXMLToString(terrain.getName()), tags);
if (!worked)
new Alert(Alert.AlertType.ERROR, "Fehler!"
......
......@@ -90,6 +90,7 @@ public class TerrainController {
public void parseXML(String terrainName) {
try (FileOutputStream fileOutputStream = new FileOutputStream("terrains/" + terrainName + ".xml")) {
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(fileOutputStream);
writer.writeStartDocument("UTF-8", "1.0");
......@@ -145,7 +146,6 @@ public class TerrainController {
}
writer.writeEndElement();
writer.writeEndElement();
} catch (XMLStreamException e) {
e.printStackTrace();
......@@ -230,14 +230,9 @@ public class TerrainController {
if (node.getNodeType() == Node.ELEMENT_NODE) {
int column = Integer.parseInt(columnList.item(i).getAttributes().getNamedItem("index").getNodeValue());
int row = Integer.parseInt(node.getAttributes().getNamedItem("index").getNodeValue());
System.out.print("Value [" + column + "] [" + row + "] : ");
map[column][row] = Integer.parseInt(node.getTextContent());
System.out.println(node.getTextContent());
}
}
System.out.println();
}
terrain.setMap(map);
......
......@@ -155,17 +155,23 @@ public class DerbyDatabase {
public boolean updateExample(String code, String terrain, String... tags) {
Connection conn;
PreparedStatement preparedStatement;
ResultSet resultSet;
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(connectionURL);
conn = DriverManager.getConnection(connectionURL);
// check ob beispiel bereits in datenbank existiert
PreparedStatement preparedStatement = conn.prepareStatement(queryExampleId);
preparedStatement = conn.prepareStatement(queryExampleId);
preparedStatement.setString(1, code);
preparedStatement.setString(2, terrain);
ResultSet resultSet = preparedStatement.executeQuery();
resultSet = preparedStatement.executeQuery();
// wenn es bereits existiert wird false zurueckgeben und das speichern schlaegt fehl
if (resultSet.next()) {
conn.close();
preparedStatement.close();
resultSet.close();
return false;
}
......@@ -211,6 +217,10 @@ public class DerbyDatabase {
preparedStatement.execute();
}
conn.close();
preparedStatement.close();
resultSet.close();
return true;
} catch (Exception e) {
e.printStackTrace();
......@@ -233,8 +243,11 @@ public class DerbyDatabase {
String terrain = resultSet.getString(2);
entry = new AbstractMap.SimpleEntry<>(code, terrain);
conn.close();
preparedStatement.close();
resultSet.close();
} catch (Exception e) {
e.printStackTrace();
}
return entry;
}
......
package network;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.UUID;
public interface ITutor extends Remote {
UUID sendCodeTerrain(CodeTerrain codeTerrain);
UUID sendCodeTerrain(CodeTerrain codeTerrain) throws RemoteException;
CodeTerrain getAnswer(UUID id);
CodeTerrain getAnswer(UUID id) throws RemoteException;
}
......@@ -2,6 +2,7 @@ package network;
import controller.PropertiesManager;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
......@@ -17,6 +18,7 @@ public class NetworkManager {
private final PropertiesManager propertiesManager = PropertiesManager.getInstance();
private Student student;
private Tutor tutor;
private Registry registry;
private NetworkManager() {
String role = propertiesManager.getString("role");
......@@ -42,7 +44,7 @@ public class NetworkManager {
try {
this.tutor = new Tutor();
LocateRegistry.createRegistry(port);
Registry registry = LocateRegistry.getRegistry(port);
registry = LocateRegistry.getRegistry(port);
registry.rebind("Tutor", tutor);
} catch (RemoteException e) {
e.printStackTrace();
......@@ -64,9 +66,22 @@ public class NetworkManager {
return tutor.getNextRequest();
}
public void addAnswer(String code, String terrain) {
public boolean addAnswer(String code, String terrain) {
CodeTerrain codeTerrain = new CodeTerrain(code, terrain);
tutor.addAnswer(codeTerrain);
return tutor.addAnswer(codeTerrain);
}
// clean server shutdown
public void unbind() {
if (tutor != null) {
try {
registry.unbind("Tutor");
Tutor.unexportObject(tutor, true);
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}
}
}
}
package network;
import java.rmi.ConnectException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
......@@ -28,7 +29,7 @@ public class Student {
Registry registry = LocateRegistry.getRegistry(host, port);
tutor = (ITutor) registry.lookup("Tutor");
} catch (RemoteException e) {
e.printStackTrace();
System.out.println("keine Verbindung zum Tutor!");
} catch (NotBoundException e) {
e.printStackTrace();
}
......@@ -36,11 +37,20 @@ public class Student {
}
public void sendRequest(CodeTerrain codeTerrain) {
requestId = tutor.sendCodeTerrain(codeTerrain);
try {
requestId = tutor.sendCodeTerrain(codeTerrain);
} catch (RemoteException e) {
e.printStackTrace();
}
}
public CodeTerrain getRequest() {
return tutor.getAnswer(requestId);
try {
return tutor.getAnswer(requestId);
} catch (RemoteException e) {
e.printStackTrace();
}
return null;
}
}
......@@ -9,7 +9,9 @@ public class Tutor extends UnicastRemoteObject implements ITutor {
private final Queue<CodeTerrain> requests = new LinkedList<>();
private final Map<UUID, CodeTerrain> answerMap = new HashMap<>();
private UUID lastId;
// eine queue der IDs damit bei die lastId nicht immer ueberschrieben wird, wenn
// eine zweite CodeTerrain nachricht ankommt, waehrend der tutor noch eine bearbeitet
private final Queue<UUID> lastIds = new LinkedList<>();
protected Tutor() throws RemoteException {
......@@ -20,7 +22,8 @@ public class Tutor extends UnicastRemoteObject implements ITutor {
@Override
public UUID sendCodeTerrain(CodeTerrain codeTerrain) {
requests.add(codeTerrain);
lastId = UUID.randomUUID();
UUID lastId = UUID.randomUUID();
lastIds.add(lastId);
return lastId;
}
......@@ -35,8 +38,14 @@ public class Tutor extends UnicastRemoteObject implements ITutor {
return requests.poll();
}
public void addAnswer(CodeTerrain codeTerrain) {
answerMap.put(lastId, codeTerrain);
public boolean addAnswer(CodeTerrain codeTerrain) {
UUID lastId = lastIds.poll();
if (lastId != null) {
answerMap.put(lastId, codeTerrain);
return true;
} else {
return false;
}
}
}
......@@ -31,7 +31,7 @@ import java.util.*;
/**
* Created by Stefan Michel on 14.11.17.
*/
public class MainViewCreationFX extends VBox implements Observer {
public class MainViewCreationFX extends BorderPane implements Observer {
private final MenuBar menuBar = new MenuBar();
private final ToolBar toolBar = new ToolBar();
......@@ -84,26 +84,23 @@ public class MainViewCreationFX extends VBox implements Observer {
//toolbar erstellen
buildToolBarFX();
splitPane.getItems().addAll(textArea, terrainPane);
VBox vBox = new VBox();
vBox.getChildren().addAll(menuBar, toolBar);
//TODO anderen weg finden das center den gesamten bereich ausfuellt
splitPane.setPrefHeight(400);
splitPane.getItems().addAll(textArea, terrainPane);
//label erstellen
footer = new Label("Herzlich willkommen!");
footer.setMinHeight(20);
//elemente ins Borderpane einsetzen
BorderPane borderPane = new BorderPane();
borderPane.setTop(toolBar);
borderPane.setCenter(splitPane);
borderPane.setBottom(footer);
this.setTop(vBox);
this.setCenter(splitPane);
this.setBottom(footer);
//elemente in die VBox einsetzen und returnen
this.getChildren().addAll(menuBar, borderPane);
// this.getBottom().maxHeight(0);
//ioController erstellen
ioController.setMainViewCreationFX(this);
}
......@@ -201,33 +198,65 @@ public class MainViewCreationFX extends VBox implements Observer {
private Menu buildTutorMenu() {
MenuItem anfrageLaden = new MenuItem("Anfrage _laden");
MenuItem anfrageBeantworten = new MenuItem("Anfrage _beantworten");
anfrageLaden.setDisable(false);
anfrageBeantworten.setDisable(true);
anfrageLaden.setOnAction(event -> {
CodeTerrain codeTerrain = networkManager.loadRequest();
textArea.setText(codeTerrain.getCode());
terrainController.loadTerrainFromString(codeTerrain.getTerrain());
if (codeTerrain != null) {
textArea.setText(codeTerrain.getCode());
terrainController.loadTerrainFromString(codeTerrain.getTerrain());
anfrageLaden.setDisable(true);
anfrageBeantworten.setDisable(false);
} else {
new Alert(Alert.AlertType.ERROR, "keine Anfrage die geladen werden kann!").show();
}
});
MenuItem anfrageBeantworten = new MenuItem("Anfrage _beantworten");
anfrageBeantworten.setOnAction(event -> {
String terrain = terrainController.getXMLString();
String code = getInputText();
networkManager.addAnswer(code, terrain);
if (!networkManager.addAnswer(code, terrain))
new Alert(Alert.AlertType.ERROR, "du hast keine Anfrage geladen!").show();
else
anfrageBeantworten.setDisable(true);
anfrageLaden.setDisable(false);
});
return new Menu("T_utor", null, anfrageLaden, anfrageBeantworten);
}
private Menu buildStudentMenu() {
MenuItem anfrageSchicken = new MenuItem("Anfrage _schicken");
MenuItem anfrageHolen = new MenuItem("Anfrage _holen");
anfrageSchicken.setDisable(false);
anfrageHolen.setDisable(true);
anfrageSchicken.setOnAction(event -> {
String terrain = terrainController.getXMLString();
String code = getInputText();
networkManager.sendRequest(code, terrain);
try {
networkManager.sendRequest(code, terrain);
anfrageSchicken.setDisable(true);
anfrageHolen.setDisable(false);
} catch (NullPointerException e) {
new Alert(Alert.AlertType.ERROR, "keine Verbindung zum Tutor!").show();
}
});
MenuItem anfrageHolen = new MenuItem("Anfrage _holen");
anfrageHolen.setOnAction(event -> {
CodeTerrain codeTerrain = networkManager.getRequest();
textArea.setText(codeTerrain.getCode());
terrainController.loadTerrainFromString(codeTerrain.getTerrain());
if (codeTerrain != null) {
textArea.setText(codeTerrain.getCode());
terrainController.loadTerrainFromString(codeTerrain.getTerrain());
anfrageHolen.setDisable(true);
anfrageSchicken.setDisable(false);
} else {
new Alert(Alert.AlertType.ERROR, "du hast noch keine Antwort!").show();
}
});
return new Menu("St_udent", null, anfrageSchicken, anfrageHolen);
......@@ -318,6 +347,7 @@ public class MainViewCreationFX extends VBox implements Observer {
MenuItem drucken = new MenuItem("_Drucken");
MenuItem groesse = new MenuItem("_Größe ändern...");
groesse.setOnAction(event -> new SetMapSizeDialogBox(terrain));
//Togglemenu wird gebaut
ToggleGroup toggleGroup = new ToggleGroup();
......