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

95 lines
3.3 KiB
Java
Executable File

package envoy.server.processors;
import java.io.IOException;
import envoy.data.User.UserStatus;
import envoy.event.UserStatusChangeEvent;
import envoy.server.data.PersistenceManager;
import envoy.server.data.User;
import envoy.server.net.ConnectionManager;
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> {
private static ObjectWriteProxy writeProxy;
private static PersistenceManager persistenceManager = PersistenceManager.getInstance();
@Override
public Class<UserStatusChangeEvent> getInputClass() { return UserStatusChangeEvent.class; }
@Override
public void process(UserStatusChangeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException {
// new status should not equal old status
if (input.get().equals(persistenceManager.getUserByID(input.getID()).getStatus())) {
System.out.println("Received an unnecessary UserStatusChangeEvent");
return;
}
updateUserStatus(input);
}
/**
* Sets the {@link UserStatus} for a given user. Both offline contacts and
* currently online contacts are notified.
*
* @param user the {@link UserStatusChangeEvent} that signals the change
* @since Envoy Server Standalone v0.1-alpha
*/
public static void updateUserStatus(User user) {
// handling for newly logged in clients
persistenceManager.updateContact(user);
// handling for contacts that are already online
notifyContacts(user);
}
/**
* @param evt the {@link UserStatusChangeEvent}
* @since Envoy Server Standalone v0.1-alpha
*/
public static void updateUserStatus(UserStatusChangeEvent evt) { updateUserStatus(persistenceManager.getUserByID(evt.getID())); }
/**
* notifies active contacts of this {@link User} that his {@link UserStatus} has
* changed
*
* @param user the {@link User}
* @since Envoy Server Standalone v0.1-alpha
*/
private static void notifyContacts(User user) {
UserStatusChangeEvent evt = new UserStatusChangeEvent(user.getID(), user.getStatus());
ConnectionManager connectionManager = ConnectionManager.getInstance();
try {
for (envoy.server.data.Contact contact : user.getContacts())
if (connectionManager.isOnline(contact.getID())) writeProxy.write(connectionManager.getSocketId(contact.getID()), evt);
} catch (IOException e) {
e.printStackTrace();
System.err.println("Could not notify online contacts of user " + evt.getID() + " that his status has been changed");
}
}
/**
* This method is only called by the LoginCredentialProcessor because every
* user needs to login (open a socket) before changing his status.
* Needed to ensure propagation of events because an uninitialized writeProxy
* would cause problems.
*
* @param writeProxy the writeProxy that is used to send objects back to clients
* @since Envoy Server Standalone v0.1-alpha
*/
public static void setWriteProxy(ObjectWriteProxy writeProxy) { UserStatusChangeProcessor.writeProxy = writeProxy; }
// TODO may cause an problem if two clients log in at the same time.
// Change Needed.
}