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-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-06-11 12:06:06 +02:00
|
|
|
import envoy.util.EnvoyLog;
|
2020-02-01 23:37:44 +01:00
|
|
|
|
|
|
|
/**
|
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();
|
|
|
|
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-02-02 13:34:28 +01:00
|
|
|
updateUserStatus(input);
|
2020-02-01 23:37:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the {@link UserStatus} for a given user. Both offline contacts and
|
|
|
|
* currently online contacts are notified.
|
|
|
|
*
|
2020-06-20 09:57:54 +02:00
|
|
|
* @param user the {@link UserStatusChange} that signals the change
|
2020-02-01 23:37:44 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-02-05 21:32:53 +01:00
|
|
|
|
|
|
|
public static void updateUserStatus(User user) {
|
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-07-06 11:40:13 +02:00
|
|
|
writeProxy.writeToOnlineContacts(user.getContacts(), new UserStatusChange(user.getID(), user.getStatus()));
|
2020-02-05 21:32:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-20 09:57:54 +02:00
|
|
|
* @param evt the {@link UserStatusChange}
|
2020-02-05 21:32:53 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-06-20 09:57:54 +02:00
|
|
|
public static void updateUserStatus(UserStatusChange evt) { updateUserStatus(persistenceManager.getUserByID(evt.getID())); }
|
2020-02-01 23:37:44 +01:00
|
|
|
|
2020-02-02 13:34:28 +01:00
|
|
|
/**
|
|
|
|
* This method is only called by the LoginCredentialProcessor because every
|
|
|
|
* user needs to login (open a socket) before changing his status.
|
2020-02-04 21:34:53 +01:00
|
|
|
* Needed to ensure propagation of events because an uninitialized writeProxy
|
2020-02-02 13:34:28 +01:00
|
|
|
* 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; }
|
2020-02-05 21:53:36 +01:00
|
|
|
// TODO may cause an problem if two clients log in at the same time.
|
|
|
|
// Change Needed.
|
|
|
|
}
|