fixes #129 - contacts are now sent as a part of the user

(if they were not already)...
Additionally renamed some methods named `...Id()` to `...ID()`
This commit is contained in:
delvh 2020-04-06 22:55:27 +02:00
parent 2fbff47752
commit c6f0bff957
16 changed files with 139 additions and 154 deletions

View File

@ -43,7 +43,10 @@ public class Startup {
processors.add(new ContactOperationProcessor());
Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
initializeCurrentMessageId();
// Initialize the current message ID
PersistenceManager persistenceManager = PersistenceManager.getInstance();
if (persistenceManager.getConfigItemByID("currentMessageId") == null)
persistenceManager.addConfigItem(new ConfigItem("currentMessageId", "0"));
server.start();
server.getSocketProcessor().registerSocketIdListener(ConnectionManager.getInstance());
@ -53,9 +56,4 @@ public class Startup {
System.out.println("Stopped");
System.exit(0);
}
private static void initializeCurrentMessageId() {
PersistenceManager persMan = PersistenceManager.getInstance();
if (persMan.getConfigItemById("currentMessageId") == null) persMan.addConfigItem(new ConfigItem("currentMessageId", "0"));
}
}

View File

@ -2,14 +2,7 @@ package envoy.server.data;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;
import javax.persistence.*;
/**
* This class acts as a superclass for all contacts, being {@link User}s and
@ -32,7 +25,7 @@ public abstract class Contact {
protected long id;
protected String name;
@ManyToMany(targetEntity = Contact.class, cascade = CascadeType.ALL)
@ManyToMany(targetEntity = Contact.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
protected Set<Contact> contacts;
/**
@ -81,4 +74,10 @@ public abstract class Contact {
* @since Envoy Server Standalone v0.1-beta
*/
public void setContacts(Set<Contact> contacts) { this.contacts = contacts; }
/**
* {@inheritDoc}
*/
@Override
public String toString() { return String.format("%s[id=%d,name=%s, contacts=%s]", getClass().getSimpleName(), id, name, contacts); }
}

View File

@ -24,7 +24,7 @@ import envoy.data.User;
@Entity
@Table(name = "groups")
@NamedQuery(query = "SELECT g FROM Group g WHERE g.name = :name", name = "getGroupByName")
public class Group extends Contact {
public final class Group extends Contact {
/**
* {@inheritDoc}

View File

@ -74,15 +74,15 @@ public class Message {
* @since Envoy Server Standalone v0.1-alpha
*/
public Message(envoy.data.Message message) {
PersistenceManager persMan = PersistenceManager.getInstance();
PersistenceManager persistenceManager = PersistenceManager.getInstance();
id = message.getID();
status = message.getStatus();
text = message.getText();
creationDate = message.getCreationDate();
receivedDate = message.getReceivedDate();
readDate = message.getReadDate();
sender = persMan.getUserById(message.getSenderID());
recipient = persMan.getUserById(message.getRecipientID());
sender = persistenceManager.getUserByID(message.getSenderID());
recipient = persistenceManager.getUserByID(message.getRecipientID());
forwarded = message.isForwarded();
// TODO: attachment = message.getAttachment().toByteArray();DOES NOT WORK YET
}

View File

@ -37,7 +37,7 @@ public class PersistenceManager {
ConnectionManager.getInstance()
.getOnlineUsers()
.stream()
.map(this::getUserById)
.map(this::getUserByID)
.forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); entityManager.merge(user); });
transaction.commit();
}));
@ -120,7 +120,7 @@ public class PersistenceManager {
* @return the user with the specified id
* @since Envoy Server Standalone v0.1-alpha
*/
public User getUserById(long id) { return entityManager.find(User.class, id); }
public User getUserByID(long id) { return entityManager.find(User.class, id); }
/**
* Searches for a {@link Group} with a specific ID.
@ -129,7 +129,7 @@ public class PersistenceManager {
* @return the group with the specific id
* @since Envoy Server Standalone v0.1-beta
*/
public Group getGroupById(long id) { return entityManager.find(Group.class, id); }
public Group getGroupByID(long id) { return entityManager.find(Group.class, id); }
/**
* Searches for a {@link Contact} with a specific ID.
@ -138,7 +138,7 @@ public class PersistenceManager {
* @return the contact with the specific id
* @since Envoy Server Standalone v0.1-beta
*/
public Contact getContactById(long id) { return entityManager.find(Contact.class, id); }
public Contact getContactByID(long id) { return entityManager.find(Contact.class, id); }
/**
* Searched for a {@link User} with a specific name.
@ -180,14 +180,14 @@ public class PersistenceManager {
* @return the message with the specified id
* @since Envoy Server Standalone v0.1-alpha
*/
public Message getMessageById(long id) { return entityManager.find(Message.class, id); }
public Message getMessageByID(long id) { return entityManager.find(Message.class, id); }
/**
* @param key the name of this {@link ConfigItem}
* @return the {@link ConfigItem} with the given name
* @since Envoy Server Standalone v0.1-alpha
*/
public ConfigItem getConfigItemById(String key) { return entityManager.find(ConfigItem.class, key); }
public ConfigItem getConfigItemByID(String key) { return entityManager.find(ConfigItem.class, key); }
/**
* Returns all messages received while being offline or the ones that have
@ -217,7 +217,7 @@ public class PersistenceManager {
public List<User> searchUsers(String searchPhrase, long userId) {
return entityManager.createNamedQuery("searchUsers")
.setParameter("searchPhrase", searchPhrase + "%")
.setParameter("context", getUserById(userId))
.setParameter("context", getUserByID(userId))
.getResultList();
}
@ -231,8 +231,8 @@ public class PersistenceManager {
public void addUserContact(long userId1, long userId2) {
// Get users by ID
Contact u1 = getContactById(userId1);
Contact u2 = getContactById(userId2);
Contact u1 = getContactByID(userId1);
Contact u2 = getContactByID(userId2);
// Add users to each others contact lists
u1.getContacts().add(u2);

View File

@ -30,7 +30,7 @@ import javax.persistence.*;
name = "searchUsers"
) }
)
public class User extends Contact {
public final class User extends Contact {
private byte[] passwordHash;

View File

@ -49,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);

View File

@ -32,7 +32,7 @@ public class ContactOperationProcessor implements ObjectProcessor<ContactOperati
// Notify the contact if online
if (ConnectionManager.getInstance().isOnline(contactId)) writeProxy.write(connectionManager.getSocketId(contactId),
Arrays.asList(PersistenceManager.getInstance().getUserById(userID).toCommon()));
Arrays.asList(PersistenceManager.getInstance().getUserByID(userID).toCommon()));
break;
default:
System.err.printf("Received %s with an unsupported operation.%n", evt);

View File

@ -1,48 +1,48 @@
package envoy.server.processors;
import java.io.IOException;
import envoy.event.GroupCreationEvent;
import envoy.server.data.Contact;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>GroupCreationProcessor.java</strong><br>
* Created: <strong>26.03.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-beta
*/
public class GroupCreationProcessor implements ObjectProcessor<GroupCreationEvent> {
private final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private final ConnectionManager connectionManager = ConnectionManager.getInstance();
@Override
public void process(GroupCreationEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException {
envoy.server.data.Group group = new envoy.server.data.Group();
group.setName(input.get());
input.getInitialMemberIDs().stream().map(persistenceManager::getUserById).forEach(group.getContacts()::add);
group.getContacts().add(persistenceManager.getUserById(connectionManager.getUserIdBySocketId(socketID)));
persistenceManager.addContact(group);
writeProxy.write(socketID, group);
group.getContacts()
.stream()
.map(Contact::getID)
.filter(connectionManager::isOnline)
.map(connectionManager::getSocketId)
.forEach(memberSocketID -> {
try {
writeProxy.write(memberSocketID, group);
} catch (IOException e) {
e.printStackTrace();
}
});
}
@Override
public Class<GroupCreationEvent> getInputClass() { return GroupCreationEvent.class; }
}
package envoy.server.processors;
import java.io.IOException;
import envoy.event.GroupCreationEvent;
import envoy.server.data.Contact;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>GroupCreationProcessor.java</strong><br>
* Created: <strong>26.03.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-beta
*/
public class GroupCreationProcessor implements ObjectProcessor<GroupCreationEvent> {
private final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private final ConnectionManager connectionManager = ConnectionManager.getInstance();
@Override
public void process(GroupCreationEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException {
envoy.server.data.Group group = new envoy.server.data.Group();
group.setName(input.get());
input.getInitialMemberIDs().stream().map(persistenceManager::getUserByID).forEach(group.getContacts()::add);
group.getContacts().add(persistenceManager.getUserByID(connectionManager.getUserIdBySocketId(socketID)));
persistenceManager.addContact(group);
writeProxy.write(socketID, group);
group.getContacts()
.stream()
.map(Contact::getID)
.filter(connectionManager::isOnline)
.map(connectionManager::getSocketId)
.forEach(memberSocketID -> {
try {
writeProxy.write(memberSocketID, group);
} catch (IOException e) {
e.printStackTrace();
}
});
}
@Override
public Class<GroupCreationEvent> getInputClass() { return GroupCreationEvent.class; }
}

View File

@ -1,54 +1,54 @@
package envoy.server.processors;
import java.io.IOException;
import envoy.event.GroupResizeEvent;
import envoy.server.data.Contact;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>GroupResizeProcessor.java</strong><br>
* Created: <strong>03.04.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-beta
*/
public class GroupResizeProcessor implements ObjectProcessor<GroupResizeEvent> {
PersistenceManager persistenceManager = PersistenceManager.getInstance();
ConnectionManager connectionManager = ConnectionManager.getInstance();
@Override
public void process(GroupResizeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException {
envoy.server.data.Group group = persistenceManager.getGroupById(input.getGroupID());
switch (input.getOperation()) {
case ADD:
group.getContacts().add(persistenceManager.getUserById(input.get().getID()));
break;
case REMOVE:
group.getContacts().remove(persistenceManager.getUserById(input.get().getID()));
break;
}
persistenceManager.updateContact(group);
writeProxy.write(socketID, group);
group.getContacts()
.stream()
.map(Contact::getID)
.filter(connectionManager::isOnline)
.map(connectionManager::getSocketId)
.forEach(memberSocketID -> {
try {
writeProxy.write(memberSocketID, group);
} catch (IOException e) {
e.printStackTrace();
}
});
}
@Override
public Class<GroupResizeEvent> getInputClass() { return GroupResizeEvent.class; }
}
package envoy.server.processors;
import java.io.IOException;
import envoy.event.GroupResizeEvent;
import envoy.server.data.Contact;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>GroupResizeProcessor.java</strong><br>
* Created: <strong>03.04.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-beta
*/
public class GroupResizeProcessor implements ObjectProcessor<GroupResizeEvent> {
PersistenceManager persistenceManager = PersistenceManager.getInstance();
ConnectionManager connectionManager = ConnectionManager.getInstance();
@Override
public void process(GroupResizeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException {
envoy.server.data.Group group = persistenceManager.getGroupByID(input.getGroupID());
switch (input.getOperation()) {
case ADD:
group.getContacts().add(persistenceManager.getUserByID(input.get()));
break;
case REMOVE:
group.getContacts().remove(persistenceManager.getUserByID(input.get()));
break;
}
persistenceManager.updateContact(group);
writeProxy.write(socketID, group);
group.getContacts()
.stream()
.map(Contact::getID)
.filter(connectionManager::isOnline)
.map(connectionManager::getSocketId)
.forEach(memberSocketID -> {
try {
writeProxy.write(memberSocketID, group);
} catch (IOException e) {
e.printStackTrace();
}
});
}
@Override
public Class<GroupResizeEvent> getInputClass() { return GroupResizeEvent.class; }
}

View File

@ -44,7 +44,7 @@ public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorR
* @since Envoy Server Standalone v0.1-beta
*/
public static IDGenerator createIDGenerator(long range) {
ConfigItem currentID = PersistenceManager.getInstance().getConfigItemById("currentMessageId");
ConfigItem currentID = PersistenceManager.getInstance().getConfigItemByID("currentMessageId");
IDGenerator generator = new IDGenerator(Integer.parseInt(currentID.getValue()), range);
currentID.setValue(String.valueOf(Integer.parseInt(currentID.getValue()) + range));
PersistenceManager.getInstance().updateConfigItem(currentID);

View File

@ -1,12 +1,7 @@
package envoy.server.processors;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.*;
import javax.persistence.NoResultException;
@ -56,18 +51,12 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
user.setStatus(UserStatus.ONLINE);
UserStatusChangeProcessor.updateUserStatus(user);
// Create contacts
var contacts = user.getContacts().stream().map(envoy.server.data.Contact::toCommon).collect(Collectors.toList());
contacts.add(user.toCommon());
// Complete handshake
System.out.println("Sending user...");
writeProxy.write(socketID, user.toCommon());
System.out.println("Sending contacts...");
writeProxy.write(socketID, contacts);
System.out.println("Acquiring pending messages for the client...");
List<Message> pendingMessages = PersistenceManager.getInstance().getPendingMessages(user);
for (Message msg : pendingMessages) {
for (Message msg : pendingMessages)
if (msg.getStatus() == MessageStatus.SENT) {
System.out.println("Sending message " + msg.toCommonMessage());
writeProxy.write(socketID, msg.toCommonMessage());
@ -79,7 +68,6 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
System.out.println("Sending messageStatusChangeEvent " + evt);
writeProxy.write(socketID, evt);
}
}
}
@Override

View File

@ -29,7 +29,7 @@ public class MessageProcessor implements ObjectProcessor<Message> {
public void process(Message message, long socketID, ObjectWriteProxy writeProxy) {
message.nextStatus();
ConnectionManager connectionManager = ConnectionManager.getInstance();
Contact recipient = PersistenceManager.getInstance().getContactById(message.getID());
Contact recipient = PersistenceManager.getInstance().getContactByID(message.getID());
if (recipient instanceof envoy.server.data.User) {
System.out.println("The received message is a direct message.");
@ -43,7 +43,7 @@ public class MessageProcessor implements ObjectProcessor<Message> {
}
} else {
System.out.println("The received message is a group message.");
final var members = PersistenceManager.getInstance().getGroupById(message.getRecipientID()).getContacts();
final var members = PersistenceManager.getInstance().getGroupByID(message.getRecipientID()).getContacts();
final var generator = IDGeneratorRequestProcessor.createIDGenerator(members.size());
members.forEach(user -> {
envoy.data.Message returnMessage = new MessageBuilder(message.getRecipientID(), user.getID(), generator)

View File

@ -27,7 +27,7 @@ public class MessageStatusChangeProcessor implements ObjectProcessor<MessageStat
// Any other status than READ is not supposed to be sent to the server
if (input.get() != MessageStatus.READ) throw new IOException(new EnvoyException("Message " + input + " has an invalid status"));
envoy.server.data.Message msg = persistenceManager.getMessageById(input.getID());
envoy.server.data.Message msg = persistenceManager.getMessageByID(input.getID());
msg.setStatus(input.get());
msg.setReadDate(input.getDate());
persistenceManager.updateMessage(msg);

View File

@ -23,7 +23,7 @@ public class NameChangeProcessor implements ObjectProcessor<NameChangeEvent> {
public void process(NameChangeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException {
PersistenceManager persistenceManager = PersistenceManager.getInstance();
ConnectionManager connectionManager = ConnectionManager.getInstance();
Contact toUpdate = persistenceManager.getContactById(input.getID());
Contact toUpdate = persistenceManager.getContactByID(input.getID());
toUpdate.setName(input.get());
persistenceManager.updateContact(toUpdate);

View File

@ -30,7 +30,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
@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())) {
if (input.get().equals(persistenceManager.getUserByID(input.getID()).getStatus())) {
System.out.println("Received an unnecessary UserStatusChangeEvent");
return;
}
@ -58,7 +58,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
* @param evt the {@link UserStatusChangeEvent}
* @since Envoy Server Standalone v0.1-alpha
*/
public static void updateUserStatus(UserStatusChangeEvent evt) { updateUserStatus(persistenceManager.getUserById(evt.getID())); }
public static void updateUserStatus(UserStatusChangeEvent evt) { updateUserStatus(persistenceManager.getUserByID(evt.getID())); }
/**
* notifies active contacts of this {@link User} that his {@link UserStatus} has