List of usage examples for io.netty.channel ChannelHandlerContext channel
Channel channel();
From source file:alluxio.worker.netty.AbstractWriteHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception { if (!acceptMessage(object)) { ctx.fireChannelRead(object);/*from w w w . ja v a2 s. c o m*/ return; } RPCProtoMessage msg = (RPCProtoMessage) object; Protocol.WriteRequest writeRequest = msg.getMessage().asWriteRequest(); try (LockResource lr = new LockResource(mLock)) { boolean isNewContextCreated = false; if (mContext == null || mContext.isDoneUnsafe()) { // We create a new context if the previous request completes (done flag is true) or the // context is still null (an empty channel so far). And in this case, we create a new one as // catching exceptions and replying errors // leverages data structures in context, regardless of the request is valid or not. // TODO(binfan): remove the dependency on an instantiated request context which is required // to reply errors to client side. mContext = createRequestContext(writeRequest); isNewContextCreated = true; } // Only initialize (open the writers) if this is the first packet in the block/file. if (writeRequest.getOffset() == 0) { // 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. Preconditions.checkState(isNewContextCreated); initRequestContext(mContext); } // If we have seen an error, return early and release the data. This can // happen for (1) those mis-behaving clients who first sends some invalid requests, then // then some random data, or (2) asynchronous requests arrive after the previous request fails // and triggers abortion. It can leak memory if we do not release buffers here. if (mContext.getError() != null) { if (msg.getPayloadDataBuffer() != null) { msg.getPayloadDataBuffer().release(); } LOG.warn("Ignore the request {} due to the error {} on context", mContext.getRequest(), mContext.getError()); return; } else { // Validate the write request. The validation is performed only when no error is in the // context in order to prevent excessive logging on the subsequent arrived asynchronous // requests after a previous request fails and triggers the abortion validateWriteRequest(writeRequest, msg.getPayloadDataBuffer()); } ByteBuf buf; if (writeRequest.getEof()) { buf = EOF; } else if (writeRequest.getCancel()) { buf = CANCEL; } else { DataBuffer dataBuffer = msg.getPayloadDataBuffer(); Preconditions.checkState(dataBuffer != null && dataBuffer.getLength() > 0); Preconditions.checkState(dataBuffer.getNettyOutput() instanceof ByteBuf); buf = (ByteBuf) dataBuffer.getNettyOutput(); mContext.setPosToQueue(mContext.getPosToQueue() + buf.readableBytes()); } if (!mContext.isPacketWriterActive()) { mContext.setPacketWriterActive(true); mPacketWriterExecutor.submit(createPacketWriter(mContext, ctx.channel())); } mContext.getPackets().offer(buf); if (tooManyPacketsInFlight()) { NettyUtils.disableAutoRead(ctx.channel()); } } }
From source file:alluxio.worker.netty.AbstractWriteHandler.java
License:Apache License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { LOG.error("Exception caught in AbstractWriteHandler for channel {}:", ctx.channel(), cause); pushAbortPacket(ctx.channel(), new Error(AlluxioStatusException.fromThrowable(cause), true)); }
From source file:alluxio.worker.netty.AbstractWriteHandler.java
License:Apache License
@Override public void channelUnregistered(ChannelHandlerContext ctx) { pushAbortPacket(ctx.channel(), new Error(new InternalException("channel unregistered"), false)); ctx.fireChannelUnregistered();//w ww .java2 s . c om }
From source file:alluxio.worker.netty.DataServerReadHandler.java
License:Apache License
@Override public void channelUnregistered(ChannelHandlerContext ctx) { setError(ctx.channel(), new Error(null, false, Protocol.Status.Code.INTERNAL)); ctx.fireChannelUnregistered();//from www .j ava 2 s . c om }
From source file:alluxio.worker.netty.DataServerReadHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception { if (!acceptMessage(object)) { ctx.fireChannelRead(object);/* w w w .j a v a 2 s . c o m*/ return; } Protocol.ReadRequest msg = ((RPCProtoMessage) object).getMessage().getMessage(); if (msg.getCancel()) { setCancel(ctx.channel()); return; } reset(); String error = validateReadRequest(msg); if (!error.isEmpty()) { setError(ctx.channel(), new Error(new IllegalArgumentException(error), true, Protocol.Status.Code.INVALID_ARGUMENT)); return; } initializeRequest(msg); try (LockResource lr = new LockResource(mLock)) { mPosToQueue = mRequest.mStart; mPosToWrite = mRequest.mStart; mPacketReaderExecutor.submit(new PacketReader(ctx.channel())); mPacketReaderActive = true; } }
From source file:alluxio.worker.netty.DataServerReadHandler.java
License:Apache License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { LOG.error("Exception caught {} in BlockReadDataServerHandler.", cause); setError(ctx.channel(), new Error(cause, true, Protocol.Status.Code.INTERNAL)); }
From source file:alluxio.worker.netty.DataServerWriteHandler.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 .c om return; } RPCProtoMessage msg = (RPCProtoMessage) object; Protocol.WriteRequest writeRequest = msg.getMessage().getMessage(); // Only initialize (open the readers) if this is the first packet in the block/file. if (writeRequest.getOffset() == 0) { initializeRequest(msg); } // Validate msg and return error if invalid. Init variables if necessary. String error = validateRequest(msg); if (!error.isEmpty()) { pushAbortPacket(ctx.channel(), new Error(new IllegalArgumentException(error), true, Protocol.Status.Code.INVALID_ARGUMENT)); return; } try (LockResource lr = new LockResource(mLock)) { // If we have seen an error, return early and release the data. This can only // happen for those mis-behaving clients who first sends some invalid requests, then // then some random data. It can leak memory if we do not release buffers here. if (mError != null) { if (msg.getPayloadDataBuffer() != null) { msg.getPayloadDataBuffer().release(); } return; } ByteBuf buf; if (writeRequest.getEof()) { buf = EOF; } else if (writeRequest.getCancel()) { buf = CANCEL; } else { DataBuffer dataBuffer = msg.getPayloadDataBuffer(); Preconditions.checkState(dataBuffer != null && dataBuffer.getLength() > 0); assert dataBuffer.getNettyOutput() instanceof ByteBuf; buf = (ByteBuf) dataBuffer.getNettyOutput(); mPosToQueue += buf.readableBytes(); } if (!mPacketWriterActive) { mPacketWriterActive = true; mPacketWriterExecutor.submit(new PacketWriter(ctx.channel())); } mPackets.offer(buf); if (tooManyPacketsInFlight()) { NettyUtils.disableAutoRead(ctx.channel()); } } }
From source file:alluxio.worker.netty.DataServerWriteHandler.java
License:Apache License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { LOG.error("Failed to write block.", cause); pushAbortPacket(ctx.channel(), new Error(cause, true, Protocol.Status.Code.INTERNAL)); }
From source file:alluxio.worker.netty.DataServerWriteHandler.java
License:Apache License
@Override public void channelUnregistered(ChannelHandlerContext ctx) { pushAbortPacket(ctx.channel(), new Error(null, false, Protocol.Status.Code.INTERNAL)); ctx.fireChannelUnregistered();//from w w w. j av a 2s . c o m }
From source file:app.WebSocketServerHandler.java
License:Apache License
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) { // Handle a bad request. if (!req.decoderResult().isSuccess()) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST)); return;/*w w w. j a va2s .c o m*/ } // Allow only GET methods. if (req.method() != GET) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); return; } // Handshake WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true, 5 * 1024 * 1024); handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { handshaker.handshake(ctx.channel(), req); channels.add(ctx.channel()); } }