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();
}
}
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();
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations