#6 configure MailboxServer to register its address with the Nameserver
This commit is contained in:
parent
d12dc78ee1
commit
cd926f61b9
@ -3,7 +3,13 @@ package dslab.mailbox;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
import java.net.InetAddress;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.rmi.NotBoundException;
|
||||||
|
import java.rmi.RemoteException;
|
||||||
|
import java.rmi.registry.LocateRegistry;
|
||||||
|
import java.rmi.registry.Registry;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
@ -15,6 +21,9 @@ import at.ac.tuwien.dsg.orvell.annotation.Command;
|
|||||||
import dslab.ComponentFactory;
|
import dslab.ComponentFactory;
|
||||||
import dslab.Email;
|
import dslab.Email;
|
||||||
import dslab.Message;
|
import dslab.Message;
|
||||||
|
import dslab.nameserver.AlreadyRegisteredException;
|
||||||
|
import dslab.nameserver.INameserverRemote;
|
||||||
|
import dslab.nameserver.InvalidDomainException;
|
||||||
import dslab.util.Config;
|
import dslab.util.Config;
|
||||||
|
|
||||||
public class MailboxServer implements IMailboxServer, Runnable {
|
public class MailboxServer implements IMailboxServer, Runnable {
|
||||||
@ -31,6 +40,9 @@ public class MailboxServer implements IMailboxServer, Runnable {
|
|||||||
private final ConcurrentHashMap<Email, LinkedList<Message>> messageStorage = new ConcurrentHashMap<>();
|
private final ConcurrentHashMap<Email, LinkedList<Message>> messageStorage = new ConcurrentHashMap<>();
|
||||||
private final ConcurrentHashMap<String, String> userStorage = new ConcurrentHashMap<>();
|
private final ConcurrentHashMap<String, String> userStorage = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private Registry registry;
|
||||||
|
private INameserverRemote nameserverRemote;
|
||||||
|
|
||||||
public static volatile Integer id;
|
public static volatile Integer id;
|
||||||
|
|
||||||
|
|
||||||
@ -65,6 +77,19 @@ public class MailboxServer implements IMailboxServer, Runnable {
|
|||||||
this.dmtpServerPort = config.getInt("dmtp.tcp.port");
|
this.dmtpServerPort = config.getInt("dmtp.tcp.port");
|
||||||
this.dmapServerPort = config.getInt("dmap.tcp.port");
|
this.dmapServerPort = config.getInt("dmap.tcp.port");
|
||||||
logger.setLevel(Level.ALL);
|
logger.setLevel(Level.ALL);
|
||||||
|
|
||||||
|
// registry
|
||||||
|
String registryName = config.getString("root_id");
|
||||||
|
String registryHost = config.getString("registry.host");
|
||||||
|
int registryPort = config.getInt("registry.port");
|
||||||
|
try {
|
||||||
|
registry = LocateRegistry.getRegistry(registryHost, registryPort);
|
||||||
|
nameserverRemote = (INameserverRemote) registry.lookup(registryName);
|
||||||
|
String localIp = InetAddress.getLocalHost().getHostAddress();
|
||||||
|
nameserverRemote.registerMailboxServer(domain, localIp + ":" + this.dmtpServerPort);
|
||||||
|
} catch (RemoteException | NotBoundException | InvalidDomainException | AlreadyRegisteredException | UnknownHostException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -5,21 +5,34 @@ import java.rmi.RemoteException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class NameServerRemote implements INameserverRemote, Serializable {
|
public class NameServerRemote implements INameserverRemote, Serializable {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private ConcurrentHashMap<String, INameserverRemote> subdomains;
|
private ConcurrentHashMap<String, INameserverRemote> subdomains;
|
||||||
|
private ConcurrentHashMap<String, String> addresses;
|
||||||
|
|
||||||
public NameServerRemote(String name) {
|
public NameServerRemote(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.subdomains = new ConcurrentHashMap<>();
|
this.subdomains = new ConcurrentHashMap<>();
|
||||||
|
this.addresses = new ConcurrentHashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getNameservers() {
|
public List<String> getNameservers() {
|
||||||
return new ArrayList<>(subdomains.keySet());
|
return new ArrayList<>(subdomains.keySet());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getAddresses() {
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
AtomicInteger count = new AtomicInteger(0);
|
||||||
|
addresses.forEach((x, y) -> stringBuilder
|
||||||
|
.append(count.incrementAndGet()).append(".").append(" ")
|
||||||
|
.append(x).append(" ")
|
||||||
|
.append(y).append("\n"));
|
||||||
|
return stringBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
@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);
|
||||||
@ -40,7 +53,16 @@ public class NameServerRemote implements INameserverRemote, Serializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerMailboxServer(String domain, String address) throws RemoteException, AlreadyRegisteredException, InvalidDomainException {
|
public void registerMailboxServer(String domain, String address) throws RemoteException, AlreadyRegisteredException, InvalidDomainException {
|
||||||
|
if (!domain.contains(".")) {
|
||||||
|
addresses.putIfAbsent(domain, address);
|
||||||
|
} else {
|
||||||
|
String[] domainParts = domain.split("\\.");
|
||||||
|
String topDomain = domainParts[domainParts.length - 1];
|
||||||
|
INameserverRemote topDomainNameServer = subdomains.get(topDomain);
|
||||||
|
String subdomainsString = domain.substring(0, domain.length() - topDomain.length() - 1);
|
||||||
|
topDomainNameServer.registerMailboxServer(subdomainsString, address);
|
||||||
|
addresses.forEach((k, v) -> System.out.println(k + " " + v));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -74,6 +74,7 @@ public class Nameserver implements INameserver {
|
|||||||
|
|
||||||
shell = new Shell(in, out)
|
shell = new Shell(in, out)
|
||||||
.register("nameservers", ((input, context) -> this.nameservers()))
|
.register("nameservers", ((input, context) -> this.nameservers()))
|
||||||
|
.register("addresses", ((input, context) -> this.addresses()))
|
||||||
.register("shutdown", (((input, context) -> {
|
.register("shutdown", (((input, context) -> {
|
||||||
System.out.println("shutting down...");
|
System.out.println("shutting down...");
|
||||||
this.shutdown();
|
this.shutdown();
|
||||||
@ -95,7 +96,7 @@ public class Nameserver implements INameserver {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addresses() {
|
public void addresses() {
|
||||||
// TODO
|
this.out.println(nameServerLocal.getAddresses());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user