Search in sources :

Example 1 with DatagramResponseProcess

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);
    }
}
Also used : SocketException(com.robo4j.socket.http.SocketException) ServerPathConfig(com.robo4j.socket.http.units.ServerPathConfig) DatagramChannel(java.nio.channels.DatagramChannel) DatagramResponseProcess(com.robo4j.socket.http.request.DatagramResponseProcess) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) PathHttpMethod(com.robo4j.socket.http.units.PathHttpMethod)

Example 2 with DatagramResponseProcess

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;
}
Also used : DatagramResponseProcess(com.robo4j.socket.http.request.DatagramResponseProcess)

Aggregations

DatagramResponseProcess (com.robo4j.socket.http.request.DatagramResponseProcess)2 SocketException (com.robo4j.socket.http.SocketException)1 PathHttpMethod (com.robo4j.socket.http.units.PathHttpMethod)1 ServerPathConfig (com.robo4j.socket.http.units.ServerPathConfig)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 DatagramChannel (java.nio.channels.DatagramChannel)1