From 2fbff4775289cd150ec5ea1e31d2ae351617eb7a Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Fri, 3 Apr 2020 19:35:39 +0200 Subject: [PATCH] Implemented GroupCreationProcessor and GroupResizeProcessor --- .../processors/GroupCreationProcessor.java | 22 ++++++-- .../processors/GroupResizeProcessor.java | 54 +++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 src/main/java/envoy/server/processors/GroupResizeProcessor.java diff --git a/src/main/java/envoy/server/processors/GroupCreationProcessor.java b/src/main/java/envoy/server/processors/GroupCreationProcessor.java index 01b133c..90dae85 100644 --- a/src/main/java/envoy/server/processors/GroupCreationProcessor.java +++ b/src/main/java/envoy/server/processors/GroupCreationProcessor.java @@ -3,7 +3,9 @@ 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; /** @@ -17,16 +19,28 @@ import envoy.server.net.ObjectWriteProxy; 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()); - // TODO adjust event, so it sends a members list as well, which can be initially - // set here + 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); // TODO Prepare the client to receive the group object after sending the - // groupCreationEvent to the server. + 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 diff --git a/src/main/java/envoy/server/processors/GroupResizeProcessor.java b/src/main/java/envoy/server/processors/GroupResizeProcessor.java new file mode 100644 index 0000000..d7ee615 --- /dev/null +++ b/src/main/java/envoy/server/processors/GroupResizeProcessor.java @@ -0,0 +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; } +}