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:com.google.devtools.build.lib.remote.blobstore.http.AbstractHttpHandler.java

License:Open Source License

@SuppressWarnings("FutureReturnValueIgnored")
@Override
public void flush(ChannelHandlerContext ctx) {
    ctx.flush();
}

From source file:com.gxkj.demo.netty.echo.EchoClientHandler.java

License:Apache License

/**
 * ???/*ww  w . j ava2 s  .co  m*/
 */
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    ctx.flush();
    logger.info("?");
}

From source file:com.gxkj.demo.netty.echo.EchoServerHandler.java

License:Apache License

/**
 * ???/*from  w  w w .j a  va2s.  c om*/
 */
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    ctx.flush();
    logger.info("read end");
}

From source file:com.gxkj.demo.netty.telnet.TelnetServerHandler.java

License:Apache License

/**
 * ?/*from  w w w . ja v a2s .com*/
 */
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    // Send greeting for a new connection.
    ctx.write("???Welcome to " + InetAddress.getLocalHost().getHostName() + "!\r\n");
    ctx.write("???It is " + new Date() + " now.\r\n");
    ctx.flush();
}

From source file:com.heliosapm.tsdblite.handlers.http.HttpStaticFileServerHandler.java

License:Open Source License

private void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
    if (!request.decoderResult().isSuccess()) {
        sendError(ctx, BAD_REQUEST);/*  w  ww  . jav  a 2s  .c  om*/
        return;
    }

    if (request.method() != GET) {
        sendError(ctx, METHOD_NOT_ALLOWED);
        return;
    }

    final String uri = request.uri().replace("/api/s", "/");
    final String path = uri;
    if (path == null) {
        sendError(ctx, FORBIDDEN);
        return;
    }

    File file = new File(staticRootDir, path);
    if (file.isHidden() || !file.exists()) {
        sendError(ctx, NOT_FOUND);
        return;
    }

    if (file.isDirectory()) {
        if (uri.endsWith("/")) {
            sendListing(ctx, file);
        } else {
            sendRedirect(ctx, uri + '/');
        }
        return;
    }

    if (!file.isFile()) {
        sendError(ctx, FORBIDDEN);
        return;
    }

    // Cache Validation
    String ifModifiedSince = request.headers().getAsString(IF_MODIFIED_SINCE);
    if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
        SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
        Date ifModifiedSinceDate = dateFormatter.parse(ifModifiedSince);

        // Only compare up to the second because the datetime format we send to the client
        // does not have milliseconds
        long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000;
        long fileLastModifiedSeconds = file.lastModified() / 1000;
        if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) {
            sendNotModified(ctx);
            return;
        }
    }

    RandomAccessFile raf;
    try {
        raf = new RandomAccessFile(file, "r");
    } catch (FileNotFoundException ignore) {
        sendError(ctx, NOT_FOUND);
        return;
    }
    long fileLength = raf.length();

    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    HttpUtil.setContentLength(response, fileLength);
    setContentTypeHeader(response, file);
    setDateAndCacheHeaders(response, file);
    if (HttpUtil.isKeepAlive(request)) {
        response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }

    // Write the initial line and the header.
    ctx.write(response);

    // Write the content.
    ChannelFuture sendFileFuture;
    ChannelFuture lastContentFuture;
    if (ctx.pipeline().get(SslHandler.class) == null) {
        sendFileFuture = ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength),
                ctx.newProgressivePromise());
        // Write the end marker.
        lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
    } else {
        sendFileFuture = ctx.write(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)),
                ctx.newProgressivePromise());
        // HttpChunkedInput will write the end marker (LastHttpContent) for us.
        lastContentFuture = sendFileFuture;
    }

    sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
        @Override
        public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
            if (total < 0) { // total unknown
                System.err.println(future.channel() + " Transfer progress: " + progress);
            } else {
                System.err.println(future.channel() + " Transfer progress: " + progress + " / " + total);
            }
        }

        @Override
        public void operationComplete(ChannelProgressiveFuture future) {
            System.err.println(future.channel() + " Transfer complete.");
        }
    });

    // Decide whether to close the connection or not.
    if (!HttpUtil.isKeepAlive(request)) {
        // Close the connection when the whole content is written out.
        lastContentFuture.addListener(ChannelFutureListener.CLOSE);
    }
    ctx.flush();
}

From source file:com.hop.hhxx.example.http2.helloworld.server.HelloWorldHttp2Handler.java

License:Apache License

/**
 * Sends a "Hello World" DATA frame to the client.
 *///from  w w w . j a v a 2 s.  com
private void sendResponse(ChannelHandlerContext ctx, int streamId, ByteBuf payload) {
    // Send a frame for the response status
    Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
    encoder().writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise());
    encoder().writeData(ctx, streamId, payload, 0, true, ctx.newPromise());
    ctx.flush();
}

From source file:com.hxr.javatone.concurrency.netty.official.echo.EchoClientHandler.java

License:Apache License

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

From source file:com.ict.twitter.netty.server.SendTaskClientHandler.java

License:Apache License

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

From source file:com.l2jmobius.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." + Config.EOL);
        future.addListener(ChannelFutureListener.CLOSE);
        ctx.flush();
        return;//from   w ww .j a v  a2  s.  c om
    }

    // Send greeting for a new connection.
    ctx.write("Welcome to the telnet session." + Config.EOL);
    ctx.write("It is " + new Date() + " now." + Config.EOL);
    ctx.write("Please enter your password:" + Config.EOL);
    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." + Config.EOL);
        ctx.attr(AUTHORIZED).set(Boolean.TRUE);
    }
    ctx.flush();
}

From source file:com.lb.netty.protoc.SubReqClientHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    long l = System.currentTimeMillis();
    System.out.println("start : " + l);
    for (int i = 0; i < 100; i++) {
        ctx.write(subReq(i));/*from www. j ava 2 s .  com*/
    }
    System.out.println("end : " + (System.currentTimeMillis() - l));
    ctx.flush();
}