Search in sources :

Example 6 with DefaultStompFrame

use of io.netty.handler.codec.stomp.DefaultStompFrame in project alien4cloud by alien4cloud.

the class StompConnection method listen.

/**
 * Start listening on web socket for specific data type. When data arrived the callback onData method will be called, when error happens the onError method
 * will be notified
 *
 * @param topic the topic to listen to
 * @param callback the callback to notify data or error
 * @param <T> type of the data
 */
public <T> void listen(final String topic, IStompCallback<T> callback) {
    this.stompClientHandler.listen(topic, callback);
    this.stompClientHandler.connectFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(final ChannelFuture future) throws Exception {
            StompFrame subscribeFrame = new DefaultStompFrame(StompCommand.SUBSCRIBE);
            subscribeFrame.headers().set(StompHeaders.DESTINATION, topic);
            subscribeFrame.headers().set(StompHeaders.ID, String.valueOf(COUNTER.incrementAndGet()));
            stompChannel.writeAndFlush(subscribeFrame);
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) StompFrame(io.netty.handler.codec.stomp.StompFrame) ChannelFutureListener(io.netty.channel.ChannelFutureListener) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) URISyntaxException(java.net.URISyntaxException)

Example 7 with DefaultStompFrame

use of io.netty.handler.codec.stomp.DefaultStompFrame in project netty by netty.

the class StompChatHandler method transformToMessage.

private static StompFrame transformToMessage(StompFrame sendFrame, StompSubscription subscription) {
    StompFrame messageFrame = new DefaultStompFrame(StompCommand.MESSAGE, sendFrame.content().retainedDuplicate());
    String id = UUID.randomUUID().toString();
    messageFrame.headers().set(MESSAGE_ID, id).set(SUBSCRIPTION, subscription.id()).set(CONTENT_LENGTH, Integer.toString(messageFrame.content().readableBytes()));
    CharSequence contentType = sendFrame.headers().get(CONTENT_TYPE);
    if (contentType != null) {
        messageFrame.headers().set(CONTENT_TYPE, contentType);
    }
    return messageFrame;
}
Also used : StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame)

Example 8 with DefaultStompFrame

use of io.netty.handler.codec.stomp.DefaultStompFrame in project netty by netty.

the class StompChatHandler method sendErrorFrame.

private static void sendErrorFrame(String message, String description, ChannelHandlerContext ctx) {
    StompFrame errorFrame = new DefaultStompFrame(StompCommand.ERROR);
    errorFrame.headers().set(MESSAGE, message);
    if (description != null) {
        errorFrame.content().writeCharSequence(description, CharsetUtil.UTF_8);
    }
    ctx.writeAndFlush(errorFrame).addListener(ChannelFutureListener.CLOSE);
}
Also used : StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame)

Example 9 with DefaultStompFrame

use of io.netty.handler.codec.stomp.DefaultStompFrame in project netty by netty.

the class StompClientHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, StompFrame frame) throws Exception {
    String subscrReceiptId = "001";
    String disconReceiptId = "002";
    switch(frame.command()) {
        case CONNECTED:
            StompFrame subscribeFrame = new DefaultStompFrame(StompCommand.SUBSCRIBE);
            subscribeFrame.headers().set(StompHeaders.DESTINATION, StompClient.TOPIC);
            subscribeFrame.headers().set(StompHeaders.RECEIPT, subscrReceiptId);
            subscribeFrame.headers().set(StompHeaders.ID, "1");
            System.out.println("connected, sending subscribe frame: " + subscribeFrame);
            state = ClientState.AUTHENTICATED;
            ctx.writeAndFlush(subscribeFrame);
            break;
        case RECEIPT:
            String receiptHeader = frame.headers().getAsString(StompHeaders.RECEIPT_ID);
            if (state == ClientState.AUTHENTICATED && receiptHeader.equals(subscrReceiptId)) {
                StompFrame msgFrame = new DefaultStompFrame(StompCommand.SEND);
                msgFrame.headers().set(StompHeaders.DESTINATION, StompClient.TOPIC);
                msgFrame.content().writeBytes("some payload".getBytes());
                System.out.println("subscribed, sending message frame: " + msgFrame);
                state = ClientState.SUBSCRIBED;
                ctx.writeAndFlush(msgFrame);
            } else if (state == ClientState.DISCONNECTING && receiptHeader.equals(disconReceiptId)) {
                System.out.println("disconnected");
                ctx.close();
            } else {
                throw new IllegalStateException("received: " + frame + ", while internal state is " + state);
            }
            break;
        case MESSAGE:
            if (state == ClientState.SUBSCRIBED) {
                System.out.println("received frame: " + frame);
                StompFrame disconnFrame = new DefaultStompFrame(StompCommand.DISCONNECT);
                disconnFrame.headers().set(StompHeaders.RECEIPT, disconReceiptId);
                System.out.println("sending disconnect frame: " + disconnFrame);
                state = ClientState.DISCONNECTING;
                ctx.writeAndFlush(disconnFrame);
            }
            break;
        default:
            break;
    }
}
Also used : DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame)

Example 10 with DefaultStompFrame

use of io.netty.handler.codec.stomp.DefaultStompFrame in project netty by netty.

the class StompEncoderBenchmark method setup.

@Setup(Level.Trial)
public void setup() {
    byte[] bytes = new byte[contentLength];
    ThreadLocalRandom.current().nextBytes(bytes);
    content = Unpooled.wrappedBuffer(bytes);
    ByteBuf testContent = Unpooled.unreleasableBuffer(content.asReadOnly());
    StompHeadersSubframe headersSubframe = ExampleStompHeadersSubframe.EXAMPLES.get(headersType);
    stompFrame = new DefaultStompFrame(headersSubframe.command(), testContent);
    stompFrame.headers().setAll(headersSubframe.headers());
    stompEncoder = new StompSubframeEncoder();
    context = new EmbeddedChannelWriteReleaseHandlerContext(pooledAllocator ? PooledByteBufAllocator.DEFAULT : UnpooledByteBufAllocator.DEFAULT, stompEncoder) {

        @Override
        protected void handleException(Throwable t) {
            handleUnexpectedException(t);
        }
    };
}
Also used : StompHeadersSubframe(io.netty.handler.codec.stomp.StompHeadersSubframe) EmbeddedChannelWriteReleaseHandlerContext(io.netty.microbench.channel.EmbeddedChannelWriteReleaseHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) StompSubframeEncoder(io.netty.handler.codec.stomp.StompSubframeEncoder) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) Setup(org.openjdk.jmh.annotations.Setup)

Aggregations

DefaultStompFrame (io.netty.handler.codec.stomp.DefaultStompFrame)10 StompFrame (io.netty.handler.codec.stomp.StompFrame)9 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 ByteBuf (io.netty.buffer.ByteBuf)1 StompHeadersSubframe (io.netty.handler.codec.stomp.StompHeadersSubframe)1 StompSubframeEncoder (io.netty.handler.codec.stomp.StompSubframeEncoder)1 EmbeddedChannelWriteReleaseHandlerContext (io.netty.microbench.channel.EmbeddedChannelWriteReleaseHandlerContext)1 URISyntaxException (java.net.URISyntaxException)1 Setup (org.openjdk.jmh.annotations.Setup)1