Merge branch 'develop' into f/groupMessages

Conflicts:
	src/main/java/envoy/server/processors/GroupMessageProcessor.java
	src/main/java/envoy/server/processors/MessageProcessor.java
This commit is contained in:
Kai S. K. Engelbart 2020-06-27 11:32:08 +02:00
commit be40f24904
26 changed files with 376 additions and 304 deletions

1
.github/CODEOWNERS vendored Normal file
View File

@ -0,0 +1 @@
* @CyB3RC0nN0R

View File

@ -1,2 +1,3 @@
# envoy-server-standalone # envoy-server-standalone
Standalone server for the Envoy project Standalone server for the
<a href="https://github.com/informatik-ag-ngl/envoy-client"><img src="https://raw.githubusercontent.com/informatik-ag-ngl/envoy-client/develop/src/main/resources/icons/envoy_logo.png" align="left" width="200" height="200">Envoy project</a>

View File

@ -0,0 +1,94 @@
package enovy.server.util;
import java.util.regex.Pattern;
/**
* Implements a comparison algorithm between Envoy versions and defines minimal
* and maximal client versions compatible with this server.
* <p>
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>VersionUtils.java</strong><br>
* Created: <strong>23.06.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-beta
*/
public class VersionUtils {
/**
* The minimal client version compatible with this server.
*
* @since Envoy Server Standalone v0.1-beta
*/
public static final String MIN_CLIENT_VERSION = "0.1-beta";
/**
* The maximal client version compatible with this server.
*
* @since Envoy Server Standalone v0.1-beta
*/
public static final String MAX_CLIENT_VERSION = "0.1-beta";
private static final Pattern versionPattern = Pattern.compile("(?<major>\\d).(?<minor>\\d)(?:-(?<suffix>\\w+))?");
private VersionUtils() {}
/**
* Parses an Envoy Client version string and checks whether that version is
* compatible with this server.
*
* @param version the version string to parse
* @return {@code true} if the given version is compatible with this server
* @since Envoy Server Standalone v0.1-beta
*/
public static boolean verifyCompatibility(String version) {
final var currentMatcher = versionPattern.matcher(version);
if (!currentMatcher.matches()) return false;
final var minMatcher = versionPattern.matcher(MIN_CLIENT_VERSION);
final var maxMatcher = versionPattern.matcher(MAX_CLIENT_VERSION);
if (!minMatcher.matches() || !maxMatcher.matches()) throw new RuntimeException("Invalid min or max client version configured!");
// Compare suffixes
{
final var currentSuffix = convertSuffix(currentMatcher.group("suffix"));
final var minSuffix = convertSuffix(minMatcher.group("suffix"));
final var maxSuffix = convertSuffix(maxMatcher.group("suffix"));
if (currentSuffix < minSuffix || currentSuffix > maxSuffix) return false;
}
// Compare major
{
final var currentMajor = Integer.parseInt(currentMatcher.group("major"));
final var minMajor = Integer.parseInt(minMatcher.group("major"));
final var maxMajor = Integer.parseInt(maxMatcher.group("major"));
if (currentMajor < minMajor || currentMajor > maxMajor) return false;
}
// Compare minor
{
final var currentMinor = Integer.parseInt(currentMatcher.group("minor"));
final var minMinor = Integer.parseInt(minMatcher.group("minor"));
final var maxMinor = Integer.parseInt(maxMatcher.group("minor"));
if (currentMinor < minMinor || currentMinor > maxMinor) return false;
}
return true;
}
private static int convertSuffix(String suffix) {
switch (suffix == null ? "" : suffix) {
case "alpha":
return 0;
case "beta":
return 1;
default:
return 2;
}
}
}

View File

@ -0,0 +1,11 @@
/**
* This package contains utility classes used in Envoy Server.
* <p>
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>package-info.java</strong><br>
* Created: <strong>23.06.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-beta
*/
package enovy.server.util;

View File

@ -46,7 +46,7 @@ public class Startup {
final var items = new HashMap<String, ConfigItem<?>>(); final var items = new HashMap<String, ConfigItem<?>>();
items.put("homeDirectory", items.put("homeDirectory",
new ConfigItem<>("homeDirectory", "h", File::new, new File(System.getProperty("user.home"), ".envoy-server"), true)); new ConfigItem<>("homeDirectory", "h", File::new, new File(System.getProperty("user.home"), ".envoy-server"), true));
items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.SEVERE, true)); items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.WARNING, true));
items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.FINEST, true)); items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.FINEST, true));
final var config = new Config(); final var config = new Config();

View File

@ -1,19 +1,9 @@
package envoy.server.data; package envoy.server.data;
import java.util.Date; import java.time.LocalDateTime;
import java.util.Set; import java.util.Set;
import javax.persistence.CascadeType; import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/** /**
* 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
@ -29,7 +19,7 @@ import javax.persistence.TemporalType;
@Entity @Entity
@Table(name = "contacts") @Table(name = "contacts")
@Inheritance(strategy = InheritanceType.JOINED) @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Contact { public abstract class Contact {
@Id @Id
@ -37,10 +27,10 @@ public abstract class Contact {
protected long id; protected long id;
protected String name; protected String name;
@Temporal(TemporalType.TIMESTAMP) @Column(name = "creation_date")
private Date creationDate; private LocalDateTime creationDate;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @ManyToMany(fetch = FetchType.EAGER)
protected Set<Contact> contacts; protected Set<Contact> contacts;
/** /**
@ -104,17 +94,14 @@ public abstract class Contact {
* @return the creationDate * @return the creationDate
* @since Envoy Server Standalone v0.1-beta * @since Envoy Server Standalone v0.1-beta
*/ */
public Date getCreationDate() { return creationDate; } public LocalDateTime getCreationDate() { return creationDate; }
/** /**
* @param creationDate the creationDate to set * @param creationDate the creationDate to set
* @since Envoy Server Standalone v0.1-beta * @since Envoy Server Standalone v0.1-beta
*/ */
public void setCreationDate(Date creationDate) { this.creationDate = creationDate; } public void setCreationDate(LocalDateTime creationDate) { this.creationDate = creationDate; }
/**
* {@inheritDoc}
*/
@Override @Override
public String toString() { return String.format("%s[id=%d,name=%s, contacts=%s]", getClass().getSimpleName(), id, name, contacts); } public String toString() { return String.format("%s[id=%d,name=%s, %d contact(s)]", getClass().getSimpleName(), id, name, contacts.size()); }
} }

View File

