List of usage examples for io.netty.channel ChannelHandlerContext channel
Channel channel();
From source file:NettyInboundHttpTargetHandler.java
License:Apache License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace();// w w w . ja va 2 s . c o m NettyHttpTransportSourceHandler.closeOnFlush(ctx.channel()); }
From source file:NettyHttpTransportSourceHandler.java
License:Apache License
/** * activating registered handler to accept events. * * @param ctx/*from w ww . j a v a 2s. c o m*/ * @throws Exception */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { final Channel inboundChannel = ctx.channel(); Bootstrap b = new Bootstrap(); b.group(inboundChannel.eventLoop()).channel(ctx.channel().getClass()); b.handler(new NettyTargetHandlerInitilizer(inboundChannel)).option(ChannelOption.AUTO_READ, false); b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); b.option(ChannelOption.TCP_NODELAY, true); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 15000); b.option(ChannelOption.SO_SNDBUF, 1048576); b.option(ChannelOption.SO_RCVBUF, 1048576); ChannelFuture f = b.connect(NettyHttpListner.HOST, NettyHttpListner.HOST_PORT); outboundChannel = f.channel(); f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { // connection complete start to read first data inboundChannel.read(); } else { // Close the connection if the connection attempt has failed. inboundChannel.close(); } } }); }
From source file:NettyHttpTransportSourceHandler.java
License:Apache License
/** * receiving events through netty.//from w ww .ja v a 2 s .c o m * * @param ctx * @param msg * @throws Exception */ @Override public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception { if (outboundChannel.isActive()) { outboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { // was able to flush out data, start to read the next chunk ctx.channel().read(); } else { future.channel().close(); } } }); } else { // System.out.println("Outbound Channel Not Active"); } }
From source file:NettyHttpTransportSourceHandler.java
License:Apache License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // System.out.println("Exception caught"); cause.printStackTrace();//w w w. j a v a 2 s . co m closeOnFlush(ctx.channel()); }
From source file:adalightserver.http.HttpServer.java
License:Apache License
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // res.headers().set("Access-Control-Allow-Methods", "POST, OPTIONS, GET"); // res.headers().set("Access-Control-Allow-Origin", "*"); // res.headers().set("Access-Control-Allow-Headers", "*"); // Generate an error page if response getStatus code is not OK (200). if (res.getStatus().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);//from w w w .ja v a 2 s .co m buf.release(); HttpHeaders.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:adapter.server.AdapterHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, Global.GlobalCommandMessage globalCommandMessage) throws Exception { // TODO Auto-generated method stub if (globalCommandMessage.hasHeader()) { Logger.DEBUG("Inter-Cluster message recieved from " + globalCommandMessage.getHeader().getNodeId()); }/*from w w w .j a v a 2 s .c om*/ if (globalCommandMessage.hasQuery()) { if (globalCommandMessage.getQuery().getAction() == Storage.Action.GET) { GlobalCommandMessage response = QueryMessageHandler.getHandler(globalCommandMessage); ctx.channel().writeAndFlush(response); } else if (globalCommandMessage.getQuery().getAction() == Storage.Action.STORE) { GlobalCommandMessage response = QueryMessageHandler.postHandler(globalCommandMessage); ctx.channel().writeAndFlush(response); } } if (globalCommandMessage.hasResponse()) { Logger.DEBUG("Inter-Cluster message response from " + globalCommandMessage.getHeader().getNodeId()); if (globalCommandMessage.getResponse().getAction() == Storage.Action.GET) { ByteString imageByteString = globalCommandMessage.getResponse().getData(); byte[] imageArray = imageByteString.toByteArray(); System.out.write(imageArray); // BufferedImage imag = ImageIO.read(new ByteArrayInputStream(imageArray)); // ImageIO.write(imag, "jpg",new File(outputPath, "received.jpg")); } else if (globalCommandMessage.getResponse().getAction() == Storage.Action.STORE) { String key = globalCommandMessage.getResponse().getKey(); System.out.println("The Key recieved from adapter layer is" + key); } } }
From source file:alluxio.network.protocol.RPCMessageDecoder.java
License:Apache License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { LOG.error("Error in decoding message. Possible Client/DataServer version incompatibility: " + cause.getMessage());//w w w.j a v a 2 s .co m // Return an error message to the client. ctx.channel().writeAndFlush(new RPCErrorResponse(RPCResponse.Status.DECODE_ERROR)) .addListener(ChannelFutureListener.CLOSE); }
From source file:alluxio.worker.netty.AbstractReadHandler.java
License:Apache License
@Override public void channelUnregistered(ChannelHandlerContext ctx) { // The channel is closed so the client cannot receive this message. setError(ctx.channel(), new Error(new InternalException("Channel has been unregistered"), false)); ctx.fireChannelUnregistered();/* w w w. jav a 2 s.c o m*/ }
From source file:alluxio.worker.netty.AbstractReadHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception { if (!acceptMessage(object)) { ctx.fireChannelRead(object);//from ww w . j a v a 2 s .co m return; } Protocol.ReadRequest msg = ((RPCProtoMessage) object).getMessage().asReadRequest(); if (msg.getCancel()) { setCancel(ctx.channel()); return; } // Expected state: context equals null as this handler is new for request, or the previous // context is not active (done / cancel / abort). Otherwise, notify the client an illegal state. // Note that, we reset the context before validation msg as validation may require to update // error in context. try (LockResource lr = new LockResource(mLock)) { Preconditions.checkState(mContext == null || !mContext.isPacketReaderActive()); mContext = createRequestContext(msg); } validateReadRequest(msg); try (LockResource lr = new LockResource(mLock)) { mContext.setPosToQueue(mContext.getRequest().getStart()); mContext.setPosToWrite(mContext.getRequest().getStart()); mPacketReaderExecutor.submit(createPacketReader(mContext, ctx.channel())); mContext.setPacketReaderActive(true); } }
From source file:alluxio.worker.netty.AbstractReadHandler.java
License:Apache License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { LOG.error("Exception caught in AbstractReadHandler for channel {}:", ctx.channel(), cause); setError(ctx.channel(), new Error(AlluxioStatusException.fromThrowable(cause), true)); }