Merge pull request #7 from informatik-ag-ngl/f/basic_infrastructure

Added basic infrastructure
This commit is contained in:
Kai S. K. Engelbart 2019-12-29 11:11:53 +02:00 committed by GitHub
commit fe05ff23cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 134 additions and 1 deletions

View File

@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.ui.text.custom_code_templates=

15
pom.xml
View File

@ -5,7 +5,7 @@
<groupId>informatik-ag-ngl</groupId>
<artifactId>envoy-server-standalone</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.1-alpha</version>
<name>Envoy Server</name>
<url>https://github.com/informatik-ag-ngl/envoy-server-standalone</url>
@ -17,6 +17,19 @@
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>informatik-ag-ngl</groupId>
<artifactId>envoy-common</artifactId>
<version>0.2-alpha</version>
</dependency>
<dependency>
<groupId>informatik-ag-ngl</groupId>
<artifactId>java-nio-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>envoy-server-standalone</finalName>
</build>

View File

@ -0,0 +1,31 @@
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: <strong>envoy-server-standalone</strong><br>
* File: <strong>ObjectMessageProcessor.java</strong><br>
* Created: <strong>28.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone 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 + 4, message.length - 4))) {
Object obj = in.readObject();
// TODO: Process pipeline
System.out.println("Read object: " + obj.toString());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,66 @@
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: <strong>envoy-server-standalone</strong><br>
* File: <strong>ObjectMessageReader.java</strong><br>
* Created: <strong>28.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-alpha
*/
public class ObjectMessageReader implements IMessageReader {
private List<Message> 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);
// 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<Message> getMessages() { return completeMessages; }
}

View File

@ -0,0 +1,21 @@
package envoy.server;
import java.io.IOException;
import com.jenkov.nioserver.Server;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>Startup.java</strong><br>
* Created: <strong>24.12.2019</strong><br>
*
* @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();
}
}