From e13438eefddbf0e5eee6edfb6b8bb76b1d4c78a6 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 28 Dec 2019 15:48:39 +0200 Subject: [PATCH] Added basic lifecycle with object reading capabilities * Added java-nio-server dependency * Added ObjectMessageReader and ObjectMessageProcessor * Added Startup class starting a Server instance --- .settings/org.eclipse.jdt.ui.prefs | 2 + pom.xml | 8 +++ .../envoy/server/ObjectMessageProcessor.java | 32 ++++++++++ .../envoy/server/ObjectMessageReader.java | 58 +++++++++++++++++++ src/main/java/envoy/server/Startup.java | 21 +++++++ 5 files changed, 121 insertions(+) create mode 100644 .settings/org.eclipse.jdt.ui.prefs create mode 100644 src/main/java/envoy/server/ObjectMessageProcessor.java create mode 100644 src/main/java/envoy/server/ObjectMessageReader.java create mode 100644 src/main/java/envoy/server/Startup.java diff --git a/.settings/org.eclipse.jdt.ui.prefs b/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 0000000..7103be7 --- /dev/null +++ b/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.ui.text.custom_code_templates= diff --git a/pom.xml b/pom.xml index 3d1e16d..144ebd0 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,14 @@ 1.8 + + + informatik-ag-ngl + java-nio-server + 0.0.1-SNAPSHOT + + + envoy-server-standalone diff --git a/src/main/java/envoy/server/ObjectMessageProcessor.java b/src/main/java/envoy/server/ObjectMessageProcessor.java new file mode 100644 index 0000000..bd2a735 --- /dev/null +++ b/src/main/java/envoy/server/ObjectMessageProcessor.java @@ -0,0 +1,32 @@ +package envoy.server; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; + +import com.jenkov.nioserver.IMessageProcessor; +import com.jenkov.nioserver.Message; +import com.jenkov.nioserver.WriteProxy; + +/** + * Project: envoy-server-standalone
+ * File: ObjectMessageProcessor.java
+ * Created: 28.12.2019
+ * + * @author Kai S. K. Engelbart + * @since Envoy Server v0.1-alpha + */ +public class ObjectMessageProcessor implements IMessageProcessor { + + @Override + public void process(Message message, WriteProxy writeProxy) { + try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 1, message.length - 1))) { + Object obj = in.readObject(); + + // TODO: Process pipeline + System.out.println("Read object: " + obj.toString()); + } catch (IOException | ClassNotFoundException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/envoy/server/ObjectMessageReader.java b/src/main/java/envoy/server/ObjectMessageReader.java new file mode 100644 index 0000000..2249954 --- /dev/null +++ b/src/main/java/envoy/server/ObjectMessageReader.java @@ -0,0 +1,58 @@ +package envoy.server; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +import com.jenkov.nioserver.IMessageReader; +import com.jenkov.nioserver.Message; +import com.jenkov.nioserver.MessageBuffer; +import com.jenkov.nioserver.Socket; + +/** + * Project: envoy-server-standalone
+ * File: ObjectMessageReader.java
+ * Created: 28.12.2019
+ * + * @author Kai S. K. Engelbart + * @since Envoy Server Standalone v0.1-alpha + */ +public class ObjectMessageReader implements IMessageReader { + + private List completeMessages = new ArrayList<>(); + private Message nextMessage; + private MessageBuffer messageBuffer; + + @Override + public void init(MessageBuffer messageBuffer) { + this.messageBuffer = messageBuffer; + nextMessage = messageBuffer.getMessage(); + } + + @Override + public void read(Socket socket, ByteBuffer buffer) throws IOException { + socket.read(buffer); + buffer.flip(); + + if (!buffer.hasRemaining()) { + buffer.clear(); + return; + } + + nextMessage.writeToMessage(buffer); + + int length = nextMessage.sharedArray[nextMessage.offset]; + if(nextMessage.length - nextMessage.offset >= length) { + Message message = messageBuffer.getMessage(); + message.writePartialMessageToMessage(nextMessage, nextMessage.offset + length); + completeMessages.add(nextMessage); + nextMessage = message; + } + + buffer.clear(); + } + + @Override + public List getMessages() { return completeMessages; } +} \ No newline at end of file diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java new file mode 100644 index 0000000..1c0932b --- /dev/null +++ b/src/main/java/envoy/server/Startup.java @@ -0,0 +1,21 @@ +package envoy.server; + +import java.io.IOException; + +import com.jenkov.nioserver.Server; + +/** + * Project: envoy-server-standalone
+ * File: Startup.java
+ * Created: 24.12.2019
+ * + * @author Kai S. K. Engelbart + * @since Envoy Server Standalone v0.1-alpha + */ +public class Startup { + + public static void main(String[] args) throws IOException { + Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor()); + server.start(); + } +} \ No newline at end of file