From d0cf1c73f2b0d1e645d38b7ad87cf78702c1fb38 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 1 Feb 2020 23:37:44 +0100 Subject: [PATCH] Added UserStatusChangeProcessor additionally added UserStatus updating in the LoginCredentialProcessor yet missing: method to handle clients going offline --- pom.xml | 2 +- .../java/envoy/server/ConnectionManager.java | 2 +- src/main/java/envoy/server/Startup.java | 1 + .../server/database/PersistenceManager.java | 13 ++++ .../server/net/ObjectMessageProcessor.java | 1 - .../envoy/server/net/ObjectWriteProxy.java | 2 +- .../processors/LoginCredentialProcessor.java | 16 ++-- .../processors/UserStatusChangeProcessor.java | 78 +++++++++++++++++++ 8 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 src/main/java/envoy/server/processors/UserStatusChangeProcessor.java diff --git a/pom.xml b/pom.xml index db5b8da..0b85902 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - develop-SNAPSHOT + f~user_status_change_event-SNAPSHOT com.github.informatik-ag-ngl diff --git a/src/main/java/envoy/server/ConnectionManager.java b/src/main/java/envoy/server/ConnectionManager.java index 202334f..ffc64e1 100644 --- a/src/main/java/envoy/server/ConnectionManager.java +++ b/src/main/java/envoy/server/ConnectionManager.java @@ -64,7 +64,7 @@ public class ConnectionManager implements ISocketIdListener { } /** - * @param userId the ID of the user registered at the a socket + * @param userId the ID of the user registered at a socket * @return the ID of the socket * @since Envoy Server Standalone v0.1-alpha */ diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index 097399e..82d9915 100644 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -36,6 +36,7 @@ public class Startup { processors.add(new LoginCredentialProcessor()); processors.add(new MessageProcessor()); processors.add(new MessageStatusChangeProcessor()); + processors.add(new UserStatusChangeProcessor()); processors.add(new IdGeneratorRequestProcessor()); Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors)); diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index 3ec686a..614a641 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -5,6 +5,7 @@ import java.util.List; import javax.persistence.EntityManager; import javax.persistence.Persistence; +import envoy.data.User.UserStatus; import envoy.server.data.ConfigItem; import envoy.server.data.Message; import envoy.server.data.User; @@ -164,4 +165,16 @@ public class PersistenceManager { // TODO current solution gets all users, not just contacts. Should be changed to // entityManager.createNamedQuery("getContactsOfUser").setParameter("user", // user).getResultList(); + + /** + * Updates the {@link UserStatus} in the database. + * + * @param user the {@link User} who changes his status + * @param status the new status of that user + * @since Envoy Server Standalone v0.1-alpha + */ + public void updateUserStatusInDatabase(User user, UserStatus status) { + user.setStatus(status); + persistenceManager.updateUser(user); + } } \ No newline at end of file diff --git a/src/main/java/envoy/server/net/ObjectMessageProcessor.java b/src/main/java/envoy/server/net/ObjectMessageProcessor.java index 226fb69..3bbe43e 100644 --- a/src/main/java/envoy/server/net/ObjectMessageProcessor.java +++ b/src/main/java/envoy/server/net/ObjectMessageProcessor.java @@ -33,7 +33,6 @@ public class ObjectMessageProcessor implements IMessageProcessor { */ public ObjectMessageProcessor(Set> processors) { this.processors = processors; } - @SuppressWarnings("unchecked") @Override public void process(Message message, WriteProxy writeProxy) { try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) { diff --git a/src/main/java/envoy/server/net/ObjectWriteProxy.java b/src/main/java/envoy/server/net/ObjectWriteProxy.java index 2ec9223..825daeb 100644 --- a/src/main/java/envoy/server/net/ObjectWriteProxy.java +++ b/src/main/java/envoy/server/net/ObjectWriteProxy.java @@ -22,7 +22,7 @@ public class ObjectWriteProxy { private final WriteProxy writeProxy; /** - * Creates an instance of @link{ObjectWriteProxy}. + * Creates an instance of {@link ObjectWriteProxy}. * * @param writeProxy the {@link WriteProxy} to write objects to another client * @since Envoy Server Standalone v0.1-alpha diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 376989a..274d6fd 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -10,6 +10,7 @@ import envoy.data.Contacts; import envoy.data.LoginCredentials; import envoy.data.Message.MessageStatus; import envoy.data.User; +import envoy.event.UserStatusChangeEvent; import envoy.server.ConnectionManager; import envoy.server.ObjectProcessor; import envoy.server.data.Message; @@ -29,7 +30,7 @@ import envoy.server.net.ObjectWriteProxy; */ public class LoginCredentialProcessor implements ObjectProcessor { - private PersistenceManager persistenceManager = PersistenceManager.getPersistenceManager(); + private PersistenceManager persistenceManager = PersistenceManager.getPersistenceManager(); @Override public Class getInputClass() { return LoginCredentials.class; } @@ -41,9 +42,10 @@ public class LoginCredentialProcessor implements ObjectProcessor + *
+ * Project: envoy-server-standalone
+ * File: UserStatusChangeProcessor.java
+ * Created: 1 Feb 2020
+ * + * @author Leon Hofmeister + * @since Envoy Server Standalone v0.1-alpha + */ +public class UserStatusChangeProcessor implements ObjectProcessor { + + @Override + public Class getInputClass() { return UserStatusChangeEvent.class; } + + @Override + public void process(UserStatusChangeEvent input, long socketId, ObjectWriteProxy writeProxy) throws IOException { + + PersistenceManager perMan = PersistenceManager.getPersistenceManager(); + // new status should not equal old status + if (input.get().equals(perMan.getUserById(input.getId()).getStatus())) { + System.out.println("Received an unnecessary UserStatusChangeEvent"); + return; + } + updateUserStatus(input, writeProxy); + + } + + /** + * Sets the {@link UserStatus} for a given user. Both offline contacts and + * currently online contacts are notified. + * + * @param evt the {@link UserStatusChangeEvent} that signals the change + * @param writeProxy the {@link ObjectWriteProxy} that is used to send objects + * back to clients + * @throws IOException if sending this update failed for any contact + * @since Envoy Server Standalone v0.1-alpha + */ + public static void updateUserStatus(UserStatusChangeEvent evt, ObjectWriteProxy writeProxy) throws IOException { + PersistenceManager perMan = PersistenceManager.getPersistenceManager(); + envoy.server.data.User user = perMan.getUserById(evt.getId()); + // handling for newly logged in clients + perMan.updateUserStatusInDatabase(user, evt.get()); + + // handling for contacts that are already online + notifyContacts(evt, user, writeProxy); + } + + /** + * notifies active contacts of this {@link User} that his {@link UserStatus} has + * changed + * + * @param evt the {@link UserStatusChangeEvent} to send to other clients + * @param user the {@link User} + * @param writeProxy the {@link ObjectWriteProxy} that is used to send objects + * back to clients + * @throws IOException if sending this update failed for any contact + * @since Envoy Server Standalone v0.1-alpha + */ + public static void notifyContacts(UserStatusChangeEvent evt, envoy.server.data.User user, ObjectWriteProxy writeProxy) throws IOException { + ConnectionManager conMan = ConnectionManager.getInstance(); + for (User contact : user.getContacts()) + if (conMan.isOnline(contact.getId())) writeProxy.write(conMan.getSocketId(contact.getId()), evt); + } + +} \ No newline at end of file