Java Code Examples for org.jboss.netty.channel.Channel#isConnected()

The following examples show how to use org.jboss.netty.channel.Channel#isConnected() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IsisLspQueueConsumer.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Process refresh LSP.
 *
 * @param wrapper LSP wrapper instance
 */
private void processRefreshLsp(LspWrapper wrapper) {
    if (wrapper.isSelfOriginated()) { //self originated
        DefaultIsisInterface isisInterface = (DefaultIsisInterface) wrapper.isisInterface();
        Channel channel = isisInterface.channel();
        if (channel != null && channel.isConnected()) {
            LsPdu lsPdu = (LsPdu) wrapper.lsPdu();
            lsPdu.setSequenceNumber(isisInterface.isisLsdb().lsSequenceNumber(
                    IsisPduType.get(lsPdu.pduType())));
            lsPdu.setRemainingLifeTime(IsisConstants.LSPMAXAGE);
            byte[] lspBytes = lsPdu.asBytes();
            lspBytes = IsisUtil.addLengthAndMarkItInReserved(lspBytes, IsisConstants.LENGTHPOSITION,
                    IsisConstants.LENGTHPOSITION + 1,
                    IsisConstants.RESERVEDPOSITION);
            lspBytes = IsisUtil.addChecksum(lspBytes, IsisConstants.CHECKSUMPOSITION,
                    IsisConstants.CHECKSUMPOSITION + 1);
            //write to the channel
            channel.write(IsisUtil.framePacket(lspBytes, isisInterface.interfaceIndex()));
            // Updating the database with resetting remaining life time to default.
            IsisLsdb isisDb = isisInterface.isisLsdb();
            isisDb.addLsp(lsPdu, true, isisInterface);
            log.debug("LSPQueueConsumer: processRefreshLsp - Flooded SelfOriginated LSP {}",
                    wrapper.lsPdu());
        }
    }
}
 
Example 2
Source File: ShuffleHandler.java    From tez with Apache License 2.0 6 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
    throws Exception {
  Channel ch = e.getChannel();
  Throwable cause = e.getCause();
  if (cause instanceof TooLongFrameException) {
    sendError(ctx, BAD_REQUEST);
    return;
  } else if (cause instanceof IOException) {
    if (cause instanceof ClosedChannelException) {
      LOG.debug("Ignoring closed channel error", cause);
      return;
    }
    String message = String.valueOf(cause.getMessage());
    if (IGNORABLE_ERROR_MESSAGE.matcher(message).matches()) {
      LOG.debug("Ignoring client socket close", cause);
      return;
    }
  }

  LOG.error("Shuffle error: ", cause);
  if (ch.isConnected()) {
    LOG.error("Shuffle error " + e);
    sendError(ctx, INTERNAL_SERVER_ERROR);
  }
}
 
Example 3
Source File: NettyChannel.java    From anima with GNU General Public License v3.0 6 votes vote down vote up
static NettyChannel getOrAddChannel(AppConf conf,Channel channel, ChannelHandler handler) {
	if (channel == null) {
		return null;
	}
	NettyChannel ret = channelMap.get(channel);
	if (ret == null) {
		NettyChannel nc = new NettyChannel(conf, channel, handler);
		if (channel.isConnected()) {
			ret = channelMap.putIfAbsent(channel, nc);
		}
		if (ret == null) {
			ret = nc;
		}
	}
	return ret;
}
 
Example 4
Source File: ShuffleHandler.java    From RDFS with Apache License 2.0 6 votes vote down vote up
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
    throws Exception {
  Channel ch = e.getChannel();
  Throwable cause = e.getCause();
  if (cause instanceof TooLongFrameException) {
    sendError(ctx, BAD_REQUEST);
    return;
  }

  LOG.error("Shuffle error: ", cause);
  shuffleMetrics.failedOutput();
  if (ch.isConnected()) {
    LOG.error("Shuffle error " + e);
    sendError(ctx, INTERNAL_SERVER_ERROR);
  }
}
 
