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:jj.http.server.HttpRequestListeningHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest request) throws Exception {
    this.request = request;

    if (request.getDecoderResult().isFailure()) {
        // respond with BAD_REQUEST and close the connection
        // (unless we are being proxied and the connection is keep-alive, that is)
        BAD_REQUEST.writeAndFlush(ctx).addListener(ChannelFutureListener.CLOSE);

    } else if (methodHandlers.containsKey(request.getMethod())) {

        HttpMethodHandler methodHandler = methodHandlers.get(request.getMethod()).get();
        methodHandler.request(request);/*from   www. j av  a 2 s . c om*/

        ChannelPipeline p = ctx.pipeline();
        p.addAfter(name, "method handler", methodHandler);

        ctx.fireChannelRead(request);

    } else {
        // respond with NOT_IMPLEMENTED
        // unless we are being proxied and the connection is keep-alive
        NOT_IMPLEMENTED.writeAndFlush(ctx).addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:jj.repl.telnet.TelnetConnectionHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
    dumpBuffer("received", msg);
    // sanity check
    assert state != null : "got into read without expecting anything next";
    switch (state) {

    case AwaitingClientNegotiation:
        // cancel the awaiting timer
        awaitingInitialNegotiation.cancelKey().cancel();
        // try to read it
        ctx.writeAndFlush(makeMessage(IAC, DO, TRANSMIT_BINARY));
        //ctx.writeAndFlush(makeMessage(IAC, SB, TERMINAL_TYPE, TerminalType.SEND, IAC, SE));
        state = State.AwaitingWillTransmitBinary;
        break;// w w w . j  ava 2 s .c  om

    case AwaitingWillTransmitBinary:
        if (msg.equals(makeMessage(IAC, WILL, TRANSMIT_BINARY))) {
            ctx.writeAndFlush(makeMessage(IAC, WILL, CHARSET));
            state = State.AwaitingWillNegotiateCharset;
        } else {
            state = State.Done;
        }
        break;

    case AwaitingWillNegotiateCharset:

        dumpBuffer("charset!", msg);
        state = State.Done;
        break;

    default:
        ctx.fireChannelRead(msg);

    }
}

From source file:jlibs.wamp4j.netty.NettyWebSocket.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof WebSocketFrame) {
        WebSocketFrame frame = (WebSocketFrame) msg;
        try {/*from  w w  w . jav a  2  s  .  c  o m*/
            if (frame instanceof TextWebSocketFrame || frame instanceof BinaryWebSocketFrame) {
                if (listener != null) {
                    MessageType type = frame instanceof TextWebSocketFrame ? MessageType.text
                            : MessageType.binary;
                    is.reset(frame.content());
                    listener.onMessage(this, type, is);
                }
            } else if (frame instanceof PingWebSocketFrame)
                ctx.write(new PongWebSocketFrame(frame.content().retain()));
            else if (frame instanceof CloseWebSocketFrame)
                handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
        } finally {
            frame.release();
        }
    } else
        ctx.fireChannelRead(msg);
}

From source file:jp.ac.keio.sfc.ht.memsys.ghost.server.GhostRequestServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    GhostRequest m = (GhostRequest) msg;
    GhostResponse res = null;/* www. j av  a 2 s.c  om*/
    if (m.TYPE.equals(GhostRequestTypes.INIT)) {
        String appId = gateway.registerApplication(m.PARAMS.getData(BundleKeys.APP_NAME));
        Bundle bundle = new Bundle();
        bundle.putData(BundleKeys.APP_ID, appId);
        res = new GhostResponse(GhostResponseTypes.SUCCESS, GhostRequestTypes.INIT, bundle);
    } else if (m.TYPE.equals(GhostRequestTypes.REGISTERTASK)) {
        Timeout timeout = new Timeout(20, TimeUnit.SECONDS);
        Future<Object> f = gateway.registerTask(m);
        GhostResponse result = (GhostResponse) Await.result(f, timeout.duration());

        res = new GhostResponse(GhostResponseTypes.SUCCESS, GhostRequestTypes.REGISTERTASK, null);
    } else if (m.TYPE.equals(GhostRequestTypes.EXECUTE)) {

        Future<Object> f = gateway.executeTask(m);
        ctx.fireUserEventTriggered((Object) f);

        //GhostResponse result = (GhostResponse) Await.result(f, timeout.duration());
        //res = new GhostResponse(GhostResponseTypes.SUCCESS, GhostRequestTypes.EXECUTE, null);
    } else {
        System.out.println("[Ghost Request Server Handler] UNKNOWN REQUEST");
    }
    if (res != null) {
        ctx.fireChannelRead(res);
    }
}

From source file:me.ferrybig.p2pnetwork.codec.PacketPreProcessor.java

