98 lines
3.5 KiB
Java
98 lines
3.5 KiB
Java
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> {
|
|
|
|
private static ObjectWriteProxy writeProxy;
|
|
private static PersistenceManager persistenceManager = PersistenceManager.getInstance();
|
|
|
|
@Override
|
|
public Class<UserStatusChangeEvent> getInputClass() { return UserStatusChangeEvent.class; }
|
|
|
|
@Override
|
|
public void process(UserStatusChangeEvent input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
|
// new status should not equal old status
|
|
if (input.get().equals(persistenceManager.getUserById(input.getId()).getStatus())) {
|
|
System.out.println("Received an unnecessary UserStatusChangeEvent");
|
|
return;
|
|
}
|
|
updateUserStatus(input);
|
|
|
|
}
|
|
|
|
/**
|
|
* Sets the {@link UserStatus} for a given user. Both offline contacts and
|
|
* currently online contacts are notified.
|
|
*
|
|
* @param user the {@link UserStatusChangeEvent} that signals the change
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
|
|
public static void updateUserStatus(User user) {
|
|
// handling for newly logged in clients
|
|
persistenceManager.updateUser(user);
|
|
|
|
// handling for contacts that are already online
|
|
notifyContacts(user);
|
|
}
|
|
|
|
/**
|
|
* @param evt the {@link UserStatusChangeEvent}
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public static void updateUserStatus(UserStatusChangeEvent evt) { updateUserStatus(persistenceManager.getUserById(evt.getId())); }
|
|
|
|
/**
|
|
* notifies active contacts of this {@link User} that his {@link UserStatus} has
|
|
* changed
|
|
*
|
|
* @param evt the {@link UserStatusChangeEvent} to send to other clients
|
|
* @param user the {@link User}
|
|
* @throws IOException if sending this update failed for any contact
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
private static void notifyContacts(User user) {
|
|
UserStatusChangeEvent evt = new UserStatusChangeEvent(user.getId(), user.getStatus());
|
|
ConnectionManager connectionManager = ConnectionManager.getInstance();
|
|
try {
|
|
for (User contact : user.getContacts())
|
|
if (connectionManager.isOnline(contact.getId())) writeProxy.write(connectionManager.getSocketId(contact.getId()), evt);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
System.err.println("Could not notify online contacts of user " + evt.getId() + " that his status has been changed");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
}
|