Example 5
Source File: PcepMessageDecoder.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel,
        ChannelBuffer buffer) throws Exception {
    log.debug("Message received.");
    if (!channel.isConnected()) {
        log.info("Channel is not connected.");
        // In testing, I see decode being called AFTER decode last.
        // This check avoids that from reading corrupted frames
        return null;
    }

    HexDump.pcepHexDump(buffer);

    // Buffer can contain multiple messages, also may contain out of bound message.
    // Read the message one by one from buffer and parse it. If it encountered out of bound message,
    // then mark the reader index and again take the next chunk of messages from the channel
    // and parse again from the marked reader index.
    PcepMessageReader<PcepMessage> reader = PcepFactories.getGenericReader();
    List<PcepMessage> msgList = (List<PcepMessage>) ctx.getAttachment();

    if (msgList == null) {
        msgList = new LinkedList<>();
    }

    try {
        while (buffer.readableBytes() > 0) {
            buffer.markReaderIndex();
            PcepMessage message = reader.readFrom(buffer);
            msgList.add(message);
        }
        ctx.setAttachment(null);
        return msgList;
    } catch (PcepOutOfBoundMessageException e) {
        log.debug("PCEP message decode error");
        buffer.resetReaderIndex();
        buffer.discardReadBytes();
        ctx.setAttachment(msgList);
    }
    return null;
}
 
Example 6
Source File: HealthCheckManager.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private PinpointServer getPinpointServer(Channel channel) {
    if (channel == null) {
        return null;
    }
    if (!channel.isConnected()) {
        return null;
    }

    Object attachment = channel.getAttachment();
    if (attachment instanceof PinpointServer) {
        return (PinpointServer) attachment;
    } else {
        return null;
    }
}
 
Example 7
Source File: PinpointClientHandshaker.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
public void handshakeStart(Channel channel) {
    logger.info("{} handshakeStart() started. channel:{}", id, channel);
    
    if (channel == null) {
        logger.warn("{} handshakeStart() failed. caused:channel must not be null.", id);
        return;
    }
    
    if (!channel.isConnected()) {
        logger.warn("{} handshakeStart() failed. caused:channel is not connected.", id);
        return;
    }

    if (!state.compareAndSet(STATE_INIT, STATE_STARTED)) {
        logger.warn("{} handshakeStart() failed. caused:unexpected state.", id);
        return;
    }
    
    HandshakeJob handshakeJob = null;
    try {
        handshakeJob = createHandshakeJob(channel);
    } catch (Exception e) {
        logger.warn("{} create HandshakeJob failed. caused:{}", id, e.getMessage(), e);
    }

    if (handshakeJob == null) {
        logger.warn("{} handshakeStart() failed. caused:handshakeJob must not be null.", id);
        handshakeAbort();
        return;
    }

    handshake(handshakeJob);
    reserveHandshake(handshakeJob);
    logger.info("{} handshakeStart() completed. channel:{}, data:{}", id, channel, handshakeData);
}
 
Example 8
Source File: DefaultPinpointClientHandler.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private void sendClosedPacket(Channel channel) {
    if (!channel.isConnected()) {
        logger.debug("{} sendClosedPacket() failed. Error:channel already closed.", objectUniqName);
        return;
    }

    logger.debug("{} sendClosedPacket() started.", objectUniqName);

    ClientClosePacket clientClosePacket = new ClientClosePacket();
    ChannelFuture write = write0(clientClosePacket, sendClosePacketFailFutureListener);
    write.awaitUninterruptibly(3000, TimeUnit.MILLISECONDS);
}
 
Example 9
Source File: ImapChannelUpstreamHandler.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    try (Closeable closeable = IMAPMDCContext.from(ctx, attributes)) {
        imapCommandsMetric.increment();
        ImapSession session = (ImapSession) attributes.get(ctx.getChannel());
        ImapResponseComposer response = (ImapResponseComposer) ctx.getAttachment();
        ImapMessage message = (ImapMessage) e.getMessage();
        ChannelPipeline cp = ctx.getPipeline();

        try {
            if (cp.get(NettyConstants.EXECUTION_HANDLER) != null) {
                cp.addBefore(NettyConstants.EXECUTION_HANDLER, NettyConstants.HEARTBEAT_HANDLER, heartbeatHandler);
            } else {
                cp.addBefore(NettyConstants.CORE_HANDLER, NettyConstants.HEARTBEAT_HANDLER, heartbeatHandler);

            }
            final ResponseEncoder responseEncoder = new ResponseEncoder(encoder, response);
            processor.process(message, responseEncoder, session);

            if (session.getState() == ImapSessionState.LOGOUT) {
                // Make sure we close the channel after all the buffers were flushed out
                Channel channel = ctx.getChannel();
                if (channel.isConnected()) {
                    channel.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
                }
            }
            final IOException failure = responseEncoder.getFailure();

            if (failure != null) {
                LOGGER.info(failure.getMessage());
                LOGGER.debug("Failed to write {}", message, failure);
                throw failure;
            }
        } finally {
            ctx.getPipeline().remove(NettyConstants.HEARTBEAT_HANDLER);
        }

        super.messageReceived(ctx, e);
    }
}
 
Example 10
Source File: DefaultIsisInterface.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Sends LS PDU message to channel.
 *
 * @param lsp     LS PDU message instance
 * @param channel channel instance
 */
