Merge branch 'develop' into f/refactored_package

This commit is contained in:
delvh 2020-06-28 09:54:45 +02:00 committed by GitHub
commit 4270532ffc
3 changed files with 64 additions and 40 deletions

View File

@ -1,2 +1,3 @@
# envoy-server-standalone
Standalone server for the Envoy project
Standalone server for the
<a href="https://github.com/informatik-ag-ngl/envoy-client"><img src="https://raw.githubusercontent.com/informatik-ag-ngl/envoy-client/develop/src/main/resources/icons/envoy_logo.png" align="left" width="200" height="200">Envoy project</a>

View File

@ -20,6 +20,7 @@ import envoy.server.data.User;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;
import envoy.server.util.VersionUtils;
import envoy.util.Bounds;
import envoy.util.EnvoyLog;
/**
@ -38,7 +39,7 @@ public final class LoginCredentialProcessor implements ObjectProcessor<LoginCred
private final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final Logger logger = EnvoyLog.getLogger(LoginCredentialProcessor.class);
private static final Logger logger = EnvoyLog.getLogger(LoginCredentialProcessor.class);
@Override
public void process(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException {
@ -76,25 +77,31 @@ public final class LoginCredentialProcessor implements ObjectProcessor<LoginCred
return;
}
} else {
try {
// Checking that no user already has this identifier
PersistenceManager.getInstance().getUserByName(credentials.getIdentifier());
// This code only gets executed if this user already exists
logger.info("The requested user already exists.");
writeProxy.write(socketID, new HandshakeRejection(USERNAME_TAKEN));
// Validate user name
if (!Bounds.isValidContactName(credentials.getIdentifier())) {
logger.info("The requested user name is not valid.");
writeProxy.write(socketID, new HandshakeRejection(INTERNAL_ERROR));
return;
} catch (NoResultException e) {
// Creation of a new user
user = new User();
user.setName(credentials.getIdentifier());
user.setLastSeen(LocalDateTime.now());
user.setStatus(ONLINE);
user.setPasswordHash(credentials.getPasswordHash());
user.setContacts(new HashSet<>());
persistenceManager.addContact(user);
logger.info("Registered new " + user);
}
try {
// Checking that no user already has this identifier
PersistenceManager.getInstance().getUserByName(credentials.getIdentifier());
// This code only gets executed if this user already exists
logger.info("The requested user already exists.");
writeProxy.write(socketID, new HandshakeRejection(USERNAME_TAKEN));
return;
} catch (NoResultException e) {
// Creation of a new user
user = new User();
user.setName(credentials.getIdentifier());
user.setLastSeen(LocalDateTime.now());
user.setStatus(ONLINE);
user.setPasswordHash(credentials.getPasswordHash());
user.setContacts(new HashSet<>());
persistenceManager.addContact(user);
logger.info("Registered new " + user);
}
}
logger.info(user + " successfully authenticated.");
@ -109,14 +116,21 @@ public final class LoginCredentialProcessor implements ObjectProcessor<LoginCred
final var pendingMessages = PersistenceManager.getInstance().getPendingMessages(user);
logger.fine("Sending " + pendingMessages.size() + " pending messages to " + user + "...");
for (var msg : pendingMessages) {
final var msgCommon = msg.toCommon();
if (msg.getStatus() == MessageStatus.SENT) {
// Send the message
writeProxy.write(socketID, msgCommon);
msg.received();
if (connectionManager.isOnline(msg.getSender().getID()))
writeProxy.write(connectionManager.getSocketID(msg.getSender().getID()), new MessageStatusChange(msgCommon));
PersistenceManager.getInstance().updateMessage(msg);
// Notify the sender about the delivery
if (connectionManager.isOnline(msg.getSender().getID())) {
msgCommon.nextStatus();
writeProxy.write(connectionManager.getSocketID(msg.getSender().getID()), new MessageStatusChange(msgCommon));
}
} else writeProxy.write(socketID, new MessageStatusChange(msgCommon));
}
}

View File

@ -1,21 +1,22 @@
package envoy.server.processors;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityExistsException;
import envoy.data.Message;
import envoy.data.Message.MessageStatus;
import envoy.event.MessageStatusChange;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;
import envoy.util.EnvoyLog;
/**
* This {@link ObjectProcessor} handles incoming {@link Message}s.<br>
* <br>
* This {@link ObjectProcessor} handles incoming {@link Message}s.
* <p>
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>MessageProcessor.java</strong><br>
* Created: <strong>30.12.2019</strong><br>
@ -26,6 +27,8 @@ import envoy.util.EnvoyLog;
*/
public class MessageProcessor implements ObjectProcessor<Message> {
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final Logger logger = EnvoyLog.getLogger(MessageProcessor.class);
@Override
@ -35,26 +38,32 @@ public class MessageProcessor implements ObjectProcessor<Message> {
return;
}
message.nextStatus();
ConnectionManager connectionManager = ConnectionManager.getInstance();
sendToUser(connectionManager, message, writeProxy);
// Convert to server message
final var serverMessage = new envoy.server.data.Message(message);
try {
PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message));
} catch (EntityExistsException e) {
logger.warning("Received a message with an id that already exists");
}
}
private void sendToUser(ConnectionManager connectionManager, Message message, ObjectWriteProxy writeProxy) {
if (connectionManager.isOnline(message.getRecipientID())) try {
// If recipient is online, send the message directly
writeProxy.write(connectionManager.getSocketID(message.getRecipientID()), message);
// Update the message status to RECEIVED
message.setReceivedDate(LocalDateTime.now());
message.nextStatus();
// Persist the message
persistenceManager.addMessage(serverMessage);
// Send the message to the recipient if online
if (connectionManager.isOnline(message.getRecipientID())) {
writeProxy.write(connectionManager.getSocketID(message.getRecipientID()), message);
// Increment status
message.nextStatus();
serverMessage.received();
persistenceManager.updateMessage(serverMessage);
// Notify the sender about the delivery
// Note that the exact time stamp might differ slightly
writeProxy.write(socketID, new MessageStatusChange(message));
}
} catch (EntityExistsException e) {
logger.log(Level.WARNING, "Received " + message + " with an ID that already exists!");
} catch (IOException e) {
logger.warning("Recipient online. Failed to send message" + message.getID());
e.printStackTrace();
logger.log(Level.WARNING, "Failed to deliver " + message + ":", e);
}
}