From 4e385396adc920d6679002dd89d108ddeddd595d Mon Sep 17 00:00:00 2001 From: delvh Date: Mon, 6 Apr 2020 22:55:27 +0200 Subject: [PATCH] fixes #129 - contacts are now sent as a part of the user (if they were not already)... Additionally renamed some methods named `...Id()` to `...ID()` --- src/main/java/envoy/server/Startup.java | 10 +- src/main/java/envoy/server/data/Contact.java | 17 ++- src/main/java/envoy/server/data/Group.java | 2 +- src/main/java/envoy/server/data/Message.java | 6 +- .../envoy/server/data/PersistenceManager.java | 18 +-- src/main/java/envoy/server/data/User.java | 2 +- .../envoy/server/net/ConnectionManager.java | 2 +- .../processors/ContactOperationProcessor.java | 2 +- .../processors/GroupCreationProcessor.java | 96 ++++++++-------- .../processors/GroupResizeProcessor.java | 108 +++++++++--------- .../IDGeneratorRequestProcessor.java | 2 +- .../processors/LoginCredentialProcessor.java | 16 +-- .../server/processors/MessageProcessor.java | 4 +- .../MessageStatusChangeProcessor.java | 2 +- .../processors/NameChangeProcessor.java | 2 +- .../processors/UserStatusChangeProcessor.java | 4 +- 16 files changed, 139 insertions(+), 154 deletions(-) diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index a4875ba..bf30945 100755 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -43,7 +43,10 @@ public class Startup { processors.add(new ContactOperationProcessor()); Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors)); - initializeCurrentMessageId(); + // Initialize the current message ID + PersistenceManager persistenceManager = PersistenceManager.getInstance(); + if (persistenceManager.getConfigItemByID("currentMessageId") == null) + persistenceManager.addConfigItem(new ConfigItem("currentMessageId", "0")); server.start(); server.getSocketProcessor().registerSocketIdListener(ConnectionManager.getInstance()); @@ -53,9 +56,4 @@ public class Startup { System.out.println("Stopped"); System.exit(0); } - - private static void initializeCurrentMessageId() { - PersistenceManager persMan = PersistenceManager.getInstance(); - if (persMan.getConfigItemById("currentMessageId") == null) persMan.addConfigItem(new ConfigItem("currentMessageId", "0")); - } } diff --git a/src/main/java/envoy/server/data/Contact.java b/src/main/java/envoy/server/data/Contact.java index 81107d4..eea8fe1 100644 --- a/src/main/java/envoy/server/data/Contact.java +++ b/src/main/java/envoy/server/data/Contact.java @@ -2,14 +2,7 @@ package envoy.server.data; import java.util.Set; -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; -import javax.persistence.ManyToMany; +import javax.persistence.*; /** * This class acts as a superclass for all contacts, being {@link User}s and @@ -32,7 +25,7 @@ public abstract class Contact { protected long id; protected String name; - @ManyToMany(targetEntity = Contact.class, cascade = CascadeType.ALL) + @ManyToMany(targetEntity = Contact.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER) protected Set contacts; /** @@ -81,4 +74,10 @@ public abstract class Contact { * @since Envoy Server Standalone v0.1-beta */ public void setContacts(Set contacts) { this.contacts = contacts; } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { return String.format("%s[id=%d,name=%s, contacts=%s]", getClass().getSimpleName(), id, name, contacts); } } diff --git a/src/main/java/envoy/server/data/Group.java b/src/main/java/envoy/server/data/Group.java index 6301624..3b6cac8 100644 --- a/src/main/java/envoy/server/data/Group.java +++ b/src/main/java/envoy/server/data/Group.java @@ -24,7 +24,7 @@ import envoy.data.User; @Entity @Table(name = "groups") @NamedQuery(query = "SELECT g FROM Group g WHERE g.name = :name", name = "getGroupByName") -public class Group extends Contact { +public final class Group extends Contact { /** * {@inheritDoc} diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java index 17a12d8..f82658a 100755 --- a/src/main/java/envoy/server/data/Message.java +++ b/src/main/java/envoy/server/data/Message.java @@ -74,15 +74,15 @@ public class Message { * @since Envoy Server Standalone v0.1-alpha */ public Message(envoy.data.Message message) { - PersistenceManager persMan = PersistenceManager.getInstance(); + PersistenceManager persistenceManager = PersistenceManager.getInstance(); id = message.getID(); status = message.getStatus(); text = message.getText(); creationDate = message.getCreationDate(); receivedDate = message.getReceivedDate(); readDate = message.getReadDate(); - sender = persMan.getUserById(message.getSenderID()); - recipient = persMan.getUserById(message.getRecipientID()); + sender = persistenceManager.getUserByID(message.getSenderID()); + recipient = persistenceManager.getUserByID(message.getRecipientID()); forwarded = message.isForwarded(); // TODO: attachment = message.getAttachment().toByteArray();DOES NOT WORK YET } diff --git a/src/main/java/envoy/server/data/PersistenceManager.java b/src/main/java/envoy/server/data/PersistenceManager.java index 53eb670..aeecf77 100755 --- a/src/main/java/envoy/server/data/PersistenceManager.java +++ b/src/main/java/envoy/server/data/PersistenceManager.java @@ -37,7 +37,7 @@ public class PersistenceManager { ConnectionManager.getInstance() .getOnlineUsers() .stream() - .map(this::getUserById) + .map(this::getUserByID) .forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); entityManager.merge(user); }); transaction.commit(); })); @@ -120,7 +120,7 @@ public class PersistenceManager { * @return the user with the specified id * @since Envoy Server Standalone v0.1-alpha */ - public User getUserById(long id) { return entityManager.find(User.class, id); } + public User getUserByID(long id) { return entityManager.find(User.class, id); } /** * Searches for a {@link Group} with a specific ID. @@ -129,7 +129,7 @@ public class PersistenceManager { * @return the group with the specific id * @since Envoy Server Standalone v0.1-beta */ - public Group getGroupById(long id) { return entityManager.find(Group.class, id); } + public Group getGroupByID(long id) { return entityManager.find(Group.class, id); } /** * Searches for a {@link Contact} with a specific ID. @@ -138,7 +138,7 @@ public class PersistenceManager { * @return the contact with the specific id * @since Envoy Server Standalone v0.1-beta */ - public Contact getContactById(long id) { return entityManager.find(Contact.class, id); } + public Contact getContactByID(long id) { return entityManager.find(Contact.class, id); } /** * Searched for a {@link User} with a specific name. @@ -180,14 +180,14 @@ public class PersistenceManager { * @return the message with the specified id * @since Envoy Server Standalone v0.1-alpha */ - public Message getMessageById(long id) { return entityManager.find(Message.class, id); } + public Message getMessageByID(long id) { return entityManager.find(Message.class, id); } /** * @param key the name of this {@link ConfigItem} * @return the {@link ConfigItem} with the given name * @since Envoy Server Standalone v0.1-alpha */ - public ConfigItem getConfigItemById(String key) { return entityManager.find(ConfigItem.class, key); } + public ConfigItem getConfigItemByID(String key) { return entityManager.find(ConfigItem.class, key); } /** * Returns all messages received while being offline or the ones that have @@ -217,7 +217,7 @@ public class PersistenceManager { public List searchUsers(String searchPhrase, long userId) { return entityManager.createNamedQuery("searchUsers") .setParameter("searchPhrase", searchPhrase + "%") - .setParameter("context", getUserById(userId)) + .setParameter("context", getUserByID(userId)) .getResultList(); } @@ -231,8 +231,8 @@ public class PersistenceManager { public void addUserContact(long userId1, long userId2) { // Get users by ID - Contact u1 = getContactById(userId1); - Contact u2 = getContactById(userId2); + Contact u1 = getContactByID(userId1); + Contact u2 = getContactByID(userId2); // Add users to each others contact lists u1.getContacts().add(u2); diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index 2f788f0..0e63ac1 100755 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -30,7 +30,7 @@ import javax.persistence.*; name = "searchUsers" ) } ) -public class User extends Contact { +public final class User extends Contact { private byte[] passwordHash; diff --git a/src/main/java/envoy/server/net/ConnectionManager.java b/src/main/java/envoy/server/net/ConnectionManager.java index 883f510..ef384bb 100755 --- a/src/main/java/envoy/server/net/ConnectionManager.java +++ b/src/main/java/envoy/server/net/ConnectionManager.java @@ -49,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); diff --git a/src/main/java/envoy/server/processors/ContactOperationProcessor.java b/src/main/java/envoy/server/processors/ContactOperationProcessor.java index fa67c55..e187f60 100755 --- a/src/main/java/envoy/server/processors/ContactOperationProcessor.java +++ b/src/main/java/envoy/server/processors/ContactOperationProcessor.java @@ -32,7 +32,7 @@ public class ContactOperationProcessor implements ObjectProcessorenvoy-server-standalone
- * File: GroupCreationProcessor.java
- * Created: 26.03.2020
- * - * @author Maximilian Käfer - * @since Envoy Server Standalone v0.1-beta - */ -public class GroupCreationProcessor implements ObjectProcessor { - - private final PersistenceManager persistenceManager = PersistenceManager.getInstance(); - private final ConnectionManager connectionManager = ConnectionManager.getInstance(); - - @Override - public void process(GroupCreationEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { - envoy.server.data.Group group = new envoy.server.data.Group(); - group.setName(input.get()); - input.getInitialMemberIDs().stream().map(persistenceManager::getUserById).forEach(group.getContacts()::add); - group.getContacts().add(persistenceManager.getUserById(connectionManager.getUserIdBySocketId(socketID))); - persistenceManager.addContact(group); - writeProxy.write(socketID, group); - group.getContacts() - .stream() - .map(Contact::getID) - .filter(connectionManager::isOnline) - .map(connectionManager::getSocketId) - .forEach(memberSocketID -> { - try { - writeProxy.write(memberSocketID, group); - } catch (IOException e) { - e.printStackTrace(); - } - }); - } - - @Override - public Class getInputClass() { return GroupCreationEvent.class; } -} +package envoy.server.processors; + +import java.io.IOException; + +import envoy.event.GroupCreationEvent; +import envoy.server.data.Contact; +import envoy.server.data.PersistenceManager; +import envoy.server.net.ConnectionManager; +import envoy.server.net.ObjectWriteProxy; + +/** + * Project: envoy-server-standalone
+ * File: GroupCreationProcessor.java
+ * Created: 26.03.2020
+ * + * @author Maximilian Käfer + * @since Envoy Server Standalone v0.1-beta + */ +public class GroupCreationProcessor implements ObjectProcessor { + + private final PersistenceManager persistenceManager = PersistenceManager.getInstance(); + private final ConnectionManager connectionManager = ConnectionManager.getInstance(); + + @Override + public void process(GroupCreationEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { + envoy.server.data.Group group = new envoy.server.data.Group(); + group.setName(input.get()); + input.getInitialMemberIDs().stream().map(persistenceManager::getUserByID).forEach(group.getContacts()::add); + group.getContacts().add(persistenceManager.getUserByID(connectionManager.getUserIdBySocketId(socketID))); + persistenceManager.addContact(group); + writeProxy.write(socketID, group); + group.getContacts() + .stream() + .map(Contact::getID) + .filter(connectionManager::isOnline) + .map(connectionManager::getSocketId) + .forEach(memberSocketID -> { + try { + writeProxy.write(memberSocketID, group); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + + @Override + public Class getInputClass() { return GroupCreationEvent.class; } +} diff --git a/src/main/java/envoy/server/processors/GroupResizeProcessor.java b/src/main/java/envoy/server/processors/GroupResizeProcessor.java index d7ee615..fbf39df 100644 --- a/src/main/java/envoy/server/processors/GroupResizeProcessor.java +++ b/src/main/java/envoy/server/processors/GroupResizeProcessor.java @@ -1,54 +1,54 @@ -package envoy.server.processors; - -import java.io.IOException; - -import envoy.event.GroupResizeEvent; -import envoy.server.data.Contact; -import envoy.server.data.PersistenceManager; -import envoy.server.net.ConnectionManager; -import envoy.server.net.ObjectWriteProxy; - -/** - * Project: envoy-server-standalone
- * File: GroupResizeProcessor.java
- * Created: 03.04.2020
- * - * @author Maximilian Käfer - * @since Envoy Server Standalone v0.1-beta - */ -public class GroupResizeProcessor implements ObjectProcessor { - - PersistenceManager persistenceManager = PersistenceManager.getInstance(); - ConnectionManager connectionManager = ConnectionManager.getInstance(); - - @Override - public void process(GroupResizeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { - envoy.server.data.Group group = persistenceManager.getGroupById(input.getGroupID()); - switch (input.getOperation()) { - case ADD: - group.getContacts().add(persistenceManager.getUserById(input.get().getID())); - break; - - case REMOVE: - group.getContacts().remove(persistenceManager.getUserById(input.get().getID())); - break; - } - persistenceManager.updateContact(group); - writeProxy.write(socketID, group); - group.getContacts() - .stream() - .map(Contact::getID) - .filter(connectionManager::isOnline) - .map(connectionManager::getSocketId) - .forEach(memberSocketID -> { - try { - writeProxy.write(memberSocketID, group); - } catch (IOException e) { - e.printStackTrace(); - } - }); - } - - @Override - public Class getInputClass() { return GroupResizeEvent.class; } -} +package envoy.server.processors; + +import java.io.IOException; + +import envoy.event.GroupResizeEvent; +import envoy.server.data.Contact; +import envoy.server.data.PersistenceManager; +import envoy.server.net.ConnectionManager; +import envoy.server.net.ObjectWriteProxy; + +/** + * Project: envoy-server-standalone
+ * File: GroupResizeProcessor.java
+ * Created: 03.04.2020
+ * + * @author Maximilian Käfer + * @since Envoy Server Standalone v0.1-beta + */ +public class GroupResizeProcessor implements ObjectProcessor { + + PersistenceManager persistenceManager = PersistenceManager.getInstance(); + ConnectionManager connectionManager = ConnectionManager.getInstance(); + + @Override + public void process(GroupResizeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { + envoy.server.data.Group group = persistenceManager.getGroupByID(input.getGroupID()); + switch (input.getOperation()) { + case ADD: + group.getContacts().add(persistenceManager.getUserByID(input.get())); + break; + + case REMOVE: + group.getContacts().remove(persistenceManager.getUserByID(input.get())); + break; + } + persistenceManager.updateContact(group); + writeProxy.write(socketID, group); + group.getContacts() + .stream() + .map(Contact::getID) + .filter(connectionManager::isOnline) + .map(connectionManager::getSocketId) + .forEach(memberSocketID -> { + try { + writeProxy.write(memberSocketID, group); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + + @Override + public Class getInputClass() { return GroupResizeEvent.class; } +} diff --git a/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java b/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java index 6d27a93..4a00618 100755 --- a/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java +++ b/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java @@ -44,7 +44,7 @@ public class IDGeneratorRequestProcessor implements ObjectProcessor pendingMessages = PersistenceManager.getInstance().getPendingMessages(user); - for (Message msg : pendingMessages) { + for (Message msg : pendingMessages) if (msg.getStatus() == MessageStatus.SENT) { System.out.println("Sending message " + msg.toCommonMessage()); writeProxy.write(socketID, msg.toCommonMessage()); @@ -79,7 +68,6 @@ public class LoginCredentialProcessor implements ObjectProcessor { public void process(Message message, long socketID, ObjectWriteProxy writeProxy) { message.nextStatus(); ConnectionManager connectionManager = ConnectionManager.getInstance(); - Contact recipient = PersistenceManager.getInstance().getContactById(message.getID()); + Contact recipient = PersistenceManager.getInstance().getContactByID(message.getID()); if (recipient instanceof envoy.server.data.User) { System.out.println("The received message is a direct message."); @@ -43,7 +43,7 @@ public class MessageProcessor implements ObjectProcessor { } } else { System.out.println("The received message is a group message."); - final var members = PersistenceManager.getInstance().getGroupById(message.getRecipientID()).getContacts(); + final var members = PersistenceManager.getInstance().getGroupByID(message.getRecipientID()).getContacts(); final var generator = IDGeneratorRequestProcessor.createIDGenerator(members.size()); members.forEach(user -> { envoy.data.Message returnMessage = new MessageBuilder(message.getRecipientID(), user.getID(), generator) diff --git a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java index 587fd0e..d88e120 100755 --- a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java @@ -27,7 +27,7 @@ public class MessageStatusChangeProcessor implements ObjectProcessor { public void process(NameChangeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { PersistenceManager persistenceManager = PersistenceManager.getInstance(); ConnectionManager connectionManager = ConnectionManager.getInstance(); - Contact toUpdate = persistenceManager.getContactById(input.getID()); + Contact toUpdate = persistenceManager.getContactByID(input.getID()); toUpdate.setName(input.get()); persistenceManager.updateContact(toUpdate); diff --git a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java index 68547fc..ad63480 100755 --- a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java @@ -30,7 +30,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor