...
 
Commits (2)
<?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$/classes/production/StefanHamster" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
......
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/resources" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
......@@ -70,4 +70,5 @@ public class PropertiesManager {
public String getString(String key) {
return prop.getProperty(key);
}
}
......@@ -77,6 +77,11 @@ public class TerrainController {
}
}
public String getXMLString() {
parseXML(terrain.getName());
return loadXMLToString(terrain.getName());
}
public void parseXMLAndGetTerrainName() {
String terrainName = new SetTerrainNameDialogBox().setTerrainNameDialogBox();
parseXML(terrainName);
......
package network;
import java.rmi.Remote;
import java.util.UUID;
public interface ITutor extends Remote {
CodeTerrain sendCodeTerrain(CodeTerrain codeTerrain);
UUID sendCodeTerrain(CodeTerrain codeTerrain);
CodeTerrain getAnswer(UUID id);
}
package network;
import controller.PropertiesManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
* Created by Stefan Michel on 29.01.18.
* <p>
* bestimmt ob es ein Student oder Tutor sein soll und behandelt diese
*/
public class NetworkManager {
private static NetworkManager ourInstance = new NetworkManager();
private final PropertiesManager propertiesManager = PropertiesManager.getInstance();
private Student student;
private Tutor tutor;
private NetworkManager() {
String role = propertiesManager.getString("role");
String host = propertiesManager.getString("tutorhost");
int port = Integer.parseInt(propertiesManager.getString("tutorport"));
setUpNetwork(role, host, port);
}
public static NetworkManager getInstance() {
return ourInstance;
}
// TODO: vielleicht ueber State Pattern ?
private void setUpNetwork(String role, String host, int port) {
if (role.equals("tutor")) {
setUpTutor(port);
} else {
student = new Student(host, port);
}
}
private void setUpTutor(int port) {
try {
this.tutor = new Tutor();
LocateRegistry.createRegistry(port);
Registry registry = LocateRegistry.getRegistry(port);
registry.rebind("Tutor", tutor);
} catch (RemoteException e) {
e.printStackTrace();
}
}
// fuer Studenten
public void sendRequest(String code, String terrain) {
CodeTerrain codeTerrain = new CodeTerrain(code, terrain);
student.sendRequest(codeTerrain);
}
public CodeTerrain getRequest() {
return student.getRequest();
}
// fuer Tutoren
public CodeTerrain loadRequest() {
return tutor.getNextRequest();
}
public void addAnswer(String code, String terrain) {
CodeTerrain codeTerrain = new CodeTerrain(code, terrain);
tutor.addAnswer(codeTerrain);
}
}
package network;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.UUID;
/**
* Created by Stefan Michel on 29.01.18.
*/
public class Student {
private UUID requestId;
private String host;
private int port;
private ITutor tutor;
public Student(String host, int port) {
this.host = host;
this.port = port;
setUpTutor();
}
private void setUpTutor() {
try {
Registry registry = LocateRegistry.getRegistry(host, port);
tutor = (ITutor) registry.lookup("Tutor");
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}
}
public void sendRequest(CodeTerrain codeTerrain) {
requestId = tutor.sendCodeTerrain(codeTerrain);
}
public CodeTerrain getRequest() {
return tutor.getAnswer(requestId);
}
}
......@@ -2,20 +2,41 @@ package network;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.LinkedList;
import java.util.Queue;
import java.util.*;
// vieles ist aus den Lösungen der Kommilitonen gekommen
public class Tutor extends UnicastRemoteObject implements ITutor {
private final Queue<CodeTerrain> codeTerrains = new LinkedList<>();
private final Queue<CodeTerrain> requests = new LinkedList<>();
private final Map<UUID, CodeTerrain> answerMap = new HashMap<>();
private UUID lastId;
protected Tutor() throws RemoteException {
}
// METHODS FROM INTERFACE
@Override
public UUID sendCodeTerrain(CodeTerrain codeTerrain) {
requests.add(codeTerrain);
lastId = UUID.randomUUID();
return lastId;
}
@Override
public CodeTerrain sendCodeTerrain(CodeTerrain codeTerrain) {
return null;
public CodeTerrain getAnswer(UUID id) {
return answerMap.remove(id);
}
// OWN METHODS
public CodeTerrain getNextRequest() {
return requests.poll();
}
public void addAnswer(CodeTerrain codeTerrain) {
answerMap.put(lastId, codeTerrain);
}
}
package network;
import controller.PropertiesManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class TutorManager {
private final PropertiesManager propertiesManager = PropertiesManager.getInstance();
private final int port = Integer.parseInt(propertiesManager.getString("port"));
private ITutor tutor;
private Registry registry;
public TutorManager() {
try {
this.tutor = new Tutor();
LocateRegistry.createRegistry(port);
this.registry = LocateRegistry.getRegistry(port);
this.registry.rebind("Tutor", tutor);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
......@@ -14,6 +14,8 @@ import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import model.*;
import network.CodeTerrain;
import network.NetworkManager;
import java.io.File;
import java.io.IOException;
......@@ -47,6 +49,7 @@ public class MainViewCreationFX extends VBox implements Observer {
private MenuItem start, pause, stop;
private Slider slider;
private PropertiesManager propertiesManager = PropertiesManager.getInstance();
private NetworkManager networkManager = NetworkManager.getInstance();
public MainViewCreationFX(Terrain terrain, TerrainController terrainController, IOController ioController,
HamsterController hamsterController, ThreadManager threadManager, DatabaseManager databaseManager) {
......@@ -198,14 +201,34 @@ public class MainViewCreationFX extends VBox implements Observer {
private Menu buildTutorMenu() {
MenuItem anfrageLaden = new MenuItem("Anfrage _laden");
anfrageLaden.setOnAction(event -> {
CodeTerrain codeTerrain = networkManager.getRequest();
textArea.setText(codeTerrain.getCode());
terrainController.loadTerrainFromString(codeTerrain.getTerrain());
});
MenuItem anfrageBeantworten = new MenuItem("Anfrage _beantworten");
anfrageBeantworten.setOnAction(event -> {
String terrain = terrainController.getXMLString();
String code = getInputText();
networkManager.addAnswer(code, terrain);
});
return new Menu("T_utor", null, anfrageLaden, anfrageBeantworten);
}
private Menu buildStudentMenu() {
MenuItem anfrageSchicken = new MenuItem("Anfrage _schicken");
anfrageSchicken.setOnAction(event -> {
String terrain = terrainController.getXMLString();
String code = getInputText();
networkManager.sendRequest(code, terrain);
});
MenuItem anfrageHolen = new MenuItem("Anfrage _holen");
anfrageHolen.setOnAction(event -> {
CodeTerrain codeTerrain = networkManager.getRequest();
textArea.setText(codeTerrain.getCode());
terrainController.loadTerrainFromString(codeTerrain.getTerrain());
});
return new Menu("St_udent", null, anfrageSchicken, anfrageHolen);
}
......