Example usage for io.netty.channel ChannelHandlerContext fireChannelRead

List of usage examples for io.netty.channel ChannelHandlerContext fireChannelRead

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext fireChannelRead.

Prototype

@Override
    ChannelHandlerContext fireChannelRead(Object msg);

Source Link

Usage

From source file:io.hydramq.network.server.TopicProtocol.java

License:Open Source License

public void onCommand(ChannelHandlerContext ctx, Command command) {
    if (command instanceof TopicHandshake) {
        TopicHandshake handshake = (TopicHandshake) command;
        Topic topic = topicManager.topic(handshake.getTopicName());
        ctx.channel().attr(TOPIC_KEY).set(topic);
        Map<PartitionId, PartitionFlags> knownStates = new HashMap<>();
        topic.discoverPartitions(knownStates::put);

        TopicHandshake reply = handshake.reply(max(handshake.getVersion(), maxVersion), knownStates);
        send(ctx, reply);/*from w ww  .  j a  va2 s.co m*/

        ctx.channel().attr(ChannelAttributes.DISCOVER_PARTITIONS_LISTENER)
                .set((partitionId, partitionState) -> send(ctx,
                        new PartitionsDiscoveredNotification(partitionId, partitionState)));
        if (topic instanceof TopicInternal) {
            ((TopicInternal) topic).discoverPartitions(
                    ctx.channel().attr(ChannelAttributes.DISCOVER_PARTITIONS_LISTENER).get(), CONTINUOUSLY,
                    knownStates);
        } else {
            getTopic(ctx).discoverPartitions(
                    ctx.channel().attr(ChannelAttributes.DISCOVER_PARTITIONS_LISTENER).get(), CONTINUOUSLY);
        }
    } else if (command instanceof PartitionIdWriteRequest) {
        PartitionIdWriteRequest writeRequest = (PartitionIdWriteRequest) command;
        messageWrites.mark();
        messageTotal.mark();
        try {
            getTopic(ctx).write(writeRequest.getPartitionId(), writeRequest.getMessage())
                    // TODO: Convert these to just send as commands, and let the pipeline take care of it
                    .thenAccept(aVoid -> send(ctx, Acknowledgement.replyTo(writeRequest)))
                    .exceptionally(throwable -> {
                        send(ctx, new Error(writeRequest.correlationId(), 0)); // TODO: better error translation
                        return null;
                    });
        } catch (HydraRuntimeException e) {
            logger.error("Error writing messages", e);
            send(ctx, new Error(writeRequest.correlationId(), 0)); // TODO: better error translation
        }
    } else if (command instanceof PartitionIdReadRequest) {
        PartitionIdReadRequest readRequest = (PartitionIdReadRequest) command;
        try {
            getTopic(ctx).read(readRequest.getPartitionId(), readRequest.getMessageOffset(),
                    readRequest.getMaxMessages()).thenAccept(messages -> {
                        if (messages.size() > 0) {
                            messageReads.mark(messages.size());
                            messageTotal.mark(messages.size());
                        }
                        send(ctx, new ReadResponse(readRequest.correlationId(), messages));
                    }).exceptionally(throwable -> {
                        logger.warn("Error reading messages", throwable);
                        send(ctx, new Error(readRequest.correlationId(), 0)); // TODO: better error translation
                        return null;
                    });
        } catch (HydraRuntimeException e) {
            logger.error("Error reading messages", e);
            send(ctx, new Error(readRequest.correlationId(), 0)); // TODO: better error translation
        }
    } else if (command instanceof PartitionInfoRequest) {
        PartitionInfoRequest request = (PartitionInfoRequest) command;
        try {
            getTopic(ctx).partitionInfo(request.getPartitionId()).thenAccept(partitionInfo -> {
                send(ctx, request.reply(partitionInfo));
            }).exceptionally(throwable -> {
                send(ctx, new Error(request.correlationId(), 0));
                return null;
            });
        } catch (HydraRuntimeException e) {
            logger.error("Error reading PartitionInfo", e);
            send(ctx, new Error(request.correlationId(), 0)); // TODO: better error translation
        }
    } else if (command instanceof LockListenerRequest) {
        LockListenerRequest request = (LockListenerRequest) command;
        try {
            if (request.isRegistering()) {
                NetworkLockListener listener = new NetworkLockListener(request.getClientKey(),
                        request.getLockGroup(), ctx.channel());
                ctx.channel().attr(LOCK_LISTENERS).get().putIfAbsent(request.getClientKey(), listener);
                ctx.channel().attr(TOPIC_KEY).get().acquirePartitionLocks(request.getLockGroup(), listener,
                        CONTINUOUSLY);
            } else {
                LockListener lockListener = ctx.channel().attr(LOCK_LISTENERS).get()
                        .get(request.getClientKey());
                if (lockListener != null) {
                    ctx.channel().attr(TOPIC_KEY).get().acquirePartitionLocks(request.getLockGroup(),
                            lockListener, REMOVE);
                }
            }
            ChannelUtils.ack(ctx.channel(), request);

        } catch (HydraRuntimeException e) {
            logger.error("Error reading PartitionInfo", e);
            send(ctx, new Error(request.correlationId(), 0)); // TODO: better error translation
        }
    } else if (command instanceof CursorInfoRequest) {
        CursorInfoRequest request = (CursorInfoRequest) command;

        try {
            getTopic(ctx).cursor(request.getPartitionId(), request.getCursorName()).thenAccept(cursorInfo -> {
                send(ctx, request.reply(cursorInfo));
            }).exceptionally(throwable -> {
                send(ctx, new Error(request.correlationId(), 0));
                return null;
            });
        } catch (HydraRuntimeException e) {
            logger.error("Error reading PartitionInfo", e);
            send(ctx, new Error(request.correlationId(), 0)); // TODO: better error translation
        }

    } else if (command instanceof WriteCursorRequest) {
        WriteCursorRequest request = (WriteCursorRequest) command;
        try {
            getTopic(ctx).cursor(request.getPartitionId(), request.getCursorName(), request.getOffset())
                    .thenAccept(aVoid -> {
                        send(ctx, Acknowledgement.replyTo(request));
                    }).exceptionally(throwable -> {
                        send(ctx, new Error(request.correlationId(), 0));
                        return null;
                    });
        } catch (HydraRuntimeException e) {
            logger.error("Error writing Cursor", e);
            send(ctx, new Error(request.correlationId(), 0)); // TODO: better error translation
        }
        // TODO: should all command handlers simply throw and let the sync handle it?
    } else {
        ctx.fireChannelRead(command);
    }
}

