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:
parent
a1d4f1d999
commit
6c094bdccd
2
.settings/org.eclipse.jdt.ui.prefs
Normal file
2
.settings/org.eclipse.jdt.ui.prefs
Normal file
@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.ui.text.custom_code_templates=
|
8
pom.xml
8
pom.xml
@ -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>
|
||||
|
32
src/main/java/envoy/server/ObjectMessageProcessor.java
Normal file
32
src/main/java/envoy/server/ObjectMessageProcessor.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
58
src/main/java/envoy/server/ObjectMessageReader.java
Normal file
58
src/main/java/envoy/server/ObjectMessageReader.java
Normal 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; }
|
||||
}
|
21
src/main/java/envoy/server/Startup.java
Normal file
21
src/main/java/envoy/server/Startup.java
Normal 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user