From aa52cddf6a8e698638de6a02ae98c574c05d974b Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 9 Feb 2020 16:37:53 +0100 Subject: [PATCH] added methods to signal an invalid login additionally added a failsafe to set all users to offline in case of shutdown of the server --- .../java/envoy/server/ConnectionManager.java | 12 ++++++++ .../server/database/PersistenceManager.java | 12 +++++++- .../processors/LoginCredentialProcessor.java | 30 +++++++++++-------- .../server/processors/MessageProcessor.java | 2 +- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/main/java/envoy/server/ConnectionManager.java b/src/main/java/envoy/server/ConnectionManager.java index 9dfd56b..73985bd 100644 --- a/src/main/java/envoy/server/ConnectionManager.java +++ b/src/main/java/envoy/server/ConnectionManager.java @@ -1,8 +1,10 @@ package envoy.server; +import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -98,4 +100,14 @@ public class ConnectionManager implements ISocketIdListener { * @since Envoy Server Standalone v0.1-alpha */ public boolean isOnline(long userId) { return sockets.containsKey(userId); } + + /** + * @return the userId of all users who are currently online + * @since Envoy Server Standalone v0.1-alpha + */ + public List getOnlineUsers() { + List onlineUsers = new ArrayList<>(); + sockets.forEach((userId, unimportant) -> onlineUsers.add(userId)); + return onlineUsers; + } } diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index 84587e6..c7b6f11 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -1,11 +1,14 @@ package envoy.server.database; +import java.util.Date; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; +import envoy.data.User.UserStatus; +import envoy.server.ConnectionManager; import envoy.server.data.ConfigItem; import envoy.server.data.Message; import envoy.server.data.User; @@ -32,7 +35,14 @@ public class PersistenceManager { */ private PersistenceManager() { transaction.begin(); - Runtime.getRuntime().addShutdownHook(new Thread(() -> transaction.commit())); + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + ConnectionManager.getInstance() + .getOnlineUsers() + .stream() + .map(this::getUserById) + .forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); updateUser(user); }); + transaction.commit(); + })); } /** diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 9020937..31ddbbd 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -44,7 +44,7 @@ public class LoginCredentialProcessor implements ObjectProcessor { // Update the message status to RECEIVED message.setReceivedDate(new Date()); message.nextStatus(); - writeProxy.write(connectionManager.getSocketId(message.getSenderId()), new MessageStatusChangeEvent(message)); + writeProxy.write(socketId, new MessageStatusChangeEvent(message)); } catch (IOException e) { System.err.println("Recipient online. Failed to send message" + message.getId()); e.printStackTrace();