use of com.robo4j.socket.http.request.DatagramResponseProcess in project robo4j by Robo4J.
the class ReadDatagramSelectionKeyHandler method handle.
// TODO: 2/25/18 (miro) -> Datagram type -> not only string is possible
@Override
public SelectionKey handle() {
final DatagramChannel channel = (DatagramChannel) key.channel();
try {
ByteBuffer buffer = serverContext.getPropertySafe(ByteBuffer.class, PROPERTY_BYTE_BUFFER);
buffer.clear();
channel.receive(buffer);
buffer.flip();
String message = ChannelBufferUtils.byteBufferToString(buffer);
final String[] headerAndBody = message.split(HTTP_HEADER_BODY_DELIMITER);
final String firstLine = RoboHttpUtils.correctLine(headerAndBody[0]);
final String[] tokens = firstLine.split(HttpConstant.HTTP_EMPTY_SEP);
final String body = headerAndBody[1];
final ServerPathConfig serverPathConfig = serverContext.getPathConfig(new PathHttpMethod(tokens[1], null));
final RoboReference<Object> roboReference = serverPathConfig.getRoboUnit();
final SocketDecoder<Object, Object> decoder = codecRegistry.getDecoder(roboReference.getMessageType());
final Object decodedMessage = decoder.decode(body);
serverPathConfig.getRoboUnit().sendMessage(decodedMessage);
final DatagramResponseProcess responseProcess = new DatagramResponseProcess(tokens[1], roboReference, decodedMessage);
outBuffers.put(key, responseProcess);
return key;
} catch (IOException e) {
throw new SocketException("hanlde", e);
}
}
use of com.robo4j.socket.http.request.DatagramResponseProcess in project robo4j by Robo4J.
the class WriteDatagramSelectionKeyHandler method handle.
@Override
public SelectionKey handle() {
// final DatagramChannel channel = (DatagramChannel) key.channel();
// ByteBuffer buffer = ByteBuffer.allocate(serverContext.getPropertySafe(Integer.class, PROPERTY_BUFFER_CAPACITY));
final DatagramResponseProcess responseProcess = outBuffers.get(key);
// buffer.clear();
// buffer.put("ACCEPTED".getBytes());
// buffer.flip();
// try {
// channel.write(buffer);
// } catch (IOException e) {
// throw new SocketException("handle", e);
// }
RoboReference<Object> reference = responseProcess.getTarget();
Object responseMessage = responseProcess.getResult();
reference.sendMessage(responseMessage);
System.out.println("Wrote: " + responseMessage);
key.cancel();
return key;
}
Aggregations