fixed not updating user status and implemented easier to read structure
This commit is contained in:
parent
98e5245780
commit
092a4610c1
@ -1,5 +1,6 @@
|
||||
package envoy.server;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
@ -7,8 +8,7 @@ import java.util.Set;
|
||||
|
||||
import com.jenkov.nioserver.ISocketIdListener;
|
||||
|
||||
import envoy.data.User;
|
||||
import envoy.event.UserStatusChangeEvent;
|
||||
import envoy.data.User.UserStatus;
|
||||
import envoy.server.database.PersistenceManager;
|
||||
import envoy.server.processors.UserStatusChangeProcessor;
|
||||
|
||||
@ -51,9 +51,10 @@ public class ConnectionManager implements ISocketIdListener {
|
||||
public void socketCancelled(long socketId) {
|
||||
if (!pendingSockets.remove(socketId)) {
|
||||
// notifying contacts of this users offline-going
|
||||
long clientId = getUserIdBySocketId(socketId);
|
||||
User user = new User(clientId, PersistenceManager.getPersistenceManager().getUserById(clientId).getName());
|
||||
UserStatusChangeProcessor.updateUserStatus(new UserStatusChangeEvent(user));
|
||||
envoy.server.data.User user = PersistenceManager.getPersistenceManager().getUserById(getUserIdBySocketId(socketId));
|
||||
user.setStatus(UserStatus.OFFLINE);
|
||||
user.setLastSeen(new Date());
|
||||
UserStatusChangeProcessor.updateUserStatus(user);
|
||||
|
||||
// removing the socket
|
||||
sockets.entrySet().stream().filter(e -> e.getValue() == socketId).forEach(e -> sockets.remove(e.getValue()));
|
||||
|
@ -5,7 +5,6 @@ import java.util.List;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import envoy.data.User.UserStatus;
|
||||
import envoy.server.data.ConfigItem;
|
||||
import envoy.server.data.Message;
|
||||
import envoy.server.data.User;
|
||||
@ -220,20 +219,4 @@ public class PersistenceManager {
|
||||
// TODO current solution gets all users, not just contacts. Should be changed to
|
||||
// entityManager.createNamedQuery("getContactsOfUser").setParameter("user",
|
||||
// user).getResultList();
|
||||
|
||||
/**
|
||||
* Updates the {@link UserStatus} in the database.
|
||||
*
|
||||
* @param user the {@link User} who changes his status
|
||||
* @param status the new status of that user
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void updateUserStatus(User user, UserStatus status) {
|
||||
if (user.getStatus().equals(status))
|
||||
System.out.println("Received an UserStatusChangeEvent for user " + user.getId() + " to update that this user already has");
|
||||
else {
|
||||
user.setStatus(status);
|
||||
persistenceManager.updateUser(user);
|
||||
}
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@ import envoy.data.Contacts;
|
||||
import envoy.data.LoginCredentials;
|
||||
import envoy.data.Message.MessageStatus;
|
||||
import envoy.data.User;
|
||||
import envoy.event.UserStatusChangeEvent;
|
||||
import envoy.data.User.UserStatus;
|
||||
import envoy.server.ConnectionManager;
|
||||
import envoy.server.ObjectProcessor;
|
||||
import envoy.server.data.Message;
|
||||
@ -48,7 +48,8 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
|
||||
|
||||
// notifies contacts of this users online-going and updates his status in the
|
||||
// database
|
||||
UserStatusChangeProcessor.updateUserStatus(new UserStatusChangeEvent(user.toCommonUser()));
|
||||
user.setStatus(UserStatus.ONLINE);
|
||||
UserStatusChangeProcessor.updateUserStatus(user);
|
||||
|
||||
// Create contacts
|
||||
Contacts contacts = new Contacts(user.getId(),
|
||||
|
@ -22,16 +22,16 @@ import envoy.server.net.ObjectWriteProxy;
|
||||
*/
|
||||
public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChangeEvent> {
|
||||
|
||||
private static ObjectWriteProxy writeProxy;
|
||||
private static ObjectWriteProxy writeProxy;
|
||||
private static PersistenceManager persistenceManager = PersistenceManager.getPersistenceManager();
|
||||
|
||||
@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())) {
|
||||
if (input.get().equals(persistenceManager.getUserById(input.getId()).getStatus())) {
|
||||
System.out.println("Received an unnecessary UserStatusChangeEvent");
|
||||
return;
|
||||
}
|
||||
@ -43,17 +43,24 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
|
||||
* Sets the {@link UserStatus} for a given user. Both offline contacts and
|
||||
* currently online contacts are notified.
|
||||
*
|
||||
* @param evt the {@link UserStatusChangeEvent} that signals the change
|
||||
* @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) {
|
||||
// handling for newly logged in clients
|
||||
PersistenceManager perMan = PersistenceManager.getPersistenceManager();
|
||||
envoy.server.data.User user = perMan.getUserById(evt.getId());
|
||||
perMan.updateUserStatus(user, evt.get());
|
||||
|
||||
// handling for contacts that are already online
|
||||
notifyContacts(evt, user);
|
||||
updateUserStatus(persistenceManager.getUserById(evt.getId()));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,11 +72,12 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
|
||||
* @throws IOException if sending this update failed for any contact
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
private static void notifyContacts(UserStatusChangeEvent evt, envoy.server.data.User user) {
|
||||
ConnectionManager conMan = ConnectionManager.getInstance();
|
||||
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 (conMan.isOnline(contact.getId())) writeProxy.write(conMan.getSocketId(contact.getId()), evt);
|
||||
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 changed");
|
||||
|
Reference in New Issue
Block a user