2020-02-01 23:37:44 +01:00
|
|
|
package envoy.server.processors;
|
|
|
|
|
2020-06-11 12:06:06 +02:00
|
|
|
import java.util.logging.Logger;
|
2020-02-01 23:37:44 +01:00
|
|
|
|
|
|
|
import envoy.data.User.UserStatus;
|
2020-06-20 09:57:54 +02:00
|
|
|
import envoy.event.UserStatusChange;
|
2020-10-19 18:17:51 +02:00
|
|
|
import envoy.util.EnvoyLog;
|
|
|
|
|
2020-09-25 14:29:23 +02:00
|
|
|
import envoy.server.data.*;
|
2020-02-01 23:37:44 +01:00
|
|
|
import envoy.server.net.ObjectWriteProxy;
|
|
|
|
|
|
|
|
/**
|
2020-07-06 11:40:13 +02:00
|
|
|
* This processor handles incoming {@link UserStatusChange}.
|
2020-02-01 23:37:44 +01:00
|
|
|
*
|
|
|
|
* @author Leon Hofmeister
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-08-22 13:37:07 +02:00
|
|
|
public final class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChange> {
|
2020-02-01 23:37:44 +01:00
|
|
|
|
2020-07-06 11:40:13 +02:00
|
|
|
private static ObjectWriteProxy writeProxy;
|
2020-02-02 13:34:28 +01:00
|
|
|
|
2020-07-06 11:40:13 +02:00
|
|
|
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
|
2020-10-19 18:17:51 +02:00
|
|
|
private static final Logger logger =
|
|
|
|
EnvoyLog.getLogger(UserStatusChangeProcessor.class);
|
2020-06-11 12:06:06 +02:00
|
|
|
|
2020-02-01 23:37:44 +01:00
|
|
|
@Override
|
2020-07-06 11:40:13 +02:00
|
|
|
public void process(UserStatusChange input, long socketID, ObjectWriteProxy writeProxy) {
|
2020-02-01 23:37:44 +01:00
|
|
|
// new status should not equal old status
|
2020-04-06 22:55:27 +02:00
|
|
|
if (input.get().equals(persistenceManager.getUserByID(input.getID()).getStatus())) {
|
2020-06-20 09:57:54 +02:00
|
|
|
logger.warning("Received an unnecessary UserStatusChange");
|
2020-02-01 23:37:44 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-10-07 23:43:30 +02:00
|
|
|
updateUserStatus(persistenceManager.getUserByID(input.getID()), input.get());
|
2020-02-01 23:37:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Sets the {@link UserStatus} for a given user. Both offline contacts and currently online
|
|
|
|
* contacts are notified.
|
2020-02-01 23:37:44 +01:00
|
|
|
*
|
2020-10-07 23:43:30 +02:00
|
|
|
* @param user the user whose status has changed
|
|
|
|
* @param newStatus the new status of that user
|
2020-02-01 23:37:44 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-02-05 21:32:53 +01:00
|
|
|
|
2020-10-07 23:43:30 +02:00
|
|
|
public static void updateUserStatus(User user, UserStatus newStatus) {
|
|
|
|
user.setStatus(newStatus);
|
2020-06-11 12:06:06 +02:00
|
|
|
|
|
|
|
// Handling for newly logged in clients
|
2020-03-25 16:34:55 +01:00
|
|
|
persistenceManager.updateContact(user);
|
2020-02-01 23:37:44 +01:00
|
|
|
|
2020-06-11 12:06:06 +02:00
|
|
|
// Handling for contacts that are already online
|
2020-10-19 18:17:51 +02:00
|
|
|
writeProxy.writeToOnlineContacts(user.getContacts(),
|
|
|
|
new UserStatusChange(user.getID(), user.getStatus()));
|
2020-02-05 21:32:53 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 13:34:28 +01:00
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* 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.
|
2020-02-02 13:34:28 +01:00
|
|
|
*
|
|
|
|
* @param writeProxy the writeProxy that is used to send objects back to clients
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public static void setWriteProxy(ObjectWriteProxy writeProxy) {
|
|
|
|
UserStatusChangeProcessor.writeProxy = writeProxy;
|
|
|
|
}
|
2020-02-05 21:53:36 +01:00
|
|
|
// TODO may cause an problem if two clients log in at the same time.
|
|
|
|
// Change Needed.
|
|
|
|
}
|