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

The following examples show how to use org.jboss.netty.channel.Channel#isConnected() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: onos   File: IsisLspQueueConsumer.java    License: 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 Project: anima   File: NettyChannel.java    License: 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 3
Source Project: RDFS   File: ShuffleHandler.java    License: 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 4
Source Project: tez   File: ShuffleHandler.java    License: 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 5
Source Project: dubbox   File: NettyClient.java    License: 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 6
Source Project: dubbox-hystrix   File: NettyClient.java    License: 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 7
Source Project: onos   File: FpmFrameDecoder.java    License: 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 8
Source Project: dubbox   File: NettyClient.java    License: 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 9
Source Project: onos   File: BgpMessageDecoder.java    License: 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 10
Source Project: dubbox   File: NettyClient.java    License: 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 11
Source Project: onos   File: DefaultIsisInterface.java    License: 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 12
@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 13
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 14
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 15
Source Project: pinpoint   File: HealthCheckManager.java    License: 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 16
Source Project: onos   File: PcepMessageDecoder.java    License: 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 17
/** 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 );
    }
}
 
Example 18
/**
 * 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 Project: flink   File: NetworkFailureHandler.java    License: 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 Project: flink   File: NetworkFailureHandler.java    License: 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);
	}
}