#5 implement addresses Nameserver CLI call
This commit is contained in:
parent
5210a3f931
commit
d12dc78ee1
@ -2,7 +2,8 @@ package dslab.nameserver;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.rmi.RemoteException;
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class NameServerRemote implements INameserverRemote, Serializable {
|
||||
@ -15,6 +16,10 @@ public class NameServerRemote implements INameserverRemote, Serializable {
|
||||
this.subdomains = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
public List<String> getNameservers() {
|
||||
return new ArrayList<>(subdomains.keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerNameserver(String domain, INameserverRemote nameserver) throws RemoteException, AlreadyRegisteredException, InvalidDomainException {
|
||||
// System.out.printf("Registering Nameserver %s%n", domain);
|
||||
|
||||
@ -6,6 +6,8 @@ import java.rmi.*;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
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.StopShellException;
|
||||
@ -17,9 +19,11 @@ public class Nameserver implements INameserver {
|
||||
static Registry REGISTRY;
|
||||
|
||||
private String componentId;
|
||||
private PrintStream out;
|
||||
|
||||
private String registryName;
|
||||
private INameserverRemote nameserverRemote;
|
||||
private NameServerRemote nameServerLocal;
|
||||
|
||||
private Shell shell;
|
||||
|
||||
@ -33,15 +37,16 @@ public class Nameserver implements INameserver {
|
||||
*/
|
||||
public Nameserver(String componentId, Config config, InputStream in, PrintStream out) {
|
||||
this.componentId = componentId;
|
||||
this.out = out;
|
||||
// only root nameserver creates RMI registry
|
||||
registryName = config.getString("root_id");
|
||||
String registryHost = config.getString("registry.host");
|
||||
int registryPort = config.getInt("registry.port");
|
||||
if (this.componentId.equals("ns-root")) {
|
||||
// Root Nameserver
|
||||
nameserverRemote = new NameServerRemote("Root Nameserver");
|
||||
nameServerLocal = new NameServerRemote("Root Nameserver");
|
||||
try {
|
||||
INameserverRemote stub = (INameserverRemote) UnicastRemoteObject.exportObject(nameserverRemote, registryPort);
|
||||
INameserverRemote stub = (INameserverRemote) UnicastRemoteObject.exportObject(nameServerLocal, registryPort);
|
||||
REGISTRY = LocateRegistry.createRegistry(registryPort);
|
||||
REGISTRY.rebind(registryName, stub);
|
||||
System.out.println("Nameserver bound.");
|
||||
@ -52,10 +57,11 @@ public class Nameserver implements INameserver {
|
||||
} else {
|
||||
// Zone Nameserver
|
||||
String domain = config.getString("domain");
|
||||
nameServerLocal = new NameServerRemote(domain);
|
||||
try {
|
||||
REGISTRY = LocateRegistry.getRegistry(registryHost, registryPort);
|
||||
nameserverRemote = (INameserverRemote) REGISTRY.lookup(registryName);
|
||||
nameserverRemote.registerNameserver(domain, new NameServerRemote(domain));
|
||||
nameserverRemote.registerNameserver(domain, nameServerLocal);
|
||||
} catch (RemoteException | NotBoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvalidDomainException e) {
|
||||
@ -67,6 +73,7 @@ public class Nameserver implements INameserver {
|
||||
}
|
||||
|
||||
shell = new Shell(in, out)
|
||||
.register("nameservers", ((input, context) -> this.nameservers()))
|
||||
.register("shutdown", (((input, context) -> {
|
||||
System.out.println("shutting down...");
|
||||
this.shutdown();
|
||||
@ -81,7 +88,9 @@ public class Nameserver implements INameserver {
|
||||
|
||||
@Override
|
||||
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
|
||||
@ -96,7 +105,7 @@ public class Nameserver implements INameserver {
|
||||
if (this.componentId.equals("ns-root")) {
|
||||
try {
|
||||
REGISTRY.unbind(registryName);
|
||||
UnicastRemoteObject.unexportObject(nameserverRemote, true);
|
||||
UnicastRemoteObject.unexportObject(nameServerLocal, true);
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NotBoundException e) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user