2020-02-08 15:03:03 +01:00
|
|
|
package envoy.server.processors;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2020-06-09 17:05:52 +02:00
|
|
|
import envoy.event.ElementOperation;
|
2020-04-02 16:32:23 +02:00
|
|
|
import envoy.event.contact.ContactOperationEvent;
|
2020-02-15 22:43:20 +01:00
|
|
|
import envoy.server.data.PersistenceManager;
|
2020-02-12 22:01:32 +01:00
|
|
|
import envoy.server.net.ConnectionManager;
|
2020-02-08 15:03:03 +01:00
|
|
|
import envoy.server.net.ObjectWriteProxy;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>envoy-server-standalone</strong><br>
|
|
|
|
* File: <strong>ContactOperationProcessor.java</strong><br>
|
|
|
|
* Created: <strong>08.02.2020</strong><br>
|
2020-02-10 22:29:34 +01:00
|
|
|
*
|
2020-02-08 15:03:03 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-02-10 22:29:34 +01:00
|
|
|
public class ContactOperationProcessor implements ObjectProcessor<ContactOperationEvent> {
|
2020-02-08 15:03:03 +01:00
|
|
|
|
2020-02-11 17:44:58 +01:00
|
|
|
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
|
|
|
|
|
2020-02-08 15:03:03 +01:00
|
|
|
@Override
|
2020-02-11 17:44:58 +01:00
|
|
|
public void process(ContactOperationEvent evt, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
|
|
|
switch (evt.getOperationType()) {
|
|
|
|
case ADD:
|
2020-03-26 20:14:09 +01:00
|
|
|
final long userID = ConnectionManager.getInstance().getUserIdBySocketId(socketId);
|
2020-03-26 16:06:55 +01:00
|
|
|
final long contactId = evt.get().getID();
|
2020-02-11 17:44:58 +01:00
|
|
|
|
2020-03-26 20:14:09 +01:00
|
|
|
System.out.printf("Adding user %s to the contact list of user %d.%n", evt.get(), userID);
|
|
|
|
PersistenceManager.getInstance().addUserContact(userID, contactId);
|
2020-02-11 17:44:58 +01:00
|
|
|
|
2020-02-11 18:15:19 +01:00
|
|
|
// Notify the contact if online
|
2020-06-09 17:05:52 +02:00
|
|
|
if (ConnectionManager.getInstance().isOnline(contactId))
|
|
|
|
writeProxy.write(connectionManager.getSocketId(contactId),
|
|
|
|
new ContactOperationEvent(PersistenceManager.getInstance().getUserByID(userID).toCommon(), ElementOperation.ADD));
|
2020-02-11 17:44:58 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
System.err.printf("Received %s with an unsupported operation.%n", evt);
|
2020-02-08 15:03:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-02-10 22:29:34 +01:00
|
|
|
public Class<ContactOperationEvent> getInputClass() { return ContactOperationEvent.class; }
|
2020-02-08 15:03:03 +01:00
|
|
|
}
|