From 0057c66d9954662509db165d1c771025a485af5d Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 26 Mar 2020 20:14:09 +0100 Subject: [PATCH] Created method to extract all currently online members of a group additionally, refactored every ".Id" to ".ID" --- .../envoy/server/net/ConnectionManager.java | 59 +++++++++++-------- .../envoy/server/net/ObjectWriteProxy.java | 6 +- .../processors/ContactOperationProcessor.java | 8 +-- .../ContactsRequestEventProcessor.java | 6 +- .../processors/GroupCreationProcessor.java | 4 +- .../IDGeneratorRequestProcessor.java | 12 ++-- .../processors/LoginCredentialProcessor.java | 38 ++++++------ .../server/processors/MessageProcessor.java | 4 +- .../MessageStatusChangeProcessor.java | 2 +- .../server/processors/ObjectProcessor.java | 4 +- .../processors/UserStatusChangeProcessor.java | 2 +- 11 files changed, 79 insertions(+), 66 deletions(-) diff --git a/src/main/java/envoy/server/net/ConnectionManager.java b/src/main/java/envoy/server/net/ConnectionManager.java index bf79091..9314b5c 100755 --- a/src/main/java/envoy/server/net/ConnectionManager.java +++ b/src/main/java/envoy/server/net/ConnectionManager.java @@ -1,15 +1,14 @@ package envoy.server.net; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; +import java.util.stream.Collectors; import com.jenkov.nioserver.ISocketIdListener; import envoy.data.User.UserStatus; +import envoy.server.data.Group; import envoy.server.data.PersistenceManager; +import envoy.server.data.User; import envoy.server.processors.UserStatusChangeProcessor; /** @@ -48,60 +47,74 @@ public class ConnectionManager implements ISocketIdListener { public static ConnectionManager getInstance() { return connectionManager; } @Override - public void socketCancelled(long socketId) { - if (!pendingSockets.remove(socketId)) { + public void socketCancelled(long socketID) { + if (!pendingSockets.remove(socketID)) { // Notify contacts of this users offline-going - envoy.server.data.User user = PersistenceManager.getInstance().getUserById(getUserIdBySocketId(socketId)); + envoy.server.data.User user = PersistenceManager.getInstance().getUserById(getUserIdBySocketId(socketID)); user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); UserStatusChangeProcessor.updateUserStatus(user); // Remove the socket - sockets.entrySet().removeIf(e -> e.getValue() == socketId); + sockets.entrySet().removeIf(e -> e.getValue() == socketID); } } @Override - public void socketRegistered(long socketId) { pendingSockets.add(socketId); } + public void socketRegistered(long socketID) { pendingSockets.add(socketID); } /** * Associates a socket ID with a user ID. * - * @param userId the user ID - * @param socketId the socket ID + * @param userID the user ID + * @param socketID the socket ID * @since Envoy Server Standalone v0.1-alpha */ - public void registerUser(long userId, long socketId) { - sockets.put(userId, socketId); - pendingSockets.remove(socketId); + public void registerUser(long userID, long socketID) { + sockets.put(userID, socketID); + pendingSockets.remove(socketID); } /** - * @param userId the ID of the user registered at 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 */ - public long getSocketId(long userId) { return sockets.get(userId); } + public long getSocketId(long userID) { return sockets.get(userID); } /** - * @param socketId the id of the socket whose User is needed + * @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(); + 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 + * @param userID the ID of the user to check for * @return {@code true} if the user is online * @since Envoy Server Standalone v0.1-alpha */ - public boolean isOnline(long userId) { return sockets.containsKey(userId); } + public boolean isOnline(long userID) { return sockets.containsKey(userID); } /** - * @return the userId of all users who are currently online + * @return the userIDs of all users who are currently online * @since Envoy Server Standalone v0.1-alpha */ public Set getOnlineUsers() { return sockets.keySet(); } + + /** + * Returns all members of a group who are currently online. + * + * @param group the group to search for + * @return a set of all IDs of currently active members in this group + * @since Envoy Server Standalone v0.1-beta + */ + public Set getOnlineUsersOfGroup(Group group) { + Set onlineMembers = new HashSet<>(); + Set members = group.getMembers().stream().map(User::getID).collect(Collectors.toSet()); + members.forEach(userID -> { if (isOnline(userID)) onlineMembers.add(userID); }); + return onlineMembers; + } } diff --git a/src/main/java/envoy/server/net/ObjectWriteProxy.java b/src/main/java/envoy/server/net/ObjectWriteProxy.java index 825daeb..238e817 100755 --- a/src/main/java/envoy/server/net/ObjectWriteProxy.java +++ b/src/main/java/envoy/server/net/ObjectWriteProxy.java @@ -30,15 +30,15 @@ public class ObjectWriteProxy { public ObjectWriteProxy(WriteProxy writeProxy) { this.writeProxy = writeProxy; } /** - * @param recipientSocketId the socket id of the recipient + * @param recipientSocketID the socket id of the recipient * @param obj the object to return to the client * @throws IOException if the serialization of the object failed * @since Envoy Server Standalone v0.1-alpha */ - public void write(long recipientSocketId, Object obj) throws IOException { + public void write(long recipientSocketID, Object obj) throws IOException { // Create message targeted at the client Message response = writeProxy.getMessage(); - response.socketId = recipientSocketId; + response.socketId = recipientSocketID; // Serialize object to byte array byte[] objBytes = SerializationUtils.writeToByteArray(obj); diff --git a/src/main/java/envoy/server/processors/ContactOperationProcessor.java b/src/main/java/envoy/server/processors/ContactOperationProcessor.java index aaa9a29..f9f9133 100755 --- a/src/main/java/envoy/server/processors/ContactOperationProcessor.java +++ b/src/main/java/envoy/server/processors/ContactOperationProcessor.java @@ -25,15 +25,15 @@ public class ContactOperationProcessor implements ObjectProcessor getInputClass() { return IDGeneratorRequest.class; } @Override - public void process(IDGeneratorRequest input, long socketId, ObjectWriteProxy writeProxy) throws IOException { + public void process(IDGeneratorRequest input, long socketID, ObjectWriteProxy writeProxy) throws IOException { System.out.println("Received id generation request."); var generator = createIDGenerator(); System.out.println("Sending new id generator " + generator); - writeProxy.write(socketId, generator); + writeProxy.write(socketID, generator); } /** @@ -44,10 +44,10 @@ public class IDGeneratorRequestProcessor implements ObjectProcessor pendingMessages = PersistenceManager.getInstance().getUnreadMessages(user); for (Message msg : pendingMessages) { System.out.println("Sending message " + msg.toCommonMessage()); - writeProxy.write(socketId, msg.toCommonMessage()); + writeProxy.write(socketID, msg.toCommonMessage()); msg.setReceivedDate(new Date()); msg.setStatus(MessageStatus.RECEIVED); PersistenceManager.getInstance().updateMessage(msg); @@ -79,13 +79,13 @@ public class LoginCredentialProcessor implements ObjectProcessor getInputClass() { return LoginCredentials.class; } - private envoy.server.data.User getUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException { - return credentials.isRegistration() ? newUser(credentials, socketId, writeProxy) : checkForExistingUser(credentials, socketId, writeProxy); + private envoy.server.data.User getUser(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException { + return credentials.isRegistration() ? newUser(credentials, socketID, writeProxy) : checkForExistingUser(credentials, socketID, writeProxy); } /** * @param credentials the input to evaluate - * @param socketId the socket ID at which the client performing the handshake + * @param socketID the socket ID at which the client performing the handshake * is connected * @param writeProxy the {@link ObjectWriteProxy} to use if login was not * successful @@ -93,34 +93,34 @@ public class LoginCredentialProcessor implements ObjectProcessor { @Override - public void process(Message message, long socketId, ObjectWriteProxy writeProxy) { + public void process(Message message, long socketID, ObjectWriteProxy writeProxy) { message.nextStatus(); ConnectionManager connectionManager = ConnectionManager.getInstance(); Contact recipient = PersistenceManager.getInstance().getContactById(message.getID()); @@ -36,7 +36,7 @@ public class MessageProcessor implements ObjectProcessor { sendToUser(connectionManager, message, writeProxy); // Sending a messageStatusChangeEvent to the sender try { - writeProxy.write(socketId, new MessageStatusChangeEvent(message)); + writeProxy.write(socketID, new MessageStatusChangeEvent(message)); } catch (IOException e) { System.err.println("Could not send messageStatusChangeEvent to the sender of this message with ID: " + message.getID()); e.printStackTrace(); diff --git a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java index bd9c215..587fd0e 100755 --- a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java @@ -23,7 +23,7 @@ public class MessageStatusChangeProcessor implements ObjectProcessor { /** * @param input the request object - * @param socketId the ID of the socket from which the object was received + * @param socketID the ID of the socket from which the object was received * @param writeProxy the object that allows writing to a client * @throws IOException if something went wrong during processing * @since Envoy Server Standalone v0.1-alpha */ - void process(T input, long socketId, ObjectWriteProxy writeProxy) throws IOException; + void process(T input, long socketID, ObjectWriteProxy writeProxy) throws IOException; } diff --git a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java index 82559ac..3e1b5d3 100755 --- a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java @@ -28,7 +28,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor getInputClass() { return UserStatusChangeEvent.class; } @Override - public void process(UserStatusChangeEvent input, long socketId, ObjectWriteProxy writeProxy) throws IOException { + public void process(UserStatusChangeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { // new status should not equal old status if (input.get().equals(persistenceManager.getUserById(input.getID()).getStatus())) { System.out.println("Received an unnecessary UserStatusChangeEvent");