Added UserStatusChangeProcessor

additionally added UserStatus updating in the LoginCredentialProcessor
yet missing: method to handle clients going offline
This commit is contained in:
delvh 2020-02-01 23:37:44 +01:00
parent 6932a62aa9
commit 5b28f2f25b
8 changed files with 103 additions and 12 deletions

View File

@ -28,7 +28,7 @@
<dependency> <dependency>
<groupId>com.github.informatik-ag-ngl</groupId> <groupId>com.github.informatik-ag-ngl</groupId>
<artifactId>envoy-common</artifactId> <artifactId>envoy-common</artifactId>
<version>develop-SNAPSHOT</version> <version>f~user_status_change_event-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.informatik-ag-ngl</groupId> <groupId>com.github.informatik-ag-ngl</groupId>

View File

@ -64,7 +64,7 @@ public class ConnectionManager implements ISocketIdListener {
} }
/** /**
* @param userId the ID of the user registered at the a socket * @param userId the ID of the user registered at a socket
* @return the ID of the socket * @return the ID of the socket
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */

View File

@ -36,6 +36,7 @@ public class Startup {
processors.add(new LoginCredentialProcessor()); processors.add(new LoginCredentialProcessor());
processors.add(new MessageProcessor()); processors.add(new MessageProcessor());
processors.add(new MessageStatusChangeProcessor()); processors.add(new MessageStatusChangeProcessor());
processors.add(new UserStatusChangeProcessor());
processors.add(new IdGeneratorRequestProcessor()); processors.add(new IdGeneratorRequestProcessor());
Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors)); Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));

View File

@ -5,6 +5,7 @@ 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;
@ -164,4 +165,16 @@ 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 updateUserStatusInDatabase(User user, UserStatus status) {
user.setStatus(status);
persistenceManager.updateUser(user);
}
} }

View File

@ -33,7 +33,6 @@ public class ObjectMessageProcessor implements IMessageProcessor {
*/ */
public ObjectMessageProcessor(Set<ObjectProcessor<?>> processors) { this.processors = processors; } public ObjectMessageProcessor(Set<ObjectProcessor<?>> processors) { this.processors = processors; }
@SuppressWarnings("unchecked")
@Override @Override
public void process(Message message, WriteProxy writeProxy) { public void process(Message message, WriteProxy writeProxy) {
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) { try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) {

View File

@ -22,7 +22,7 @@ public class ObjectWriteProxy {
private final WriteProxy writeProxy; private final WriteProxy writeProxy;
/** /**
* Creates an instance of @link{ObjectWriteProxy}. * Creates an instance of {@link ObjectWriteProxy}.
* *
* @param writeProxy the {@link WriteProxy} to write objects to another client * @param writeProxy the {@link WriteProxy} to write objects to another client
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha

View File

@ -10,6 +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.server.ConnectionManager; import envoy.server.ConnectionManager;
import envoy.server.ObjectProcessor; import envoy.server.ObjectProcessor;
import envoy.server.data.Message; import envoy.server.data.Message;
@ -41,9 +42,10 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
envoy.server.data.User user = getUser(input); envoy.server.data.User user = getUser(input);
// Not logged in successfully // Not logged in successfully
if (user == null) { if (user == null) return;
return; // notifies contacts of this users online-going and updates his status in the
} // database
UserStatusChangeProcessor.updateUserStatus(new UserStatusChangeEvent(user.toCommonUser()), writeProxy);
ConnectionManager.getInstance().registerUser(user.getId(), socketId); ConnectionManager.getInstance().registerUser(user.getId(), socketId);
@ -84,11 +86,9 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
} else { } else {
user = persistenceManager.getUserByName(credentials.getName()); user = persistenceManager.getUserByName(credentials.getName());
// TODO: Implement error when user does not exist // TODO: Implement error when user does not exist
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) { if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) // TODO: Wrong Password Response
// TODO: Wrong Password Response
return null; return null;
} }
}
return user; return user;
} }
} }

View File

@ -0,0 +1,78 @@
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> {
@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;
}
updateUserStatus(input, writeProxy);
}
/**
* 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 writeProxy the {@link ObjectWriteProxy} that is used to send objects
* back to clients
* @throws IOException if sending this update failed for any contact
* @since Envoy Server Standalone v0.1-alpha
*/
public static void updateUserStatus(UserStatusChangeEvent evt, ObjectWriteProxy writeProxy) throws IOException {
PersistenceManager perMan = PersistenceManager.getPersistenceManager();
envoy.server.data.User user = perMan.getUserById(evt.getId());
// handling for newly logged in clients
perMan.updateUserStatusInDatabase(user, evt.get());
// handling for contacts that are already online
notifyContacts(evt, user, writeProxy);
}
/**
* 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}
* @param writeProxy the {@link ObjectWriteProxy} that is used to send objects
* back to clients
* @throws IOException if sending this update failed for any contact
* @since Envoy Server Standalone v0.1-alpha
*/
public static void notifyContacts(UserStatusChangeEvent evt, envoy.server.data.User user, ObjectWriteProxy writeProxy) throws IOException {
ConnectionManager conMan = ConnectionManager.getInstance();
for (User contact : user.getContacts())
if (conMan.isOnline(contact.getId())) writeProxy.write(conMan.getSocketId(contact.getId()), evt);
}
}