Restored compatability with envoy-common

additionally added NameChangeProcessor
This commit is contained in:
delvh 2020-04-02 16:32:23 +02:00
parent 198ba2a52d
commit e6cf3af745
12 changed files with 175 additions and 160 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~groups-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.informatik-ag-ngl</groupId> <groupId>com.github.informatik-ag-ngl</groupId>

View File

@ -1,62 +1,77 @@
package envoy.server.data; package envoy.server.data;
import javax.persistence.GeneratedValue; import java.util.Set;
import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.*;
import javax.persistence.MappedSuperclass;
/**
/** * This class acts as a superclass for all contacts, being {@link User}s and
* This class acts as a superclass for all contacts, being {@link User}s and * {@link Group}s. <br>
* {@link Group}s. <br> * <br>
* <br> * Project: <strong>envoy-server-standalone</strong><br>
* Project: <strong>envoy-server-standalone</strong><br> * File: <strong>Contact.java</strong><br>
* File: <strong>Contact.java</strong><br> * Created: <strong>24.03.2020</strong><br>
* Created: <strong>24.03.2020</strong><br> *
* * @author Maximilian K&auml;fer
* @author Maximilian K&auml;fer * @since Envoy Server Standalone v0.1-alpha
* @since Envoy Server Standalone v0.1-alpha */
*/
@MappedSuperclass
@MappedSuperclass // TODO add queries
// TODO add queries public abstract class Contact {
public abstract class Contact {
@Id
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = GenerationType.IDENTITY) protected long id;
protected long id; protected String name;
protected String name;
@ManyToMany(targetEntity = Contact.class, cascade = CascadeType.ALL)
/** protected Set<Contact> contacts;
* @return a envoy.data.Contact object of this envoy.server.data.Contact object.
* @since Envoy Server Standalone v0.1-beta /**
*/ * @return a {@link envoy.data.Contact} object of this envoy.server.data.Contact
public abstract envoy.data.Contact toCommon(); * object.
* @since Envoy Server Standalone v0.1-beta
/** */
* @return the ID of this contact. public abstract envoy.data.Contact toCommon();
* @since Envoy Server Standalone v0.1-beta
*/ /**
public long getID() { return id; } * @return the ID of this contact.
* @since Envoy Server Standalone v0.1-beta
/** */
* Sets the ID of this contact. public long getID() { return id; }
*
* @param id to set for this contact /**
* @since Envoy Server Standalone v0.1-beta * Sets the ID of this contact.
*/ *
public void setID(long id) { this.id = id; } * @param id to set for this contact
* @since Envoy Server Standalone v0.1-beta
/** */
* @return the name of this contact. public void setID(long id) { this.id = id; }
* @since Envoy Server Standalone v0.1-beta
*/ /**
public String getName() { return name; } * @return the name of this contact.
* @since Envoy Server Standalone v0.1-beta
/** */
* Sets the name of this contact. public String getName() { return name; }
*
* @param name to set for this contact /**
* @since Envoy Server Standalone v0.1-beta * Sets the name of this contact.
*/ *
public void setName(String name) { this.name = name; } * @param name to set for this contact
} * @since Envoy Server Standalone v0.1-beta
*/
public void setName(String name) { this.name = name; }
/**
* @return the contacts
* @since Envoy Server Standalone v0.1-beta
*/
public Set<Contact> getContacts() { return contacts; }
/**
* @param contacts the contacts to set
* @since Envoy Server Standalone v0.1-beta
*/
public void setContacts(Set<Contact> contacts) { this.contacts = contacts; }
}

View File

@ -1,54 +1,34 @@
package envoy.server.data; package envoy.server.data;
import java.util.List; import java.util.stream.Collectors;
import java.util.stream.Collectors;
import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Entity; import envoy.data.User;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries; /**
import javax.persistence.NamedQuery; * This class serves as a way to let Hibernate communicate with the server
import javax.persistence.Table; * without bringing the dependency of JPA/Hibernate into the client.<br>
* It will be referenced as "database group" to clarify between the different
/** * group objects.<br>
* This class serves as a way to let Hibernate communicate with the server * <br>
* without bringing the dependency of JPA/Hibernate into the client.<br> * Project: <strong>envoy-server-standalone</strong><br>
* It will be referenced as "database group" to clarify between the different * File: <strong>Group.java</strong><br>
* group objects.<br> * Created: <strong>24.03.2020</strong><br>
* <br> *
* Project: <strong>envoy-server-standalone</strong><br> * @author Maximilian K&auml;fer
* File: <strong>Group.java</strong><br> * @since Envoy Server Standalone v0.1-alpha
* Created: <strong>24.03.2020</strong><br> */
* @Entity
* @author Maximilian K&auml;fer @Table(name = "groups")
* @since Envoy Server Standalone v0.1-alpha @NamedQueries({ @NamedQuery(query = "SELECT g FROM Group g WHERE g.name = :name", name = "getGroupByName") })
*/ public class Group extends Contact {
@Entity
@Table(name = "groups") /**
@NamedQueries({ @NamedQuery(query = "SELECT g FROM Group g WHERE g.name = :name", name = "getGroupByName") }) * {@inheritDoc}
public class Group extends Contact { */
@Override
@ManyToMany(targetEntity = Group.class, cascade = CascadeType.ALL) public envoy.data.Group toCommon() {
private List<User> members; return new envoy.data.Group(id, name, contacts.parallelStream().map(Contact::toCommon).map(User.class::cast).collect(Collectors.toSet()));
}
/** }
* {@inheritDoc}
*/
@Override
public envoy.data.Group toCommon() { return new envoy.data.Group(id, name, members.stream().map(User::getID).collect(Collectors.toList())); }
/**
* @return a list of all users that are a member of this group.
* @since Envoy Server Standalone v0.1-beta
*/
public List<User> getMembers() { return members; }
/**
* Sets the members of this group.
*
* @param members a list of all users, that should be assigned to this group.
* @since Envoy Server Standalone v0.1-beta
*/
public void setMembers(List<User> members) { this.members = members; }
}

