Search in sources :

Example 1 with SocketException

use of com.robo4j.socket.http.SocketException in project robo4j by Robo4J.

the class OutboundHttpSocketChannelHandler method getDecoratedResponse.

private HttpDecoratedResponse getDecoratedResponse(ByteChannel byteChannel, HttpPathMethodDTO pathMethod) {
    lock.lock();
    try {
        final HttpDecoratedResponse result = channelResponseBuffer.getHttpDecoratedResponseByChannel(byteChannel);
        result.addCallbacks(pathMethod.getCallbacks());
        return result;
    } catch (IOException e) {
        throw new SocketException("message body write problem", e);
    } finally {
        lock.unlock();
    }
}
Also used : HttpDecoratedResponse(com.robo4j.socket.http.message.HttpDecoratedResponse) SocketException(com.robo4j.socket.http.SocketException) IOException(java.io.IOException)

Example 2 with SocketException

use of com.robo4j.socket.http.SocketException in project robo4j by Robo4J.

the class ReadSelectionKeyHandler method handle.

@Override
public SelectionKey handle() {
    SocketChannel channel = (SocketChannel) key.channel();
    lock.lock();
    try {
        final HttpDecoratedRequest decoratedRequest = channelRequestBuffer.getHttpDecoratedRequestByChannel(channel);
        final RoboRequestFactory factory = new RoboRequestFactory(codecRegistry);
        final RoboRequestCallable callable = new RoboRequestCallable(context, serverContext, decoratedRequest, factory);
        final Future<HttpResponseProcess> futureResult = context.getScheduler().submit(callable);
        final HttpResponseProcess result = extractRoboResponseProcess(futureResult);
        outBuffers.put(key, result);
        registerSelectionKey(channel);
        return key;
    } catch (IOException e) {
        throw new SocketException(e.getMessage());
    } finally {
        lock.unlock();
    }
}
Also used : HttpDecoratedRequest(com.robo4j.socket.http.message.HttpDecoratedRequest) SocketChannel(java.nio.channels.SocketChannel) SocketException(com.robo4j.socket.http.SocketException) RoboRequestCallable(com.robo4j.socket.http.request.RoboRequestCallable) HttpResponseProcess(com.robo4j.socket.http.request.HttpResponseProcess) IOException(java.io.IOException) RoboRequestFactory(com.robo4j.socket.http.request.RoboRequestFactory)

Example 3 with SocketException

use of com.robo4j.socket.http.SocketException 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 4 with SocketException

use of com.robo4j.socket.http.SocketException in project robo4j by Robo4J.

the class ChannelUtils method initDatagramChannel.

public static DatagramChannel initDatagramChannel(DatagramConnectionType connectionType, SocketContext<?> context) {
    try {
        final DatagramChannel result = DatagramChannel.open();
        SocketAddress address = getSocketAddressByContext(context);
        switch(connectionType) {
            case SERVER:
                DatagramSocket socket = result.socket();
                result.configureBlocking(false);
                socket.bind(address);
                break;
            case CLIENT:
                result.connect(address);
                break;
            default:
                throw new SocketException("int not supported: " + connectionType);
        }
        return result;
    } catch (Exception e) {
        SimpleLoggingUtil.error(ChannelUtils.class, "init datagram socket channel", e);
        throw new SocketException("init datagram socket channel", e);
    }
}
Also used : SocketException(com.robo4j.socket.http.SocketException) DatagramSocket(java.net.DatagramSocket) DatagramChannel(java.nio.channels.DatagramChannel) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SocketException(com.robo4j.socket.http.SocketException) IOException(java.io.IOException)

Example 5 with SocketException

use of com.robo4j.socket.http.SocketException in project robo4j by Robo4J.

the class ChannelUtils method getSocketAddressByContext.

public static SocketAddress getSocketAddressByContext(SocketContext<?> context) {
    if (context instanceof ClientContext) {
        final String clientHost = context.getPropertySafe(String.class, PROPERTY_HOST);
        final int clientPort = context.getPropertySafe(Integer.class, PROPERTY_SOCKET_PORT);
        return new InetSocketAddress(clientHost, clientPort);
    } else if (context instanceof ServerContext) {
        final int serverPort = context.getPropertySafe(Integer.class, PROPERTY_SOCKET_PORT);
        return new InetSocketAddress(serverPort);
    } else {
        throw new SocketException("invalid context" + context);
    }
}
Also used : SocketException(com.robo4j.socket.http.SocketException) ServerContext(com.robo4j.socket.http.units.ServerContext) InetSocketAddress(java.net.InetSocketAddress) ClientContext(com.robo4j.socket.http.units.ClientContext)

Aggregations

SocketException (com.robo4j.socket.http.SocketException)7 IOException (java.io.IOException)6 InetSocketAddress (java.net.InetSocketAddress)3 HttpResponseProcess (com.robo4j.socket.http.request.HttpResponseProcess)2 ByteBuffer (java.nio.ByteBuffer)2 DatagramChannel (java.nio.channels.DatagramChannel)2 SocketChannel (java.nio.channels.SocketChannel)2 HttpDecoratedRequest (com.robo4j.socket.http.message.HttpDecoratedRequest)1 HttpDecoratedResponse (com.robo4j.socket.http.message.HttpDecoratedResponse)1 HttpDenominator (com.robo4j.socket.http.message.HttpDenominator)1 HttpResponseDenominator (com.robo4j.socket.http.message.HttpResponseDenominator)1 DatagramResponseProcess (com.robo4j.socket.http.request.DatagramResponseProcess)1 RoboRequestCallable (com.robo4j.socket.http.request.RoboRequestCallable)1 RoboRequestFactory (com.robo4j.socket.http.request.RoboRequestFactory)1 ClientContext (com.robo4j.socket.http.units.ClientContext)1 PathHttpMethod (com.robo4j.socket.http.units.PathHttpMethod)1 ServerContext (com.robo4j.socket.http.units.ServerContext)1 ServerPathConfig (com.robo4j.socket.http.units.ServerPathConfig)1 DatagramSocket (java.net.DatagramSocket)1 SocketAddress (java.net.SocketAddress)1