From source file:io.jsync.http.impl.cgbystrom.FlashPolicyHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buffer = (ByteBuf) msg;/*from www. j a  v  a 2 s.  c o  m*/
    int index = buffer.readerIndex();
    switch (state) {
    case MAGIC1:
        if (!buffer.isReadable()) {
            return;
        }
        final int magic1 = buffer.getUnsignedByte(index++);
        state = ParseState.MAGIC2;
        if (magic1 != '<') {
            ctx.fireChannelRead(buffer);
            ctx.pipeline().remove(this);
            return;
        }
        // fall through
    case MAGIC2:
        if (!buffer.isReadable()) {
            return;
        }
        final int magic2 = buffer.getUnsignedByte(index);
        if (magic2 != 'p') {
            ctx.fireChannelRead(buffer);
            ctx.pipeline().remove(this);
        } else {
            ctx.writeAndFlush(Unpooled.copiedBuffer(XML, CharsetUtil.UTF_8))
                    .addListener(ChannelFutureListener.CLOSE);
        }
    }
}

From source file:io.liveoak.container.traversal.BaseResponder.java

License:Open Source License

protected void dispatchInvocation(Runnable invocation) {
    ChannelHandlerContext context = this.ctx.pipeline().context(RESOURCE_READ_DECODER);
    HttpRequestBodyHandler.Invocation completion = new HttpRequestBodyHandler.Invocation(invocation);
    context.fireChannelRead(completion);
    // signal we're ready to read some more.
    context.read();//from w  w w . ja v a 2 s.c om
}