@ -5,7 +5,6 @@ import java.util.stream.Collectors;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.NamedQueries; import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery; import javax.persistence.NamedQuery;
import javax.persistence.Table;
/** /**
* Represents a group inside the database. Referred to as "server group" as * Represents a group inside the database. Referred to as "server group" as
@ -19,7 +18,6 @@ import javax.persistence.Table;
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
@Entity @Entity
@Table(name = "groups")
@NamedQueries({ @NamedQueries({
@NamedQuery( @NamedQuery(
name = Group.findByName, name = Group.findByName,
@ -46,17 +44,11 @@ public class Group extends Contact {
*/ */
public static final String findPendingGroups = "Group.findPendingGroups"; public static final String findPendingGroups = "Group.findPendingGroups";
/**
* {@inheritDoc}
*/
@Override @Override
public envoy.data.Group toCommon() { public envoy.data.Group toCommon() {
return new envoy.data.Group(id, name, contacts.parallelStream().map(User.class::cast).map(User::toFlatCommon).collect(Collectors.toSet())); return new envoy.data.Group(id, name, contacts.parallelStream().map(User.class::cast).map(User::toFlatCommon).collect(Collectors.toSet()));
} }
/**
* {@inheritDoc}
*/
@Override @Override
protected envoy.data.Group toFlatCommon() { return toCommon(); } protected envoy.data.Group toFlatCommon() { return toCommon(); }
} }

View File

