From 6c094bdccd7036f003e8da2cd7fa247ecd667831 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 28 Dec 2019 15:48:39 +0200 Subject: [PATCH 1/3] 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 From 19ec7c120e6309a589f4a034b71183d7d98b7c5b Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 29 Dec 2019 10:48:15 +0200 Subject: [PATCH 2/3] Changed length of encoded message length to 4 bytes --- pom.xml | 7 ++++++- .../envoy/server/ObjectMessageProcessor.java | 3 +-- .../java/envoy/server/ObjectMessageReader.java | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 144ebd0..b85a456 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ informatik-ag-ngl envoy-server-standalone - 0.0.1-SNAPSHOT + 0.1-alpha Envoy Server https://github.com/informatik-ag-ngl/envoy-server-standalone @@ -18,6 +18,11 @@ + + informatik-ag-ngl + envoy-common + 0.2-alpha + informatik-ag-ngl java-nio-server diff --git a/src/main/java/envoy/server/ObjectMessageProcessor.java b/src/main/java/envoy/server/ObjectMessageProcessor.java index bd2a735..3f52e07 100644 --- a/src/main/java/envoy/server/ObjectMessageProcessor.java +++ b/src/main/java/envoy/server/ObjectMessageProcessor.java @@ -20,9 +20,8 @@ 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))) { + try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) { Object obj = in.readObject(); - // TODO: Process pipeline System.out.println("Read object: " + obj.toString()); } catch (IOException | ClassNotFoundException e) { diff --git a/src/main/java/envoy/server/ObjectMessageReader.java b/src/main/java/envoy/server/ObjectMessageReader.java index 2249954..9a20c7f 100644 --- a/src/main/java/envoy/server/ObjectMessageReader.java +++ b/src/main/java/envoy/server/ObjectMessageReader.java @@ -41,18 +41,26 @@ public class ObjectMessageReader implements IMessageReader { } nextMessage.writeToMessage(buffer); - - int length = nextMessage.sharedArray[nextMessage.offset]; - if(nextMessage.length - nextMessage.offset >= length) { + + // Get message length + if (nextMessage.length - nextMessage.offset < 4) return; + int length = fromByteArray(nextMessage.sharedArray, nextMessage.offset) + 4; + + if (nextMessage.length - nextMessage.offset >= length) { Message message = messageBuffer.getMessage(); message.writePartialMessageToMessage(nextMessage, nextMessage.offset + length); completeMessages.add(nextMessage); nextMessage = message; } - + buffer.clear(); } + 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 public List getMessages() { return completeMessages; } } \ No newline at end of file From 29fa697f2779b24a6e4d8757ac930daa5108bc7e Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 29 Dec 2019 11:09:26 +0200 Subject: [PATCH 3/3] Fixed Javadoc error --- src/main/java/envoy/server/ObjectMessageProcessor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/envoy/server/ObjectMessageProcessor.java b/src/main/java/envoy/server/ObjectMessageProcessor.java index 3f52e07..77a086b 100644 --- a/src/main/java/envoy/server/ObjectMessageProcessor.java +++ b/src/main/java/envoy/server/ObjectMessageProcessor.java @@ -14,7 +14,7 @@ import com.jenkov.nioserver.WriteProxy; * Created: 28.12.2019
* * @author Kai S. K. Engelbart - * @since Envoy Server v0.1-alpha + * @since Envoy Server Standalone v0.1-alpha */ public class ObjectMessageProcessor implements IMessageProcessor {