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>
<groupId>com.github.informatik-ag-ngl</groupId>
<artifactId>envoy-common</artifactId>
<version>develop-SNAPSHOT</version>
<version>f~groups-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.informatik-ag-ngl</groupId>

View File

@ -1,62 +1,77 @@
package envoy.server.data;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
/**
* This class acts as a superclass for all contacts, being {@link User}s and
* {@link Group}s. <br>
* <br>
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>Contact.java</strong><br>
* Created: <strong>24.03.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-alpha
*/
@MappedSuperclass
// TODO add queries
public abstract class Contact {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected long id;
protected String name;
/**
* @return a envoy.data.Contact object of this envoy.server.data.Contact object.
* @since Envoy Server Standalone v0.1-beta
*/
public abstract envoy.data.Contact toCommon();
/**
* @return the ID of this contact.
* @since Envoy Server Standalone v0.1-beta
*/
public long getID() { return id; }
/**
* Sets the ID of this contact.
*
* @param id to set for this contact
* @since Envoy Server Standalone v0.1-beta
*/
public void setID(long id) { this.id = id; }
/**
* @return the name of this contact.
* @since Envoy Server Standalone v0.1-beta
*/
public String getName() { return name; }
/**
* Sets the name of this contact.
*
* @param name to set for this contact
* @since Envoy Server Standalone v0.1-beta
*/
public void setName(String name) { this.name = name; }
}
package envoy.server.data;
import java.util.Set;
import javax.persistence.*;
/**
* This class acts as a superclass for all contacts, being {@link User}s and
* {@link Group}s. <br>
* <br>
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>Contact.java</strong><br>
* Created: <strong>24.03.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-alpha
*/
@MappedSuperclass
// TODO add queries
public abstract class Contact {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected long id;
protected String name;
@ManyToMany(targetEntity = Contact.class, cascade = CascadeType.ALL)
protected Set<Contact> contacts;
/**
* @return a {@link envoy.data.Contact} object of this envoy.server.data.Contact
* object.
* @since Envoy Server Standalone v0.1-beta
*/
public abstract envoy.data.Contact toCommon();
/**
* @return the ID of this contact.
* @since Envoy Server Standalone v0.1-beta
*/
public long getID() { return id; }
/**
* Sets the ID of this contact.
*
* @param id to set for this contact
* @since Envoy Server Standalone v0.1-beta
*/
public void setID(long id) { this.id = id; }
/**
* @return the name of this contact.
* @since Envoy Server Standalone v0.1-beta
*/
public String getName() { return name; }
/**
* Sets the name of this contact.
*
* @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;
import java.util.List;
import java.util.stream.Collectors;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
* This class serves as a way to let Hibernate communicate with the server
* 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>
* <br>
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>Group.java</strong><br>
* Created: <strong>24.03.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-alpha
*/
@Entity
@Table(name = "groups")
@NamedQueries({ @NamedQuery(query = "SELECT g FROM Group g WHERE g.name = :name", name = "getGroupByName") })
public class Group extends Contact {
@ManyToMany(targetEntity = Group.class, cascade = CascadeType.ALL)
private List<User> members;
/**
* {@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; }
}
package envoy.server.data;
import java.util.stream.Collectors;
import javax.persistence.*;
import envoy.data.User;
/**
* This class serves as a way to let Hibernate communicate with the server
* 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>
* <br>
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>Group.java</strong><br>
* Created: <strong>24.03.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-alpha
*/
@Entity
@Table(name = "groups")
@NamedQueries({ @NamedQuery(query = "SELECT g FROM Group g WHERE g.name = :name", name = "getGroupByName") })
public class Group extends Contact {
/**
* {@inheritDoc}
*/
@Override
public envoy.data.Group toCommon() {
return new envoy.data.Group(id, name, contacts.parallelStream().map(Contact::toCommon).map(User.class::cast).collect(Collectors.toSet()));
}
}

View File

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

View File

@ -1,7 +1,7 @@
package envoy.server.data;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.persistence.*;
@ -38,14 +38,13 @@ public class User extends Contact {
private Date lastSeen;
private envoy.data.User.UserStatus status;
@ManyToMany(targetEntity = User.class, cascade = CascadeType.ALL)
private List<User> contacts;
/**
* {@inheritDoc}
*/
@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}
@ -85,18 +84,4 @@ public class User extends Contact {
* @see User#getStatus()
*/
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.server.data.Group;
import envoy.server.data.PersistenceManager;
import envoy.server.data.User;
import envoy.server.processors.UserStatusChangeProcessor;
/**
@ -113,7 +112,7 @@ public class ConnectionManager implements ISocketIdListener {
*/
public Set<Long> getOnlineUsersOfGroup(Group group) {
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); });
return onlineMembers;
}

View File

@ -3,8 +3,7 @@ package envoy.server.processors;
import java.io.IOException;
import java.util.Arrays;
import envoy.data.Contacts;
import envoy.event.ContactOperationEvent;
import envoy.event.contact.ContactOperationEvent;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;
@ -33,7 +32,7 @@ public class ContactOperationProcessor implements ObjectProcessor<ContactOperati
// Notify the contact if online
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;
default:
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.util.stream.Collectors;
import envoy.data.Contacts;
import envoy.event.ContactSearchRequest;
import envoy.event.ContactSearchResult;
import envoy.data.Contact;
import envoy.event.contact.ContactSearchRequest;
import envoy.event.contact.ContactSearchResult;
import envoy.server.data.PersistenceManager;
import envoy.server.data.User;
import envoy.server.net.ConnectionManager;
@ -23,7 +23,7 @@ import envoy.server.net.ObjectWriteProxy;
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
* contacts are excluded from the result.
*

View File

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

View File

@ -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()).getMembers();
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

@ -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());
ConnectionManager connectionManager = ConnectionManager.getInstance();
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);
} catch (IOException e) {
e.printStackTrace();