From 2c9223236fa2e25c2e7bf04893361a3c14ec49b2 Mon Sep 17 00:00:00 2001 From: kske Date: Mon, 30 Dec 2019 15:53:40 +0200 Subject: [PATCH] Added ObjectProcessor interface with some implementations --- .../server/LoginCredentialProcessor.java | 27 +++++ .../java/envoy/server/MessageProcessor.java | 20 ++++ .../envoy/server/ObjectMessageProcessor.java | 100 +++++------------- .../java/envoy/server/ObjectProcessor.java | 17 +++ src/main/java/envoy/server/Startup.java | 7 +- 5 files changed, 99 insertions(+), 72 deletions(-) create mode 100644 src/main/java/envoy/server/LoginCredentialProcessor.java create mode 100644 src/main/java/envoy/server/MessageProcessor.java create mode 100644 src/main/java/envoy/server/ObjectProcessor.java diff --git a/src/main/java/envoy/server/LoginCredentialProcessor.java b/src/main/java/envoy/server/LoginCredentialProcessor.java new file mode 100644 index 0000000..37f36e7 --- /dev/null +++ b/src/main/java/envoy/server/LoginCredentialProcessor.java @@ -0,0 +1,27 @@ +package envoy.server; + +import envoy.data.LoginCredentials; +import envoy.data.User; + +/** + * Project: envoy-server-standalone
+ * File: LoginCredentialProcessor.java
+ * Created: 30.12.2019
+ * + * @author Kai S. K. Engelbart + * @since + */ +public class LoginCredentialProcessor implements ObjectProcessor { + + // TODO: Acquire user IDs from database + private static long currentUserId = 1; + + @Override + public User process(LoginCredentials input) { + System.out.println("Received login credentials " + input); + return new User(currentUserId++, input.getName()); + } + + @Override + public Class getInputClass() { return LoginCredentials.class; } +} diff --git a/src/main/java/envoy/server/MessageProcessor.java b/src/main/java/envoy/server/MessageProcessor.java new file mode 100644 index 0000000..85313ca --- /dev/null +++ b/src/main/java/envoy/server/MessageProcessor.java @@ -0,0 +1,20 @@ +package envoy.server; + +import envoy.data.Message; + +/** + * Project: envoy-server-standalone
+ * File: MessageProcessor.java
+ * Created: 30.12.2019
+ * + * @author Kai S. K. Engelbart + * @since + */ +public class MessageProcessor implements ObjectProcessor { + + @Override + public Void process(Message input) { return null; } + + @Override + public Class getInputClass() { return Message.class; } +} diff --git a/src/main/java/envoy/server/ObjectMessageProcessor.java b/src/main/java/envoy/server/ObjectMessageProcessor.java index 7c9592b..8bf8ad6 100644 --- a/src/main/java/envoy/server/ObjectMessageProcessor.java +++ b/src/main/java/envoy/server/ObjectMessageProcessor.java @@ -5,15 +5,12 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.util.Set; import com.jenkov.nioserver.IMessageProcessor; import com.jenkov.nioserver.Message; import com.jenkov.nioserver.WriteProxy; -import envoy.data.LoginCredentials; -import envoy.data.User; -import envoy.event.Event; - /** * Project: envoy-server-standalone
* File: ObjectMessageProcessor.java
@@ -24,80 +21,41 @@ import envoy.event.Event; */ public class ObjectMessageProcessor implements IMessageProcessor { + private final Set> processors; + + public ObjectMessageProcessor(Set> processors) { this.processors = processors; } + + @SuppressWarnings("unchecked") @Override public void process(Message message, WriteProxy writeProxy) { try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) { Object obj = in.readObject(); - System.out.println("Read object: " + obj.toString()); - handleObject(message, writeProxy, obj); + + // Process object + processors.stream() + .filter(p -> p.getInputClass().isInstance(obj)) + .forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> { + Object responseObj = p.process(p.getInputClass().cast(obj)); + if (responseObj != null) { + // 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) { + e.printStackTrace(); + } + byte[] objBytes = baos.toByteArray(); + response.writeToMessage(objBytes); + writeProxy.enqueue(response); + } + }); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } - - /** - * This method splits incoming objects into the different objects that are - * relevant to the server and guides them to their predestined spot. - * - * @param request the {@link Message} in which the objects are saved - * @param writeProxy the writeProxy to define the resulting socket for - * @param obj the object that has been read out of the {@link Message} - * @throws IllegalArgumentException if the object given is neither an - * {@link envoy.data.Message}, an {@link Event} - * nor a {@link LoginCredentials} - * @since Envoy Server Standalone v0.1-alpha - */ - private void handleObject(Message request, WriteProxy writeProxy, Object obj) throws IllegalArgumentException { - - long currentUserID = 0; // TODO temporary. Only for testing purposes - boolean responseToSameSocket = false, immediateResponse = true; - Object usage; - - // determining the type of the incoming object - if (obj instanceof envoy.data.Message) {// if object is Message - envoy.data.Message cast = (envoy.data.Message) obj; - usage = cast.getClass(); - immediateResponse = isRecipientAvailable(-1); // TODO replace with wanted clientID - } else if (obj instanceof Event) {// if object is Event - usage = (Event) obj; - immediateResponse = isRecipientAvailable(-1); // TODO replace with wanted clientID - } else if (obj instanceof LoginCredentials) {// if object is LoginCredential - responseToSameSocket = true; - LoginCredentials cast = (LoginCredentials) obj; - usage = new User(currentUserID++, cast.getName()); - } else throw new IllegalArgumentException(); - - // handling of incoming object - if (immediateResponse) { - Message response = writeProxy.getMessage(); - if (responseToSameSocket) { - response.socketId = request.socketId; - } else { - response.socketId = -0;// TODO temporary.Needs to be replaced - return; - } - // Serialize object to byte array - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (ObjectOutputStream oout = new ObjectOutputStream(baos)) { - oout.writeObject(usage); - } catch (IOException e) { - e.printStackTrace(); - } - byte[] objBytes = baos.toByteArray(); - response.writeToMessage(objBytes); - writeProxy.enqueue(response); - } - } - - /** - * This method determines if the recipient is online - * - * @param otherClientID the ID of the recipient - * @return true, if the recipient is online - * @since Envoy Server Standalone v0.1-alpha - */ - private boolean isRecipientAvailable(long otherClientID) { - return false;// TODO needs to be adapted to return true if the wanted client is online - } } \ No newline at end of file diff --git a/src/main/java/envoy/server/ObjectProcessor.java b/src/main/java/envoy/server/ObjectProcessor.java new file mode 100644 index 0000000..a056d64 --- /dev/null +++ b/src/main/java/envoy/server/ObjectProcessor.java @@ -0,0 +1,17 @@ +package envoy.server; + + +/** + * Project: envoy-server-standalone
+ * File: ObjectProcessor.java
+ * Created: 30.12.2019
+ * + * @author Kai S. K. Engelbart + * @since Envoy Server Standalone v0.1-alpha + */ +public interface ObjectProcessor { + + U process(T input); + + Class getInputClass(); +} \ No newline at end of file diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index 1c0932b..b389320 100644 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -1,6 +1,8 @@ package envoy.server; import java.io.IOException; +import java.util.HashSet; +import java.util.Set; import com.jenkov.nioserver.Server; @@ -15,7 +17,10 @@ import com.jenkov.nioserver.Server; public class Startup { public static void main(String[] args) throws IOException { - Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor()); + Set> processors = new HashSet<>(); + processors.add(new LoginCredentialProcessor()); + processors.add(new MessageProcessor()); + Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors)); server.start(); } } \ No newline at end of file