#5 #8 set up Shell, run it in a separate thread and implement shutdown CLI call

This commit is contained in:
Rasha Djurdjevic 2021-01-06 04:40:15 +01:00
parent 6b4dfcd966
commit 5210a3f931

View File

@ -7,6 +7,8 @@ import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry; import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject; import java.rmi.server.UnicastRemoteObject;
import at.ac.tuwien.dsg.orvell.Shell;
import at.ac.tuwien.dsg.orvell.StopShellException;
import dslab.ComponentFactory; import dslab.ComponentFactory;
import dslab.util.Config; import dslab.util.Config;
@ -14,6 +16,13 @@ public class Nameserver implements INameserver {
static Registry REGISTRY; static Registry REGISTRY;
private String componentId;
private String registryName;
private INameserverRemote nameserverRemote;
private Shell shell;
/** /**
* Creates a new server instance. * Creates a new server instance.
* *
@ -23,13 +32,14 @@ public class Nameserver implements INameserver {
* @param out the output stream to write console output to * @param out the output stream to write console output to
*/ */
public Nameserver(String componentId, Config config, InputStream in, PrintStream out) { public Nameserver(String componentId, Config config, InputStream in, PrintStream out) {
this.componentId = componentId;
// only root nameserver creates RMI registry // only root nameserver creates RMI registry
String registryName = config.getString("root_id"); registryName = config.getString("root_id");
String registryHost = config.getString("registry.host"); String registryHost = config.getString("registry.host");
int registryPort = config.getInt("registry.port"); int registryPort = config.getInt("registry.port");
if (componentId.equals("ns-root")) { if (this.componentId.equals("ns-root")) {
// Root Nameserver // Root Nameserver
INameserverRemote nameserverRemote = new NameServerRemote("Root Nameserver"); nameserverRemote = new NameServerRemote("Root Nameserver");
try { try {
INameserverRemote stub = (INameserverRemote) UnicastRemoteObject.exportObject(nameserverRemote, registryPort); INameserverRemote stub = (INameserverRemote) UnicastRemoteObject.exportObject(nameserverRemote, registryPort);
REGISTRY = LocateRegistry.createRegistry(registryPort); REGISTRY = LocateRegistry.createRegistry(registryPort);
@ -44,7 +54,7 @@ public class Nameserver implements INameserver {
String domain = config.getString("domain"); String domain = config.getString("domain");
try { try {
REGISTRY = LocateRegistry.getRegistry(registryHost, registryPort); REGISTRY = LocateRegistry.getRegistry(registryHost, registryPort);
INameserverRemote nameserverRemote = (INameserverRemote) REGISTRY.lookup(registryName); nameserverRemote = (INameserverRemote) REGISTRY.lookup(registryName);
nameserverRemote.registerNameserver(domain, new NameServerRemote(domain)); nameserverRemote.registerNameserver(domain, new NameServerRemote(domain));
} catch (RemoteException | NotBoundException e) { } catch (RemoteException | NotBoundException e) {
e.printStackTrace(); e.printStackTrace();
@ -55,11 +65,18 @@ public class Nameserver implements INameserver {
} }
System.out.printf("Zone Nameserver %s started!%n", domain); System.out.printf("Zone Nameserver %s started!%n", domain);
} }
shell = new Shell(in, out)
.register("shutdown", (((input, context) -> {
System.out.println("shutting down...");
this.shutdown();
throw new StopShellException();
})));
} }
@Override @Override
public void run() { public void run() {
// TODO shell.run();
} }
@Override @Override
@ -74,7 +91,21 @@ public class Nameserver implements INameserver {
@Override @Override
public void shutdown() { public void shutdown() {
// TODO // shell is already interrupted
// so just close other things
if (this.componentId.equals("ns-root")) {
try {
REGISTRY.unbind(registryName);
UnicastRemoteObject.unexportObject(nameserverRemote, true);
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}
} else {
System.out.println("Zone Nameserver...");
}
System.out.println("Nameserver shut down successfully!");
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {