use of io.netty.handler.codec.stomp.StompFrame in project alien4cloud by alien4cloud.
the class StompClientHandler method beginStomp.
public void beginStomp(Channel channel) throws Exception {
StompFrame connFrame = new DefaultStompFrame(StompCommand.CONNECT);
connFrame.headers().set(StompHeaders.ACCEPT_VERSION, "1.2");
channel.writeAndFlush(connFrame);
if (log.isDebugEnabled()) {
log.debug("Begin web socket connection");
}
}
use of io.netty.handler.codec.stomp.StompFrame in project alien4cloud by alien4cloud.
the class StompClientHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {
StompFrame frame = (StompFrame) msg;
String destination = null;
if (frame.headers().get(StompHeaders.DESTINATION) != null) {
destination = frame.headers().get(StompHeaders.DESTINATION).toString();
}
if (log.isDebugEnabled()) {
log.debug("Received frame {} from topic {}", toString(frame), destination);
}
IStompCallback callback = null;
if (destination != null) {
callback = handlers.get(destination);
if (callback == null) {
throw new IllegalStateException("Received message for a topic that was never registered before");
}
}
switch(frame.command()) {
case CONNECTED:
connectFuture.setSuccess();
break;
case MESSAGE:
if (String.class == callback.getExpectedDataType()) {
callback.onData(frame.headers().get(StompHeaders.DESTINATION).toString(), frame.content().toString(Charset.forName("UTF-8")));
} else {
callback.onData(frame.headers().get(StompHeaders.DESTINATION).toString(), JsonUtil.readObject(new ByteBufInputStream(frame.content()), callback.getExpectedDataType()));
}
break;
case ERROR:
String frameText = toString(frame);
log.error("Received stomp error {} for topic {}", frameText, destination);
callback.onError(new StompErrorException("Stomp error for destination " + destination + " :\n" + frameText));
break;
default:
frameText = toString(frame);
log.error("Received unknown frame {} for topic {}", frameText, destination);
callback.onError(new StompUnknownCommandException("Unknown stomp command " + frame.command() + " from frame :\n" + frameText));
break;
}
}
use of io.netty.handler.codec.stomp.StompFrame in project netty by netty.
the class StompClientHandler method channelActive.
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
state = ClientState.AUTHENTICATING;
StompFrame connFrame = new DefaultStompFrame(StompCommand.CONNECT);
connFrame.headers().set(StompHeaders.ACCEPT_VERSION, "1.2");
connFrame.headers().set(StompHeaders.HOST, StompClient.HOST);
connFrame.headers().set(StompHeaders.LOGIN, StompClient.LOGIN);
connFrame.headers().set(StompHeaders.PASSCODE, StompClient.PASSCODE);
ctx.writeAndFlush(connFrame);
}
use of io.netty.handler.codec.stomp.StompFrame in project netty by netty.
the class StompChatHandler method onSubscribe.
private void onSubscribe(ChannelHandlerContext ctx, StompFrame inboundFrame) {
String destination = inboundFrame.headers().getAsString(DESTINATION);
String subscriptionId = inboundFrame.headers().getAsString(ID);
if (destination == null || subscriptionId == null) {
sendErrorFrame("missed header", "Required 'destination' or 'id' header missed", ctx);
return;
}
Set<StompSubscription> subscriptions = chatDestinations.get(destination);
if (subscriptions == null) {
subscriptions = new HashSet<StompSubscription>();
Set<StompSubscription> previousSubscriptions = chatDestinations.putIfAbsent(destination, subscriptions);
if (previousSubscriptions != null) {
subscriptions = previousSubscriptions;
}
}
final StompSubscription subscription = new StompSubscription(subscriptionId, destination, ctx.channel());
if (subscriptions.contains(subscription)) {
sendErrorFrame("duplicate subscription", "Received duplicate subscription id=" + subscriptionId, ctx);
return;
}
subscriptions.add(subscription);
ctx.channel().closeFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
chatDestinations.get(subscription.destination()).remove(subscription);
}
});
String receiptId = inboundFrame.headers().getAsString(RECEIPT);
if (receiptId != null) {
StompFrame receiptFrame = new DefaultStompFrame(StompCommand.RECEIPT);
receiptFrame.headers().set(RECEIPT_ID, receiptId);
ctx.writeAndFlush(receiptFrame);
}
}
use of io.netty.handler.codec.stomp.StompFrame in project netty by netty.
the class StompChatHandler method onConnect.
private static void onConnect(ChannelHandlerContext ctx, StompFrame inboundFrame) {
String acceptVersions = inboundFrame.headers().getAsString(ACCEPT_VERSION);
StompVersion handshakeAcceptVersion = ctx.channel().attr(StompVersion.CHANNEL_ATTRIBUTE_KEY).get();
if (acceptVersions == null || !acceptVersions.contains(handshakeAcceptVersion.version())) {
sendErrorFrame("invalid version", "Received invalid version, expected " + handshakeAcceptVersion.version(), ctx);
return;
}
StompFrame connectedFrame = new DefaultStompFrame(StompCommand.CONNECTED);
connectedFrame.headers().set(VERSION, handshakeAcceptVersion.version()).set(SERVER, "Netty-Server").set(HEART_BEAT, "0,0");
ctx.writeAndFlush(connectedFrame);
}
Aggregations