use of io.scalecube.streams.ChannelContext in project scalecube by scalecube.
the class NettyStreamMessageHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ChannelContext channelContext = ChannelSupport.getChannelContextIfExist(ctx);
if (channelContext == null) {
LOGGER.error("Failed to handle message, channel context is null on channel: {}", ctx.channel());
ChannelSupport.releaseRefCount(msg);
ctx.channel().close();
return;
}
try {
channelContext.postReadSuccess(StreamMessageCodec.decode((ByteBuf) msg));
} catch (Exception throwable) {
ChannelSupport.releaseRefCount(msg);
channelContext.postReadError(throwable);
}
}
use of io.scalecube.streams.ChannelContext in project scalecube by scalecube.
the class GatewaySocketIoListener method onMessage.
@Override
public void onMessage(Session session, ByteBuf buf) {
String channelContextId = sessionIdToChannelContextId.get(session.getSessionId());
if (channelContextId == null) {
LOGGER.error("Can't find channel context id by session id: {}", session.getSessionId());
ChannelSupport.releaseRefCount(buf);
session.disconnect();
return;
}
ChannelContext channelContext = ChannelContext.getIfExist(channelContextId);
if (channelContext == null) {
ChannelSupport.releaseRefCount(buf);
LOGGER.error("Failed to handle message, channel context is null by id: {}", channelContextId);
session.disconnect();
return;
}
try {
channelContext.postReadSuccess(StreamMessageCodec.decode(buf));
} catch (Exception throwable) {
ChannelSupport.releaseRefCount(buf);
channelContext.postReadError(throwable);
}
}
use of io.scalecube.streams.ChannelContext in project scalecube by scalecube.
the class GatewaySocketIoListener method onConnect.
@Override
public void onConnect(Session session) {
// create channel context
InetSocketAddress remoteAddress = (InetSocketAddress) session.getRemoteAddress();
String host = remoteAddress.getAddress().getHostAddress();
int port = remoteAddress.getPort();
ChannelContext channelContext = ChannelContext.create(Address.create(host, port));
// save mapping
sessionIdToChannelContextId.put(session.getSessionId(), channelContext.getId());
// register cleanup process upfront
channelContext.listenClose(input -> {
if (session.getState() == Session.State.CONNECTED) {
session.disconnect();
}
});
// bind channelContext
eventStream.subscribe(channelContext);
channelContext.listenWrite().map(Event::getMessageOrThrow).subscribe(message -> {
ByteBuf buf = StreamMessageCodec.encode(message);
// release ByteBuf
ChannelSupport.releaseRefCount(message.data());
try {
session.send(buf);
channelContext.postWriteSuccess(message);
} catch (Exception throwable) {
channelContext.postWriteError(message, throwable);
}
}, throwable -> {
LOGGER.error("Fatal exception occured on channel context: {}, cause: {}", channelContext.getId(), throwable);
session.disconnect();
});
}
use of io.scalecube.streams.ChannelContext in project scalecube by scalecube.
the class GatewayHttpMessageHandler method channelActive.
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ChannelContext channelContext = ChannelSupport.getChannelContextIfExist(ctx);
if (channelContext == null) {
LOGGER.error("Can't find channel context on channel: {}", ctx.channel());
ctx.channel().close();
return;
}
channelContext.listenWrite().map(Event::getMessageOrThrow).subscribe(message -> {
Qualifier qualifier = Qualifier.fromString(message.qualifier());
FullHttpResponse response;
if (!Q_ERROR_NAMESPACE.equalsIgnoreCase(qualifier.getNamespace())) {
response = message.containsData() ? HttpCodecUtil.okResponse((ByteBuf) message.data()) : HttpCodecUtil.emptyResponse();
} else {
if (message.dataOfType(ErrorData.class)) {
// => ErrorData
response = HttpCodecUtil.errorResponse(qualifier, (ErrorData) message.data());
} else if (message.dataOfType(ByteBuf.class)) {
// => ByteBuf assumed
response = HttpCodecUtil.errorResponse(qualifier, (ByteBuf) message.data());
} else {
response = HttpCodecUtil.emptyErrorResponse(qualifier);
}
}
// Hint: at this point we could add logic on writing headers to the response
ctx.writeAndFlush(response).addListener((ChannelFutureListener) future -> {
if (!future.isSuccess()) {
channelContext.postWriteError(message, future.cause());
} else {
channelContext.postWriteSuccess(message);
}
});
}, throwable -> {
LOGGER.error("Fatal exception occured on channel context: {}, cause: {}", channelContext.getId(), throwable);
ctx.channel().close();
});
super.channelActive(ctx);
}
use of io.scalecube.streams.ChannelContext in project scalecube by scalecube.
the class GatewayHttpMessageHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (!(msg instanceof FullHttpRequest)) {
// pass it through
super.channelRead(ctx, msg);
return;
}
ChannelContext channelContext = ChannelSupport.getChannelContextIfExist(ctx);
if (channelContext == null) {
LOGGER.error("Failed to handle message, channel context is null on channel: {}", ctx.channel());
ChannelSupport.releaseRefCount(msg);
ctx.channel().close();
return;
}
try {
FullHttpRequest request = (FullHttpRequest) msg;
// get qualifier
// Hint: qualifier format could be changed to start from '/' there be saving from substringing
String qualifier = request.uri().substring(1);
StreamMessage.Builder builder = StreamMessage.builder().qualifier(qualifier);
if (request.content().isReadable()) {
builder.data(request.content());
}
channelContext.postReadSuccess(builder.build());
} catch (Exception throwable) {
// Hint: at this point we could save at least request headers and put them into error
ChannelSupport.releaseRefCount(msg);
channelContext.postReadError(throwable);
}
}
Aggregations