Example usage for io.netty.channel ChannelHandlerContext flush

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

Introduction

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

Prototype

@Override
    ChannelHandlerContext flush();

Source Link

Usage

From source file:org.jooby.Netty.java

License:Apache License

protected static ChannelHandler handler() {
    byte[] helloWorld = "Hello World".getBytes(StandardCharsets.UTF_8);
    return new ChannelInboundHandlerAdapter() {
        @Override/*  w w w .  j ava2s . c o  m*/
        public void channelReadComplete(final ChannelHandlerContext ctx) {
            ctx.flush();
        }

        @Override
        public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
            if (msg instanceof HttpRequest) {
                HttpRequest req = (HttpRequest) msg;

                if (HttpHeaders.is100ContinueExpected(req)) {
                    ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
                }
                boolean keepAlive = HttpHeaders.isKeepAlive(req);
                FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK,
                        Unpooled.wrappedBuffer(helloWorld));
                response.headers().set(CONTENT_TYPE, "text/plain");
                response.headers().set(CONTENT_LENGTH, response.content().readableBytes());

                if (!keepAlive) {
                    ctx.write(response).addListener(ChannelFutureListener.CLOSE);
                } else {
                    response.headers().set(CONNECTION, Values.KEEP_ALIVE);
                    ctx.write(response);
                }
            }

        }

        @Override
        public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
            cause.printStackTrace();
            ctx.close();
        }
    };
}

From source file:org.kaaproject.kaa.server.transports.tcp.transport.netty.KaaTcpDecoder.java

License:Apache License

public void channelReadCompete(ChannelHandlerContext ctx) throws Exception { // NOSONAR
    ctx.flush();
}

From source file:org.l2junity.gameserver.network.telnet.TelnetServerHandler.java

License:Open Source License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    String ip = ctx.channel().remoteAddress().toString();
    ip = ip.substring(1, ip.lastIndexOf(':')); // Trim out /127.0.0.1:14013

    if (!Config.TELNET_HOSTS.contains(ip)) {
        final ChannelFuture future = ctx
                .write("Your ip: " + ip + " is not allowed to connect." + System.lineSeparator());
        future.addListener(ChannelFutureListener.CLOSE);
        ctx.flush();
        return;/*from  ww w  .j ava2  s  . com*/
    }

    // Send greeting for a new connection.
    ctx.write("Welcome to the L2J Unity telnet session." + System.lineSeparator());
    ctx.write("It is " + new Date() + " now." + System.lineSeparator());
    ctx.write("Please enter your password:" + System.lineSeparator());
    if (!Config.TELNET_PASSWORD.isEmpty()) {
        // Ask password
        ctx.write("Password:");
        ctx.attr(AUTHORIZED).set(Boolean.FALSE);
    } else {
        ctx.write("Type 'help' to see all available commands." + System.lineSeparator());
        ctx.attr(AUTHORIZED).set(Boolean.TRUE);
    }
    ctx.flush();
}

From source file:org.opendaylight.controller.netconf.netty.EchoServerHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf byteBuf = (ByteBuf) msg;/*  www .j  av a2s .c om*/
    String message = byteBuf.toString(Charsets.UTF_8);
    LOG.info("writing back '{}'", message);
    ctx.write(msg);
    fromLastNewLine += message;
    for (String line : splitter.split(fromLastNewLine)) {
        if ("quit".equals(line)) {
            LOG.info("closing server ctx");
            ctx.flush();
            ctx.close();
            break;
        }
        fromLastNewLine = line; // last line should be preserved
    }

    // do not release byteBuf as it is handled back
}

From source file:org.opendaylight.controller.netconf.netty.EchoServerHandler.java

License:Open Source License

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    LOG.debug("flushing");
    ctx.flush();
}

From source file:org.opendaylight.usc.test.EchoServerTcpHandler.java

License:Open Source License

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    LOG.trace("channelReadComplete");
    ctx.flush();
}

From source file:org.restexpress.pipeline.DefaultRequestHandler.java

License:Apache License

@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    ctx.flush();
    super.channelReadComplete(ctx);
}

From source file:org.rzo.netty.ahessian.log.OutLogger.java

License:Apache License

@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
    if (_stateOnly) {
        ctx.flush();
        return;//w w  w .j ava2 s  .  c  om
    }
    super.flush(ctx);

}

From source file:org.teiid.transport.ObjectEncoder.java

License:Apache License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    ByteBuf out = allocateBuffer(ctx, this.estimatedLength, this.preferDirect);
    int startIdx = out.writerIndex();
    ByteBufOutputStream bout = new ByteBufOutputStream(out);
    bout.write(LENGTH_PLACEHOLDER);/*from w  ww  . ja  va  2 s.co  m*/
    final CompactObjectOutputStream oout = new CompactObjectOutputStream(bout);
    try {
        oout.writeObject(msg);
        ExternalizeUtil.writeCollection(oout, oout.getReferences());
        oout.flush();
        oout.close();

        int endIdx = out.writerIndex();
        out.setInt(startIdx, endIdx - startIdx - 4);

        if (out.isReadable()) {
            ctx.write(out, promise);
            for (InputStream is : oout.getStreams()) {
                ctx.write(new AnonymousChunkedStream(new BufferedInputStream(is, CHUNK_SIZE)), promise);
            }
        } else {
            out.release();
            ctx.write(Unpooled.EMPTY_BUFFER, promise);
        }
        ctx.flush();
        out = null;
    } catch (Throwable t) {
        throw new FailedWriteException(msg, t);
    } finally {
        if (out != null) {
            out.release();
        }
    }
}

From source file:org.tiger.netty.http.websocket.server.WebSocketServerHandler.java

License:Apache License

private void handleWebSocketFrame(final ChannelHandlerContext ctx, WebSocketFrame frame) {

    // ?//w w w. j av  a2s .c  o m
    if (frame instanceof CloseWebSocketFrame) {
        handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
        return;
    }
    // ?Ping?
    if (frame instanceof PingWebSocketFrame) {
        ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
        return;
    }
    // ?????
    if (!(frame instanceof TextWebSocketFrame)) {
        throw new UnsupportedOperationException(
                String.format("%s frame types not supported", frame.getClass().getName()));
    }

    // ?
    final String request = ((TextWebSocketFrame) frame).text();
    if (logger.isLoggable(Level.FINE)) {
        logger.fine(String.format("%s received %s", ctx.channel(), request));
    }

    new Thread() {
        @Override
        public void run() {
            while (true) {
                ctx.channel().write(new TextWebSocketFrame("" + new Date().toString()));
                ctx.flush();//?flush??
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
    /*ctx.channel().write(
       new TextWebSocketFrame(request
          + " , Netty WebSocket?"
          + new java.util.Date().toString()));*/
}