70 lines
2.4 KiB
Java
Executable File
70 lines
2.4 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(input);
|
|
}
|
|
|
|
/**
|
|
* Sets the {@link UserStatus} for a given user. Both offline contacts and
|
|
* currently online contacts are notified.
|
|
*
|
|
* @param user the {@link UserStatusChange} 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
|
|
writeProxy.writeToOnlineContacts(user.getContacts(), new UserStatusChange(user.getID(), user.getStatus()));
|
|
}
|
|
|
|
/**
|
|
* @param evt the {@link UserStatusChange}
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public static void updateUserStatus(UserStatusChange evt) { updateUserStatus(persistenceManager.getUserByID(evt.getID())); }
|
|
|
|
/**
|
|
* 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.
|
|
}
|