View File

@ -124,7 +124,7 @@ public class PersistenceManager {
/** /**
* Searches for a {@link Group} with a specific ID. * Searches for a {@link Group} with a specific ID.
* *
* @param id the id to search for * @param id the id to search for
* @return the group with the specific id * @return the group with the specific id
* @since Envoy Server Standalone v0.1-beta * @since Envoy Server Standalone v0.1-beta
@ -133,7 +133,7 @@ public class PersistenceManager {
/** /**
* Searches for a {@link Contact} with a specific ID. * Searches for a {@link Contact} with a specific ID.
* *
* @param id the id to search for * @param id the id to search for
* @return the contact with the specific id * @return the contact with the specific id
* @since Envoy Server Standalone v0.1-beta * @since Envoy Server Standalone v0.1-beta
@ -227,8 +227,8 @@ public class PersistenceManager {
public void addUserContact(long userId1, long userId2) { public void addUserContact(long userId1, long userId2) {
// Get users by ID // Get users by ID
User u1 = getUserById(userId1); Contact u1 = getContactById(userId1);
User u2 = getUserById(userId2); Contact u2 = getContactById(userId2);
// Add users to each others contact lists // Add users to each others contact lists
u1.getContacts().add(u2); u1.getContacts().add(u2);

View File

@ -1,7 +1,7 @@
package envoy.server.data; package envoy.server.data;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.stream.Collectors;
import javax.persistence.*; import javax.persistence.*;
@ -38,14 +38,13 @@ public class User extends Contact {
private Date lastSeen; private Date lastSeen;
private envoy.data.User.UserStatus status; private envoy.data.User.UserStatus status;
@ManyToMany(targetEntity = User.class, cascade = CascadeType.ALL)
private List<User> contacts;
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public envoy.data.User toCommon() { return new envoy.data.User(id, name, status); } public envoy.data.User toCommon() {
return new envoy.data.User(id, name, status, contacts.stream().map(Contact::toCommon).collect(Collectors.toSet()));
}
/** /**
* @return the passwordHash of a {link envoy.data.User} * @return the passwordHash of a {link envoy.data.User}
@ -85,18 +84,4 @@ public class User extends Contact {
* @see User#getStatus() * @see User#getStatus()
*/ */
public void setStatus(envoy.data.User.UserStatus status) { this.status = status; } public void setStatus(envoy.data.User.UserStatus status) { this.status = status; }
/**
* @return the contacts of a {link envoy.data.User}
* @since Envoy Server Standalone v0.1-alpha
*/
public List<User> getContacts() { return contacts; }
/**
* @param contacts the contacts to set
* @since Envoy Server Standalone v0.1-alpha
* @see User#getContacts()
*/
public void setContacts(List<User> contacts) { this.contacts = contacts; }
} }

View File

@ -8,7 +8,6 @@ import com.jenkov.nioserver.ISocketIdListener;
import envoy.data.User.UserStatus; import envoy.data.User.UserStatus;
import envoy.server.data.Group; import envoy.server.data.Group;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.data.User;
import envoy.server.processors.UserStatusChangeProcessor; import envoy.server.processors.UserStatusChangeProcessor;
/** /**
@ -113,7 +112,7 @@ public class ConnectionManager implements ISocketIdListener {
*/ */
public Set<Long> getOnlineUsersOfGroup(Group group) { public Set<Long> getOnlineUsersOfGroup(Group group) {
Set<Long> onlineMembers = new HashSet<>(); Set<Long> onlineMembers = new HashSet<>();
Set<Long> members = group.getMembers().stream().map(User::getID).collect(Collectors.toSet()); Set<Long> members = group.getContacts().stream().map(envoy.server.data.Contact::getID).collect(Collectors.toSet());
members.forEach(userID -> { if (isOnline(userID)) onlineMembers.add(userID); }); members.forEach(userID -> { if (isOnline(userID)) onlineMembers.add(userID); });
return onlineMembers; return onlineMembers;
} }

View File

