#5 implement addresses Nameserver CLI call

This commit is contained in:
Rasha Djurdjevic 2021-01-06 05:28:26 +01:00
parent 5210a3f931
commit d12dc78ee1
2 changed files with 22 additions and 8 deletions

View File

@ -2,7 +2,8 @@ package dslab.nameserver;
import java.io.Serializable; import java.io.Serializable;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.Arrays; import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class NameServerRemote implements INameserverRemote, Serializable { public class NameServerRemote implements INameserverRemote, Serializable {
@ -15,6 +16,10 @@ public class NameServerRemote implements INameserverRemote, Serializable {
this.subdomains = new ConcurrentHashMap<>(); this.subdomains = new ConcurrentHashMap<>();
} }
public List<String> getNameservers() {
return new ArrayList<>(subdomains.keySet());
}
@Override @Override
public void registerNameserver(String domain, INameserverRemote nameserver) throws RemoteException, AlreadyRegisteredException, InvalidDomainException { public void registerNameserver(String domain, INameserverRemote nameserver) throws RemoteException, AlreadyRegisteredException, InvalidDomainException {
// System.out.printf("Registering Nameserver %s%n", domain); // System.out.printf("Registering Nameserver %s%n", domain);

View File

@ -6,6 +6,8 @@ import java.rmi.*;
import java.rmi.registry.LocateRegistry; 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 java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import at.ac.tuwien.dsg.orvell.Shell; import at.ac.tuwien.dsg.orvell.Shell;
import at.ac.tuwien.dsg.orvell.StopShellException; import at.ac.tuwien.dsg.orvell.StopShellException;
@ -17,9 +19,11 @@ public class Nameserver implements INameserver {
static Registry REGISTRY; static Registry REGISTRY;
private String componentId; private String componentId;
private PrintStream out;
private String registryName; private String registryName;
private INameserverRemote nameserverRemote; private INameserverRemote nameserverRemote;
private NameServerRemote nameServerLocal;
private Shell shell; private Shell shell;
@ -33,15 +37,16 @@ public class Nameserver implements INameserver {
*/ */
public Nameserver(String componentId, Config config, InputStream in, PrintStream out) { public Nameserver(String componentId, Config config, InputStream in, PrintStream out) {
this.componentId = componentId; this.componentId = componentId;
this.out = out;
// only root nameserver creates RMI registry // only root nameserver creates RMI registry
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 (this.componentId.equals("ns-root")) { if (this.componentId.equals("ns-root")) {
// Root Nameserver // Root Nameserver
nameserverRemote = new NameServerRemote("Root Nameserver"); nameServerLocal = new NameServerRemote("Root Nameserver");
try { try {
INameserverRemote stub = (INameserverRemote) UnicastRemoteObject.exportObject(nameserverRemote, registryPort); INameserverRemote stub = (INameserverRemote) UnicastRemoteObject.exportObject(nameServerLocal, registryPort);
REGISTRY = LocateRegistry.createRegistry(registryPort); REGISTRY = LocateRegistry.createRegistry(registryPort);
REGISTRY.rebind(registryName, stub); REGISTRY.rebind(registryName, stub);
System.out.println("Nameserver bound."); System.out.println("Nameserver bound.");
@ -52,10 +57,11 @@ public class Nameserver implements INameserver {
} else { } else {
// Zone Nameserver // Zone Nameserver
String domain = config.getString("domain"); String domain = config.getString("domain");
nameServerLocal = new NameServerRemote(domain);
try { try {
REGISTRY = LocateRegistry.getRegistry(registryHost, registryPort); REGISTRY = LocateRegistry.getRegistry(registryHost, registryPort);
nameserverRemote = (INameserverRemote) REGISTRY.lookup(registryName); nameserverRemote = (INameserverRemote) REGISTRY.lookup(registryName);
nameserverRemote.registerNameserver(domain, new NameServerRemote(domain)); nameserverRemote.registerNameserver(domain, nameServerLocal);
} catch (RemoteException | NotBoundException e) { } catch (RemoteException | NotBoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (InvalidDomainException e) { } catch (InvalidDomainException e) {
@ -67,6 +73,7 @@ public class Nameserver implements INameserver {
} }
shell = new Shell(in, out) shell = new Shell(in, out)
.register("nameservers", ((input, context) -> this.nameservers()))
.register("shutdown", (((input, context) -> { .register("shutdown", (((input, context) -> {
System.out.println("shutting down..."); System.out.println("shutting down...");
this.shutdown(); this.shutdown();
@ -81,7 +88,9 @@ public class Nameserver implements INameserver {
@Override @Override
public void nameservers() { public void nameservers() {
// TODO List<String> nameservers = nameServerLocal.getNameservers();
AtomicInteger count = new AtomicInteger(0);
nameservers.forEach(nameserver -> out.printf("%d. %s%n", count.incrementAndGet(), nameserver));
} }
@Override @Override
@ -96,7 +105,7 @@ public class Nameserver implements INameserver {
if (this.componentId.equals("ns-root")) { if (this.componentId.equals("ns-root")) {
try { try {
REGISTRY.unbind(registryName); REGISTRY.unbind(registryName);
UnicastRemoteObject.unexportObject(nameserverRemote, true); UnicastRemoteObject.unexportObject(nameServerLocal, true);
} catch (RemoteException e) { } catch (RemoteException e) {
e.printStackTrace(); e.printStackTrace();
} catch (NotBoundException e) { } catch (NotBoundException e) {