This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java

78 lines
2.9 KiB
Java

package envoy.server.processors;
import java.io.IOException;
import envoy.data.User.UserStatus;
import envoy.event.UserStatusChangeEvent;
import envoy.server.ConnectionManager;
import envoy.server.ObjectProcessor;
import envoy.server.data.User;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ObjectWriteProxy;
/**
* This processor handles incoming {@link UserStatusChangeEvent}.<br>
* <br>
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>UserStatusChangeProcessor.java</strong><br>
* Created: <strong>1 Feb 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Server Standalone v0.1-alpha
*/
public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChangeEvent> {
@Override
public Class<UserStatusChangeEvent> getInputClass() { return UserStatusChangeEvent.class; }
@Override
public void process(UserStatusChangeEvent input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
PersistenceManager perMan = PersistenceManager.getPersistenceManager();
// new status should not equal old status
if (input.get().equals(perMan.getUserById(input.getId()).getStatus())) {
System.out.println("Received an unnecessary UserStatusChangeEvent");
return;
}
updateUserStatus(input, writeProxy);
}
/**
* Sets the {@link UserStatus} for a given user. Both offline contacts and
* currently online contacts are notified.
*
* @param evt the {@link UserStatusChangeEvent} that signals the change
* @param writeProxy the {@link ObjectWriteProxy} that is used to send objects
* back to clients
* @throws IOException if sending this update failed for any contact
* @since Envoy Server Standalone v0.1-alpha
*/
public static void updateUserStatus(UserStatusChangeEvent evt, ObjectWriteProxy writeProxy) throws IOException {
PersistenceManager perMan = PersistenceManager.getPersistenceManager();
envoy.server.data.User user = perMan.getUserById(evt.getId());
// handling for newly logged in clients
perMan.updateUserStatus(user, evt.get());
// handling for contacts that are already online
notifyContacts(evt, user, writeProxy);
}
/**
* notifies active contacts of this {@link User} that his {@link UserStatus} has
* changed
*
* @param evt the {@link UserStatusChangeEvent} to send to other clients
* @param user the {@link User}
* @param writeProxy the {@link ObjectWriteProxy} that is used to send objects
* back to clients
* @throws IOException if sending this update failed for any contact
* @since Envoy Server Standalone v0.1-alpha
*/
public static void notifyContacts(UserStatusChangeEvent evt, envoy.server.data.User user, ObjectWriteProxy writeProxy) throws IOException {
ConnectionManager conMan = ConnectionManager.getInstance();
for (User contact : user.getContacts())
if (conMan.isOnline(contact.getId())) writeProxy.write(conMan.getSocketId(contact.getId()), evt);
}
}