Commit b4cc1f26 authored by Stefan Michel's avatar Stefan Michel

SetMapSizeDialogBox fertiggestellt, anbindung an Model fehlt noch

parent 1d78af2c
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("host");
int port = Integer.parseInt(propertiesManager.getString("port"));
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();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment