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

The following are Jave code examples for showing how to use isConnected() of the org.jboss.netty.channel.Channel class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: hadoop   File: ShuffleHandler.java   Source Code and License Vote up 6 votes
@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 2
Project: athena   File: OFMessageDecoder.java   Source Code and License Vote up 6 votes
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel,
        ChannelBuffer buffer) throws Exception {
    if (!channel.isConnected()) {
        // In testing, I see decode being called AFTER decode last.
        // This check avoids that from reading corrupted frames
        return null;
    }

    // Note that a single call to decode results in reading a single
    // OFMessage from the channel buffer, which is passed on to, and processed
    // by, the controller (in OFChannelHandler).
    // This is different from earlier behavior (with the original openflowj),
    // where we parsed all the messages in the buffer, before passing on
    // a list of the parsed messages to the controller.
    // The performance *may or may not* not be as good as before.
    OFMessageReader<OFMessage> reader = OFFactories.getGenericReader();
    OFMessage message = reader.readFrom(buffer);

    return message;
}
 
Example 3
Project: athena   File: IsisLspQueueConsumer.java   Source Code and License Vote up 6 votes
/**
 * Process refresh LSP.
 *
 * @param wrapper LSP wrapper instance
 */
private void processRefreshLsp(LspWrapper wrapper) throws Exception {
    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 4
Project: iTAP-controller   File: OFMessageDecoder.java   Source Code and License Vote up 6 votes
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel,
                        ChannelBuffer buffer) throws Exception {
    if (!channel.isConnected()) {
        // In testing, I see decode being called AFTER decode last.
        // This check avoids that from reading corrupted frames
        return null;
    }

    List<OFMessage> messageList = new ArrayList<OFMessage>();
    for (;;) {
        OFMessage message = reader.readFrom(buffer);
        if (message == null)
            break;
        messageList.add(message);
    }
    return messageList.isEmpty() ? null : messageList;
}
 
Example 5
Project: EatDubbo   File: NettyClient.java   Source Code and License Vote up 5 votes
@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
Project: dubbo2   File: NettyClient.java   Source Code and License Vote up 5 votes
@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
Project: dubbox-hystrix   File: NettyClient.java   Source Code and License Vote up 5 votes
@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 8
Project: athena   File: PcepMessageDecoder.java   Source Code and License Vote up 5 votes
@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 9
Project: athena   File: DefaultIsisInterface.java   Source Code and License Vote up 5 votes
/**
 * 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 10
Project: athena   File: DefaultIsisInterface.java   Source Code and License Vote up 5 votes
/**
 * Sends the partial sequence number PDU.
 *
 * @param lspEntryRequestList list of lsp entry request
 * @param isisPduType         intermediate system PDU type
 * @param channel             netty channel instance
 */
private void sendPsnPduMessage(List<LspEntry> lspEntryRequestList, IsisPduType isisPduType, Channel channel) {
    IsisHeader isisHeader = new LspGenerator().getHeader(isisPduType);
    Psnp psnp = new Psnp(isisHeader);
    psnp.setSourceId(lspKeyP2P(this.systemId));
    TlvHeader tlvHeader = new TlvHeader();
    tlvHeader.setTlvType(TlvType.LSPENTRY.value());
    tlvHeader.setTlvLength(0);
    LspEntriesTlv lspEntriesTlv = new LspEntriesTlv(tlvHeader);
    for (LspEntry lspEntry : lspEntryRequestList) {
        lspEntry.setLspChecksum(0);
        lspEntry.setLspSequenceNumber(0);
        lspEntry.setRemainingTime(0);
        lspEntriesTlv.addLspEntry(lspEntry);
    }
    psnp.addTlv(lspEntriesTlv);
    //write it to channel buffer.
    byte[] psnpBytes = psnp.asBytes();
    psnpBytes = IsisUtil.addLengthAndMarkItInReserved(psnpBytes, IsisConstants.LENGTHPOSITION,
                                                      IsisConstants.LENGTHPOSITION + 1,
                                                      IsisConstants.RESERVEDPOSITION);
    flagValue = false;
    //write to the channel
    if (channel != null && channel.isConnected() && channel.isOpen()) {
        channel.write(IsisUtil.framePacket(psnpBytes, interfaceIndex));
    }
}
 
Example 11
Project: athena   File: BgpMessageDecoder.java   Source Code and License Vote up 5 votes
@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);
        }

        return msgList;
    } catch (Exception e) {
        log.debug("Bgp protocol message decode error");
        buffer.resetReaderIndex();
        buffer.discardReadBytes();
        ctx.setAttachment(msgList);
    }
    return null;
}
 
