2020-02-01 23:37:44 +01:00
|
|
|
package envoy.server.processors;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import envoy.data.User.UserStatus;
|
|
|
|
import envoy.event.UserStatusChangeEvent;
|
|
|
|
import envoy.server.ConnectionManager;
|
|
|
|
import envoy.server.ObjectProcessor;
|
|
|
|
import envoy.server.data.User;
|
|
|
|
import envoy.server.database.PersistenceManager;
|
|
|
|
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> {
|
|
|
|
|
2020-02-02 13:34:28 +01:00
|
|
|
private static ObjectWriteProxy writeProxy;
|
|
|
|
|
2020-02-01 23:37:44 +01:00
|
|
|
@Override
|
|
|
|
public Class<UserStatusChangeEvent> getInputClass() { return UserStatusChangeEvent.class; }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void process(UserStatusChangeEvent input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
|
|
|
PersistenceManager perMan = PersistenceManager.getPersistenceManager();
|
|
|
|
// new status should not equal old status
|
|
|
|
if (input.get().equals(perMan.getUserById(input.getId()).getStatus())) {
|
|
|
|
System.out.println("Received an unnecessary UserStatusChangeEvent");
|
|
|
|
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-02-02 13:34:28 +01:00
|
|
|
* @param evt the {@link UserStatusChangeEvent} that signals the change
|
2020-02-01 23:37:44 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-02-02 13:34:28 +01:00
|
|
|
public static void updateUserStatus(UserStatusChangeEvent evt) {
|
|
|
|
// handling for newly logged in clients
|
2020-02-01 23:37:44 +01:00
|
|
|
PersistenceManager perMan = PersistenceManager.getPersistenceManager();
|
|
|
|
envoy.server.data.User user = perMan.getUserById(evt.getId());
|
2020-02-02 12:32:10 +01:00
|
|
|
perMan.updateUserStatus(user, evt.get());
|
2020-02-01 23:37:44 +01:00
|
|
|
|
|
|
|
// handling for contacts that are already online
|
2020-02-02 13:34:28 +01:00
|
|
|
notifyContacts(evt, user);
|
2020-02-01 23:37:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* notifies active contacts of this {@link User} that his {@link UserStatus} has
|
|
|
|
* changed
|
|
|
|
*
|
2020-02-02 13:34:28 +01:00
|
|
|
* @param evt the {@link UserStatusChangeEvent} to send to other clients
|
|
|
|
* @param user the {@link User}
|
2020-02-01 23:37:44 +01:00
|
|
|
* @throws IOException if sending this update failed for any contact
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-02-02 13:34:28 +01:00
|
|
|
private static void notifyContacts(UserStatusChangeEvent evt, envoy.server.data.User user) {
|
2020-02-01 23:37:44 +01:00
|
|
|
ConnectionManager conMan = ConnectionManager.getInstance();
|
2020-02-02 13:34:28 +01:00
|
|
|
try {
|
|
|
|
for (User contact : user.getContacts())
|
|
|
|
if (conMan.isOnline(contact.getId())) writeProxy.write(conMan.getSocketId(contact.getId()), evt);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2020-02-04 21:09:59 +01:00
|
|
|
System.err.println("Could not notify online contacts of user " + evt.getId() + " that his status changed");
|
2020-02-02 13:34:28 +01:00
|
|
|
}
|
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.
|
|
|
|
* Needed to ensure propagation of events because an uninitialised 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; }
|
|
|
|
|
2020-02-01 23:37:44 +01:00
|
|
|
}
|