Implemented GroupCreationProcessor and GroupResizeProcessor

This commit is contained in:
DieGurke 2020-04-03 19:35:39 +02:00
parent 64bc34cf61
commit 2fbff47752
2 changed files with 72 additions and 4 deletions

View File

@ -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<GroupCreationEvent> {
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

View File

@ -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: <strong>envoy-server-standalone</strong><br>
* File: <strong>GroupResizeProcessor.java</strong><br>
* Created: <strong>03.04.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-beta
*/
public class GroupResizeProcessor implements ObjectProcessor<GroupResizeEvent> {
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<GroupResizeEvent> getInputClass() { return GroupResizeEvent.class; }
}