From 2b1adad1ec1fff978c78a1dadb6f5572152d80af Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 11 Feb 2020 17:44:58 +0100 Subject: [PATCH] Implemented online contact notification about contact list modification --- .../processors/ContactOperationProcessor.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/java/envoy/server/processors/ContactOperationProcessor.java b/src/main/java/envoy/server/processors/ContactOperationProcessor.java index 475f8a2..0395026 100644 --- a/src/main/java/envoy/server/processors/ContactOperationProcessor.java +++ b/src/main/java/envoy/server/processors/ContactOperationProcessor.java @@ -1,9 +1,10 @@ package envoy.server.processors; import java.io.IOException; +import java.util.Arrays; +import envoy.data.Contacts; import envoy.event.ContactOperationEvent; -import envoy.event.ContactOperationEvent.Operation; import envoy.server.ConnectionManager; import envoy.server.ObjectProcessor; import envoy.server.database.PersistenceManager; @@ -19,12 +20,25 @@ import envoy.server.net.ObjectWriteProxy; */ public class ContactOperationProcessor implements ObjectProcessor { + private static final ConnectionManager connectionManager = ConnectionManager.getInstance(); + @Override - public void process(ContactOperationEvent operation, long socketId, ObjectWriteProxy writeProxy) throws IOException { - if (operation.getOperationType() == Operation.ADD) { - long userId = ConnectionManager.getInstance().getUserIdBySocketId(socketId); - System.out.printf("Adding user %s to the contact list of user %d.%n", operation.get(), userId); - PersistenceManager.getPersistenceManager().addContact(userId, operation.get().getId()); + public void process(ContactOperationEvent evt, long socketId, ObjectWriteProxy writeProxy) throws IOException { + switch (evt.getOperationType()) { + case ADD: + final long userId = ConnectionManager.getInstance().getUserIdBySocketId(socketId); + final long contactId = evt.get().getId(); + + System.out.printf("Adding user %s to the contact list of user %d.%n", evt.get(), userId); + PersistenceManager.getPersistenceManager().addContact(userId, contactId); + + // Notify the contact + // TODO: cache notification for offline mode + if (ConnectionManager.getInstance().isOnline(contactId)) writeProxy.write(connectionManager.getSocketId(contactId), + new Contacts(Arrays.asList(PersistenceManager.getPersistenceManager().getUserById(userId).toCommonUser()))); + break; + default: + System.err.printf("Received %s with an unsupported operation.%n", evt); } }