#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.server.UnicastRemoteObject;
import at.ac.tuwien.dsg.orvell.Shell;
import at.ac.tuwien.dsg.orvell.StopShellException;
import dslab.ComponentFactory;
import dslab.util.Config;
@ -14,6 +16,13 @@ public class Nameserver implements INameserver {
static Registry REGISTRY;
private String componentId;
private String registryName;
private INameserverRemote nameserverRemote;
private Shell shell;
/**
* Creates a new server instance.
*
@ -23,13 +32,14 @@ public class Nameserver implements INameserver {
* @param out the output stream to write console output to
*/
public Nameserver(String componentId, Config config, InputStream in, PrintStream out) {
this.componentId = componentId;
// only root nameserver creates RMI registry
String registryName = config.getString("root_id");
registryName = config.getString("root_id");
String registryHost = config.getString("registry.host");
int registryPort = config.getInt("registry.port");
if (componentId.equals("ns-root")) {
if (this.componentId.equals("ns-root")) {
// Root Nameserver
INameserverRemote nameserverRemote = new NameServerRemote("Root Nameserver");
nameserverRemote = new NameServerRemote("Root Nameserver");
try {
INameserverRemote stub = (INameserverRemote) UnicastRemoteObject.exportObject(nameserverRemote, registryPort);
REGISTRY = LocateRegistry.createRegistry(registryPort);
@ -44,7 +54,7 @@ public class Nameserver implements INameserver {
String domain = config.getString("domain");
try {
REGISTRY = LocateRegistry.getRegistry(registryHost, registryPort);
INameserverRemote nameserverRemote = (INameserverRemote) REGISTRY.lookup(registryName);
nameserverRemote = (INameserverRemote) REGISTRY.lookup(registryName);
nameserverRemote.registerNameserver(domain, new NameServerRemote(domain));
} catch (RemoteException | NotBoundException e) {
e.printStackTrace();
@ -55,11 +65,18 @@ public class Nameserver implements INameserver {
}
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
public void run() {
// TODO
shell.run();
}
@Override
@ -74,7 +91,21 @@ public class Nameserver implements INameserver {
@Override
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 {