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