Example 12
Project: athena   File: FpmFrameDecoder.java   Source Code and License Vote up 5 votes
@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 13
Project: abhot   File: VersionCommand.java   Source Code and License Vote up 5 votes
@Override
public void execute(Channel chan, String[] command) throws DatastoreException
{
	m_counter.incrementAndGet();
	if (chan.isConnected())
	{
		Package thisPackage = getClass().getPackage();
		String versionString = thisPackage.getImplementationTitle()+" "+thisPackage.getImplementationVersion();
		chan.write(versionString+"\n");
	}
}
 
Example 14
Project: dubbocloud   File: NettyClient.java   Source Code and License Vote up 5 votes
@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 15
Project: athena   File: DefaultIsisInterface.java   Source Code and License Vote up 4 votes
/**
 * Processes LS PDU message.
 *
 * @param isisMessage LS pdu message instance
 * @param channel     channel instance
 */
public void processLsPduMessage(IsisMessage isisMessage, Channel channel) {
    log.debug("Enters processLsPduMessage ...!!!");
    IsisNeighbor neighbor = neighbouringRouter(isisMessage.sourceMac());
    if (networkType == IsisNetworkType.BROADCAST && neighbor == null) {
        return;
    }

    LsPdu lsPdu = (LsPdu) isisMessage;
    LspWrapper wrapper = isisLsdb.findLsp(lsPdu.isisPduType(), lsPdu.lspId());
    if (wrapper == null || isisLsdb.isNewerOrSameLsp(lsPdu, wrapper.lsPdu()).equalsIgnoreCase("latest")) {
        if (wrapper != null) {               // verify if the LSA - is your own LSA - get system ID and compare LSP
            String lspKey = isisLsdb.lspKey(systemId);
            if (lsPdu.lspId().equals(lspKey)) {
                lsPdu.setSequenceNumber(lsPdu.sequenceNumber() + 1);
                if (lsPdu.pduType() == IsisPduType.L1LSPDU.value()) {
                    // setting the ls sequence number
                    isisLsdb.setL1LspSeqNo(lsPdu.sequenceNumber());
                } else if (lsPdu.pduType() == IsisPduType.L2LSPDU.value()) {
                    // setting the ls sequence number
                    isisLsdb.setL2LspSeqNo(lsPdu.sequenceNumber());
                }
                isisLsdb.addLsp(lsPdu, true, this);
                sendLsp(lsPdu, channel);
            } else {
                isisLsdb.addLsp(lsPdu, false, this);
            }


        } else {
            //not exist in the database or latest, then add it in database
            isisLsdb.addLsp(lsPdu, false, this);
        }
    }

    //If network type is P2P, acknowledge with a PSNP
    if (networkType() == IsisNetworkType.P2P) {
        IsisPduType psnpType = null;
        if (IsisPduType.get(lsPdu.pduType()) == IsisPduType.L1LSPDU) {
            psnpType = IsisPduType.L1PSNP;
        } else if (IsisPduType.get(lsPdu.pduType()) == IsisPduType.L2LSPDU) {
            psnpType = IsisPduType.L2PSNP;
        }
        IsisHeader isisHeader = new LspGenerator().getHeader(psnpType);
        Psnp psnp = new Psnp(isisHeader);
        psnp.setSourceId(lspKeyP2P(this.systemId));
        TlvHeader tlvHeader = new TlvHeader();
        tlvHeader.setTlvType(TlvType.LSPENTRY.value());
        tlvHeader.setTlvLength(0);
        LspEntriesTlv lspEntriesTlv = new LspEntriesTlv(tlvHeader);
        LspEntry lspEntry = new LspEntry();
        lspEntry.setLspChecksum(lsPdu.checkSum());
        lspEntry.setLspId(lsPdu.lspId());
        lspEntry.setLspSequenceNumber(lsPdu.sequenceNumber());
        lspEntry.setRemainingTime(lsPdu.remainingLifeTime());
        lspEntriesTlv.addLspEntry(lspEntry);
        psnp.addTlv(lspEntriesTlv);

        //write it to channel buffer.
        byte[] psnpBytes = psnp.asBytes();
        psnpBytes = IsisUtil.addLengthAndMarkItInReserved(psnpBytes, IsisConstants.LENGTHPOSITION,
                                                          IsisConstants.LENGTHPOSITION + 1,
                                                          IsisConstants.RESERVEDPOSITION);
        flagValue = false;
        //write to the channel
        if (channel != null && channel.isConnected() && channel.isOpen()) {
            channel.write(IsisUtil.framePacket(psnpBytes, interfaceIndex));
        }
    }
}