Merge pull request #10 from informatik-ag-ngl/f/message_processing
Implemented the processing of messages sent by clients
This commit is contained in:
		
							
								
								
									
										22
									
								
								.classpath
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								.classpath
									
									
									
									
									
								
							@@ -6,11 +6,6 @@
 | 
				
			|||||||
			<attribute name="maven.pomderived" value="true"/>
 | 
								<attribute name="maven.pomderived" value="true"/>
 | 
				
			||||||
		</attributes>
 | 
							</attributes>
 | 
				
			||||||
	</classpathentry>
 | 
						</classpathentry>
 | 
				
			||||||
	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
 | 
					 | 
				
			||||||
		<attributes>
 | 
					 | 
				
			||||||
			<attribute name="maven.pomderived" value="true"/>
 | 
					 | 
				
			||||||
		</attributes>
 | 
					 | 
				
			||||||
	</classpathentry>
 | 
					 | 
				
			||||||
	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
 | 
						<classpathentry kind="src" output="target/test-classes" path="src/test/java">
 | 
				
			||||||
		<attributes>
 | 
							<attributes>
 | 
				
			||||||
			<attribute name="optional" value="true"/>
 | 
								<attribute name="optional" value="true"/>
 | 
				
			||||||
@@ -18,12 +13,6 @@
 | 
				
			|||||||
			<attribute name="test" value="true"/>
 | 
								<attribute name="test" value="true"/>
 | 
				
			||||||
		</attributes>
 | 
							</attributes>
 | 
				
			||||||
	</classpathentry>
 | 
						</classpathentry>
 | 
				
			||||||
	<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
 | 
					 | 
				
			||||||
		<attributes>
 | 
					 | 
				
			||||||
			<attribute name="maven.pomderived" value="true"/>
 | 
					 | 
				
			||||||
			<attribute name="test" value="true"/>
 | 
					 | 
				
			||||||
		</attributes>
 | 
					 | 
				
			||||||
	</classpathentry>
 | 
					 | 
				
			||||||
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
 | 
						<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
 | 
				
			||||||
		<attributes>
 | 
							<attributes>
 | 
				
			||||||
			<attribute name="maven.pomderived" value="true"/>
 | 
								<attribute name="maven.pomderived" value="true"/>
 | 
				
			||||||
@@ -35,5 +24,16 @@
 | 
				
			|||||||
		</attributes>
 | 
							</attributes>
 | 
				
			||||||
	</classpathentry>
 | 
						</classpathentry>
 | 
				
			||||||
	<classpathentry kind="src" path="/envoy-common"/>
 | 
						<classpathentry kind="src" path="/envoy-common"/>
 | 
				
			||||||
 | 
						<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
 | 
				
			||||||
 | 
							<attributes>
 | 
				
			||||||
 | 
								<attribute name="maven.pomderived" value="true"/>
 | 
				
			||||||
 | 
							</attributes>
 | 
				
			||||||
 | 
						</classpathentry>
 | 
				
			||||||
 | 
						<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
 | 
				
			||||||
 | 
							<attributes>
 | 
				
			||||||
 | 
								<attribute name="maven.pomderived" value="true"/>
 | 
				
			||||||
 | 
								<attribute name="test" value="true"/>
 | 
				
			||||||
 | 
							</attributes>
 | 
				
			||||||
 | 
						</classpathentry>
 | 
				
			||||||
	<classpathentry kind="output" path="target/classes"/>
 | 
						<classpathentry kind="output" path="target/classes"/>
 | 
				
			||||||
</classpath>
 | 
					</classpath>
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										11
									
								
								.project
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								.project
									
									
									
									
									
								
							@@ -5,11 +5,21 @@
 | 
				
			|||||||
	<projects>
 | 
						<projects>
 | 
				
			||||||
	</projects>
 | 
						</projects>
 | 
				
			||||||
	<buildSpec>
 | 
						<buildSpec>
 | 
				
			||||||
 | 
							<buildCommand>
 | 
				
			||||||
 | 
								<name>org.eclipse.wst.common.project.facet.core.builder</name>
 | 
				
			||||||
 | 
								<arguments>
 | 
				
			||||||
 | 
								</arguments>
 | 
				
			||||||
 | 
							</buildCommand>
 | 
				
			||||||
		<buildCommand>
 | 
							<buildCommand>
 | 
				
			||||||
			<name>org.eclipse.jdt.core.javabuilder</name>
 | 
								<name>org.eclipse.jdt.core.javabuilder</name>
 | 
				
			||||||
			<arguments>
 | 
								<arguments>
 | 
				
			||||||
			</arguments>
 | 
								</arguments>
 | 
				
			||||||
		</buildCommand>
 | 
							</buildCommand>
 | 
				
			||||||
 | 
							<buildCommand>
 | 
				
			||||||
 | 
								<name>org.eclipse.wst.validation.validationbuilder</name>
 | 
				
			||||||
 | 
								<arguments>
 | 
				
			||||||
 | 
								</arguments>
 | 
				
			||||||
 | 
							</buildCommand>
 | 
				
			||||||
		<buildCommand>
 | 
							<buildCommand>
 | 
				
			||||||
			<name>org.eclipse.m2e.core.maven2Builder</name>
 | 
								<name>org.eclipse.m2e.core.maven2Builder</name>
 | 
				
			||||||
			<arguments>
 | 
								<arguments>
 | 
				
			||||||
@@ -19,5 +29,6 @@
 | 
				
			|||||||
	<natures>
 | 
						<natures>
 | 
				
			||||||
		<nature>org.eclipse.jdt.core.javanature</nature>
 | 
							<nature>org.eclipse.jdt.core.javanature</nature>
 | 
				
			||||||
		<nature>org.eclipse.m2e.core.maven2Nature</nature>
 | 
							<nature>org.eclipse.m2e.core.maven2Nature</nature>
 | 
				
			||||||
 | 
							<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
 | 
				
			||||||
	</natures>
 | 
						</natures>
 | 
				
			||||||
</projectDescription>
 | 
					</projectDescription>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,14 @@
 | 
				
			|||||||
eclipse.preferences.version=1
 | 
					eclipse.preferences.version=1
 | 
				
			||||||
 | 
					org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
 | 
				
			||||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
 | 
					org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
 | 
				
			||||||
 | 
					org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
 | 
				
			||||||
org.eclipse.jdt.core.compiler.compliance=1.8
 | 
					org.eclipse.jdt.core.compiler.compliance=1.8
 | 
				
			||||||
 | 
					org.eclipse.jdt.core.compiler.debug.lineNumber=generate
 | 
				
			||||||
 | 
					org.eclipse.jdt.core.compiler.debug.localVariable=generate
 | 
				
			||||||
 | 
					org.eclipse.jdt.core.compiler.debug.sourceFile=generate
 | 
				
			||||||
 | 
					org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
 | 
				
			||||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
 | 
					org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
 | 
				
			||||||
 | 
					org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
 | 
				
			||||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
 | 
					org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
 | 
				
			||||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
 | 
					org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
 | 
				
			||||||
org.eclipse.jdt.core.compiler.release=disabled
 | 
					org.eclipse.jdt.core.compiler.release=disabled
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										3
									
								
								.settings/org.eclipse.jpt.core.prefs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								.settings/org.eclipse.jpt.core.prefs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					eclipse.preferences.version=1
 | 
				
			||||||
 | 
					org.eclipse.jpt.core.platform=hibernate2_1
 | 
				
			||||||
 | 
					org.eclipse.jpt.jpa.core.discoverAnnotatedClasses=true
 | 
				
			||||||
