Some refactorings based on suggestions from @delvh
This commit is contained in:
parent
f1c49f0b95
commit
a1cfd728a5
@ -213,26 +213,26 @@ public class PersistenceManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a user to the contact list of another user and vice versa.
|
||||
* Adds a contact to the contact list of another contact and vice versa.
|
||||
*
|
||||
* @param userId1 the ID of the first user
|
||||
* @param userId2 the ID of the second user
|
||||
* @param contactID1 the ID of the first contact
|
||||
* @param contactID2 the ID of the second contact
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void addUserContact(long userId1, long userId2) {
|
||||
public void addContactBidirectional(long contactID1, long contactID2) {
|
||||
|
||||
// Get users by ID
|
||||
Contact u1 = getContactByID(userId1);
|
||||
Contact u2 = getContactByID(userId2);
|
||||
Contact c1 = getContactByID(contactID1);
|
||||
Contact c2 = getContactByID(contactID2);
|
||||
|
||||
// Add users to each others contact lists
|
||||
u1.getContacts().add(u2);
|
||||
u2.getContacts().add(u1);
|
||||
c1.getContacts().add(c2);
|
||||
c2.getContacts().add(c1);
|
||||
|
||||
// Synchronize changes with the database
|
||||
transaction.begin();
|
||||
entityManager.merge(u1);
|
||||
entityManager.merge(u2);
|
||||
entityManager.merge(c1);
|
||||
entityManager.merge(c2);
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,6 @@
|
||||
package envoy.server.net;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.jenkov.nioserver.ISocketIdListener;
|
||||
@ -53,7 +49,7 @@ public class ConnectionManager implements ISocketIdListener {
|
||||
public void socketCancelled(long socketID) {
|
||||
if (!pendingSockets.remove(socketID)) {
|
||||
// Notify contacts of this users offline-going
|
||||
envoy.server.data.User user = PersistenceManager.getInstance().getUserByID(getUserIdBySocketId(socketID));
|
||||
envoy.server.data.User user = PersistenceManager.getInstance().getUserByID(getUserIdBySocketID(socketID));
|
||||
user.setStatus(UserStatus.OFFLINE);
|
||||
user.setLastSeen(new Date());
|
||||
UserStatusChangeProcessor.updateUserStatus(user);
|
||||
@ -83,14 +79,14 @@ public class ConnectionManager implements ISocketIdListener {
|
||||
* @return the ID of the socket
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public long getSocketId(long userID) { return sockets.get(userID); }
|
||||
public long getSocketID(long userID) { return sockets.get(userID); }
|
||||
|
||||
/**
|
||||
* @param socketID the id of the socket whose User is needed
|
||||
* @return the userId associated with this socketId
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public long getUserIdBySocketId(long socketID) {
|
||||
public long getUserIdBySocketID(long socketID) {
|
||||
return sockets.entrySet().stream().filter(entry -> entry.getValue().equals(socketID)).findFirst().get().getKey();
|
||||
}
|
||||
|
||||
@ -108,8 +104,6 @@ public class ConnectionManager implements ISocketIdListener {
|
||||
public Set<Long> getOnlineUsers() { return sockets.keySet(); }
|
||||
|
||||
/**
|
||||
* Returns all members of a group who are currently online.
|
||||
*
|
||||
* @param group the group to search for
|
||||
* @return a set of all IDs of currently active members in this group
|
||||
* @since Envoy Server Standalone v0.1-beta
|
||||
|
@ -27,15 +27,15 @@ public class ContactOperationProcessor implements ObjectProcessor<ContactOperati
|
||||
public void process(ContactOperationEvent evt, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
||||
switch (evt.getOperationType()) {
|
||||
case ADD:
|
||||
final long userID = ConnectionManager.getInstance().getUserIdBySocketId(socketId);
|
||||
final long userID = ConnectionManager.getInstance().getUserIdBySocketID(socketId);
|
||||
final long contactId = evt.get().getID();
|
||||
|
||||
logger.fine(String.format("Adding user %s to the contact list of user %d.%n", evt.get(), userID));
|
||||
PersistenceManager.getInstance().addUserContact(userID, contactId);
|
||||
PersistenceManager.getInstance().addContactBidirectional(userID, contactId);
|
||||
|
||||
// Notify the contact if online
|
||||
if (ConnectionManager.getInstance().isOnline(contactId))
|
||||
writeProxy.write(connectionManager.getSocketId(contactId),
|
||||
writeProxy.write(connectionManager.getSocketID(contactId),
|
||||
new ContactOperationEvent(PersistenceManager.getInstance().getUserByID(userID).toCommon(), ElementOperation.ADD));
|
||||
break;
|
||||
default:
|
||||
|
@ -33,7 +33,7 @@ public class ContactSearchProcessor implements ObjectProcessor<ContactSearchRequ
|
||||
public void process(ContactSearchRequest request, long socketID, ObjectWriteProxy writeProxy) throws IOException {
|
||||
writeProxy.write(socketID,
|
||||
new ContactSearchResult(PersistenceManager.getInstance()
|
||||
.searchUsers(request.get(), ConnectionManager.getInstance().getUserIdBySocketId(socketID))
|
||||
.searchUsers(request.get(), ConnectionManager.getInstance().getUserIdBySocketID(socketID))
|
||||
.stream()
|
||||
.map(User::toCommon)
|
||||
.collect(Collectors.toList())));
|
||||
|
@ -30,15 +30,15 @@ public class GroupCreationProcessor implements ObjectProcessor<GroupCreationEven
|
||||
group.setName(input.get());
|
||||
group.setContacts(new HashSet<>());
|
||||
input.getInitialMemberIDs().stream().map(persistenceManager::getUserByID).forEach(group.getContacts()::add);
|
||||
group.getContacts().add(persistenceManager.getContactByID(connectionManager.getUserIdBySocketId(socketID)));
|
||||
group.getContacts().add(persistenceManager.getContactByID(connectionManager.getUserIdBySocketID(socketID)));
|
||||
group.getContacts().forEach(c -> c.getContacts().add(group));
|
||||
group.getContacts().add(persistenceManager.getUserByID(connectionManager.getUserIdBySocketId(socketID)));
|
||||
group.getContacts().add(persistenceManager.getUserByID(connectionManager.getUserIdBySocketID(socketID)));
|
||||
persistenceManager.addContact(group);
|
||||
group.getContacts()
|
||||
.stream()
|
||||
.map(Contact::getID)
|
||||
.filter(connectionManager::isOnline)
|
||||
.map(connectionManager::getSocketId)
|
||||
.map(connectionManager::getSocketID)
|
||||
.forEach(memberSocketID -> {
|
||||
try {
|
||||
writeProxy.write(memberSocketID, new ContactOperationEvent(group.toCommon(), ElementOperation.ADD));
|
||||
|
@ -31,10 +31,8 @@ public class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
|
||||
ConnectionManager connectionManager = ConnectionManager.getInstance();
|
||||
|
||||
final var members = PersistenceManager.getInstance().getGroupByID(groupMessage.getRecipientID()).getContacts();
|
||||
for (long i = 0; i < groupMessage.getMemberStatuses().size(); i++) {
|
||||
groupMessage.getMemberStatuses().replace(i, MessageStatus.SENT);
|
||||
}
|
||||
members.forEach(user -> { setMemberStatus(connectionManager, groupMessage, user.getID()); });
|
||||
groupMessage.getMemberStatuses().replaceAll((id, oldStatus) -> MessageStatus.SENT);
|
||||
members.forEach(user -> setMemberStatus(connectionManager, groupMessage, user.getID()));
|
||||
|
||||
// Checks if all memberMessageStatuses are RECEIVED and if so sets the
|
||||
// groupMessage Status to RECEIVED.
|
||||
@ -51,7 +49,7 @@ public class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
|
||||
private void sendToMember(ConnectionManager connectionManager, GroupMessage groupMessage, long memberID, ObjectWriteProxy writeProxy) {
|
||||
if (connectionManager.isOnline(memberID)) try {
|
||||
// If recipient is online, send the groupMessage directly
|
||||
writeProxy.write(connectionManager.getSocketId(memberID), groupMessage);
|
||||
writeProxy.write(connectionManager.getSocketID(memberID), groupMessage);
|
||||
} catch (IOException e) {
|
||||
logger.warning("Recipient online. Failed to send message" + groupMessage.getID());
|
||||
e.printStackTrace();
|
||||
|
@ -46,7 +46,7 @@ public class GroupResizeProcessor implements ObjectProcessor<GroupResizeEvent> {
|
||||
.stream()
|
||||
.map(Contact::getID)
|
||||
.filter(connectionManager::isOnline)
|
||||
.map(connectionManager::getSocketId)
|
||||
.map(connectionManager::getSocketID)
|
||||
.forEach(memberSocketID -> {
|
||||
try {
|
||||
writeProxy.write(memberSocketID, commonGroup);
|
||||
|
@ -69,7 +69,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
|
||||
if (connectionManager.isOnline(msg.getSender().getID())) {
|
||||
var evt = new MessageStatusChangeEvent(msg.toCommon());
|
||||
logger.info("Sending messageStatusChangeEvent to sender " + evt);
|
||||
writeProxy.write(connectionManager.getSocketId(msg.getSender().getID()), evt);
|
||||
writeProxy.write(connectionManager.getSocketID(msg.getSender().getID()), evt);
|
||||
}
|
||||
PersistenceManager.getInstance().updateMessage(msg);
|
||||
} else {
|
||||
|
@ -8,7 +8,6 @@ import javax.persistence.EntityExistsException;
|
||||
|
||||
import envoy.data.Message;
|
||||
import envoy.data.Message.MessageStatus;
|
||||
import envoy.event.MessageStatusChangeEvent;
|
||||
import envoy.server.data.PersistenceManager;
|
||||
import envoy.server.net.ConnectionManager;
|
||||
import envoy.server.net.ObjectWriteProxy;
|
||||
@ -31,7 +30,7 @@ public class MessageProcessor implements ObjectProcessor<Message> {
|
||||
|
||||
@Override
|
||||
public void process(Message message, long socketID, ObjectWriteProxy writeProxy) {
|
||||
if (message.getStatus!=MessageStatus.WAITING) {
|
||||
if (message.getStatus() != MessageStatus.WAITING) {
|
||||
logger.warning("Received message with invalid status: " + message);
|
||||
return;
|
||||
}
|
||||
@ -49,7 +48,7 @@ public class MessageProcessor implements ObjectProcessor<Message> {
|
||||
private void sendToUser(ConnectionManager connectionManager, Message message, ObjectWriteProxy writeProxy) {
|
||||
if (connectionManager.isOnline(message.getRecipientID())) try {
|
||||
// If recipient is online, send the message directly
|
||||
writeProxy.write(connectionManager.getSocketId(message.getRecipientID()), message);
|
||||
writeProxy.write(connectionManager.getSocketID(message.getRecipientID()), message);
|
||||
// Update the message status to RECEIVED
|
||||
message.setReceivedDate(new Date());
|
||||
message.nextStatus();
|
||||
|
@ -34,7 +34,7 @@ public class MessageStatusChangeProcessor implements ObjectProcessor<MessageStat
|
||||
|
||||
// Notifies the sender of the message about the status-update to READ
|
||||
final long senderID = msg.getSender().getID();
|
||||
if (connectionManager.isOnline(senderID)) writeProxy.write(connectionManager.getSocketId(senderID), input);
|
||||
if (connectionManager.isOnline(senderID)) writeProxy.write(connectionManager.getSocketID(senderID), input);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -76,7 +76,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
|
||||
ConnectionManager connectionManager = ConnectionManager.getInstance();
|
||||
try {
|
||||
for (envoy.server.data.Contact contact : user.getContacts())
|
||||
if (connectionManager.isOnline(contact.getID())) writeProxy.write(connectionManager.getSocketId(contact.getID()), evt);
|
||||
if (connectionManager.isOnline(contact.getID())) writeProxy.write(connectionManager.getSocketID(contact.getID()), evt);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
logger.warning("Could not notify online contacts of user " + evt.getID() + " that his status has been changed");
|
||||
|
Reference in New Issue
Block a user