Added basic lifecycle with object reading capabilities

* Added java-nio-server dependency
* Added ObjectMessageReader and ObjectMessageProcessor
* Added Startup class starting a Server instance
This commit is contained in:
Kai S. K. Engelbart 2019-12-28 15:48:39 +02:00
parent 91b2d91af3
commit e13438eefd
5 changed files with 121 additions and 0 deletions

View File

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

View File

@ -17,6 +17,14 @@
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<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,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: <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 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();
}
}
}

View File

@ -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: <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);
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<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();
}
}