66 lines
2.3 KiB
Java
Executable File
66 lines
2.3 KiB
Java
Executable File
package envoy.server.processors;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
import envoy.data.User.UserStatus;
|
|
import envoy.event.UserStatusChange;
|
|
import envoy.server.data.*;
|
|
import envoy.server.net.ObjectWriteProxy;
|
|
import envoy.util.EnvoyLog;
|
|
|
|
/**
|
|
* This processor handles incoming {@link UserStatusChange}.
|
|
*
|
|
* @author Leon Hofmeister
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public final class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChange> {
|
|
|
|
private static ObjectWriteProxy writeProxy;
|
|
|
|
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
|
|
private static final Logger logger = EnvoyLog.getLogger(UserStatusChangeProcessor.class);
|
|
|
|
@Override
|
|
public void process(UserStatusChange input, long socketID, ObjectWriteProxy writeProxy) {
|
|
// new status should not equal old status
|
|
if (input.get().equals(persistenceManager.getUserByID(input.getID()).getStatus())) {
|
|
logger.warning("Received an unnecessary UserStatusChange");
|
|
return;
|
|
}
|
|
updateUserStatus(persistenceManager.getUserByID(input.getID()), input.get());
|
|
}
|
|
|
|
/**
|
|
* Sets the {@link UserStatus} for a given user. Both offline contacts and
|
|
* currently online contacts are notified.
|
|
*
|
|
* @param user the user whose status has changed
|
|
* @param newStatus the new status of that user
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
|
|
public static void updateUserStatus(User user, UserStatus newStatus) {
|
|
user.setStatus(newStatus);
|
|
|
|
// Handling for newly logged in clients
|
|
persistenceManager.updateContact(user);
|
|
|
|
// Handling for contacts that are already online
|
|
writeProxy.writeToOnlineContacts(user.getContacts(), new UserStatusChange(user.getID(), user.getStatus()));
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
}
|