@@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					<root>
 | 
				
			||||||
 | 
					  <facet id="jpt.jpa">
 | 
				
			||||||
 | 
					    <node name="libprov">
 | 
				
			||||||
 | 
					      <attribute name="provider-id" value="jpa-no-op-library-provider"/>
 | 
				
			||||||
 | 
					    </node>
 | 
				
			||||||
 | 
					  </facet>
 | 
				
			||||||
 | 
					</root>
 | 
				
			||||||
							
								
								
									
										5
									
								
								.settings/org.eclipse.wst.common.project.facet.core.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								.settings/org.eclipse.wst.common.project.facet.core.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
				
			|||||||
 | 
					<?xml version="1.0" encoding="UTF-8"?>
 | 
				
			||||||
 | 
					<faceted-project>
 | 
				
			||||||
 | 
					  <installed facet="java" version="1.8"/>
 | 
				
			||||||
 | 
					  <installed facet="jpt.jpa" version="2.1"/>
 | 
				
			||||||
 | 
					</faceted-project>
 | 
				
			||||||
							
								
								
									
										3
									
								
								.settings/org.hibernate.eclipse.console.prefs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								.settings/org.hibernate.eclipse.console.prefs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					default.configuration=envoy-server-standalone
 | 
				
			||||||
 | 
					eclipse.preferences.version=1
 | 
				
			||||||
 | 
					hibernate3.enabled=true
 | 
				
			||||||
							
								
								
									
										20
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								pom.xml
									
									
									
									
									
								
							@@ -17,22 +17,34 @@
 | 
				
			|||||||
		<maven.compiler.target>1.8</maven.compiler.target>
 | 
							<maven.compiler.target>1.8</maven.compiler.target>
 | 
				
			||||||
	</properties>
 | 
						</properties>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						<repositories>
 | 
				
			||||||
 | 
							<repository>
 | 
				
			||||||
 | 
								<id>jitpack.io</id>
 | 
				
			||||||
 | 
								<url>https://jitpack.io</url>
 | 
				
			||||||
 | 
							</repository>
 | 
				
			||||||
 | 
						</repositories>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	<dependencies>
 | 
						<dependencies>
 | 
				
			||||||
		<dependency>
 | 
							<dependency>
 | 
				
			||||||
			<groupId>informatik-ag-ngl</groupId>
 | 
								<groupId>com.github.informatik-ag-ngl</groupId>
 | 
				
			||||||
			<artifactId>envoy-common</artifactId>
 | 
								<artifactId>envoy-common</artifactId>
 | 
				
			||||||
			<version>0.2-alpha</version>
 | 
								<version>develop-SNAPSHOT</version><!-- <version>0.2-alpha</version> -->
 | 
				
			||||||
		</dependency>
 | 
							</dependency>
 | 
				
			||||||
		<dependency>
 | 
							<dependency>
 | 
				
			||||||
			<groupId>informatik-ag-ngl</groupId>
 | 
								<groupId>com.github.informatik-ag-ngl</groupId>
 | 
				
			||||||
			<artifactId>java-nio-server</artifactId>
 | 
								<artifactId>java-nio-server</artifactId>
 | 
				
			||||||
			<version>0.0.1-SNAPSHOT</version>
 | 
								<version>master-SNAPSHOT</version>
 | 
				
			||||||
		</dependency>
 | 
							</dependency>
 | 
				
			||||||
		<dependency>
 | 
							<dependency>
 | 
				
			||||||
			<groupId>org.hibernate</groupId>
 | 
								<groupId>org.hibernate</groupId>
 | 
				
			||||||
			<artifactId>hibernate-core</artifactId>
 | 
								<artifactId>hibernate-core</artifactId>
 | 
				
			||||||
			<version>5.4.10.Final</version>
 | 
								<version>5.4.10.Final</version>
 | 
				
			||||||
		</dependency>
 | 
							</dependency>
 | 
				
			||||||
 | 
							<dependency>
 | 
				
			||||||
 | 
								<groupId>org.postgresql</groupId>
 | 
				
			||||||
 | 
								<artifactId>postgresql</artifactId>
 | 
				
			||||||
 | 
								<version>42.2.9</version>
 | 
				
			||||||
 | 
							</dependency>
 | 
				
			||||||
	</dependencies>
 | 
						</dependencies>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	<build>
 | 
						<build>
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										79
									
								
								src/main/java/envoy/server/ConnectionManager.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								src/main/java/envoy/server/ConnectionManager.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
				
			|||||||
 | 
					package envoy.server;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.HashMap;
 | 
				
			||||||
 | 
					import java.util.HashSet;
 | 
				
			||||||
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					import java.util.Set;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.jenkov.nioserver.ISocketIdListener;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Project: <strong>envoy-server-standalone</strong><br>
 | 
				
			||||||
 | 
					 * File: <strong>ConnectionManager.java</strong><br>
 | 
				
			||||||
 | 
					 * Created: <strong>03.01.2020</strong><br>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Kai S. K. Engelbart
 | 
				
			||||||
 | 
					 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class ConnectionManager implements ISocketIdListener {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Contains all socket IDs that have not yet performed a handshake / acquired
 | 
				
			||||||
 | 
						 * their corresponding user ID.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						private Set<Long> pendingSockets = new HashSet<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Contains all socket IDs that have acquired a user ID as keys to these IDs.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						private Map<Long, Long> sockets = new HashMap<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private static ConnectionManager connectionManager = new ConnectionManager();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private ConnectionManager() {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return a singleton instance of this object
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public static ConnectionManager getInstance() { return connectionManager; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Override
 | 
				
			||||||
 | 
						public void socketCancelled(long socketId) {
 | 
				
			||||||
 | 
							if (!pendingSockets.remove(socketId))
 | 
				
			||||||
 | 
								sockets.entrySet().stream().filter(e -> e.getValue() == socketId).forEach(e -> sockets.remove(e.getValue()));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Override
 | 
				
			||||||
 | 
						public void socketRegistered(long socketId) { pendingSockets.add(socketId); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Associates a socket ID with a user ID.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @param userId   the user ID
 | 
				
			||||||
 | 
						 * @param socketId the socket ID
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public void registerUser(long userId, long socketId) {
 | 
				
			||||||
 | 
							sockets.put(userId, socketId);
 | 
				
			||||||
 | 
							pendingSockets.remove(userId);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @param userId the ID of the user registered at the a socket
 | 
				
			||||||
 | 
						 * @return the ID of the socket
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public long getSocketId(long userId) { return sockets.get(userId); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @param userId the ID of the user to check for
 | 
				
			||||||
 | 
						 * @return {@code true} if the user is online
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public boolean isOnline(long userId) { return sockets.containsKey(userId); }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,29 +0,0 @@
 | 
				
			|||||||
package envoy.server;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import envoy.data.LoginCredentials;
 | 
					 | 
				
			||||||
import envoy.data.User;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * This {@link ObjectProcessor} handles {@link LoginCredentials}.<br>
 | 
					 | 
				
			||||||
 * <br>
 | 
					 | 
				
			||||||
 * Project: <strong>envoy-server-standalone</strong><br>
 | 
					 | 
				
			||||||
 * File: <strong>LoginCredentialProcessor.java</strong><br>
 | 
					 | 
				
			||||||
 * Created: <strong>30.12.2019</strong><br>
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * @author Kai S. K. Engelbart
 | 
					 | 
				
			||||||
 * @since Envoy Server Standalone v0.1-alpha
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
public class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials, User> {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// TODO: Acquire user IDs from database
 | 
					 | 
				
			||||||
	private static long currentUserId = 1;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	@Override
 | 
					 | 
				
			||||||
	public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	@Override
 | 
					 | 
				
			||||||
	public User process(LoginCredentials input) {
 | 
					 | 
				
			||||||
		System.out.println("Received login credentials " + input);
 | 
					 | 
				
			||||||
		return new User(currentUserId++, input.getName());
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,22 +0,0 @@
 | 
				
			|||||||
package envoy.server;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import envoy.data.Message;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * This {@link ObjectProcessor} handles incoming {@link Message}s.<br>
 | 
					 | 
				
			||||||
 * <br>
 | 
					 | 
				
			||||||
 * Project: <strong>envoy-server-standalone</strong><br>
 | 
					 | 
				
			||||||
 * File: <strong>MessageProcessor.java</strong><br>
 | 
					 | 
				
			||||||
 * Created: <strong>30.12.2019</strong><br>
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * @author Kai S. K. Engelbart
 | 
					 | 
				
			||||||
 * @since Envoy Server Standalone v0.1-alpha
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
public class MessageProcessor implements ObjectProcessor<Message, Void> {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	@Override
 | 
					 | 
				
			||||||
	public Class<Message> getInputClass() { return Message.class; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	@Override
 | 
					 | 
				
			||||||
	public Void process(Message input) { return null; }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,5 +1,9 @@
 | 
				
			|||||||
package envoy.server;
 | 
					package envoy.server;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import envoy.server.net.ObjectWriteProxy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * This interface defines methods for processing objects of a specific
 | 
					 * This interface defines methods for processing objects of a specific
 | 
				
			||||||
 * type incoming from a client.<br>
 | 
					 * type incoming from a client.<br>
 | 
				
			||||||
@@ -10,21 +14,22 @@ package envoy.server;
 | 
				
			|||||||
 *
 | 
					 *
 | 
				
			||||||
 * @author Kai S. K. Engelbart
 | 
					 * @author Kai S. K. Engelbart
 | 
				
			||||||
 * @param <T> type of the request object
 | 
					 * @param <T> type of the request object
 | 
				
			||||||
 * @param <U> type of the response object
 | 
					 | 
				
			||||||
 * @since Envoy Server Standalone v0.1-alpha
 | 
					 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public interface ObjectProcessor<T, U> {
 | 
					public interface ObjectProcessor<T> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @return the Class of the request object
 | 
						 * @return the class of the request object
 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	Class<T> getInputClass();
 | 
						Class<T> getInputClass();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @param input      the request object
 | 
						 * @param input      the request object
 | 
				
			||||||
	 * @return the response object
 | 
						 * @param socketId   the ID of the socket from which the object was received
 | 
				
			||||||
 | 
						 * @param writeProxy the object that allows writing to a client
 | 
				
			||||||
 | 
						 * @throws IOException if something went wrong during processing
 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	U process(T input);
 | 
						void process(T input, long socketId, ObjectWriteProxy writeProxy) throws IOException;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -6,6 +6,12 @@ import java.util.Set;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import com.jenkov.nioserver.Server;
 | 
					import com.jenkov.nioserver.Server;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import envoy.server.net.ObjectMessageProcessor;
 | 
				
			||||||
 | 
					import envoy.server.net.ObjectMessageReader;
 | 
				
			||||||
 | 
					import envoy.server.processors.EventProcessor;
 | 
				
			||||||
 | 
					import envoy.server.processors.LoginCredentialProcessor;
 | 
				
			||||||
 | 
					import envoy.server.processors.MessageProcessor;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Starts the server.<br>
 | 
					 * Starts the server.<br>
 | 
				
			||||||
 * <br>
 | 
					 * <br>
 | 
				
			||||||
@@ -26,10 +32,13 @@ public class Startup {
 | 
				
			|||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public static void main(String[] args) throws IOException {
 | 
						public static void main(String[] args) throws IOException {
 | 
				
			||||||
		Set<ObjectProcessor<?, ?>> processors = new HashSet<>();
 | 
							Set<ObjectProcessor<?>> processors = new HashSet<>();
 | 
				
			||||||
		processors.add(new LoginCredentialProcessor());
 | 
							processors.add(new LoginCredentialProcessor());
 | 
				
			||||||
		processors.add(new MessageProcessor());
 | 
							processors.add(new MessageProcessor());
 | 
				
			||||||
 | 
							processors.add(new EventProcessor());
 | 
				
			||||||
 | 
							// new PersistenceManager();
 | 
				
			||||||
		Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
 | 
							Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
 | 
				
			||||||
		server.start();
 | 
							server.start();
 | 
				
			||||||
 | 
							server.getSocketProcessor().registerSocketIdListener(ConnectionManager.getInstance());
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -4,6 +4,7 @@ import java.util.Date;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import javax.persistence.Entity;
 | 
					import javax.persistence.Entity;
 | 
				
			||||||
import javax.persistence.Id;
 | 
					import javax.persistence.Id;
 | 
				
			||||||
 | 
					import javax.persistence.ManyToOne;
 | 
				
			||||||
import javax.persistence.NamedQueries;
 | 
					import javax.persistence.NamedQueries;
 | 
				
			||||||
import javax.persistence.NamedQuery;
 | 
					import javax.persistence.NamedQuery;
 | 
				
			||||||
import javax.persistence.Table;
 | 
					import javax.persistence.Table;
 | 
				
			||||||
@@ -11,6 +12,7 @@ import javax.persistence.Temporal;
 | 
				
			|||||||
import javax.persistence.TemporalType;
 | 
					import javax.persistence.TemporalType;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import envoy.data.MessageBuilder;
 | 
					import envoy.data.MessageBuilder;
 | 
				
			||||||
 | 
					import envoy.server.database.PersistenceManager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * This class serves as a way to let Hibernate communicate with the server
 | 
					 * This class serves as a way to let Hibernate communicate with the server
 | 
				
			||||||
@@ -28,22 +30,34 @@ import envoy.data.MessageBuilder;
 | 
				
			|||||||
@Entity
 | 
					@Entity
 | 
				
			||||||
@Table(name = "messages")
 | 
					@Table(name = "messages")
 | 
				
			||||||
@NamedQueries(
 | 
					@NamedQueries(
 | 
				
			||||||
	{ @NamedQuery(query = "SELECT m FROM Message m WHERE m.recipient =:recipient AND m.state = 1", name = "getUnreadMessages"), @NamedQuery(
 | 
						{ @NamedQuery(
 | 
				
			||||||
		query = "SELECT m FROM Message m WHERE m.sender =:sender AND m.state = :state",
 | 
							query = "SELECT m FROM Message m WHERE m.recipient =:recipient AND m.status = envoy.data.Message$MessageStatus.SENT",
 | 
				
			||||||
 | 
							name = "getUnreadMessages"
 | 
				
			||||||
 | 
						), @NamedQuery(
 | 
				
			||||||
 | 
							query = "SELECT m FROM Message m WHERE m.sender =:sender AND m.status = :status",
 | 
				
			||||||
		name = "find read messages"// TODO do we need this namedQuery?
 | 
							name = "find read messages"// TODO do we need this namedQuery?
 | 
				
			||||||
	), @NamedQuery(query = "SELECT m FROM Message m WHERE m.id = :messageId", name = "get message") }//TODO do we need this namedQuery?
 | 
						), @NamedQuery(query = "SELECT m FROM Message m WHERE m.id = :messageId", name = "getMessageById") }
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
public class Message {
 | 
					public class Message {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Id
 | 
						@Id
 | 
				
			||||||
	private long id;
 | 
						private long id;
 | 
				
			||||||
	private User								sender, recipient;
 | 
					
 | 
				
			||||||
 | 
						@ManyToOne
 | 
				
			||||||
 | 
						private User sender;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@ManyToOne
 | 
				
			||||||
 | 
						private User recipient;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Temporal(TemporalType.TIMESTAMP)
 | 
						@Temporal(TemporalType.TIMESTAMP)
 | 
				
			||||||
	private Date creationDate;
 | 
						private Date creationDate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Temporal(TemporalType.TIMESTAMP)
 | 
						@Temporal(TemporalType.TIMESTAMP)
 | 
				
			||||||
	private Date receivedDate;
 | 
						private Date receivedDate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Temporal(TemporalType.TIMESTAMP)
 | 
						@Temporal(TemporalType.TIMESTAMP)
 | 
				
			||||||
	private Date readDate;
 | 
						private Date readDate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private envoy.data.Message.MessageStatus	status;
 | 
						private envoy.data.Message.MessageStatus	status;
 | 
				
			||||||
	private String								text;
 | 
						private String								text;
 | 
				
			||||||
	private byte[]								attachment;
 | 
						private byte[]								attachment;
 | 
				
			||||||
@@ -62,9 +76,14 @@ public class Message {
 | 
				
			|||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public Message(envoy.data.Message message) {
 | 
						public Message(envoy.data.Message message) {
 | 
				
			||||||
 | 
							PersistenceManager persMan = PersistenceManager.getPersistenceManager();
 | 
				
			||||||
		id				= message.getId();
 | 
							id				= message.getId();
 | 
				
			||||||
		status			= message.getStatus();
 | 
							status			= message.getStatus();
 | 
				
			||||||
		text			= message.getText();
 | 
							text			= message.getText();
 | 
				
			||||||
 | 
							creationDate	= message.getCreationDate();
 | 
				
			||||||
 | 
							sender			= persMan.getUserById(message.getSenderId());
 | 
				
			||||||
 | 
							recipient		= persMan.getUserById(message.getRecipientId());
 | 
				
			||||||
 | 
							// attachment = message.getAttachment().toByteArray();DOES NOT WORK YET
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,7 @@ package envoy.server.data;
 | 
				
			|||||||
import java.util.Date;
 | 
					import java.util.Date;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import javax.persistence.ElementCollection;
 | 
				
			||||||
import javax.persistence.Entity;
 | 
					import javax.persistence.Entity;
 | 
				
			||||||
import javax.persistence.GeneratedValue;
 | 
					import javax.persistence.GeneratedValue;
 | 
				
			||||||
import javax.persistence.GenerationType;
 | 
					import javax.persistence.GenerationType;
 | 
				
			||||||
@@ -27,7 +28,7 @@ import javax.persistence.TemporalType;
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
@Entity
 | 
					@Entity
 | 
				
			||||||
@Table(name = "users")
 | 
					@Table(name = "users")
 | 
				
			||||||
@NamedQuery(query = "SELECT u FROM DBUser u WHERE u.id = :id", name = "getUserById")
 | 
					@NamedQuery(query = "SELECT u FROM User u WHERE u.id = :id", name = "getUserById")
 | 
				
			||||||
public class User {
 | 
					public class User {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Id
 | 
						@Id
 | 
				
			||||||
@@ -35,11 +36,40 @@ public class User {
 | 
				
			|||||||
	private long	id;
 | 
						private long	id;
 | 
				
			||||||
	private String	name;
 | 
						private String	name;
 | 
				
			||||||
	private byte[]	passwordHash;
 | 
						private byte[]	passwordHash;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Temporal(TemporalType.TIMESTAMP)
 | 
						@Temporal(TemporalType.TIMESTAMP)
 | 
				
			||||||
	private Date						lastSeen;
 | 
						private Date						lastSeen;
 | 
				
			||||||
	private envoy.data.User.UserStatus	status;
 | 
						private envoy.data.User.UserStatus	status;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@ElementCollection
 | 
				
			||||||
	private List<User> contacts;
 | 
						private List<User> contacts;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Creates an instance of @link{User}.
 | 
				
			||||||
 | 
						 * Solely used for JPA/ Hibernate
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public User() {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Creates an instance of @link{User}.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @param user the {@link envoy.data.User} to convert
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public User(envoy.data.User user) {
 | 
				
			||||||
 | 
							id		= user.getId();
 | 
				
			||||||
 | 
							name	= user.getName();
 | 
				
			||||||
 | 
							status	= user.getStatus();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return a database {@link User} converted into an {@link envoy.data.User}
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public envoy.data.User toCommonUser() { return new envoy.data.User(this.id, this.name); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @return the id of a {link envoy.data.User}
 | 
						 * @return the id of a {link envoy.data.User}
 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,21 @@ import envoy.server.data.User;
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
public class PersistenceManager {
 | 
					public class PersistenceManager {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private static final PersistenceManager persistenceManager = new PersistenceManager();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Creates the singleton instance of the @link{PersistenceManager}.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						private PersistenceManager() {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return the {@link PersistenceManager} singleton
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public static PersistenceManager getPersistenceManager() { return persistenceManager; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private EntityManager entityManager = Persistence.createEntityManagerFactory("envoy").createEntityManager();
 | 
						private EntityManager entityManager = Persistence.createEntityManagerFactory("envoy").createEntityManager();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@@ -55,7 +70,7 @@ public class PersistenceManager {
 | 
				
			|||||||
	public void updateMessage(Message message) { entityManager.unwrap(Session.class).merge(message); }
 | 
						public void updateMessage(Message message) { entityManager.unwrap(Session.class).merge(message); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Searches for a 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
 | 
				
			||||||
@@ -63,6 +78,17 @@ public class PersistenceManager {
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	public User getUserById(long id) { return (User) entityManager.createNamedQuery("getUserById").setParameter("id", id).getSingleResult(); }
 | 
						public User getUserById(long id) { return (User) entityManager.createNamedQuery("getUserById").setParameter("id", id).getSingleResult(); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Searches for a {@link Message} with a specific id.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @param id - the id to search for
 | 
				
			||||||
 | 
						 * @return the message with the specified id
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public Message getMessageById(long id) {
 | 
				
			||||||
 | 
							return (Message) entityManager.createNamedQuery("getMessageById").setParameter("id", id).getSingleResult();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Returns all messages received while being offline.
 | 
						 * Returns all messages received while being offline.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,16 +1,16 @@
 | 
				
			|||||||
package envoy.server;
 | 
					package envoy.server.net;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.ByteArrayInputStream;
 | 
					import java.io.ByteArrayInputStream;
 | 
				
			||||||
import java.io.ByteArrayOutputStream;
 | 
					 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.io.ObjectInputStream;
 | 
					import java.io.ObjectInputStream;
 | 
				
			||||||
import java.io.ObjectOutputStream;
 | 
					 | 
				
			||||||
import java.util.Set;
 | 
					import java.util.Set;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.jenkov.nioserver.IMessageProcessor;
 | 
					import com.jenkov.nioserver.IMessageProcessor;
 | 
				
			||||||
import com.jenkov.nioserver.Message;
 | 
					import com.jenkov.nioserver.Message;
 | 
				
			||||||
import com.jenkov.nioserver.WriteProxy;
 | 
					import com.jenkov.nioserver.WriteProxy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import envoy.server.ObjectProcessor;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Handles incoming objects.<br>
 | 
					 * Handles incoming objects.<br>
 | 
				
			||||||
 * <br>
 | 
					 * <br>
 | 
				
			||||||
@@ -23,7 +23,7 @@ import com.jenkov.nioserver.WriteProxy;
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
public class ObjectMessageProcessor implements IMessageProcessor {
 | 
					public class ObjectMessageProcessor implements IMessageProcessor {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private final Set<ObjectProcessor<?, ?>> processors;
 | 
						private final Set<ObjectProcessor<?>> processors;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * The constructor to set the {@link ObjectProcessor}s.
 | 
						 * The constructor to set the {@link ObjectProcessor}s.
 | 
				
			||||||
@@ -31,7 +31,7 @@ public class ObjectMessageProcessor implements IMessageProcessor {
 | 
				
			|||||||
	 * @param processors the {@link ObjectProcessor} to set
 | 
						 * @param processors the {@link ObjectProcessor} to set
 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public ObjectMessageProcessor(Set<ObjectProcessor<?, ?>> processors) { this.processors = processors; }
 | 
						public ObjectMessageProcessor(Set<ObjectProcessor<?>> processors) { this.processors = processors; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@SuppressWarnings("unchecked")
 | 
						@SuppressWarnings("unchecked")
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
@@ -42,23 +42,11 @@ public class ObjectMessageProcessor implements IMessageProcessor {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			// Process object
 | 
								// Process object
 | 
				
			||||||
			processors.stream().filter(p -> p.getInputClass().isInstance(obj)).forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> {
 | 
								processors.stream().filter(p -> p.getInputClass().isInstance(obj)).forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> {
 | 
				
			||||||
				Object responseObj = p.process(p.getInputClass().cast(obj));
 | 
									try {
 | 
				
			||||||
				if (responseObj != null) {
 | 
										p.process(p.getInputClass().cast(obj), message.socketId, new ObjectWriteProxy(writeProxy));
 | 
				
			||||||
					// Create message targeted at the client
 | 
					 | 
				
			||||||
					Message response = writeProxy.getMessage();
 | 
					 | 
				
			||||||
					response.socketId = message.socketId;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
					// Serialize object to byte array
 | 
					 | 
				
			||||||
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
 | 
					 | 
				
			||||||
					try (ObjectOutputStream oout = new ObjectOutputStream(baos)) {
 | 
					 | 
				
			||||||
						oout.writeObject(responseObj);
 | 
					 | 
				
			||||||
				} catch (IOException e) {
 | 
									} catch (IOException e) {
 | 
				
			||||||
					e.printStackTrace();
 | 
										e.printStackTrace();
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
					byte[] objBytes = baos.toByteArray();
 | 
					 | 
				
			||||||
					response.writeToMessage(objBytes);
 | 
					 | 
				
			||||||
					writeProxy.enqueue(response);
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
		} catch (IOException | ClassNotFoundException e) {
 | 
							} catch (IOException | ClassNotFoundException e) {
 | 
				
			||||||
			e.printStackTrace();
 | 
								e.printStackTrace();
 | 
				
			||||||
@@ -1,14 +1,13 @@
 | 
				
			|||||||
package envoy.server;
 | 
					package envoy.server.net;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.nio.ByteBuffer;
 | 
					import java.nio.ByteBuffer;
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.jenkov.nioserver.IMessageReader;
 | 
					import com.jenkov.nioserver.*;
 | 
				
			||||||
import com.jenkov.nioserver.Message;
 | 
					
 | 
				
			||||||
import com.jenkov.nioserver.MessageBuffer;
 | 
					import envoy.util.SerializationUtils;
 | 
				
			||||||
import com.jenkov.nioserver.Socket;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * This {@link IMessageReader} decodes serialized Java objects.<br>
 | 
					 * This {@link IMessageReader} decodes serialized Java objects.<br>
 | 
				
			||||||
@@ -26,11 +25,6 @@ public class ObjectMessageReader implements IMessageReader {
 | 
				
			|||||||
	private Message			nextMessage;
 | 
						private Message			nextMessage;
 | 
				
			||||||
	private MessageBuffer	messageBuffer;
 | 
						private MessageBuffer	messageBuffer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private int fromByteArray(byte[] bytes, int offset) {
 | 
					 | 
				
			||||||
		return ((bytes[offset] & 0xFF) << 24) | ((bytes[offset + 1] & 0xFF) << 16) | ((bytes[offset + 2] & 0xFF) << 8)
 | 
					 | 
				
			||||||
				| ((bytes[offset + 3] & 0xFF) << 0);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	public List<Message> getMessages() { return completeMessages; }
 | 
						public List<Message> getMessages() { return completeMessages; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -54,7 +48,7 @@ public class ObjectMessageReader implements IMessageReader {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		// Get message length
 | 
							// Get message length
 | 
				
			||||||
		if (nextMessage.length - nextMessage.offset < 4) return;
 | 
							if (nextMessage.length - nextMessage.offset < 4) return;
 | 
				
			||||||
		int length = fromByteArray(nextMessage.sharedArray, nextMessage.offset) + 4;
 | 
							int length = SerializationUtils.bytesToInt(nextMessage.sharedArray, nextMessage.offset) + 4;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (nextMessage.length - nextMessage.offset >= length) {
 | 
							if (nextMessage.length - nextMessage.offset >= length) {
 | 
				
			||||||
			Message message = messageBuffer.getMessage();
 | 
								Message message = messageBuffer.getMessage();
 | 
				
			||||||
							
								
								
									
										53
									
								
								src/main/java/envoy/server/net/ObjectWriteProxy.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								src/main/java/envoy/server/net/ObjectWriteProxy.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,53 @@
 | 
				
			|||||||
 | 
					package envoy.server.net;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.jenkov.nioserver.Message;
 | 
				
			||||||
 | 
					import com.jenkov.nioserver.WriteProxy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import envoy.util.SerializationUtils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * This class defines methods to send an object to a client.<br>
 | 
				
			||||||
 | 
					 * <br>
 | 
				
			||||||
 | 
					 * Project: <strong>envoy-server-standalone</strong><br>
 | 
				
			||||||
 | 
					 * File: <strong>ObjectWriteProxy.java</strong><br>
 | 
				
			||||||
 | 
					 * Created: <strong>04.01.2020</strong><br>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Kai S. K. Engelbart
 | 
				
			||||||
 | 
					 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class ObjectWriteProxy {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private final WriteProxy writeProxy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Creates an instance of @link{ObjectWriteProxy}.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @param writeProxy the {@link WriteProxy} to write objects to another client
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public ObjectWriteProxy(WriteProxy writeProxy) { this.writeProxy = writeProxy; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @param recipientSocketId the socket id of the recipient
 | 
				
			||||||
 | 
						 * @param obj               the object to return to the client
 | 
				
			||||||
 | 
						 * @throws IOException if the serialization of the object failed
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public void write(long recipientSocketId, Object obj) throws IOException {
 | 
				
			||||||
 | 
							// Create message targeted at the client
 | 
				
			||||||
 | 
							Message response = writeProxy.getMessage();
 | 
				
			||||||
 | 
							response.socketId = recipientSocketId;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Serialize object to byte array
 | 
				
			||||||
 | 
							byte[] objBytes = SerializationUtils.writeToByteArray(obj);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Acquire object length in bytes
 | 
				
			||||||
 | 
							byte[] objLen = SerializationUtils.intToBytes(objBytes.length);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							response.writeToMessage(objLen);
 | 
				
			||||||
 | 
							response.writeToMessage(objBytes);
 | 
				
			||||||
 | 
							writeProxy.enqueue(response);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										77
									
								
								src/main/java/envoy/server/processors/EventProcessor.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								src/main/java/envoy/server/processors/EventProcessor.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,77 @@
 | 
				
			|||||||
 | 
					package envoy.server.processors;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import envoy.data.Message;
 | 
				
			||||||
 | 
					import envoy.data.Message.MessageStatus;
 | 
				
			||||||
 | 
					import envoy.event.Event;
 | 
				
			||||||
 | 
					import envoy.event.MessageStatusChangeEvent;
 | 
				
			||||||
 | 
					import envoy.exception.EnvoyException;
 | 
				
			||||||
 | 
					import envoy.server.ConnectionManager;
 | 
				
			||||||
 | 
					import envoy.server.ObjectProcessor;
 | 
				
			||||||
 | 
					import envoy.server.database.PersistenceManager;
 | 
				
			||||||
 | 
					import envoy.server.net.ObjectWriteProxy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Project: <strong>envoy-server-standalone</strong><br>
 | 
				
			||||||
 | 
					 * File: <strong>EventProcessor.java</strong><br>
 | 
				
			||||||
 | 
					 * Created: <strong>10 Jan 2020</strong><br>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Leon Hofmeister
 | 
				
			||||||
 | 
					 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class EventProcessor implements ObjectProcessor<Event<?>> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private Event<?> event;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Creates an instance of @link{EventProcessor}.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public EventProcessor() {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@SuppressWarnings("unchecked")
 | 
				
			||||||
 | 
						@Override
 | 
				
			||||||
 | 
						public Class<Event<?>> getInputClass() { return (Class<Event<?>>) event.getClass(); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Override
 | 
				
			||||||
 | 
						public void process(Event<?> input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
 | 
				
			||||||
 | 
							event = input;
 | 
				
			||||||
 | 
							if (event instanceof MessageStatusChangeEvent) try {
 | 
				
			||||||
 | 
								applyMessageStatusChange((MessageStatusChangeEvent) event, writeProxy);
 | 
				
			||||||
 | 
							} catch (EnvoyException e) {
 | 
				
			||||||
 | 
								e.printStackTrace();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Redirects messageStatus changes to the database and to the recipient of the
 | 
				
			||||||
 | 
						 * {@link Message}.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @param event the {@link MessageStatusChangeEvent} to adjust
 | 
				
			||||||
 | 
						 * @throws EnvoyException if the {@link Message} has an invalid state
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						private void applyMessageStatusChange(MessageStatusChangeEvent event, ObjectWriteProxy writeProxy) throws EnvoyException {
 | 
				
			||||||
 | 
							if (!(event.get() == MessageStatus.READ))// check that no invalid MessageStatuses are sent
 | 
				
			||||||
 | 
								throw new EnvoyException("Message" + event.getId() + "has an invalid status");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ConnectionManager			conMan	= ConnectionManager.getInstance();
 | 
				
			||||||
 | 
							PersistenceManager			perMan	= PersistenceManager.getPersistenceManager();
 | 
				
			||||||
 | 
							envoy.server.data.Message	msg		= perMan.getMessageById(event.getId());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							msg.setStatus(event.get());
 | 
				
			||||||
 | 
							msg.setReadDate(event.getDate());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (conMan.isOnline(msg.getRecipient().getId())) try {
 | 
				
			||||||
 | 
								writeProxy.write(conMan.getSocketId(msg.getRecipient().getId()), event);
 | 
				
			||||||
 | 
							} catch (IOException e) {
 | 
				
			||||||
 | 
								System.err.println("Recipient online. Failed to send MessageStatusChangedEvent at message" + event.getId());
 | 
				
			||||||
 | 
								e.printStackTrace();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							perMan.updateMessage(msg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,61 @@
 | 
				
			|||||||
 | 
					package envoy.server.processors;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import envoy.data.Contacts;
 | 
				
			||||||
 | 
					import envoy.data.LoginCredentials;
 | 
				
			||||||
 | 
					import envoy.data.Message.MessageStatus;
 | 
				
			||||||
 | 
					import envoy.data.User;
 | 
				
			||||||
 | 
					import envoy.server.ConnectionManager;
 | 
				
			||||||
 | 
					import envoy.server.ObjectProcessor;
 | 
				
			||||||
 | 
					import envoy.server.data.Message;
 | 
				
			||||||
 | 
					import envoy.server.database.PersistenceManager;
 | 
				
			||||||
 | 
					import envoy.server.net.ObjectWriteProxy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * This {@link ObjectProcessor} handles {@link LoginCredentials}.<br>
 | 
				
			||||||
 | 
					 * <br>
 | 
				
			||||||
 | 
					 * Project: <strong>envoy-server-standalone</strong><br>
 | 
				
			||||||
 | 
					 * File: <strong>LoginCredentialProcessor.java</strong><br>
 | 
				
			||||||
 | 
					 * Created: <strong>30.12.2019</strong><br>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Kai S. K. Engelbart
 | 
				
			||||||
 | 
					 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// TODO: Acquire user IDs from database
 | 
				
			||||||
 | 
						private static long currentUserId = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Override
 | 
				
			||||||
 | 
						public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Override
 | 
				
			||||||
 | 
						public void process(LoginCredentials input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
 | 
				
			||||||
 | 
							System.out.println(String.format("Received login credentials %s from socket ID %d", input, socketId));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Create user
 | 
				
			||||||
 | 
							User user = new User(currentUserId++, input.getName());
 | 
				
			||||||
 | 
							ConnectionManager.getInstance().registerUser(socketId, user.getId());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Create contacts
 | 
				
			||||||
 | 
							Contacts contacts = new Contacts(user.getId(), new ArrayList<>());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Complete handshake
 | 
				
			||||||
 | 
							System.out.println("Sending user...");
 | 
				
			||||||
 | 
							writeProxy.write(socketId, user);
 | 
				
			||||||
 | 
							System.out.println("Sending contacts...");
 | 
				
			||||||
 | 
							writeProxy.write(socketId, contacts);
 | 
				
			||||||
 | 
							System.out.println("Sending unread messages and updating them in the database...");
 | 
				
			||||||
 | 
							List<Message> pendingMessages = PersistenceManager.getPersistenceManager().getUnreadMessages(new envoy.server.data.User(user));
 | 
				
			||||||
 | 
							pendingMessages.forEach((msg) -> {
 | 
				
			||||||
 | 
								msg.setReceivedDate(new Date());
 | 
				
			||||||
 | 
								msg.setStatus(MessageStatus.RECEIVED);
 | 
				
			||||||
 | 
								PersistenceManager.getPersistenceManager().updateMessage(msg);
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							writeProxy.write(socketId, pendingMessages);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										47
									
								
								src/main/java/envoy/server/processors/MessageProcessor.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/main/java/envoy/server/processors/MessageProcessor.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
				
			|||||||
 | 
					package envoy.server.processors;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import envoy.data.Message;
 | 
				
			||||||
 | 
					import envoy.event.MessageStatusChangeEvent;
 | 
				
			||||||
 | 
					import envoy.server.ConnectionManager;
 | 
				
			||||||
 | 
					import envoy.server.ObjectProcessor;
 | 
				
			||||||
 | 
					import envoy.server.database.PersistenceManager;
 | 
				
			||||||
 | 
					import envoy.server.net.ObjectWriteProxy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * This {@link ObjectProcessor} handles incoming {@link Message}s.<br>
 | 
				
			||||||
 | 
					 * <br>
 | 
				
			||||||
 | 
					 * Project: <strong>envoy-server-standalone</strong><br>
 | 
				
			||||||
 | 
					 * File: <strong>MessageProcessor.java</strong><br>
 | 
				
			||||||
 | 
					 * Created: <strong>30.12.2019</strong><br>
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author Kai S. K. Engelbart
 | 
				
			||||||
 | 
					 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class MessageProcessor implements ObjectProcessor<Message> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Override
 | 
				
			||||||
 | 
						public Class<Message> getInputClass() { return Message.class; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Override
 | 
				
			||||||
 | 
						public void process(Message message, long socketId, ObjectWriteProxy writeProxy) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ConnectionManager connectionManager = ConnectionManager.getInstance();
 | 
				
			||||||
 | 
							message.nextStatus();
 | 
				
			||||||
 | 
							if (connectionManager.isOnline(message.getRecipientId())) try {// if recipient is online, he receives the message directly
 | 
				
			||||||
 | 
								writeProxy.write(connectionManager.getSocketId(message.getRecipientId()), message);
 | 
				
			||||||
 | 
							} catch (IOException e) {
 | 
				
			||||||
 | 
								System.err.println("Recipient online. Failed to send message" + message.getId());
 | 
				
			||||||
 | 
								e.printStackTrace();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							try {// sender receives confirmation that the server received the message
 | 
				
			||||||
 | 
								writeProxy.write(connectionManager.getSocketId(message.getSenderId()),
 | 
				
			||||||
 | 
										new MessageStatusChangeEvent(message.getId(), message.getStatus(), new Date()));
 | 
				
			||||||
 | 
							} catch (IOException e) {
 | 
				
			||||||
 | 
								e.printStackTrace();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							PersistenceManager.getPersistenceManager().addMessage(new envoy.server.data.Message(message));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										29
									
								
								src/main/resources/META-INF/persistence.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/main/resources/META-INF/persistence.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
 | 
				
			||||||
 | 
						xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 | 
				
			||||||
 | 
						xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
 | 
				
			||||||
 | 
					             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
 | 
				
			||||||
 | 
						version="2.1">
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						<persistence-unit name="envoy"
 | 
				
			||||||
 | 
							transaction-type="RESOURCE_LOCAL">
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							<properties>
 | 
				
			||||||
 | 
								<property name="javax.persistence.jdbc.driver"
 | 
				
			||||||
 | 
									value="org.postgresql.Driver" /> <!-- DB Driver -->
 | 
				
			||||||
 | 
								<property name="javax.persistence.jdbc.url"
 | 
				
			||||||
 | 
									value="jdbc:postgresql://localhost/envoy" /> <!-- BD Mane -->
 | 
				
			||||||
 | 
								<property name="javax.persistence.jdbc.user" value="envoy" /> <!-- DB User -->
 | 
				
			||||||
 | 
								<property name="javax.persistence.jdbc.password"
 | 
				
			||||||
 | 
									value="envoy" /> <!-- DB Password -->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								<property name="hibernate.dialect"
 | 
				
			||||||
 | 
									value="org.hibernate.dialect.PostgreSQL95Dialect" /> <!-- DB Dialect -->
 | 
				
			||||||
 | 
								<property name="hibernate.hbm2ddl.auto" value="update" /> <!-- create / create-drop / update -->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								<property name="hibernate.show_sql" value="true" /> <!-- Show SQL in console -->
 | 
				
			||||||
 | 
								<property name="hibernate.format_sql" value="true" /> <!-- Show SQL formatted -->
 | 
				
			||||||
 | 
							</properties>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						</persistence-unit>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					</persistence>
 | 
				
			||||||
		Reference in New Issue
	
	Block a user