From a1cfd728a5c25eff186fcba29c981fe532d26d68 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 14 Jun 2020 16:40:51 +0200 Subject: [PATCH] Some refactorings based on suggestions from @delvh --- .../envoy/server/data/PersistenceManager.java | 20 +++++++++---------- .../envoy/server/net/ConnectionManager.java | 14 ++++--------- .../processors/ContactOperationProcessor.java | 6 +++--- .../processors/ContactSearchProcessor.java | 2 +- .../processors/GroupCreationProcessor.java | 6 +++--- .../processors/GroupMessageProcessor.java | 8 +++----- .../processors/GroupResizeProcessor.java | 2 +- .../processors/LoginCredentialProcessor.java | 2 +- .../server/processors/MessageProcessor.java | 5 ++--- .../MessageStatusChangeProcessor.java | 2 +- .../processors/UserStatusChangeProcessor.java | 2 +- src/main/resources/META-INF/persistence.xml | 2 +- 12 files changed, 31 insertions(+), 40 deletions(-) diff --git a/src/main/java/envoy/server/data/PersistenceManager.java b/src/main/java/envoy/server/data/PersistenceManager.java index 26a7240..0221fe9 100755 --- a/src/main/java/envoy/server/data/PersistenceManager.java +++ b/src/main/java/envoy/server/data/PersistenceManager.java @@ -213,26 +213,26 @@ public class PersistenceManager { } /** - * Adds a user to the contact list of another user and vice versa. + * Adds a contact to the contact list of another contact and vice versa. * - * @param userId1 the ID of the first user - * @param userId2 the ID of the second user + * @param contactID1 the ID of the first contact + * @param contactID2 the ID of the second contact * @since Envoy Server Standalone v0.1-alpha */ - public void addUserContact(long userId1, long userId2) { + public void addContactBidirectional(long contactID1, long contactID2) { // Get users by ID - Contact u1 = getContactByID(userId1); - Contact u2 = getContactByID(userId2); + Contact c1 = getContactByID(contactID1); + Contact c2 = getContactByID(contactID2); // Add users to each others contact lists - u1.getContacts().add(u2); - u2.getContacts().add(u1); + c1.getContacts().add(c2); + c2.getContacts().add(c1); // Synchronize changes with the database transaction.begin(); - entityManager.merge(u1); - entityManager.merge(u2); + entityManager.merge(c1); + entityManager.merge(c2); transaction.commit(); } diff --git a/src/main/java/envoy/server/net/ConnectionManager.java b/src/main/java/envoy/server/net/ConnectionManager.java index 03f3810..6d9beea 100755 --- a/src/main/java/envoy/server/net/ConnectionManager.java +++ b/src/main/java/envoy/server/net/ConnectionManager.java @@ -1,10 +1,6 @@ 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; @@ -53,7 +49,7 @@ public class ConnectionManager implements ISocketIdListener { 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); @@ -83,14 +79,14 @@ public class ConnectionManager implements ISocketIdListener { * @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 * @return the userId associated with this socketId * @since Envoy Server Standalone v0.1-alpha */ - public long getUserIdBySocketId(long socketID) { + public long getUserIdBySocketID(long socketID) { return sockets.entrySet().stream().filter(entry -> entry.getValue().equals(socketID)).findFirst().get().getKey(); } @@ -108,8 +104,6 @@ public class ConnectionManager implements ISocketIdListener { 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 diff --git a/src/main/java/envoy/server/processors/ContactOperationProcessor.java b/src/main/java/envoy/server/processors/ContactOperationProcessor.java index c47e516..9e181ae 100755 --- a/src/main/java/envoy/server/processors/ContactOperationProcessor.java +++ b/src/main/java/envoy/server/processors/ContactOperationProcessor.java @@ -27,15 +27,15 @@ public class ContactOperationProcessor implements ObjectProcessor()); input.getInitialMemberIDs().stream().map(persistenceManager::getUserByID).forEach(group.getContacts()::add); - group.getContacts().add(persistenceManager.getContactByID(connectionManager.getUserIdBySocketId(socketID))); + group.getContacts().add(persistenceManager.getContactByID(connectionManager.getUserIdBySocketID(socketID))); group.getContacts().forEach(c -> c.getContacts().add(group)); - group.getContacts().add(persistenceManager.getUserByID(connectionManager.getUserIdBySocketId(socketID))); + group.getContacts().add(persistenceManager.getUserByID(connectionManager.getUserIdBySocketID(socketID))); persistenceManager.addContact(group); group.getContacts() .stream() .map(Contact::getID) .filter(connectionManager::isOnline) - .map(connectionManager::getSocketId) + .map(connectionManager::getSocketID) .forEach(memberSocketID -> { try { writeProxy.write(memberSocketID, new ContactOperationEvent(group.toCommon(), ElementOperation.ADD)); diff --git a/src/main/java/envoy/server/processors/GroupMessageProcessor.java b/src/main/java/envoy/server/processors/GroupMessageProcessor.java index 89b5527..809df61 100644 --- a/src/main/java/envoy/server/processors/GroupMessageProcessor.java +++ b/src/main/java/envoy/server/processors/GroupMessageProcessor.java @@ -31,10 +31,8 @@ public class GroupMessageProcessor implements ObjectProcessor { ConnectionManager connectionManager = ConnectionManager.getInstance(); final var members = PersistenceManager.getInstance().getGroupByID(groupMessage.getRecipientID()).getContacts(); - for (long i = 0; i < groupMessage.getMemberStatuses().size(); i++) { - groupMessage.getMemberStatuses().replace(i, MessageStatus.SENT); - } - members.forEach(user -> { setMemberStatus(connectionManager, groupMessage, user.getID()); }); + groupMessage.getMemberStatuses().replaceAll((id, oldStatus) -> MessageStatus.SENT); + members.forEach(user -> setMemberStatus(connectionManager, groupMessage, user.getID())); // Checks if all memberMessageStatuses are RECEIVED and if so sets the // groupMessage Status to RECEIVED. @@ -51,7 +49,7 @@ public class GroupMessageProcessor implements ObjectProcessor { private void sendToMember(ConnectionManager connectionManager, GroupMessage groupMessage, long memberID, ObjectWriteProxy writeProxy) { if (connectionManager.isOnline(memberID)) try { // If recipient is online, send the groupMessage directly - writeProxy.write(connectionManager.getSocketId(memberID), groupMessage); + writeProxy.write(connectionManager.getSocketID(memberID), groupMessage); } catch (IOException e) { logger.warning("Recipient online. Failed to send message" + groupMessage.getID()); e.printStackTrace(); diff --git a/src/main/java/envoy/server/processors/GroupResizeProcessor.java b/src/main/java/envoy/server/processors/GroupResizeProcessor.java index dc99535..79c2be6 100644 --- a/src/main/java/envoy/server/processors/GroupResizeProcessor.java +++ b/src/main/java/envoy/server/processors/GroupResizeProcessor.java @@ -46,7 +46,7 @@ public class GroupResizeProcessor implements ObjectProcessor { .stream() .map(Contact::getID) .filter(connectionManager::isOnline) - .map(connectionManager::getSocketId) + .map(connectionManager::getSocketID) .forEach(memberSocketID -> { try { writeProxy.write(memberSocketID, commonGroup); diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 7c12ec2..7e33f2b 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -69,7 +69,7 @@ public class LoginCredentialProcessor implements ObjectProcessor { @Override public void process(Message message, long socketID, ObjectWriteProxy writeProxy) { - if (message.getStatus!=MessageStatus.WAITING) { + if (message.getStatus() != MessageStatus.WAITING) { logger.warning("Received message with invalid status: " + message); return; } @@ -49,7 +48,7 @@ public class MessageProcessor implements ObjectProcessor { private void sendToUser(ConnectionManager connectionManager, Message message, ObjectWriteProxy writeProxy) { if (connectionManager.isOnline(message.getRecipientID())) try { // If recipient is online, send the message directly - writeProxy.write(connectionManager.getSocketId(message.getRecipientID()), message); + writeProxy.write(connectionManager.getSocketID(message.getRecipientID()), message); // Update the message status to RECEIVED message.setReceivedDate(new Date()); message.nextStatus(); diff --git a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java index 242f627..2ca7c46 100755 --- a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java @@ -34,7 +34,7 @@ public class MessageStatusChangeProcessor implements ObjectProcessor - \ No newline at end of file +