From 092a4610c113eab4d422331fba676dc7eeb1a15d Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 5 Feb 2020 21:32:53 +0100 Subject: [PATCH] fixed not updating user status and implemented easier to read structure --- .../java/envoy/server/ConnectionManager.java | 11 +++--- .../server/database/PersistenceManager.java | 17 --------- .../processors/LoginCredentialProcessor.java | 5 +-- .../processors/UserStatusChangeProcessor.java | 36 +++++++++++-------- 4 files changed, 31 insertions(+), 38 deletions(-) diff --git a/src/main/java/envoy/server/ConnectionManager.java b/src/main/java/envoy/server/ConnectionManager.java index a1b9945..9dfd56b 100644 --- a/src/main/java/envoy/server/ConnectionManager.java +++ b/src/main/java/envoy/server/ConnectionManager.java @@ -1,5 +1,6 @@ package envoy.server; +import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -7,8 +8,7 @@ import java.util.Set; import com.jenkov.nioserver.ISocketIdListener; -import envoy.data.User; -import envoy.event.UserStatusChangeEvent; +import envoy.data.User.UserStatus; import envoy.server.database.PersistenceManager; import envoy.server.processors.UserStatusChangeProcessor; @@ -51,9 +51,10 @@ public class ConnectionManager implements ISocketIdListener { public void socketCancelled(long socketId) { if (!pendingSockets.remove(socketId)) { // notifying contacts of this users offline-going - long clientId = getUserIdBySocketId(socketId); - User user = new User(clientId, PersistenceManager.getPersistenceManager().getUserById(clientId).getName()); - UserStatusChangeProcessor.updateUserStatus(new UserStatusChangeEvent(user)); + envoy.server.data.User user = PersistenceManager.getPersistenceManager().getUserById(getUserIdBySocketId(socketId)); + user.setStatus(UserStatus.OFFLINE); + user.setLastSeen(new Date()); + UserStatusChangeProcessor.updateUserStatus(user); // removing the socket sockets.entrySet().stream().filter(e -> e.getValue() == socketId).forEach(e -> sockets.remove(e.getValue())); diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index 7f2d715..2d611ee 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -5,7 +5,6 @@ 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; @@ -220,20 +219,4 @@ 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 updateUserStatus(User user, UserStatus status) { - if (user.getStatus().equals(status)) - System.out.println("Received an UserStatusChangeEvent for user " + user.getId() + " to update that this user already has"); - else { - user.setStatus(status); - persistenceManager.updateUser(user); - } - } } \ No newline at end of file diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 636b218..acd2b2e 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -10,7 +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.data.User.UserStatus; import envoy.server.ConnectionManager; import envoy.server.ObjectProcessor; import envoy.server.data.Message; @@ -48,7 +48,8 @@ public class LoginCredentialProcessor implements ObjectProcessor { - private static ObjectWriteProxy writeProxy; + private static ObjectWriteProxy writeProxy; + private static PersistenceManager persistenceManager = PersistenceManager.getPersistenceManager(); @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())) { + if (input.get().equals(persistenceManager.getUserById(input.getId()).getStatus())) { System.out.println("Received an unnecessary UserStatusChangeEvent"); return; } @@ -43,17 +43,24 @@ public class UserStatusChangeProcessor implements ObjectProcessor