From af865b77ad00273b748e0392322f336166af07f7 Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 2 Feb 2020 13:34:28 +0100 Subject: [PATCH] Added status update for when a client goes offline --- .../java/envoy/server/ConnectionManager.java | 19 ++++++++ .../processors/LoginCredentialProcessor.java | 3 +- .../processors/UserStatusChangeProcessor.java | 44 ++++++++++++------- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/main/java/envoy/server/ConnectionManager.java b/src/main/java/envoy/server/ConnectionManager.java index ffc64e1..2d7aac5 100644 --- a/src/main/java/envoy/server/ConnectionManager.java +++ b/src/main/java/envoy/server/ConnectionManager.java @@ -7,6 +7,11 @@ import java.util.Set; import com.jenkov.nioserver.ISocketIdListener; +import envoy.data.User; +import envoy.event.UserStatusChangeEvent; +import envoy.server.database.PersistenceManager; +import envoy.server.processors.UserStatusChangeProcessor; + /** * Project: envoy-server-standalone
* File: ConnectionManager.java
@@ -44,6 +49,11 @@ public class ConnectionManager implements ISocketIdListener { @Override public void socketCancelled(long 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)); + // removing the socket if (!pendingSockets.remove(socketId)) sockets.entrySet().stream().filter(e -> e.getValue() == socketId).forEach(e -> sockets.remove(e.getValue())); } @@ -70,6 +80,15 @@ public class ConnectionManager implements ISocketIdListener { */ public long getSocketId(long userId) { return sockets.get(userId); } + /** + * @param socketId the id of the socket whose User is needed + * @return the userId associated with this socketId + * @since Envoy Server Standalone v0.1-alpha + */ + public long getUserIdBySocketId(long socketId) { + return sockets.entrySet().stream().filter((entry) -> entry.getValue().equals(socketId)).findFirst().get().getKey(); + } + /** * @param userId the ID of the user to check for * @return {@code true} if the user is online diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 274d6fd..f0373f3 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -37,6 +37,7 @@ public class LoginCredentialProcessor implements ObjectProcessor { + private static ObjectWriteProxy writeProxy; + @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); + updateUserStatus(input); } @@ -42,37 +43,48 @@ public class UserStatusChangeProcessor implements ObjectProcessor