@ -3,8 +3,7 @@ package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import envoy.data.Contacts; import envoy.event.contact.ContactOperationEvent;
import envoy.event.ContactOperationEvent;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy; import envoy.server.net.ObjectWriteProxy;
@ -33,7 +32,7 @@ public class ContactOperationProcessor implements ObjectProcessor<ContactOperati
// Notify the contact if online // Notify the contact if online
if (ConnectionManager.getInstance().isOnline(contactId)) writeProxy.write(connectionManager.getSocketId(contactId), if (ConnectionManager.getInstance().isOnline(contactId)) writeProxy.write(connectionManager.getSocketId(contactId),
new Contacts(Arrays.asList(PersistenceManager.getInstance().getUserById(userID).toCommon()))); Arrays.asList(PersistenceManager.getInstance().getUserById(userID).toCommon()));
break; break;
default: default:
System.err.printf("Received %s with an unsupported operation.%n", evt); System.err.printf("Received %s with an unsupported operation.%n", evt);

View File

@ -3,9 +3,9 @@ package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import envoy.data.Contacts; import envoy.data.Contact;
import envoy.event.ContactSearchRequest; import envoy.event.contact.ContactSearchRequest;
import envoy.event.ContactSearchResult; import envoy.event.contact.ContactSearchResult;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.data.User; import envoy.server.data.User;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
@ -23,7 +23,7 @@ import envoy.server.net.ObjectWriteProxy;
public class ContactsRequestEventProcessor implements ObjectProcessor<ContactSearchRequest> { public class ContactsRequestEventProcessor implements ObjectProcessor<ContactSearchRequest> {
/** /**
* Writes a {@link Contacts} list to the client containing all {@link User}s * Writes a list of contacts to the client containing all {@link Contact}s
* matching the search phrase contained inside the request. The client and their * matching the search phrase contained inside the request. The client and their
* contacts are excluded from the result. * contacts are excluded from the result.
* *

View File

@ -1,16 +1,11 @@
package envoy.server.processors; package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
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;
@ -57,8 +52,8 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
UserStatusChangeProcessor.updateUserStatus(user); UserStatusChangeProcessor.updateUserStatus(user);
// Create contacts // Create contacts
Contacts contacts = new Contacts(user.getContacts().stream().map(envoy.server.data.User::toCommon).collect(Collectors.toList())); var contacts = user.getContacts().stream().map(envoy.server.data.Contact::toCommon).collect(Collectors.toList());
contacts.getContacts().add(user.toCommon()); contacts.add(user.toCommon());
// Complete handshake // Complete handshake
System.out.println("Sending user..."); System.out.println("Sending user...");
@ -143,7 +138,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
user.setLastSeen(new Date()); user.setLastSeen(new Date());
user.setStatus(User.UserStatus.ONLINE); user.setStatus(User.UserStatus.ONLINE);
user.setPasswordHash(credentials.getPasswordHash()); user.setPasswordHash(credentials.getPasswordHash());
user.setContacts(new ArrayList<>()); user.setContacts(new HashSet<>());
persistenceManager.addContact(user); persistenceManager.addContact(user);
return user; return user;
} }

View File

@ -43,7 +43,7 @@ public class MessageProcessor implements ObjectProcessor<Message> {
} }
} else { } else {
System.out.println("The received message is a group message."); System.out.println("The received message is a group message.");
final var members = PersistenceManager.getInstance().getGroupById(message.getRecipientID()).getMembers(); final var members = PersistenceManager.getInstance().getGroupById(message.getRecipientID()).getContacts();
final var generator = IDGeneratorRequestProcessor.createIDGenerator(members.size()); final var generator = IDGeneratorRequestProcessor.createIDGenerator(members.size());
members.forEach(user -> { members.forEach(user -> {
envoy.data.Message returnMessage = new MessageBuilder(message.getRecipientID(), user.getID(), generator) envoy.data.Message returnMessage = new MessageBuilder(message.getRecipientID(), user.getID(), generator)

View File

@ -0,0 +1,42 @@
package envoy.server.processors;
import java.io.IOException;
import envoy.event.NameChangeEvent;
import envoy.server.data.Contact;
import envoy.server.data.PersistenceManager;
import envoy.server.data.User;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>NameChangeProcessor.java</strong><br>
* Created: <strong>26 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Server Standalone v0.1-beta
*/
public class NameChangeProcessor implements ObjectProcessor<NameChangeEvent> {
@Override
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());
toUpdate.setName(input.get());
persistenceManager.updateContact(toUpdate);
// notifying online contacts of this client of his name change
toUpdate.getContacts().stream().filter(contact -> (contact instanceof User && connectionManager.isOnline(contact.getID()))).forEach(user -> {
try {
writeProxy.write(user.getID(), input);
} catch (IOException e) {
e.printStackTrace();
}
});
}
@Override
public Class<NameChangeEvent> getInputClass() { return NameChangeEvent.class; }
}

View File

@ -71,7 +71,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
UserStatusChangeEvent evt = new UserStatusChangeEvent(user.getID(), user.getStatus()); UserStatusChangeEvent evt = new UserStatusChangeEvent(user.getID(), user.getStatus());
ConnectionManager connectionManager = ConnectionManager.getInstance(); ConnectionManager connectionManager = ConnectionManager.getInstance();
try { try {
for (User contact : user.getContacts()) 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();