Moved classes related to java-nio-server to envoy.server.net package.
This commit is contained in:
69
src/main/java/envoy/server/net/ObjectMessageProcessor.java
Normal file
69
src/main/java/envoy/server/net/ObjectMessageProcessor.java
Normal file
@ -0,0 +1,69 @@
|
||||
package envoy.server.net;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Set;
|
||||
|
||||
import com.jenkov.nioserver.IMessageProcessor;
|
||||
import com.jenkov.nioserver.Message;
|
||||
import com.jenkov.nioserver.WriteProxy;
|
||||
|
||||
import envoy.server.ObjectProcessor;
|
||||
|
||||
/**
|
||||
* Handles incoming objects.<br>
|
||||
* <br>
|
||||
* 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 {
|
||||
|
||||
private final Set<ObjectProcessor<?, ?>> processors;
|
||||
|
||||
/**
|
||||
* The constructor to set the {@link ObjectProcessor}s.
|
||||
*
|
||||
* @param processors the {@link ObjectProcessor} to set
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public ObjectMessageProcessor(Set<ObjectProcessor<?, ?>> processors) { this.processors = processors; }
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@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();
|
||||
System.out.println("Read object: " + obj.toString());
|
||||
|
||||
// Process object
|
||||
processors.stream().filter(p -> p.getInputClass().isInstance(obj)).forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> {
|
||||
Object responseObj = p.process(p.getInputClass().cast(obj));
|
||||
if (responseObj != null) {
|
||||
// Create message targeted at the client
|
||||
Message response = writeProxy.getMessage();
|
||||
response.socketId = message.socketId;
|
||||
|
||||
// Serialize object to byte array
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try (ObjectOutputStream oout = new ObjectOutputStream(baos)) {
|
||||
oout.writeObject(responseObj);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
byte[] objBytes = baos.toByteArray();
|
||||
response.writeToMessage(objBytes);
|
||||
writeProxy.enqueue(response);
|
||||
}
|
||||
});
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
68
src/main/java/envoy/server/net/ObjectMessageReader.java
Normal file
68
src/main/java/envoy/server/net/ObjectMessageReader.java
Normal file
@ -0,0 +1,68 @@
|
||||
package envoy.server.net;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* This {@link IMessageReader} decodes serialized Java objects.<br>
|
||||
* <br>
|
||||
* 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 List<Message> getMessages() { return completeMessages; }
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user