86 lines
2.8 KiB
Java
86 lines
2.8 KiB
Java
package dslab.nameserver;
|
|
|
|
import java.io.InputStream;
|
|
import java.io.PrintStream;
|
|
import java.rmi.*;
|
|
import java.rmi.registry.LocateRegistry;
|
|
import java.rmi.registry.Registry;
|
|
import java.rmi.server.UnicastRemoteObject;
|
|
|
|
import dslab.ComponentFactory;
|
|
import dslab.util.Config;
|
|
|
|
public class Nameserver implements INameserver {
|
|
|
|
static Registry REGISTRY;
|
|
|
|
/**
|
|
* Creates a new server instance.
|
|
*
|
|
* @param componentId the id of the component that corresponds to the Config resource
|
|
* @param config the component config
|
|
* @param in the input stream to read console input from
|
|
* @param out the output stream to write console output to
|
|
*/
|
|
public Nameserver(String componentId, Config config, InputStream in, PrintStream out) {
|
|
// only root nameserver creates RMI registry
|
|
String registryName = config.getString("root_id");
|
|
String registryHost = config.getString("registry.host");
|
|
int registryPort = config.getInt("registry.port");
|
|
if (componentId.equals("ns-root")) {
|
|
// Root Nameserver
|
|
INameserverRemote nameserverRemote = new NameServerRemote("Root Nameserver");
|
|
try {
|
|
INameserverRemote stub = (INameserverRemote) UnicastRemoteObject.exportObject(nameserverRemote, registryPort);
|
|
REGISTRY = LocateRegistry.createRegistry(registryPort);
|
|
REGISTRY.rebind(registryName, stub);
|
|
System.out.println("Nameserver bound.");
|
|
} catch (RemoteException e) {
|
|
System.err.println("Nameserver exception:");
|
|
e.printStackTrace();
|
|
}
|
|
} else {
|
|
// Zone Nameserver
|
|
String domain = config.getString("domain");
|
|
try {
|
|
REGISTRY = LocateRegistry.getRegistry(registryHost, registryPort);
|
|
INameserverRemote nameserverRemote = (INameserverRemote) REGISTRY.lookup(registryName);
|
|
nameserverRemote.registerNameserver(domain, new NameServerRemote(domain));
|
|
} catch (RemoteException | NotBoundException e) {
|
|
e.printStackTrace();
|
|
} catch (InvalidDomainException e) {
|
|
e.printStackTrace();
|
|
} catch (AlreadyRegisteredException e) {
|
|
e.printStackTrace();
|
|
}
|
|
System.out.printf("Zone Nameserver %s started!%n", domain);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
// TODO
|
|
}
|
|
|
|
@Override
|
|
public void nameservers() {
|
|
// TODO
|
|
}
|
|
|
|
@Override
|
|
public void addresses() {
|
|
// TODO
|
|
}
|
|
|
|
@Override
|
|
public void shutdown() {
|
|
// TODO
|
|
}
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
INameserver component = ComponentFactory.createNameserver(args[0], System.in, System.out);
|
|
component.run();
|
|
}
|
|
|
|
}
|