From source file:io.liveoak.stomp.common.AbstractControlFrameHandler.java

License:Open Source License

public void handleFrame(ChannelHandlerContext ctx, StompFrame msg) throws StompServerException {
    if (msg instanceof StompControlFrame) {
        handleControlFrame(ctx, (StompControlFrame) msg);
        return;//from  w w  w  .j  a v a 2s  . c o m
    }

    ReferenceCountUtil.retain(msg);
    ctx.fireChannelRead(msg);
}

From source file:io.liveoak.stomp.common.AbstractFrameHandler.java

License:Open Source License

public void channelRead0(ChannelHandlerContext ctx, StompFrame msg) throws Exception {
    if (this.command != null) {
        if (msg.command().equals(this.command)) {
            handleFrame(ctx, msg);/*from  w  w  w .  j a va  2s .c om*/
            return;
        }
    }

    ReferenceCountUtil.retain(msg);
    ctx.fireChannelRead(msg);
}

From source file:io.liveoak.stomp.server.protocol.ReceiptHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, StompFrame msg) throws Exception {
    String receiptId = msg.headers().get(Headers.RECEIPT);
    if (receiptId != null) {
        ctx.writeAndFlush(StompFrame.newReceiptFrame(receiptId));
    }/*from  w  w  w  .ja  v  a2 s.  c o m*/

    // retain and keep it moving upstream
    ReferenceCountUtil.retain(msg);
    ctx.fireChannelRead(msg);
}

From source file:io.liveoak.stomp.server.protocol.SubscribeHandler.java

License:Open Source License

@Override
public void handleControlFrame(ChannelHandlerContext ctx, StompControlFrame msg) throws StompServerException {
    String subscriptionId = msg.headers().get(Headers.ID);
    String destination = msg.headers().get(Headers.DESTINATION);
    StompConnection stompConnection = ctx.channel().attr(ConnectHandler.CONNECTION).get();
    this.serverContext.handleSubscribe(stompConnection, destination, subscriptionId, msg.headers());

    // retain and send upstream for RECEIPT
    ReferenceCountUtil.retain(msg);//from  w  w w .  jav  a  2 s.c om
    ctx.fireChannelRead(msg);
}

From source file:io.liveoak.stomp.server.protocol.UnsubscribeHandler.java

License:Open Source License

@Override
public void handleControlFrame(ChannelHandlerContext ctx, StompControlFrame msg) throws StompServerException {
    String subscriptionId = msg.headers().get(Headers.ID);
    StompConnection stompConnection = ctx.channel().attr(ConnectHandler.CONNECTION).get();
    this.serverContext.handleUnsubscribe(stompConnection, subscriptionId);

    // retain and send upstream for RECEIPT
    ReferenceCountUtil.retain(msg);//from w ww.j av a  2  s .  co  m
    ctx.fireChannelRead(msg);
}

From source file:io.moquette.server.netty.metrics.BytesMetricsHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    BytesMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
    metrics.incrementRead(((ByteBuf) msg).readableBytes());
    ctx.fireChannelRead(msg);
}

From source file:io.moquette.server.netty.metrics.DropWizardMetricsHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object message) {
    MqttMessage msg = (MqttMessage) message;
    MqttMessageType messageType = msg.fixedHeader().messageType();
    switch (messageType) {
    case PUBLISH:
        this.publishesMetrics.mark();
        break;//from  w ww.  j  a  v  a2  s .  c o m
    case SUBSCRIBE:
        this.subscribeMetrics.mark();
        break;
    case CONNECT:
        this.connectedClientsMetrics.inc();
        break;
    case DISCONNECT:
        this.connectedClientsMetrics.dec();
        break;
    default:
        break;
    }
    ctx.fireChannelRead(message);
}