Shorten event names, refactor, prepare compatibility verification
This commit is contained in:
		| @@ -29,6 +29,20 @@ import envoy.util.EnvoyLog; | ||||
|  */ | ||||
| public class Startup { | ||||
|  | ||||
| 	/** | ||||
| 	 * 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"; | ||||
|  | ||||
| 	/** | ||||
| 	 * Initializes the logger with a new config instance. | ||||
| 	 *  | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| package envoy.server.data; | ||||
|  | ||||
| import java.util.Date; | ||||
| import java.time.LocalDateTime; | ||||
| import java.util.Set; | ||||
|  | ||||
| import javax.persistence.*; | ||||
| @@ -28,8 +28,7 @@ public abstract class Contact { | ||||
| 	protected String	name; | ||||
|  | ||||
| 	@Column(name = "creation_date") | ||||
| 	@Temporal(TemporalType.TIMESTAMP) | ||||
| 	private Date creationDate; | ||||
| 	private LocalDateTime creationDate; | ||||
|  | ||||
| 	@ManyToMany(fetch = FetchType.EAGER) | ||||
| 	protected Set<Contact> contacts; | ||||
| @@ -95,17 +94,14 @@ public abstract class Contact { | ||||
| 	 * @return the creationDate | ||||
| 	 * @since Envoy Server Standalone v0.1-beta | ||||
| 	 */ | ||||
| 	public Date getCreationDate() { return creationDate; } | ||||
| 	public LocalDateTime getCreationDate() { return creationDate; } | ||||
|  | ||||
| 	/** | ||||
| 	 * @param creationDate the creationDate to set | ||||
| 	 * @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 | ||||
| 	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()); } | ||||
| } | ||||
|   | ||||
| @@ -1,9 +1,10 @@ | ||||
| package envoy.server.data; | ||||
|  | ||||
| import java.util.Date; | ||||
| import java.time.LocalDateTime; | ||||
|  | ||||
| import javax.persistence.*; | ||||
|  | ||||
| import envoy.data.Message.MessageStatus; | ||||
| import envoy.data.MessageBuilder; | ||||
|  | ||||
| /** | ||||
| @@ -50,16 +51,13 @@ public class Message { | ||||
| 	protected Contact recipient; | ||||
|  | ||||
| 	@Column(name = "creation_date") | ||||
| 	@Temporal(TemporalType.TIMESTAMP) | ||||
| 	protected Date creationDate; | ||||
| 	protected LocalDateTime creationDate; | ||||
|  | ||||
| 	@Column(name = "received_date") | ||||
| 	@Temporal(TemporalType.TIMESTAMP) | ||||
| 	protected Date receivedDate; | ||||
| 	protected LocalDateTime receivedDate; | ||||
|  | ||||
| 	@Column(name = "read_date") | ||||
| 	@Temporal(TemporalType.TIMESTAMP) | ||||
| 	protected Date readDate; | ||||
| 	protected LocalDateTime readDate; | ||||
|  | ||||
| 	protected String							text; | ||||
| 	protected envoy.data.Message.MessageStatus	status; | ||||
| @@ -113,6 +111,11 @@ public class Message { | ||||
| 		return message; | ||||
| 	} | ||||
|  | ||||
| 	public void received() { | ||||
| 		receivedDate	= LocalDateTime.now(); | ||||
| 		status			= MessageStatus.RECEIVED; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * @return the id of a {link envoy.data.Message} | ||||
| 	 * @since Envoy Server Standalone v0.1-alpha | ||||
| @@ -156,41 +159,41 @@ public class Message { | ||||
| 	 * @return the date at which a {link envoy.data.Message} has been created | ||||
| 	 * @since Envoy Server Standalone v0.1-alpha | ||||
| 	 */ | ||||
| 	public Date getCreationDate() { return creationDate; } | ||||
| 	public LocalDateTime getCreationDate() { return creationDate; } | ||||
|  | ||||
| 	/** | ||||
| 	 * @param creationDate the creation date to set | ||||
| 	 * @since Envoy Server Standalone v0.1-alpha | ||||
| 	 * @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 | ||||
| 	 *         the server | ||||
| 	 * @since Envoy Server Standalone v0.1-alpha | ||||
| 	 */ | ||||
| 	public Date getReceivedDate() { return receivedDate; } | ||||
| 	public LocalDateTime getReceivedDate() { return receivedDate; } | ||||
|  | ||||
| 	/** | ||||
| 	 * @param receivedDate the received date to set | ||||
| 	 * @since Envoy Server Standalone v0.1-alpha | ||||
| 	 * @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 | ||||
| 	 * @since Envoy Server Standalone v0.1-alpha | ||||
| 	 */ | ||||
| 	public Date getReadDate() { return readDate; } | ||||
| 	public LocalDateTime getReadDate() { return readDate; } | ||||
|  | ||||
| 	/** | ||||
| 	 * @param readDate the read date to set | ||||
| 	 * @since Envoy Server Standalone v0.1-alpha | ||||
| 	 * @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} | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| package envoy.server.data; | ||||
|  | ||||
| import java.util.Date; | ||||
| import java.time.LocalDateTime; | ||||
| import java.util.List; | ||||
|  | ||||
| import javax.persistence.EntityManager; | ||||
| @@ -38,7 +38,7 @@ public class PersistenceManager { | ||||
| 				.getOnlineUsers() | ||||
| 				.stream() | ||||
| 				.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(); | ||||
| 		})); | ||||
| 	} | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| package envoy.server.data; | ||||
|  | ||||
| import java.util.Date; | ||||
| import java.time.LocalDateTime; | ||||
| import java.util.Set; | ||||
| import java.util.stream.Collectors; | ||||
|  | ||||
| @@ -66,22 +66,15 @@ public class User extends Contact { | ||||
| 	private byte[] passwordHash; | ||||
|  | ||||
| 	@Column(name = "last_seen") | ||||
| 	@Temporal(TemporalType.TIMESTAMP) | ||||
| 	private Date lastSeen; | ||||
| 	private LocalDateTime lastSeen; | ||||
|  | ||||
| 	private UserStatus status; | ||||
|  | ||||
| 	/** | ||||
| 	 * {@inheritDoc} | ||||
| 	 */ | ||||
| 	@Override | ||||
| 	public envoy.data.User toCommon() { | ||||
| 		return new envoy.data.User(id, name, status, contacts.parallelStream().map(Contact::toFlatCommon).collect(Collectors.toSet())); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * {@inheritDoc} | ||||
| 	 */ | ||||
| 	@Override | ||||
| 	protected envoy.data.User toFlatCommon() { return new envoy.data.User(id, name, status, Set.of()); } | ||||
|  | ||||
| @@ -101,13 +94,13 @@ public class User extends Contact { | ||||
| 	 * @return the last date the user has been online | ||||
| 	 * @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 | ||||
| 	 * @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 | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| package envoy.server.net; | ||||
|  | ||||
| import java.time.LocalDateTime; | ||||
| import java.util.*; | ||||
| import java.util.stream.Collectors; | ||||
|  | ||||
| @@ -51,7 +52,7 @@ public class ConnectionManager implements ISocketIdListener { | ||||
| 			// Notify contacts of this users offline-going | ||||
| 			envoy.server.data.User user = PersistenceManager.getInstance().getUserByID(getUserIdBySocketID(socketID)); | ||||
| 			user.setStatus(UserStatus.OFFLINE); | ||||
| 			user.setLastSeen(new Date()); | ||||
| 			user.setLastSeen(LocalDateTime.now()); | ||||
| 			UserStatusChangeProcessor.updateUserStatus(user); | ||||
|  | ||||
| 			// Remove the socket | ||||
|   | ||||
| @@ -4,7 +4,7 @@ import java.io.IOException; | ||||
| import java.util.logging.Logger; | ||||
|  | ||||
| import envoy.event.ElementOperation; | ||||
| import envoy.event.contact.ContactOperationEvent; | ||||
| import envoy.event.contact.ContactOperation; | ||||
| import envoy.server.data.PersistenceManager; | ||||
| import envoy.server.net.ConnectionManager; | ||||
| import envoy.server.net.ObjectWriteProxy; | ||||
| @@ -18,13 +18,13 @@ import envoy.util.EnvoyLog; | ||||
|  * @author Kai S. K. Engelbart | ||||
|  * @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 Logger				logger				= EnvoyLog.getLogger(ContactOperationProcessor.class); | ||||
|  | ||||
| 	@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()) { | ||||
| 			case ADD: | ||||
| 				final long userID = ConnectionManager.getInstance().getUserIdBySocketID(socketId); | ||||
| @@ -36,7 +36,7 @@ public class ContactOperationProcessor implements ObjectProcessor<ContactOperati | ||||
| 				// Notify the contact if online | ||||
| 				if (ConnectionManager.getInstance().isOnline(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; | ||||
| 			default: | ||||
| 				logger.warning(String.format("Received %s with an unsupported operation.%n", evt)); | ||||
| @@ -44,5 +44,5 @@ public class ContactOperationProcessor implements ObjectProcessor<ContactOperati | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public Class<ContactOperationEvent> getInputClass() { return ContactOperationEvent.class; } | ||||
| 	public Class<ContactOperation> getInputClass() { return ContactOperation.class; } | ||||
| } | ||||
|   | ||||
| @@ -4,8 +4,8 @@ import java.io.IOException; | ||||
| import java.util.HashSet; | ||||
|  | ||||
| import envoy.event.ElementOperation; | ||||
| import envoy.event.GroupCreationEvent; | ||||
| import envoy.event.contact.ContactOperationEvent; | ||||
| import envoy.event.GroupCreation; | ||||
| import envoy.event.contact.ContactOperation; | ||||
| import envoy.server.data.Contact; | ||||
| import envoy.server.data.PersistenceManager; | ||||
| import envoy.server.net.ConnectionManager; | ||||
| @@ -19,17 +19,17 @@ import envoy.server.net.ObjectWriteProxy; | ||||
|  * @author Maximilian Käfer | ||||
|  * @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 ConnectionManager		connectionManager	= ConnectionManager.getInstance(); | ||||
|  | ||||
| 	@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(); | ||||
| 		group.setName(input.get()); | ||||
| 		group.setName(groupCreation.get()); | ||||
| 		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().forEach(c -> c.getContacts().add(group)); | ||||
| 		group.getContacts().add(persistenceManager.getUserByID(connectionManager.getUserIdBySocketID(socketID))); | ||||
| @@ -41,7 +41,7 @@ public class GroupCreationProcessor implements ObjectProcessor<GroupCreationEven | ||||
| 			.map(connectionManager::getSocketID) | ||||
| 			.forEach(memberSocketID -> { | ||||
| 				try { | ||||
| 					writeProxy.write(memberSocketID, new ContactOperationEvent(group.toCommon(), ElementOperation.ADD)); | ||||
| 						writeProxy.write(memberSocketID, new ContactOperation(group.toCommon(), ElementOperation.ADD)); | ||||
| 				} catch (IOException e) { | ||||
| 					e.printStackTrace(); | ||||
| 				} | ||||
| @@ -49,5 +49,5 @@ public class GroupCreationProcessor implements ObjectProcessor<GroupCreationEven | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public Class<GroupCreationEvent> getInputClass() { return GroupCreationEvent.class; } | ||||
| 	public Class<GroupCreation> getInputClass() { return GroupCreation.class; } | ||||
| } | ||||
|   | ||||
| @@ -2,7 +2,7 @@ package envoy.server.processors; | ||||
|  | ||||
| import java.io.IOException; | ||||
|  | ||||
| import envoy.event.GroupResizeEvent; | ||||
| import envoy.event.GroupResize; | ||||
| import envoy.server.data.Contact; | ||||
| import envoy.server.data.PersistenceManager; | ||||
| import envoy.server.net.ConnectionManager; | ||||
| @@ -16,24 +16,24 @@ import envoy.server.net.ObjectWriteProxy; | ||||
|  * @author Maximilian Käfer | ||||
|  * @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 ConnectionManager	connectionManager	= ConnectionManager.getInstance(); | ||||
|  | ||||
| 	@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 | ||||
| 		var group = persistenceManager.getGroupByID(input.getGroupID()); | ||||
| 		var group = persistenceManager.getGroupByID(groupResize.getGroupID()); | ||||
|  | ||||
| 		// Perform the desired operation | ||||
| 		switch (input.getOperation()) { | ||||
| 		switch (groupResize.getOperation()) { | ||||
| 			case ADD: | ||||
| 				group.getContacts().add(persistenceManager.getUserByID(input.get().getID())); | ||||
| 				group.getContacts().add(persistenceManager.getUserByID(groupResize.get().getID())); | ||||
| 				break; | ||||
| 			case REMOVE: | ||||
| 				group.getContacts().remove(persistenceManager.getUserByID(input.get().getID())); | ||||
| 				group.getContacts().remove(persistenceManager.getUserByID(groupResize.get().getID())); | ||||
| 				break; | ||||
| 		} | ||||
|  | ||||
| @@ -57,5 +57,5 @@ public class GroupResizeProcessor implements ObjectProcessor<GroupResizeEvent> { | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public Class<GroupResizeEvent> getInputClass() { return GroupResizeEvent.class; } | ||||
| 	public Class<GroupResize> getInputClass() { return GroupResize.class; } | ||||
| } | ||||
|   | ||||
| @@ -1,14 +1,12 @@ | ||||
| package envoy.server.processors; | ||||
|  | ||||
| import java.io.IOException; | ||||
| import java.util.logging.Logger; | ||||
|  | ||||
| import envoy.data.IDGenerator; | ||||
| import envoy.event.IDGeneratorRequest; | ||||
| import envoy.server.data.ConfigItem; | ||||
| import envoy.server.data.PersistenceManager; | ||||
| import envoy.server.net.ObjectWriteProxy; | ||||
| import envoy.util.EnvoyLog; | ||||
|  | ||||
| /** | ||||
|  * Project: <strong>envoy-server-standalone</strong><br> | ||||
| @@ -22,17 +20,13 @@ import envoy.util.EnvoyLog; | ||||
| public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorRequest> { | ||||
|  | ||||
| 	private static final long ID_RANGE = 200; | ||||
| 	private static final Logger	logger		= EnvoyLog.getLogger(IDGeneratorRequestProcessor.class); | ||||
|  | ||||
| 	@Override | ||||
| 	public Class<IDGeneratorRequest> getInputClass() { return IDGeneratorRequest.class; } | ||||
|  | ||||
| 	@Override | ||||
| 	public void process(IDGeneratorRequest input, long socketID, ObjectWriteProxy writeProxy) throws IOException { | ||||
| 		logger.fine("Received id generation request."); | ||||
| 		var generator = createIDGenerator(); | ||||
| 		logger.info("Sending new id generator " + generator); | ||||
| 		writeProxy.write(socketID, generator); | ||||
| 		writeProxy.write(socketID, createIDGenerator()); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
|   | ||||
| @@ -1,19 +1,23 @@ | ||||
| package envoy.server.processors; | ||||
|  | ||||
| import static envoy.data.User.UserStatus.ONLINE; | ||||
| import static envoy.event.HandshakeRejection.*; | ||||
|  | ||||
| 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 javax.persistence.NoResultException; | ||||
|  | ||||
| import envoy.data.LoginCredentials; | ||||
| import envoy.data.Message.MessageStatus; | ||||
| import envoy.data.User; | ||||
| import envoy.data.User.UserStatus; | ||||
| import envoy.event.HandshakeRejectionEvent; | ||||
| import envoy.event.MessageStatusChangeEvent; | ||||
| import envoy.server.data.Message; | ||||
| import envoy.event.HandshakeRejection; | ||||
| import envoy.event.MessageStatusChange; | ||||
| import envoy.server.Startup; | ||||
| import envoy.server.data.PersistenceManager; | ||||
| import envoy.server.data.User; | ||||
| import envoy.server.net.ConnectionManager; | ||||
| import envoy.server.net.ObjectWriteProxy; | ||||
| import envoy.util.EnvoyLog; | ||||
| @@ -29,126 +33,98 @@ import envoy.util.EnvoyLog; | ||||
|  * @author Maximilian Käfer | ||||
|  * @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 ConnectionManager		connectionManager	= ConnectionManager.getInstance(); | ||||
|  | ||||
| 	private static final Logger logger = EnvoyLog.getLogger(LoginCredentialProcessor.class); | ||||
| 	// private static final Pattern versionPattern = | ||||
| 	// Pattern.compile("v(?<major>\\d).(?<minor>\\d)(?:-(?<suffix>\\w+))?"); | ||||
|  | ||||
| 	@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); | ||||
| 		logger.info(String.format("Received login credentials %s from socket ID %d", input, socketID)); | ||||
|  | ||||
| 		envoy.server.data.User user = getUser(input, socketID, writeProxy); | ||||
|  | ||||
| 		// Not logged in successfully | ||||
| 		if (user == null) { | ||||
| 			logger.info("Rejecting handshake on socket " + socketID); | ||||
| 			return; | ||||
| 		// TODO: Verify compatibility | ||||
| 		if (Startup.MIN_CLIENT_VERSION.compareTo(credentials.getClientVersion()) <= 0 | ||||
| 				&& Startup.MAX_CLIENT_VERSION.compareTo(credentials.getClientVersion()) >= 0) { | ||||
| 			// writeProxy.write(socketID, new HandshakeRejectionEvent("Wrong version")); | ||||
| 			// 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 { | ||||
| 			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(INTERNAL_ERROR)); | ||||
| 				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); | ||||
|  | ||||
| 		// Notifies contacts of this users online-going and updates his status in the | ||||
| 		// database | ||||
| 		user.setStatus(UserStatus.ONLINE); | ||||
| 		// Change status and notify contacts about it | ||||
| 		user.setStatus(ONLINE); | ||||
| 		UserStatusChangeProcessor.updateUserStatus(user); | ||||
|  | ||||
| 		// Complete handshake | ||||
| 		logger.fine("Sending user..."); | ||||
| 		// Complete the handshake | ||||
| 		writeProxy.write(socketID, user.toCommon()); | ||||
| 		logger.fine("Acquiring pending messages for the client..."); | ||||
| 		List<Message> pendingMessages = PersistenceManager.getInstance().getPendingMessages(user); | ||||
| 		for (Message msg : pendingMessages) | ||||
|  | ||||
| 		final var pendingMessages = PersistenceManager.getInstance().getPendingMessages(user); | ||||
| 		logger.fine("Sending " + pendingMessages.size() + " pending messages to " + user + "..."); | ||||
| 		for (var msg : pendingMessages) { | ||||
| 			final var msgCommon = msg.toCommon(); | ||||
| 			if (msg.getStatus() == MessageStatus.SENT) { | ||||
| 				logger.info("Sending message " + msg.toCommon()); | ||||
| 				writeProxy.write(socketID, msg.toCommon()); | ||||
| 				msg.setReceivedDate(new Date()); | ||||
| 				msg.setStatus(MessageStatus.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); | ||||
| 				} | ||||
| 				writeProxy.write(socketID, msgCommon); | ||||
| 				msg.received(); | ||||
| 				if (connectionManager.isOnline(msg.getSender().getID())) | ||||
| 					writeProxy.write(connectionManager.getSocketID(msg.getSender().getID()), new MessageStatusChange(msgCommon)); | ||||
| 				PersistenceManager.getInstance().updateMessage(msg); | ||||
| 			} else { | ||||
| 				var evt = new MessageStatusChangeEvent(msg.toCommon()); | ||||
| 				logger.info("Sending messageStatusChangeEvent " + evt); | ||||
| 				writeProxy.write(socketID, evt); | ||||
| 			} | ||||
| 			} else writeProxy.write(socketID, new MessageStatusChange(msgCommon)); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	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; | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| package envoy.server.processors; | ||||
|  | ||||
| import java.io.IOException; | ||||
| import java.util.Date; | ||||
| import java.time.LocalDateTime; | ||||
| import java.util.logging.Logger; | ||||
|  | ||||
| import javax.persistence.EntityExistsException; | ||||
| @@ -50,7 +50,7 @@ public class MessageProcessor implements ObjectProcessor<Message> { | ||||
| 			// If recipient is online, send the message directly | ||||
| 			writeProxy.write(connectionManager.getSocketID(message.getRecipientID()), message); | ||||
| 			// Update the message status to RECEIVED | ||||
| 			message.setReceivedDate(new Date()); | ||||
| 			message.setReceivedDate(LocalDateTime.now()); | ||||
| 			message.nextStatus(); | ||||
| 		} catch (IOException e) { | ||||
| 			logger.warning("Recipient online. Failed to send message" + message.getID()); | ||||
|   | ||||
| @@ -3,7 +3,7 @@ package envoy.server.processors; | ||||
| import java.io.IOException; | ||||
|  | ||||
| import envoy.data.Message.MessageStatus; | ||||
| import envoy.event.MessageStatusChangeEvent; | ||||
| import envoy.event.MessageStatusChange; | ||||
| import envoy.exception.EnvoyException; | ||||
| import envoy.server.data.PersistenceManager; | ||||
| import envoy.server.net.ConnectionManager; | ||||
| @@ -17,26 +17,26 @@ import envoy.server.net.ObjectWriteProxy; | ||||
|  * @author Leon Hofmeister | ||||
|  * @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 ConnectionManager		connectionManager	= ConnectionManager.getInstance(); | ||||
|  | ||||
| 	@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 | ||||
| 		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()); | ||||
| 		msg.setStatus(input.get()); | ||||
| 		msg.setReadDate(input.getDate()); | ||||
| 		final var msg = persistenceManager.getMessageByID(statusChange.getID()); | ||||
| 		msg.setStatus(statusChange.get()); | ||||
| 		msg.setReadDate(statusChange.getDate()); | ||||
| 		persistenceManager.updateMessage(msg); | ||||
|  | ||||
| 		// Notifies the sender of the message about the status-update to READ | ||||
| 		final long senderID = msg.getSender().getID(); | ||||
| 		if (connectionManager.isOnline(senderID)) writeProxy.write(connectionManager.getSocketID(senderID), input); | ||||
| 		if (connectionManager.isOnline(senderID)) writeProxy.write(connectionManager.getSocketID(senderID), statusChange); | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public Class<MessageStatusChangeEvent> getInputClass() { return MessageStatusChangeEvent.class; } | ||||
| 	public Class<MessageStatusChange> getInputClass() { return MessageStatusChange.class; } | ||||
| } | ||||
|   | ||||
| @@ -2,7 +2,7 @@ package envoy.server.processors; | ||||
|  | ||||
| import java.io.IOException; | ||||
|  | ||||
| import envoy.event.NameChangeEvent; | ||||
| import envoy.event.NameChange; | ||||
| import envoy.server.data.Contact; | ||||
| import envoy.server.data.PersistenceManager; | ||||
| import envoy.server.data.User; | ||||
| @@ -17,20 +17,20 @@ import envoy.server.net.ObjectWriteProxy; | ||||
|  * @author Leon Hofmeister | ||||
|  * @since Envoy Server Standalone v0.1-beta | ||||
|  */ | ||||
| public class NameChangeProcessor implements ObjectProcessor<NameChangeEvent> { | ||||
| public class NameChangeProcessor implements ObjectProcessor<NameChange> { | ||||
|  | ||||
| 	@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(); | ||||
| 		ConnectionManager	connectionManager	= ConnectionManager.getInstance(); | ||||
| 		Contact				toUpdate			= persistenceManager.getContactByID(input.getID()); | ||||
| 		toUpdate.setName(input.get()); | ||||
| 		Contact				toUpdate			= persistenceManager.getContactByID(nameChange.getID()); | ||||
| 		toUpdate.setName(nameChange.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 -> { | ||||
| 		// Notify online contacts of the name change | ||||
| 		toUpdate.getContacts().stream().filter(User.class::isInstance).map(Contact::getID).filter(connectionManager::isOnline).forEach(userID -> { | ||||
| 			try { | ||||
| 				writeProxy.write(user.getID(), input); | ||||
| 				writeProxy.write(userID, nameChange); | ||||
| 			} catch (IOException e) { | ||||
| 				e.printStackTrace(); | ||||
| 			} | ||||
| @@ -38,5 +38,5 @@ public class NameChangeProcessor implements ObjectProcessor<NameChangeEvent> { | ||||
| 	} | ||||
|  | ||||
| 	@Override | ||||
| 	public Class<NameChangeEvent> getInputClass() { return NameChangeEvent.class; } | ||||
| 	public Class<NameChange> getInputClass() { return NameChange.class; } | ||||
| } | ||||
|   | ||||
| @@ -4,7 +4,7 @@ import java.io.IOException; | ||||
| import java.util.logging.Logger; | ||||
|  | ||||
| import envoy.data.User.UserStatus; | ||||
| import envoy.event.UserStatusChangeEvent; | ||||
| import envoy.event.UserStatusChange; | ||||
| import envoy.server.data.PersistenceManager; | ||||
| import envoy.server.data.User; | ||||
| import envoy.server.net.ConnectionManager; | ||||
| @@ -12,7 +12,7 @@ import envoy.server.net.ObjectWriteProxy; | ||||
| import envoy.util.EnvoyLog; | ||||
|  | ||||
| /** | ||||
|  * This processor handles incoming {@link UserStatusChangeEvent}.<br> | ||||
|  * This processor handles incoming {@link UserStatusChange}.<br> | ||||
|  * <br> | ||||
|  * Project: <strong>envoy-server-standalone</strong><br> | ||||
|  * File: <strong>UserStatusChangeProcessor.java</strong><br> | ||||
| @@ -21,7 +21,7 @@ import envoy.util.EnvoyLog; | ||||
|  * @author Leon Hofmeister | ||||
|  * @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 PersistenceManager	persistenceManager	= PersistenceManager.getInstance(); | ||||
| @@ -29,13 +29,13 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan | ||||
| 	private static final Logger logger = EnvoyLog.getLogger(UserStatusChangeProcessor.class); | ||||
|  | ||||
| 	@Override | ||||
| 	public Class<UserStatusChangeEvent> getInputClass() { return UserStatusChangeEvent.class; } | ||||
| 	public Class<UserStatusChange> getInputClass() { return UserStatusChange.class; } | ||||
|  | ||||
| 	@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 | ||||
| 		if (input.get().equals(persistenceManager.getUserByID(input.getID()).getStatus())) { | ||||
| 			logger.warning("Received an unnecessary UserStatusChangeEvent"); | ||||
| 			logger.warning("Received an unnecessary UserStatusChange"); | ||||
| 			return; | ||||
| 		} | ||||
| 		updateUserStatus(input); | ||||
| @@ -45,7 +45,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan | ||||
| 	 * Sets the {@link UserStatus} for a given user. Both offline contacts and | ||||
| 	 * 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 | ||||
| 	 */ | ||||
|  | ||||
| @@ -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 | ||||
| 	 */ | ||||
| 	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 | ||||
| @@ -72,7 +72,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan | ||||
| 	 * @since Envoy Server Standalone v0.1-alpha | ||||
| 	 */ | ||||
| 	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(); | ||||
| 		try { | ||||
| 			for (envoy.server.data.Contact contact : user.getContacts()) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user