Search in sources :

Example 1 with StompFrame

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");
    }
}
Also used : StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame)

Example 2 with StompFrame

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;
    }
}
Also used : StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) ByteBufInputStream(io.netty.buffer.ByteBufInputStream)

Example 3 with StompFrame

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);
}
Also used : DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame)

Example 4 with StompFrame

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);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) ChannelFutureListener(io.netty.channel.ChannelFutureListener) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame)

Example 5 with StompFrame

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);
}
Also used : StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame)

Aggregations

DefaultStompFrame (io.netty.handler.codec.stomp.DefaultStompFrame)10 StompFrame (io.netty.handler.codec.stomp.StompFrame)10 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 URISyntaxException (java.net.URISyntaxException)1