Fix bug where two recipients do not both receive the message

This commit is contained in:
Tobias Eidelpes 2020-11-18 11:34:52 +01:00
parent 71fa4f6267
commit 8759834955
3 changed files with 28 additions and 13 deletions

View File

@ -22,11 +22,16 @@ public class DMTPConnection implements Runnable {
private Message msg = new Message();
private final ConcurrentHashMap<Email, LinkedList<Message>> messageStorage;
private final String domain;
public DMTPConnection(Socket connection, ConcurrentHashMap<Email, LinkedList<Message>> messageStorage) {
private final ConcurrentHashMap<Email, LinkedList<Message>> messageStorage;
private final ConcurrentHashMap<String, String> userStorage;
public DMTPConnection(Socket connection, ConcurrentHashMap<Email, LinkedList<Message>> messageStorage, ConcurrentHashMap<String, String> userStorage, String domain) {
this.socket = connection;
this.messageStorage = messageStorage;
this.userStorage = userStorage;
this.domain = domain;
}
@Override
@ -69,16 +74,22 @@ public class DMTPConnection implements Runnable {
int count = 0;
try {
for (String emailAddress : emailAddresses) {
logger.info("Current email address in msg.getTo(): " + emailAddress);
Email add = new Email(emailAddress);
if (!this.messageStorage.containsKey(add)) {
throw new UnknownRecipientException("error unknown recipient " + add.toString());
if (this.domain.equals(add.getDomain())) {
logger.info("Address " + emailAddress + " belongs to this domain " + this.domain);
if (!this.userStorage.containsKey(add.getUsername())) {
logger.info("Our userStorage in domain " + this.domain + " does not contain user " + add.getUsername());
out.println("error unknown recipient " + add.toString());
} else {
logger.info("Address " + add.toString() + " belongs to this domain and user exists. Adding address to msg.To() field");
msg.addTo(add);
count++;
}
}
}
out.println("ok " + count);
} catch (MalformedInputException | UnknownRecipientException e) {
} catch (MalformedInputException e) {
out.println(e.getMessage());
}
} else if ("from".equals(userInput.split("\\s+")[0])) {

View File

@ -21,11 +21,15 @@ public class DMTPListener extends Thread {
private final Logger logger = Logger.getLogger(DMTPListener.class.getName());
private final ArrayList<DMTPConnection> clients = new ArrayList<>();
private final ExecutorService executorService = Executors.newCachedThreadPool();
private final ConcurrentHashMap<Email, LinkedList<Message>> storage;
private final ConcurrentHashMap<Email, LinkedList<Message>> messageStorage;
private final ConcurrentHashMap<String, String> userStorage;
private final String domain;
public DMTPListener(ServerSocket serverSocket, ConcurrentHashMap<Email, LinkedList<Message>> storage) {
public DMTPListener(ServerSocket serverSocket, ConcurrentHashMap<Email, LinkedList<Message>> storage, ConcurrentHashMap<String, String> userStorage, String domain) {
this.serverSocket = serverSocket;
this.storage = storage;
this.messageStorage = storage;
this.userStorage = userStorage;
this.domain = domain;
}
@Override
@ -35,7 +39,7 @@ public class DMTPListener extends Thread {
try {
Socket s = serverSocket.accept();
logger.fine("Processing incoming socket " + s.toString());
DMTPConnection dmtpConnection = new DMTPConnection(s, storage);
DMTPConnection dmtpConnection = new DMTPConnection(s, messageStorage, userStorage, domain);
clients.add(dmtpConnection);
executorService.submit(dmtpConnection);
} catch (InterruptedIOException | SocketException e) {

View File

@ -73,7 +73,7 @@ public class MailboxServer implements IMailboxServer, Runnable {
e.printStackTrace();
shutdown();
}
this.dmtpListener = new DMTPListener(this.dmtpServerSocket, this.messageStorage);
this.dmtpListener = new DMTPListener(this.dmtpServerSocket, this.messageStorage, this.userStorage, this.domain);
this.dmtpListener.start();
this.dmapListener = new DMAPListener(this.dmapServerSocket, this.messageStorage, this.userStorage);
this.dmapListener.start();