@Override
protected void channelRead0(ChannelHandlerContext ctx, Packet msg) throws Exception {
    try {/*from   w w w  . j a v  a 2 s  . c  o m*/
        Address from;
        Function<Packet, ChannelFuture> reply;
        if (msg instanceof RelayPacket) {
            RelayPacket relayPacket = (RelayPacket) msg;
            int packetId = relayPacket.getData().readInt();
            from = relayPacket.getFrom();
            msg = PacketMap.readPacket(packetId, relayPacket.getData());
            relayPacket.release(); // This is a safe release
            reply = p -> {
                ByteBuf wrapped = ctx.alloc().buffer();
                try {
                    wrapped.writeInt(PacketMap.getPacketId(p));
                    p.write(wrapped);
                    return ctx.writeAndFlush(
                            new RelayPacket(wrapped.retain(), relayPacket.getAddress(), from, 255));
                } finally {
                    p.release();
                    wrapped.release();
                }
            };
        } else {
            from = connection;
            reply = ctx::writeAndFlush;
        }
        ctx.fireChannelRead(new ProcessedPacket(from, ctx.channel(), (Packet) msg.retain(), reply));
    } finally {
        msg.release();
    }
}

From source file:me.ferrybig.p2pnetwork.codec.PacketRoutingHandler.java

@Override
protected void channelRead0(ChannelHandlerContext ctx, RelayPacket msg) throws Exception {
    if (msg.getAddress().equals(ourself)) {
        ctx.fireChannelRead(msg.retain());
        return;/* w w  w .  ja  va  2 s. co m*/
    }
    if (msg.getTtl() == 0) {
        DestinationUnreachablePacket result = new DestinationUnreachablePacket(
                DestinationUnreachablePacket.Reason.TTL_EXPIRED, msg.getAddress());
        try {

            ByteBuf buf = ctx.alloc().buffer();
            buf.writeInt(PacketMap.getPacketId(result));
            result.write(buf);
            ctx.writeAndFlush(new RelayPacket(buf, ourself, msg.getFrom(), 127));
        } finally {
            result.release();
        }
        return;
    }
    msg.decrementTTL();
    if (!router.apply(msg)) {
        DestinationUnreachablePacket result = new DestinationUnreachablePacket(
                DestinationUnreachablePacket.Reason.DESTINATION_HOST_UNREACHABLE, msg.getAddress());
        try {
            ByteBuf buf = ctx.alloc().buffer();
            result.write(buf);
            ctx.write(buf);
            ctx.writeAndFlush(new RelayPacket(buf));
        } finally {
            result.release();
        }
    }
}

From source file:me.netty.http.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *///from ww  w .ja v  a  2  s  .  c o  m
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();

    p.addLast(sourceCodec);
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null, new Http1Handler());
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:mmo.server.RouteHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
    String[] path = PATH_SEP.split(request.getUri(), -1);
    for (int i = 0; i < path.length; ++i) {
        path[i] = URI.create(path[i]).getPath();
    }/*from ww  w.j ava 2 s  . c  om*/

    String first = path.length == 1 ? "" : path[1];

    switch (first) {
    case "":
        installHandler(ctx, defaultHandler);
        break;
    case "status":
        installHandler(ctx, statusHandler);
        break;
    case "room":
        int roomId;
        if (path.length < 3) {
            roomId = 0;
        } else {
            try {
                roomId = Integer.parseInt(path[2]);
            } catch (NumberFormatException e) {
                roomId = 0;
            }
        }
        installHandler(ctx, roomHandlerFactory.create(roomId));
        break;
    case "game":
        installWebSocketHandler(ctx, path.length > 2 ? path[2] : null, request);
        return;
    default:
        installHandler(ctx, pageNotFoundHandler);
    }

    ctx.fireChannelRead(request);
}

From source file:mmo.server.RouteHandler.java

License:Open Source License

private void installWebSocketHandler(ChannelHandlerContext ctx, String playerName, FullHttpRequest request) {
    L.debug("upgrading request {} to websocket", request.getUri());

    if (playerName == null || playerName.isEmpty()) {
        playerName = "anonymous";
    }/*from   w  ww  . j  a va 2  s  .co  m*/

    // we replace all handlers, web socket will not be downgraded
    while (ctx.pipeline().last() != this) {
        ctx.pipeline().removeLast();
    }

    // add websocket handlers
    ctx.pipeline().addLast(new WebSocketServerProtocolHandler(request.getUri()),
            webSocketMessageHandlerProvider.get(), messageReceiverFactory.create(new Player(playerName)));

    // do handshake
    ctx.fireChannelRead(request);

    // route handler is not needed either
    ctx.pipeline().remove(this);
}

From source file:nenea.client.operation.UptimeClientHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    ctx.fireChannelRead(msg);
}