@ -3,10 +3,7 @@ package envoy.server.data;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import javax.persistence.ElementCollection; import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import envoy.data.MessageBuilder; import envoy.data.MessageBuilder;
@ -24,6 +21,7 @@ public class GroupMessage extends Message {
@ElementCollection @ElementCollection
private Map<Long, envoy.data.Message.MessageStatus> memberMessageStatus; private Map<Long, envoy.data.Message.MessageStatus> memberMessageStatus;
@Column(name = "last_status_change_date")
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
protected Date lastStatusChangeDate; protected Date lastStatusChangeDate;
@ -64,7 +62,6 @@ public class GroupMessage extends Message {
.setForwarded(forwarded) .setForwarded(forwarded)
.setStatus(status) .setStatus(status)
.setText(text) .setText(text)
// .setAttachment(attachment) TODO make this work
.buildGroupMessage((envoy.data.Group) recipient.toCommon(), memberMessageStatus); .buildGroupMessage((envoy.data.Group) recipient.toCommon(), memberMessageStatus);
groupMessage.setReceivedDate(receivedDate); groupMessage.setReceivedDate(receivedDate);
groupMessage.setReadDate(readDate); groupMessage.setReadDate(readDate);

View File

@ -1,9 +1,12 @@
package envoy.server.data; package envoy.server.data;
import java.util.Date; import static envoy.data.Message.MessageStatus.*;
import java.time.LocalDateTime;
import javax.persistence.*; import javax.persistence.*;
import envoy.data.Message.MessageStatus;
import envoy.data.MessageBuilder; import envoy.data.MessageBuilder;
/** /**
@ -49,14 +52,14 @@ public class Message {
@JoinColumn @JoinColumn
protected Contact recipient; protected Contact recipient;
@Temporal(TemporalType.TIMESTAMP) @Column(name = "creation_date")
protected Date creationDate; protected LocalDateTime creationDate;
@Temporal(TemporalType.TIMESTAMP) @Column(name = "received_date")
protected Date receivedDate; protected LocalDateTime receivedDate;
@Temporal(TemporalType.TIMESTAMP) @Column(name = "read_date")
protected Date readDate; protected LocalDateTime readDate;
protected String text; protected String text;
protected envoy.data.Message.MessageStatus status; protected envoy.data.Message.MessageStatus status;
@ -88,7 +91,7 @@ public class Message {
sender = persistenceManager.getUserByID(message.getSenderID()); sender = persistenceManager.getUserByID(message.getSenderID());
recipient = persistenceManager.getContactByID(message.getRecipientID()); recipient = persistenceManager.getContactByID(message.getRecipientID());
forwarded = message.isForwarded(); forwarded = message.isForwarded();
// TODO: attachment = message.getAttachment().toByteArray();DOES NOT WORK YET // TODO: Attachment
} }
/** /**
@ -104,13 +107,34 @@ public class Message {
.setDate(creationDate) .setDate(creationDate)
.setStatus(status) .setStatus(status)
.setForwarded(forwarded) .setForwarded(forwarded)
// .setAttachment(attachment) TODO make this work
.build(); .build();
message.setReceivedDate(receivedDate); message.setReceivedDate(receivedDate);
message.setReadDate(readDate); message.setReadDate(readDate);
return message; return message;
} }
/**
* Sets the message status to {@link MessageStatus#RECEIVED} and sets the
* current time stamp as the received date.
*
* @since Envoy Server Standalone v0.1-beta
*/
public void received() {
receivedDate = LocalDateTime.now();
status = RECEIVED;
}
/**
* Sets the message status to {@link MessageStatus#READ} and sets the
* current time stamp as the read date.
*
* @since Envoy Server Standalone v0.1-beta
*/
public void read() {
readDate = LocalDateTime.now();
status = READ;
}
/** /**
* @return the id of a {link envoy.data.Message} * @return the id of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
@ -154,41 +178,41 @@ public class Message {
* @return the date at which a {link envoy.data.Message} has been created * @return the date at which a {link envoy.data.Message} has been created
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
public Date getCreationDate() { return creationDate; } public LocalDateTime getCreationDate() { return creationDate; }
/** /**
* @param creationDate the creation date to set * @param creationDate the creation date to set
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
* @see Message#getCreationDate() * @see Message#getCreationDate()
*/ */
public void setCreationDate(Date creationDate) { this.creationDate = creationDate; } public void setCreationDate(LocalDateTime creationDate) { this.creationDate = creationDate; }
/** /**
* @return the date at which a {link envoy.data.Message} has been received by * @return the date at which a {link envoy.data.Message} has been received by
* the server * the server
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
public Date getReceivedDate() { return receivedDate; } public LocalDateTime getReceivedDate() { return receivedDate; }
/** /**
* @param receivedDate the received date to set * @param receivedDate the received date to set
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
* @see Message#getReceivedDate() * @see Message#getReceivedDate()
*/ */
public void setReceivedDate(Date receivedDate) { this.receivedDate = receivedDate; } public void setReceivedDate(LocalDateTime receivedDate) { this.receivedDate = receivedDate; }
/** /**
* @return the date at which a {link envoy.data.Message} has been read * @return the date at which a {link envoy.data.Message} has been read
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
public Date getReadDate() { return readDate; } public LocalDateTime getReadDate() { return readDate; }
/** /**
* @param readDate the read date to set * @param readDate the read date to set
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
* @see Message#getReadDate() * @see Message#getReadDate()
*/ */
public void setReadDate(Date readDate) { this.readDate = readDate; } public void setReadDate(LocalDateTime readDate) { this.readDate = readDate; }
/** /**
* @return the status of a {link envoy.data.Message} * @return the status of a {link envoy.data.Message}

View File

@ -1,6 +1,6 @@
package envoy.server.data; package envoy.server.data;
import java.util.Date; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
@ -38,7 +38,7 @@ public class PersistenceManager {
.getOnlineUsers() .getOnlineUsers()
.stream() .stream()
.map(this::getUserByID) .map(this::getUserByID)
.forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); entityManager.merge(user); }); .forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(LocalDateTime.now()); entityManager.merge(user); });
transaction.commit(); transaction.commit();
})); }));
} }
@ -117,7 +117,7 @@ public class PersistenceManager {
* Searches for a {@link User} with a specific ID. * Searches for a {@link User} with a specific ID.
* *
* @param id the id to search for * @param id the id to search for
* @return the user with the specified id * @return the user with the specified ID or {@code null} if none was found
* @since Envoy Server Standalone v0.1-alpha * @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); }
@ -126,7 +126,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 specified ID or {@code null} if none was found
* @since Envoy Server Standalone v0.1-beta * @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); }
@ -135,7 +135,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 specified ID or {@code null} if none was found
* @since Envoy Server Standalone v0.1-beta * @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); }
@ -166,7 +166,7 @@ public class PersistenceManager {
* Searches for a {@link Message} with a specific id. * Searches for a {@link Message} with a specific id.
* *
* @param id the id to search for * @param id the id to search for
* @return the message with the specified id * @return the message with the specified ID or {@code null} if none is found
* @since Envoy Server Standalone v0.1-alpha * @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); }
@ -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 contactID1 the ID of the first contact
* @param userId2 the ID of the second user * @param contactID2 the ID of the second contact
* @since Envoy Server Standalone v0.1-alpha * @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 // Get users by ID
Contact u1 = getContactByID(userId1); Contact c1 = getContactByID(contactID1);
Contact u2 = getContactByID(userId2); Contact c2 = getContactByID(contactID2);
// Add users to each others contact lists // Add users to each others contact lists
u1.getContacts().add(u2); c1.getContacts().add(c2);
u2.getContacts().add(u1); c2.getContacts().add(c1);
// Synchronize changes with the database // Synchronize changes with the database
transaction.begin(); transaction.begin();
entityManager.merge(u1); entityManager.merge(c1);
entityManager.merge(u2); entityManager.merge(c2);
transaction.commit(); transaction.commit();
} }

View File

@ -1,6 +1,6 @@
package envoy.server.data; package envoy.server.data;
import java.util.Date; import java.time.LocalDateTime;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -22,7 +22,6 @@ import envoy.data.User.UserStatus;
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
@Entity @Entity
@Table(name = "users")
@NamedQueries({ @NamedQueries({
@NamedQuery( @NamedQuery(
name = User.findByName, name = User.findByName,
@ -63,24 +62,19 @@ public class User extends Contact {
*/ */
public static final String searchByName = "User.searchByName"; public static final String searchByName = "User.searchByName";
@Column(name = "password_hash")
private byte[] passwordHash; private byte[] passwordHash;
@Temporal(TemporalType.TIMESTAMP) @Column(name = "last_seen")
private Date lastSeen; private LocalDateTime lastSeen;
private UserStatus status; private UserStatus status;
/**
* {@inheritDoc}
*/
@Override @Override
public envoy.data.User toCommon() { public envoy.data.User toCommon() {
return new envoy.data.User(id, name, status, contacts.parallelStream().map(Contact::toFlatCommon).collect(Collectors.toSet())); return new envoy.data.User(id, name, status, contacts.parallelStream().map(Contact::toFlatCommon).collect(Collectors.toSet()));
} }
/**
* {@inheritDoc}
*/
@Override @Override
protected envoy.data.User toFlatCommon() { return new envoy.data.User(id, name, status, Set.of()); } protected envoy.data.User toFlatCommon() { return new envoy.data.User(id, name, status, Set.of()); }
@ -100,13 +94,13 @@ public class User extends Contact {
* @return the last date the user has been online * @return the last date the user has been online
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
public Date getLastSeen() { return lastSeen; } public LocalDateTime getLastSeen() { return lastSeen; }
/** /**
* @param lastSeen the latest date at which the user has been online to set * @param lastSeen the latest date at which the user has been online to set
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
public void setLastSeen(Date lastSeen) { this.lastSeen = lastSeen; } public void setLastSeen(LocalDateTime lastSeen) { this.lastSeen = lastSeen; }
/** /**
* @return the status * @return the status

View File

@ -1,10 +1,7 @@
package envoy.server.net; package envoy.server.net;
import java.util.Date; import java.time.LocalDateTime;
import java.util.HashMap; import java.util.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.jenkov.nioserver.ISocketIdListener; import com.jenkov.nioserver.ISocketIdListener;
@ -53,9 +50,9 @@ public class ConnectionManager implements ISocketIdListener {
public void socketCancelled(long socketID) { public void socketCancelled(long socketID) {
if (!pendingSockets.remove(socketID)) { if (!pendingSockets.remove(socketID)) {
// Notify contacts of this users offline-going // 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.setStatus(UserStatus.OFFLINE);
user.setLastSeen(new Date()); user.setLastSeen(LocalDateTime.now());
UserStatusChangeProcessor.updateUserStatus(user); UserStatusChangeProcessor.updateUserStatus(user);
// Remove the socket // Remove the socket
@ -83,14 +80,14 @@ public class ConnectionManager implements ISocketIdListener {
* @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
*/ */
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 * @param socketID the id of the socket whose User is needed
* @return the userId associated with this socketId * @return the userId associated with this socketId
* @since Envoy Server Standalone v0.1-alpha * @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(); return sockets.entrySet().stream().filter(entry -> entry.getValue().equals(socketID)).findFirst().get().getKey();
} }
@ -108,8 +105,6 @@ public class ConnectionManager implements ISocketIdListener {
public Set<Long> getOnlineUsers() { return sockets.keySet(); } public Set<Long> getOnlineUsers() { return sockets.keySet(); }
/** /**
* Returns all members of a group who are currently online.
*
* @param group the group to search for * @param group the group to search for
* @return a set of all IDs of currently active members in this group * @return a set of all IDs of currently active members in this group
* @since Envoy Server Standalone v0.1-beta * @since Envoy Server Standalone v0.1-beta

View File

@ -43,7 +43,6 @@ public class ObjectWriteProxy {
// Create message targeted at the client // Create message targeted at the client
final Message response = writeProxy.getMessage(); final Message response = writeProxy.getMessage();
response.socketId = recipientSocketID; response.socketId = recipientSocketID;
logger.fine("Sending " + obj); logger.fine("Sending " + obj);
// Serialize object to byte array // Serialize object to byte array

View File

@ -4,7 +4,7 @@ import java.io.IOException;
import java.util.logging.Logger; import java.util.logging.Logger;
import envoy.event.ElementOperation; import envoy.event.ElementOperation;
import envoy.event.contact.ContactOperationEvent; import envoy.event.contact.ContactOperation;
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;
@ -18,25 +18,25 @@ import envoy.util.EnvoyLog;
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
public class ContactOperationProcessor implements ObjectProcessor<ContactOperationEvent> { public class ContactOperationProcessor implements ObjectProcessor<ContactOperation> {
private static final ConnectionManager connectionManager = ConnectionManager.getInstance(); private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final Logger logger = EnvoyLog.getLogger(ContactOperationProcessor.class); private static final Logger logger = EnvoyLog.getLogger(ContactOperationProcessor.class);
@Override @Override
public void process(ContactOperationEvent evt, long socketId, ObjectWriteProxy writeProxy) throws IOException { public void process(ContactOperation evt, long socketId, ObjectWriteProxy writeProxy) throws IOException {
switch (evt.getOperationType()) { switch (evt.getOperationType()) {
case ADD: case ADD:
final long userID = ConnectionManager.getInstance().getUserIdBySocketId(socketId); final long userID = ConnectionManager.getInstance().getUserIdBySocketID(socketId);
final long contactId = evt.get().getID(); final long contactId = evt.get().getID();
logger.fine(String.format("Adding user %s to the contact list of user %d.%n", evt.get(), userID)); 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 // Notify the contact if online
if (ConnectionManager.getInstance().isOnline(contactId)) if (ConnectionManager.getInstance().isOnline(contactId))
writeProxy.write(connectionManager.getSocketId(contactId), writeProxy.write(connectionManager.getSocketID(contactId),
new ContactOperationEvent(PersistenceManager.getInstance().getUserByID(userID).toCommon(), ElementOperation.ADD)); new ContactOperation(PersistenceManager.getInstance().getUserByID(userID).toCommon(), ElementOperation.ADD));
break; break;
default: default:
logger.warning(String.format("Received %s with an unsupported operation.%n", evt)); logger.warning(String.format("Received %s with an unsupported operation.%n", evt));
@ -44,5 +44,5 @@ public class ContactOperationProcessor implements ObjectProcessor<ContactOperati
} }
@Override @Override
public Class<ContactOperationEvent> getInputClass() { return ContactOperationEvent.class; } public Class<ContactOperation> getInputClass() { return ContactOperation.class; }
} }

View File

@ -33,7 +33,7 @@ public class ContactSearchProcessor implements ObjectProcessor<ContactSearchRequ
public void process(ContactSearchRequest request, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(ContactSearchRequest request, long socketID, ObjectWriteProxy writeProxy) throws IOException {
writeProxy.write(socketID, writeProxy.write(socketID,
new ContactSearchResult(PersistenceManager.getInstance() new ContactSearchResult(PersistenceManager.getInstance()
.searchUsers(request.get(), ConnectionManager.getInstance().getUserIdBySocketId(socketID)) .searchUsers(request.get(), ConnectionManager.getInstance().getUserIdBySocketID(socketID))
.stream() .stream()
.map(User::toCommon) .map(User::toCommon)
.collect(Collectors.toList()))); .collect(Collectors.toList())));

View File

@ -4,8 +4,8 @@ import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
import envoy.event.ElementOperation; import envoy.event.ElementOperation;
import envoy.event.GroupCreationEvent; import envoy.event.GroupCreation;
import envoy.event.contact.ContactOperationEvent; import envoy.event.contact.ContactOperation;
import envoy.server.data.Contact; import envoy.server.data.Contact;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
@ -19,29 +19,29 @@ import envoy.server.net.ObjectWriteProxy;
* @author Maximilian K&auml;fer * @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-beta * @since Envoy Server Standalone v0.1-beta
*/ */
public class GroupCreationProcessor implements ObjectProcessor<GroupCreationEvent> { public class GroupCreationProcessor implements ObjectProcessor<GroupCreation> {
private final PersistenceManager persistenceManager = PersistenceManager.getInstance(); private final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private final ConnectionManager connectionManager = ConnectionManager.getInstance(); private final ConnectionManager connectionManager = ConnectionManager.getInstance();
@Override @Override
public void process(GroupCreationEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(GroupCreation groupCreation, long socketID, ObjectWriteProxy writeProxy) throws IOException {
envoy.server.data.Group group = new envoy.server.data.Group(); envoy.server.data.Group group = new envoy.server.data.Group();
group.setName(input.get()); group.setName(groupCreation.get());
group.setContacts(new HashSet<>()); group.setContacts(new HashSet<>());
input.getInitialMemberIDs().stream().map(persistenceManager::getUserByID).forEach(group.getContacts()::add); groupCreation.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().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); persistenceManager.addContact(group);
group.getContacts() group.getContacts()
.stream() .stream()
.map(Contact::getID) .map(Contact::getID)
.filter(connectionManager::isOnline) .filter(connectionManager::isOnline)
.map(connectionManager::getSocketId) .map(connectionManager::getSocketID)
.forEach(memberSocketID -> { .forEach(memberSocketID -> {
try { try {
writeProxy.write(memberSocketID, new ContactOperationEvent(group.toCommon(), ElementOperation.ADD)); writeProxy.write(memberSocketID, new ContactOperation(group.toCommon(), ElementOperation.ADD));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -49,5 +49,5 @@ public class GroupCreationProcessor implements ObjectProcessor<GroupCreationEven
} }
@Override @Override
public Class<GroupCreationEvent> getInputClass() { return GroupCreationEvent.class; } public Class<GroupCreation> getInputClass() { return GroupCreation.class; }
} }

View File

@ -8,7 +8,7 @@ import javax.persistence.EntityExistsException;
import envoy.data.GroupMessage; import envoy.data.GroupMessage;
import envoy.data.Message.MessageStatus; import envoy.data.Message.MessageStatus;
import envoy.event.MessageStatusChangeEvent; import envoy.event.MessageStatusChange;
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;
@ -39,14 +39,14 @@ public class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
.replace(members.stream().filter(sender -> groupMessage.getSenderID() == sender.getID()).findAny().get().getID(), MessageStatus.READ); .replace(members.stream().filter(sender -> groupMessage.getSenderID() == sender.getID()).findAny().get().getID(), MessageStatus.READ);
// Checks if all memberMessageStatuses are RECEIVED and if so sets the // Checks if all memberMessageStatuses are RECEIVED and if so sets the
// groupMessage Status to RECEIVED and sends a MessageStatusChangeEvent to the // groupMessage Status to RECEIVED and sends a MessageStatusChange to the
// sender, if he is still online. // sender, if he is still online.
if (!groupMessage.getMemberStatuses().containsValue(MessageStatus.SENT)) { if (!groupMessage.getMemberStatuses().containsValue(MessageStatus.SENT)) {
groupMessage.setStatus(MessageStatus.RECEIVED); groupMessage.setStatus(MessageStatus.RECEIVED);
if (connectionManager.isOnline(connectionManager.getUserIdBySocketId(socketID))) try { if (connectionManager.isOnline(connectionManager.getUserIdBySocketID(socketID))) try {
writeProxy.write(socketID, new MessageStatusChangeEvent(groupMessage)); writeProxy.write(socketID, new MessageStatusChange(groupMessage));
} catch (IOException e) { } catch (IOException e) {
logger.warning("Sender of the groupMessage online. Failed to send MessageStatusChangeEvent"); logger.warning("Sender of the groupMessage online. Failed to send MessageStatusChange");
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -65,7 +65,7 @@ public class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
private void sendToMember(ConnectionManager connectionManager, GroupMessage groupMessage, long memberID, ObjectWriteProxy writeProxy) { private void sendToMember(ConnectionManager connectionManager, GroupMessage groupMessage, long memberID, ObjectWriteProxy writeProxy) {
if (connectionManager.isOnline(memberID)) try { if (connectionManager.isOnline(memberID)) try {
// If recipient is online, send the groupMessage directly // If recipient is online, send the groupMessage directly
writeProxy.write(connectionManager.getSocketId(memberID), groupMessage); writeProxy.write(connectionManager.getSocketID(memberID), groupMessage);
} catch (IOException e) { } catch (IOException e) {
logger.warning("Recipient online. Failed to send message" + groupMessage.getID()); logger.warning("Recipient online. Failed to send message" + groupMessage.getID());
e.printStackTrace(); e.printStackTrace();

View File

@ -2,7 +2,7 @@ package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import envoy.event.GroupResizeEvent; import envoy.event.GroupResize;
import envoy.server.data.Contact; import envoy.server.data.Contact;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
@ -16,24 +16,24 @@ import envoy.server.net.ObjectWriteProxy;
* @author Maximilian K&auml;fer * @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-beta * @since Envoy Server Standalone v0.1-beta
*/ */
public class GroupResizeProcessor implements ObjectProcessor<GroupResizeEvent> { public class GroupResizeProcessor implements ObjectProcessor<GroupResize> {
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance(); private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private static final ConnectionManager connectionManager = ConnectionManager.getInstance(); private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
@Override @Override
public void process(GroupResizeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(GroupResize groupResize, long socketID, ObjectWriteProxy writeProxy) throws IOException {
// Acquire the group to resize from the database // Acquire the group to resize from the database
var group = persistenceManager.getGroupByID(input.getGroupID()); var group = persistenceManager.getGroupByID(groupResize.getGroupID());
// Perform the desired operation // Perform the desired operation
switch (input.getOperation()) { switch (groupResize.getOperation()) {
case ADD: case ADD:
group.getContacts().add(persistenceManager.getUserByID(input.get().getID())); group.getContacts().add(persistenceManager.getUserByID(groupResize.get().getID()));
break; break;
case REMOVE: case REMOVE:
group.getContacts().remove(persistenceManager.getUserByID(input.get().getID())); group.getContacts().remove(persistenceManager.getUserByID(groupResize.get().getID()));
break; break;
} }
@ -46,7 +46,7 @@ public class GroupResizeProcessor implements ObjectProcessor<GroupResizeEvent> {
.stream() .stream()
.map(Contact::getID) .map(Contact::getID)
.filter(connectionManager::isOnline) .filter(connectionManager::isOnline)
.map(connectionManager::getSocketId) .map(connectionManager::getSocketID)
.forEach(memberSocketID -> { .forEach(memberSocketID -> {
try { try {
writeProxy.write(memberSocketID, commonGroup); writeProxy.write(memberSocketID, commonGroup);
@ -57,5 +57,5 @@ public class GroupResizeProcessor implements ObjectProcessor<GroupResizeEvent> {
} }
@Override @Override
public Class<GroupResizeEvent> getInputClass() { return GroupResizeEvent.class; } public Class<GroupResize> getInputClass() { return GroupResize.class; }
} }

View File

@ -1,14 +1,12 @@
package envoy.server.processors; package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Logger;
import envoy.data.IDGenerator; import envoy.data.IDGenerator;
import envoy.event.IDGeneratorRequest; import envoy.event.IDGeneratorRequest;
import envoy.server.data.ConfigItem; import envoy.server.data.ConfigItem;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.net.ObjectWriteProxy; import envoy.server.net.ObjectWriteProxy;
import envoy.util.EnvoyLog;
/** /**
* Project: <strong>envoy-server-standalone</strong><br> * Project: <strong>envoy-server-standalone</strong><br>
@ -22,17 +20,13 @@ import envoy.util.EnvoyLog;
public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorRequest> { public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorRequest> {
private static final long ID_RANGE = 200; private static final long ID_RANGE = 200;
private static final Logger logger = EnvoyLog.getLogger(IDGeneratorRequestProcessor.class);
@Override @Override
public Class<IDGeneratorRequest> getInputClass() { return IDGeneratorRequest.class; } public Class<IDGeneratorRequest> getInputClass() { return IDGeneratorRequest.class; }
@Override @Override
public void process(IDGeneratorRequest input, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(IDGeneratorRequest input, long socketID, ObjectWriteProxy writeProxy) throws IOException {
logger.fine("Received id generation request."); writeProxy.write(socketID, createIDGenerator());
var generator = createIDGenerator();
logger.info("Sending new id generator " + generator);
writeProxy.write(socketID, generator);
} }
/** /**

View File

@ -1,21 +1,26 @@
package envoy.server.processors; package envoy.server.processors;
import static envoy.data.User.UserStatus.ONLINE;
import static envoy.event.HandshakeRejection.*;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
import enovy.server.util.VersionUtils;
import envoy.data.LoginCredentials; import envoy.data.LoginCredentials;
import envoy.data.Message.MessageStatus; import envoy.data.Message.MessageStatus;
import envoy.data.User; import envoy.event.HandshakeRejection;
import envoy.data.User.UserStatus; import envoy.event.MessageStatusChange;
import envoy.event.HandshakeRejectionEvent;
import envoy.event.MessageStatusChangeEvent;
import envoy.server.data.Message;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.data.User;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy; import envoy.server.net.ObjectWriteProxy;
import envoy.util.Bounds;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
/** /**
@ -29,7 +34,7 @@ import envoy.util.EnvoyLog;
* @author Maximilian K&auml;fer * @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
public class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials> { public final class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials> {
private final PersistenceManager persistenceManager = PersistenceManager.getInstance(); private final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private final ConnectionManager connectionManager = ConnectionManager.getInstance(); private final ConnectionManager connectionManager = ConnectionManager.getInstance();
@ -37,118 +42,99 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
private static final Logger logger = EnvoyLog.getLogger(LoginCredentialProcessor.class); private static final Logger logger = EnvoyLog.getLogger(LoginCredentialProcessor.class);
@Override @Override
public void process(LoginCredentials input, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException {
// Cache this write proxy for user-independant notifications
UserStatusChangeProcessor.setWriteProxy(writeProxy); UserStatusChangeProcessor.setWriteProxy(writeProxy);
logger.info(String.format("Received login credentials %s from socket ID %d", input, socketID));
envoy.server.data.User user = getUser(input, socketID, writeProxy); if (!VersionUtils.verifyCompatibility(credentials.getClientVersion())) {
logger.info("The client has the wrong version.");
// Not logged in successfully writeProxy.write(socketID, new HandshakeRejection(WRONG_VERSION));
if (user == null) {
logger.info("Rejecting handshake on socket " + socketID);
return; return;
} }
// Acquire a user object (or reject the handshake if that's impossible)
User user = null;
if (!credentials.isRegistration()) {
try {
user = persistenceManager.getUserByName(credentials.getIdentifier());
// Checking if user is already online
if (connectionManager.isOnline(user.getID())) {
logger.warning(user + " is already online!");
writeProxy.write(socketID, new HandshakeRejection(INTERNAL_ERROR));
return;
}
// Evaluating the correctness of the password hash
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) {
logger.info(user + " has entered the wrong password.");
writeProxy.write(socketID, new HandshakeRejection(WRONG_PASSWORD_OR_USER));
return;
}
} catch (NoResultException e) {
logger.info("The requested user does not exist.");
writeProxy.write(socketID, new HandshakeRejection(WRONG_PASSWORD_OR_USER));
return;
}
} else {
// Validate user name
if (!Bounds.isValidContactName(credentials.getIdentifier())) {
logger.info("The requested user name is not valid.");
writeProxy.write(socketID, new HandshakeRejection(INTERNAL_ERROR));
return;
}
try {
// Checking that no user already has this identifier
PersistenceManager.getInstance().getUserByName(credentials.getIdentifier());
// This code only gets executed if this user already exists
logger.info("The requested user already exists.");
writeProxy.write(socketID, new HandshakeRejection(USERNAME_TAKEN));
return;
} catch (NoResultException e) {
// Creation of a new user
user = new User();
user.setName(credentials.getIdentifier());
user.setLastSeen(LocalDateTime.now());
user.setStatus(ONLINE);
user.setPasswordHash(credentials.getPasswordHash());
user.setContacts(new HashSet<>());
persistenceManager.addContact(user);
logger.info("Registered new " + user);
}
}
logger.info(user + " successfully authenticated.");
connectionManager.registerUser(user.getID(), socketID); connectionManager.registerUser(user.getID(), socketID);
// Notifies contacts of this users online-going and updates his status in the // Change status and notify contacts about it
// database user.setStatus(ONLINE);
user.setStatus(UserStatus.ONLINE);
UserStatusChangeProcessor.updateUserStatus(user); UserStatusChangeProcessor.updateUserStatus(user);
// Complete handshake // Complete the handshake
logger.fine("Sending user...");
writeProxy.write(socketID, user.toCommon()); writeProxy.write(socketID, user.toCommon());
logger.fine("Acquiring pending messages for the client...");
List<Message> pendingMessages = PersistenceManager.getInstance().getPendingMessages(user); final var pendingMessages = PersistenceManager.getInstance().getPendingMessages(user);
for (Message msg : pendingMessages) logger.fine("Sending " + pendingMessages.size() + " pending messages to " + user + "...");
for (var msg : pendingMessages) {
final var msgCommon = msg.toCommon();
if (msg.getStatus() == MessageStatus.SENT) { if (msg.getStatus() == MessageStatus.SENT) {
logger.info("Sending message " + msg.toCommon());
writeProxy.write(socketID, msg.toCommon()); // Send the message
msg.setReceivedDate(new Date()); writeProxy.write(socketID, msgCommon);
msg.setStatus(MessageStatus.RECEIVED); msg.received();
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);
}
PersistenceManager.getInstance().updateMessage(msg); PersistenceManager.getInstance().updateMessage(msg);
} else {
var evt = new MessageStatusChangeEvent(msg.toCommon()); // Notify the sender about the delivery
logger.info("Sending messageStatusChangeEvent " + evt); if (connectionManager.isOnline(msg.getSender().getID())) {
writeProxy.write(socketID, evt); msgCommon.nextStatus();
writeProxy.write(connectionManager.getSocketID(msg.getSender().getID()), new MessageStatusChange(msgCommon));
}
} else writeProxy.write(socketID, new MessageStatusChange(msgCommon));
} }
} }
@Override @Override
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; } public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
private envoy.server.data.User getUser(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException {
return credentials.isRegistration() ? newUser(credentials, socketID, writeProxy) : checkForExistingUser(credentials, socketID, writeProxy);
}
/**
* @param credentials the input to evaluate
* @param socketID the socket ID at which the client performing the handshake
* is connected
* @param writeProxy the {@link ObjectWriteProxy} to use if login was not
* successful
* @return the database user matching the login credentials
* @throws IOException if sending the failed login back to the client failed
* @since Envoy Server Standalone v0.1-alpha
*/
private envoy.server.data.User checkForExistingUser(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException {
try {
envoy.server.data.User user = persistenceManager.getUserByName(credentials.getIdentifier());
// Checking if user is already online
if (connectionManager.isOnline(user.getID())) {
writeProxy.write(socketID, new HandshakeRejectionEvent(HandshakeRejectionEvent.INTERNAL_ERROR));
return null;
}
// Evaluating the correctness of the password hash
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) {
writeProxy.write(socketID, new HandshakeRejectionEvent(HandshakeRejectionEvent.WRONG_PASSWORD_OR_USER));
return null;
}
return user;
} catch (NoResultException e) {
// Checking if user exists
writeProxy.write(socketID, new HandshakeRejectionEvent(HandshakeRejectionEvent.INTERNAL_ERROR));
} catch (InputMismatchException e) {
// Checking if the given password hash is correct
writeProxy.write(socketID, new HandshakeRejectionEvent(HandshakeRejectionEvent.WRONG_PASSWORD_OR_USER));
}
return null;
}
/**
* @param credentials the credentials upon which to create the new {@link User}
* @param socketID the socketID at which the client performing the handshake
* is connected
* @param writeProxy the write proxy used to notify the client about handshake
* rejection
* @return the newly created {@link User}
* @throws IOException if sending the failed login back to the client failed
* @since Envoy Server Standalone v0.1-alpha
*/
private envoy.server.data.User newUser(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException {
try {
// Checking that no user already has this identifier
PersistenceManager.getInstance().getUserByName(credentials.getIdentifier());
// this code only gets executed if this user already exists
writeProxy.write(socketID, new HandshakeRejectionEvent(HandshakeRejectionEvent.INTERNAL_ERROR));
return null;
} catch (NoResultException e) {
// Creation of a new user
envoy.server.data.User user;
user = new envoy.server.data.User();
user.setName(credentials.getIdentifier());
user.setLastSeen(new Date());
user.setStatus(User.UserStatus.ONLINE);
user.setPasswordHash(credentials.getPasswordHash());
user.setContacts(new HashSet<>());
persistenceManager.addContact(user);
return user;
}
}
} }

View File

@ -1,22 +1,21 @@
package envoy.server.processors; package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import java.util.Date; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.persistence.EntityExistsException; import javax.persistence.EntityExistsException;
import envoy.data.Message; import envoy.data.Message;
import envoy.data.Message.MessageStatus; import envoy.event.MessageStatusChange;
import envoy.event.MessageStatusChangeEvent;
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;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
/** /**
* This {@link ObjectProcessor} handles incoming {@link Message}s.<br> * This {@link ObjectProcessor} handles incoming {@link Message}s.
* <br> * <p>
* Project: <strong>envoy-server-standalone</strong><br> * Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>MessageProcessor.java</strong><br> * File: <strong>MessageProcessor.java</strong><br>
* Created: <strong>30.12.2019</strong><br> * Created: <strong>30.12.2019</strong><br>
@ -27,6 +26,8 @@ import envoy.util.EnvoyLog;
*/ */
public class MessageProcessor implements ObjectProcessor<Message> { public class MessageProcessor implements ObjectProcessor<Message> {
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final Logger logger = EnvoyLog.getLogger(MessageProcessor.class); private static final Logger logger = EnvoyLog.getLogger(MessageProcessor.class);
@Override @Override
@ -36,35 +37,32 @@ public class MessageProcessor implements ObjectProcessor<Message> {
if (message.getClass().equals(envoy.data.GroupMessage.class)) return; if (message.getClass().equals(envoy.data.GroupMessage.class)) return;
message.nextStatus(); message.nextStatus();
ConnectionManager connectionManager = ConnectionManager.getInstance();
sendToUser(connectionManager, message, writeProxy); // Convert to server message
if (message.getStatus() != MessageStatus.SENT) { final var serverMessage = new envoy.server.data.Message(message);
// Sending a messageStatusChangeEvent to the sender
try {
writeProxy.write(socketID, new MessageStatusChangeEvent(message));
} catch (IOException e) {
logger.warning("Could not send messageStatusChangeEvent to the sender of this message with ID: " + message.getID());
e.printStackTrace();
}
}
try {
PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message));
} catch (EntityExistsException e) {
logger.warning("Received a message with an id that already exists");
}
}
private void sendToUser(ConnectionManager connectionManager, Message message, ObjectWriteProxy writeProxy) { try {
if (connectionManager.isOnline(message.getRecipientID())) try {
// If recipient is online, send the message directly // Persist the message
writeProxy.write(connectionManager.getSocketId(message.getRecipientID()), message); persistenceManager.addMessage(serverMessage);
// Update the message status to RECEIVED
message.setReceivedDate(new Date()); // Send the message to the recipient if online
if (connectionManager.isOnline(message.getRecipientID())) {
writeProxy.write(connectionManager.getSocketID(message.getRecipientID()), message);
// Increment status
message.nextStatus(); message.nextStatus();
serverMessage.received();
persistenceManager.updateMessage(serverMessage);
// Notify the sender about the delivery
// Note that the exact time stamp might differ slightly
writeProxy.write(socketID, new MessageStatusChange(message));
}
} catch (EntityExistsException e) {
logger.log(Level.WARNING, "Received " + message + " with an ID that already exists!");
} catch (IOException e) { } catch (IOException e) {
logger.warning("Recipient online. Failed to send message" + message.getID()); logger.log(Level.WARNING, "Failed to deliver " + message + ":", e);
e.printStackTrace();
} }
} }

View File

@ -3,7 +3,7 @@ package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import envoy.data.Message.MessageStatus; import envoy.data.Message.MessageStatus;
import envoy.event.MessageStatusChangeEvent; import envoy.event.MessageStatusChange;
import envoy.exception.EnvoyException; import envoy.exception.EnvoyException;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
@ -17,26 +17,25 @@ import envoy.server.net.ObjectWriteProxy;
* @author Leon Hofmeister * @author Leon Hofmeister
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
public class MessageStatusChangeProcessor implements ObjectProcessor<MessageStatusChangeEvent> { public class MessageStatusChangeProcessor implements ObjectProcessor<MessageStatusChange> {
private final PersistenceManager persistenceManager = PersistenceManager.getInstance(); private final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private final ConnectionManager connectionManager = ConnectionManager.getInstance(); private final ConnectionManager connectionManager = ConnectionManager.getInstance();
@Override @Override
public void process(MessageStatusChangeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(MessageStatusChange statusChange, long socketID, ObjectWriteProxy writeProxy) throws IOException {
// Any other status than READ is not supposed to be sent to the server // 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")); if (statusChange.get() != MessageStatus.READ) throw new IOException(new EnvoyException(statusChange + " has an invalid status"));
envoy.server.data.Message msg = persistenceManager.getMessageByID(input.getID()); final var msg = persistenceManager.getMessageByID(statusChange.getID());
msg.setStatus(input.get()); msg.read();
msg.setReadDate(input.getDate());
persistenceManager.updateMessage(msg); persistenceManager.updateMessage(msg);
// Notifies the sender of the message about the status-update to READ // Notifies the sender of the message about the status-update to READ
final long senderId = msg.getSender().getID(); 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), statusChange);
} }
@Override @Override
public Class<MessageStatusChangeEvent> getInputClass() { return MessageStatusChangeEvent.class; } public Class<MessageStatusChange> getInputClass() { return MessageStatusChange.class; }
} }

View File

@ -2,7 +2,7 @@ package envoy.server.processors;
import java.io.IOException; import java.io.IOException;
import envoy.event.NameChangeEvent; import envoy.event.NameChange;
import envoy.server.data.Contact; import envoy.server.data.Contact;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.data.User; import envoy.server.data.User;
@ -17,20 +17,20 @@ import envoy.server.net.ObjectWriteProxy;
* @author Leon Hofmeister * @author Leon Hofmeister
* @since Envoy Server Standalone v0.1-beta * @since Envoy Server Standalone v0.1-beta
*/ */
public class NameChangeProcessor implements ObjectProcessor<NameChangeEvent> { public class NameChangeProcessor implements ObjectProcessor<NameChange> {
@Override @Override
public void process(NameChangeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(NameChange nameChange, long socketID, ObjectWriteProxy writeProxy) throws IOException {
PersistenceManager persistenceManager = PersistenceManager.getInstance(); PersistenceManager persistenceManager = PersistenceManager.getInstance();
ConnectionManager connectionManager = ConnectionManager.getInstance(); ConnectionManager connectionManager = ConnectionManager.getInstance();
Contact toUpdate = persistenceManager.getContactByID(input.getID()); Contact toUpdate = persistenceManager.getContactByID(nameChange.getID());
toUpdate.setName(input.get()); toUpdate.setName(nameChange.get());
persistenceManager.updateContact(toUpdate); persistenceManager.updateContact(toUpdate);
// notifying online contacts of this client of his name change // Notify online contacts of the name change
toUpdate.getContacts().stream().filter(contact -> (contact instanceof User && connectionManager.isOnline(contact.getID()))).forEach(user -> { toUpdate.getContacts().stream().filter(User.class::isInstance).map(Contact::getID).filter(connectionManager::isOnline).forEach(userID -> {
try { try {
writeProxy.write(user.getID(), input); writeProxy.write(userID, nameChange);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -38,5 +38,5 @@ public class NameChangeProcessor implements ObjectProcessor<NameChangeEvent> {
} }
@Override @Override
public Class<NameChangeEvent> getInputClass() { return NameChangeEvent.class; } public Class<NameChange> getInputClass() { return NameChange.class; }
} }

View File

@ -4,7 +4,7 @@ import java.io.IOException;
import java.util.logging.Logger; import java.util.logging.Logger;
import envoy.data.User.UserStatus; import envoy.data.User.UserStatus;
import envoy.event.UserStatusChangeEvent; import envoy.event.UserStatusChange;
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;
@ -12,7 +12,7 @@ import envoy.server.net.ObjectWriteProxy;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
/** /**
* This processor handles incoming {@link UserStatusChangeEvent}.<br> * This processor handles incoming {@link UserStatusChange}.<br>
* <br> * <br>
* Project: <strong>envoy-server-standalone</strong><br> * Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>UserStatusChangeProcessor.java</strong><br> * File: <strong>UserStatusChangeProcessor.java</strong><br>
@ -21,7 +21,7 @@ import envoy.util.EnvoyLog;
* @author Leon Hofmeister * @author Leon Hofmeister
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChangeEvent> { public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChange> {
private static ObjectWriteProxy writeProxy; private static ObjectWriteProxy writeProxy;
private static PersistenceManager persistenceManager = PersistenceManager.getInstance(); private static PersistenceManager persistenceManager = PersistenceManager.getInstance();
@ -29,13 +29,13 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
private static final Logger logger = EnvoyLog.getLogger(UserStatusChangeProcessor.class); private static final Logger logger = EnvoyLog.getLogger(UserStatusChangeProcessor.class);
@Override @Override
public Class<UserStatusChangeEvent> getInputClass() { return UserStatusChangeEvent.class; } public Class<UserStatusChange> getInputClass() { return UserStatusChange.class; }
@Override @Override
public void process(UserStatusChangeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { public void process(UserStatusChange input, long socketID, ObjectWriteProxy writeProxy) throws IOException {
// new status should not equal old status // 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())) {
logger.warning("Received an unnecessary UserStatusChangeEvent"); logger.warning("Received an unnecessary UserStatusChange");
return; return;
} }
updateUserStatus(input); updateUserStatus(input);
@ -45,7 +45,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
* Sets the {@link UserStatus} for a given user. Both offline contacts and * Sets the {@link UserStatus} for a given user. Both offline contacts and
* currently online contacts are notified. * currently online contacts are notified.
* *
* @param user the {@link UserStatusChangeEvent} that signals the change * @param user the {@link UserStatusChange} that signals the change
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
@ -59,10 +59,10 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
} }
/** /**
* @param evt the {@link UserStatusChangeEvent} * @param evt the {@link UserStatusChange}
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
public static void updateUserStatus(UserStatusChangeEvent evt) { updateUserStatus(persistenceManager.getUserByID(evt.getID())); } public static void updateUserStatus(UserStatusChange evt) { updateUserStatus(persistenceManager.getUserByID(evt.getID())); }
/** /**
* notifies active contacts of this {@link User} that his {@link UserStatus} has * notifies active contacts of this {@link User} that his {@link UserStatus} has
@ -72,11 +72,11 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
private static void notifyContacts(User user) { private static void notifyContacts(User user) {
UserStatusChangeEvent evt = new UserStatusChangeEvent(user.getID(), user.getStatus()); UserStatusChange evt = new UserStatusChange(user.getID(), user.getStatus());
ConnectionManager connectionManager = ConnectionManager.getInstance(); ConnectionManager connectionManager = ConnectionManager.getInstance();
try { try {
for (envoy.server.data.Contact 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();
logger.warning("Could not notify online contacts of user " + evt.getID() + " that his status has been changed"); logger.warning("Could not notify online contacts of user " + evt.getID() + " that his status has been changed");