private void sendLsp(LsPdu lsp, Channel channel) {
    byte[] lspBytes = lsp.asBytes();
    lspBytes = IsisUtil.addLengthAndMarkItInReserved(lspBytes, IsisConstants.LENGTHPOSITION,
                                                     IsisConstants.LENGTHPOSITION + 1,
                                                     IsisConstants.RESERVEDPOSITION);
    lspBytes = IsisUtil.addChecksum(lspBytes, IsisConstants.CHECKSUMPOSITION,
                                    IsisConstants.CHECKSUMPOSITION + 1);
    //write to the channel
    if (channel != null && channel.isConnected() && channel.isOpen()) {
        channel.write(IsisUtil.framePacket(lspBytes, interfaceIndex));
    }
}
 
Example 11
Source File: NettyClient.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
protected com.alibaba.dubbo.remoting.Channel getChannel() {
    Channel c = channel;
    if (c == null || ! c.isConnected())
        return null;
    return NettyChannel.getOrAddChannel(c, getUrl(), this);
}
 
Example 12
Source File: BgpMessageDecoder.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
    log.debug("MESSAGE IS RECEIVED.");
    if (!channel.isConnected()) {
        log.info("Channel is not connected.");
        return null;
    }

    HexDump.dump(buffer);

    BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
    List<BgpMessage> msgList = (List<BgpMessage>) ctx.getAttachment();

    if (msgList == null) {
        msgList = new LinkedList<>();
    }

    try {
        while (buffer.readableBytes() > 0) {
            buffer.markReaderIndex();
            BgpHeader bgpHeader = new BgpHeader();
            BgpMessage message = reader.readFrom(buffer, bgpHeader);
            msgList.add(message);
        }
        ctx.setAttachment(null);
        return msgList;
    } catch (Exception e) {
        log.debug("Bgp protocol message decode error");
        buffer.resetReaderIndex();
        buffer.discardReadBytes();
        ctx.setAttachment(msgList);
    }
    return null;
}
 
Example 13
Source File: NettyClient.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
protected com.alibaba.dubbo.remoting.Channel getChannel() {
    Channel c = channel;
    if (c == null || ! c.isConnected())
        return null;
    return NettyChannel.getOrAddChannel(c, getUrl(), this);
}
 
Example 14
Source File: FpmFrameDecoder.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
        throws Exception {

    if (!channel.isConnected()) {
        return null;
    }

    if (buffer.readableBytes() < FpmHeader.FPM_HEADER_LENGTH) {
        return null;
    }

    buffer.markReaderIndex();

    short version = buffer.readUnsignedByte();
    short type = buffer.readUnsignedByte();
    int length = buffer.readUnsignedShort();

    buffer.resetReaderIndex();

    if (buffer.readableBytes() < length) {
        // Not enough bytes to read a whole message
        return null;
    }

    byte[] fpmMessage = new byte[length];
    buffer.readBytes(fpmMessage);

    return FpmHeader.decode(fpmMessage, 0, fpmMessage.length);
}
 
Example 15
Source File: NettyClient.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Override
protected com.alibaba.dubbo.remoting.Channel getChannel() {
    Channel c = channel;
    if (c == null || ! c.isConnected())
        return null;
    return NettyChannel.getOrAddChannel(c, getUrl(), this);
}
 
Example 16
Source File: NettyClient.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
protected com.alibaba.dubbo.remoting.Channel getChannel() {
    Channel c = channel;
    if (c == null || ! c.isConnected())
        return null;
    return NettyChannel.getOrAddChannel(c, getUrl(), this);
}
 
Example 17
Source File: NetworkFailureHandler.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Closes the specified channel after all queued write requests are flushed.
 */
static void closeOnFlush(Channel channel) {
	if (channel.isConnected()) {
		channel.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
	}
}
 
Example 18
Source File: NetworkFailureHandler.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Closes the specified channel after all queued write requests are flushed.
 */
static void closeOnFlush(Channel channel) {
	if (channel.isConnected()) {
		channel.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
	}
}
 
Example 19
Source File: NetworkFailureHandler.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Closes the specified channel after all queued write requests are flushed.
 */
static void closeOnFlush(Channel channel) {
	if (channel.isConnected()) {
		channel.write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
	}
}
 
Example 20
Source File: MongoProxyInboundHandler.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/** Closes the specified channel after all queued write requests are flushed. */
static void closeOnFlush( Channel ch ) {
    if ( ch.isConnected() ) {
        ch.write( ChannelBuffers.EMPTY_BUFFER ).addListener( ChannelFutureListener.CLOSE );
    }
}