List of usage examples for io.netty.channel ChannelHandlerContext flush
@Override ChannelHandlerContext flush();
From source file:com.android.tools.idea.diagnostics.crash.LocalTestServer.java
License:Apache License
public void start() throws Exception { ServerBootstrap b = new ServerBootstrap(); myEventLoopGroup = new OioEventLoopGroup(); b.group(myEventLoopGroup).channel(OioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/*w w w.j av a 2s . co m*/ protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpServerCodec()); // Note: Netty's decompressor uses jcraft jzlib, which is not exported as a library // p.addLast(new HttpContentDecompressor()); p.addLast(new HttpObjectAggregator(32 * 1024)); // big enough to collect a full thread dump p.addLast(new ChannelInboundHandlerAdapter() { @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (!(msg instanceof FullHttpRequest)) { return; } FullHttpResponse response = myResponseSupplier.apply((FullHttpRequest) msg); response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain"); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()); ctx.write(response).addListener(ChannelFutureListener.CLOSE); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.write(cause.toString()).addListener(ChannelFutureListener.CLOSE); } }); } }); myChannel = b.bind(myPort).sync().channel(); }
From source file:com.antsdb.saltedfish.server.mysql.MysqlServerHandler.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (_log.isTraceEnabled()) { _log.trace(msg.toString());// ww w .j a va2 s .c o m } // process the request with error handling try { run(ctx, msg); } catch (Exception x) { if (_log.isDebugEnabled()) { _log.debug("error detail: \n{}", msg, x); } String error = x.getMessage() == null ? x.toString() : x.getMessage(); writeErrMessage(ctx, MysqlErrorCode.ER_ERROR_WHEN_EXECUTING_COMMAND, error); } // flush and finish ctx.flush(); if (_log.isTraceEnabled()) { _log.trace("end"); } }
From source file:com.baidu.jprotobuf.pbrpc.management.HttpServerInboundHandler.java
License:Apache License
private void writeResponse(ChannelHandlerContext ctx, String content) throws Exception { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(content.getBytes("UTF-8"))); response.headers().set(CONTENT_TYPE, "text/html"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); if (HttpHeaders.isKeepAlive(request)) { response.headers().set(CONNECTION, Values.KEEP_ALIVE); }/*from w ww. j a v a2s. co m*/ ctx.write(response); ctx.flush(); }
From source file:com.baidu.rigel.biplatform.ma.file.client.service.impl.FileServerClient.java
License:Open Source License
/** * ?// w w w. jav a 2s.c om * * @param server * ?? * @param port * ?? * @param request * * @return */ public Response doRequest(String server, int port, final Request request) { EventLoopGroup work = new NioEventLoopGroup(1); String message = null; try { final Response rs = new Response(ResponseStatus.FAIL, "failed", null); ChannelHandlerAdapter requestHandler = new ChannelHandlerAdapter() { /** * {@inheritDoc} */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { logger.info("successfully connect to file server"); ctx.write(request); ctx.flush(); } /** * {@inheritDoc} */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { logger.info("successfuly recieve message from file server {}", msg); Response tmpRs = (Response) msg; rs.setDatas(tmpRs.getDatas()); rs.setMessage(tmpRs.getMessage()); rs.setStatus(tmpRs.getStatus()); ctx.close(); } /** * {@inheritDoc} */ @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } /** * {@inheritDoc} */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { logger.error(cause.getMessage()); rs.setMessage(cause.getMessage()); rs.setStatus(ResponseStatus.FAIL); ctx.close(); } }; Bootstrap strap = new Bootstrap(); strap.group(work).option(ChannelOption.TCP_NODELAY, true).channel(NioSocketChannel.class) .handler(new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(NioSocketChannel chl) throws Exception { // ?? chl.pipeline().addLast(new ObjectDecoder( ClassResolvers.cacheDisabled(requestHandler.getClass().getClassLoader()))); chl.pipeline().addLast(new ObjectEncoder()); chl.pipeline().addLast(requestHandler); } }); long begin = System.currentTimeMillis(); logger.debug("Begin invoke do file operation request ... ..."); ChannelFuture future = strap.connect(server, port); future.channel().closeFuture().sync(); logger.debug( "Success execute request option cost time: " + (System.currentTimeMillis() - begin) + "ms"); return rs; } catch (InterruptedException e) { logger.error(e.getMessage(), e); message = e.getMessage(); } finally { work.shutdownGracefully(); } Response rs = new Response(ResponseStatus.FAIL, message, null); return rs; }
From source file:com.barchart.netty.common.pipeline.PingHandler.java
License:BSD License
@Override public void channelRead0(final ChannelHandlerContext ctx, final Object msg) throws Exception { if (msg instanceof Ping) { ctx.write(new Pong() { @Override/*from www . ja v a 2s. c o m*/ public long timestamp() { return System.currentTimeMillis(); } @Override public long pinged() { return ((Ping) msg).timestamp(); } }); ctx.flush(); } else if (msg instanceof Pong) { final Pong pong = (Pong) msg; // Compare the peer-received timestamp to current final long now = System.currentTimeMillis(); latency = (now - pong.pinged()) / 2; clockSkew = pong.timestamp() - (now - latency); latencySampler.update(latency); } // Send upstream ctx.fireChannelRead(msg); }
From source file:com.barchart.netty.common.pipeline.PingHandler.java
License:BSD License
public void sendPing(final ChannelHandlerContext ctx) { try {//from w w w.j a v a2s . c om if (pingFuture != null && !pingFuture.isDone()) { pingFuture.cancel(false); } ctx.write(new Ping() { @Override public long timestamp() { return System.currentTimeMillis(); } }); ctx.flush(); } finally { if (interval > 0) { pingFuture = ctx.channel().eventLoop().schedule(new Runnable() { @Override public void run() { sendPing(ctx); } }, interval, unit); } } }
From source file:com.base.research.socket.netty.ClientHandler.java
License:Apache License
@Override public void channelActive(final ChannelHandlerContext ctx) throws Exception { long timeMillis = System.currentTimeMillis(); ctx.write(timeMillis);// w w w .ja va2s . co m ctx.flush(); }
From source file:com.base.research.socket.netty.ServerHandler.java
License:Apache License
@Override public void channelRead(final ChannelHandlerContext ctx, Object msg) { // (2) Date date = (Date) msg; System.out.println("handle:" + date); ctx.fireChannelRead(date);/* w ww. j ava 2 s . co m*/ // ? long timeMillis = System.currentTimeMillis(); final ChannelFuture f = ctx.write(timeMillis); ctx.flush(); System.out.println("flush"); // final ChannelFuture f = ctx.writeAndFlush(time); // (3) f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { assert f == future; ctx.close(); System.out.println("close-----------------"); } }); }
From source file:com.bt.netty.TelnetServerHandler.java
License:Apache License
@Override 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.cdg.study.netty.echo.EchoServerHandler.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext context, Object msg) { context.write(msg); context.flush(); }