Java Code Examples for org.jboss.netty.buffer.ChannelBuffer#setShort()

The following examples show how to use org.jboss.netty.buffer.ChannelBuffer#setShort() . 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: WideCommunityIpV4Neighbour.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public int write(ChannelBuffer c) {
    int iLenStartIndex = c.writerIndex();

    Iterator<IpV4Neighbour> listIterator = ipv4Neighbour.iterator();
    c.writeByte(TYPE);

    int iLengthIndex = c.writerIndex();
    c.writeShort(0);

    while (listIterator.hasNext()) {
        IpV4Neighbour speaker = listIterator.next();
        c.writeBytes(speaker.localSpeaker().toOctets());
        c.writeBytes(speaker.remoteSpeaker().toOctets());
    }

    int length = c.writerIndex() - iLengthIndex;
    c.setShort(iLengthIndex, (short) (length - 2));

    return c.writerIndex() - iLenStartIndex;
}
 
Example 2
Source File: PcepMetricObjectVer1.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public int write(ChannelBuffer cb) throws PcepParseException {
    //write Object header
    int objStartIndex = cb.writerIndex();

    int objLenIndex = metricObjHeader.write(cb);

    if (objLenIndex <= 0) {
        throw new PcepParseException("Error: ObjectLength is " + objLenIndex);
    }

    int iFlag = (bCFlag) ? CFLAG_SET : CFLAG_RESET;
    int iTemp = iFlag << IFLAG_SHIFT_VALUE;
    iFlag = (bBFlag) ? BFLAG_SET : BFLAG_RESET;
    iTemp = iTemp | (iFlag << BTYPE_SHIFT_VALUE);
    iTemp = iTemp | bType;
    cb.writeInt(iTemp);
    cb.writeInt(iMetricVal);

    short hLength = (short) (cb.writerIndex() - objStartIndex);
    cb.setShort(objLenIndex, hLength);
    //will be helpful during print().
    metricObjHeader.setObjLen(hLength);
    return hLength;
}
 
Example 3
Source File: PcepLabelRangeResvMsgVer1.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void write(ChannelBuffer cb, PcepLabelRangeResvMsgVer1 message) throws PcepParseException {

    int startIndex = cb.writerIndex();
    // first 3 bits set to version
    cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
    // message type
    cb.writeByte(MSG_TYPE.getType());
    // Length will be set after calculating length, but currently set it as 0.
    int msgLenIndex = cb.writerIndex();

    cb.writeShort((short) 0);
    //write Label Range
    message.labelRange.write(cb);

    // update message length field
    int length = cb.writerIndex() - startIndex;
    cb.setShort(msgLenIndex, (short) length);
}
 
Example 4
Source File: PcepOpenMsgVer1.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void write(ChannelBuffer cb, PcepOpenMsgVer1 message) throws PcepParseException {
    int startIndex = cb.writerIndex();
    // first 3 bits set to version
    cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
    // message type
    cb.writeByte(MSG_TYPE.getType());
    // length is length of variable message, will be updated at the end
    // Store the position of message
    // length in buffer

    int msgLenIndex = cb.writerIndex();
    cb.writeShort(0);

    message.getPcepOpenObject().write(cb);

    // update message length field
    int iLength = cb.writerIndex() - startIndex;
    cb.setShort(msgLenIndex, (short) iLength);
}
 
Example 5
Source File: PcepLabelUpdateMsgVer1.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void write(ChannelBuffer cb, PcepLabelUpdateMsgVer1 message) throws PcepParseException {

    int startIndex = cb.writerIndex();

    // first 3 bits set to version
    cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));

    // message type
    cb.writeByte(MSG_TYPE.getType());

    // Length will be set after calculating length, but currently set it as 0.
    int msgLenIndex = cb.writerIndex();

    cb.writeShort((short) 0);
    ListIterator<PcepLabelUpdate> listIterator = message.llPcLabelUpdateList.listIterator();

    while (listIterator.hasNext()) {
        PcepLabelUpdate labelUpdate = listIterator.next();
        labelUpdate.write(cb);
    }

    // update message length field
    int length = cb.writerIndex() - startIndex;
    cb.setShort(msgLenIndex, (short) length);
}
 
Example 6
Source File: LoginPacketEncoder.java    From aion-germany with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Object encode(ChannelHandlerContext ctx, Channel arg1, Object arg2) throws Exception {
    ChannelBuffer message = (ChannelBuffer) arg2;

    int size = message.readableBytes();
    message.setShort(0, (short) (size));
    return message;
}
 
Example 7
Source File: PcepEroObjectVer1.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public int write(ChannelBuffer cb) throws PcepParseException {

    //write Object header
    int objStartIndex = cb.writerIndex();

    int objLenIndex = eroObjHeader.write(cb);

    if (objLenIndex <= 0) {
        throw new PcepParseException("Failed to write ERO object header. Index " + objLenIndex);
    }

    ListIterator<PcepValueType> listIterator = subObjectList.listIterator();

    while (listIterator.hasNext()) {
        listIterator.next().write(cb);
    }

    //Update object length now
    int length = cb.writerIndex() - objStartIndex;
    cb.setShort(objLenIndex, (short) length);
    //will be helpful during print().
    eroObjHeader.setObjLen((short) length);

    //As per RFC the length of object should be multiples of 4
    int pad = length % 4;

    if (pad != 0) {
        pad = 4 - pad;
        for (int i = 0; i < pad; i++) {
            cb.writeByte((byte) 0);
        }
        length = length + pad;
    }

    objLenIndex = cb.writerIndex();
    return objLenIndex;
}
 
Example 8
Source File: PcepErrorMsgVer1.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void write(ChannelBuffer cb, PcepErrorMsgVer1 message) throws PcepParseException {
    int startIndex = cb.writerIndex();
    // first 3 bits set to version
    cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
    // message type 0xC
    cb.writeByte(MSG_TYPE.getType());
    // length is length of variable message, will be updated at the end
    // Store the position of message
    // length in buffer
    int msgLenIndex = cb.writerIndex();
    cb.writeShort(0);
    ErrorObjListWithOpen errObjListWithOpen = message.getErrorObjListWithOpen();
    PcepErrorInfo errInfo = message.getPcepErrorInfo();

    // write ( <error-obj-list> [<Open>] ) if exists.
    // otherwise write <error> [<error-list>]

    if ((errObjListWithOpen != null)
            && (errObjListWithOpen.isErrorObjListWithOpenPresent())) {
        errObjListWithOpen.write(cb);
    } else if ((errInfo != null) && (errInfo.isErrorInfoPresent())) {
        errInfo.write(cb);
    } else {
        throw new PcepParseException("Empty PCEP-ERROR message.");
    }
    // PcepErrorMessage message length field
    int length = cb.writerIndex() - startIndex;
    cb.setShort(msgLenIndex, (short) length);
}
 
Example 9
Source File: StatefulRsvpErrorSpecTlv.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public int write(ChannelBuffer c) {
    int iStartIndex = c.writerIndex();
    c.writeShort(TYPE);
    int tlvLenIndex = c.writerIndex();
    hLength = 0;
    c.writeShort(hLength);
    if (isErrSpceObjSet) {
        rsvpErrSpecObj.write(c);
    }
    hLength = (short) (c.writerIndex() - iStartIndex);
    c.setShort(tlvLenIndex, (hLength - OBJECT_HEADER_LENGTH));

    return c.writerIndex() - iStartIndex;
}
 
Example 10
Source File: PcepRsvpUserErrorSpec.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public int write(ChannelBuffer cb) {
    int objLenIndex = objHeader.write(cb);
    cb.writeInt(enterpriseNum);
    cb.writeByte(subOrg);
    cb.writeByte(errDescLen);
    cb.writeShort(userErrorValue);
    cb.writeBytes(errDesc);

    if (llRsvpUserSpecSubObj != null) {

        ListIterator<PcepValueType> listIterator = llRsvpUserSpecSubObj.listIterator();

        while (listIterator.hasNext()) {
            PcepValueType tlv = listIterator.next();
            if (tlv == null) {
                continue;
            }
            tlv.write(cb);
            // need to take care of padding
            int pad = tlv.getLength() % 4;
            if (0 != pad) {
                pad = 4 - pad;
                for (int i = 0; i < pad; ++i) {
                    cb.writeByte((byte) 0);
                }
            }
        }
    }
    short objLen = (short) (cb.writerIndex() - objLenIndex);
    cb.setShort(objLenIndex, objLen);
    return objLen;
}
 
Example 11
Source File: LocalNodeDescriptorsTlv.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public int write(ChannelBuffer c) {
    int tlvStartIndex = c.writerIndex();
    c.writeShort(TYPE);
    int tlvLenIndex = c.writerIndex();
    hLength = 0;
    c.writeShort(0);

    ListIterator<PcepValueType> listIterator = llNodeDescriptorSubTLVs.listIterator();

    while (listIterator.hasNext()) {
        PcepValueType tlv = listIterator.next();
        if (tlv == null) {
            log.debug("TLV is null from subTlv list");
            continue;
        }
        tlv.write(c);

        // need to take care of padding
        int pad = tlv.getLength() % 4;

        if (0 != pad) {
            pad = 4 - pad;
            for (int i = 0; i < pad; ++i) {
                c.writeByte((byte) 0);
            }
        }
    }
    hLength = (short) (c.writerIndex() - tlvStartIndex);
    c.setShort(tlvLenIndex, (hLength - TLV_HEADER_LENGTH));
    return c.writerIndex() - tlvStartIndex;
}
 
Example 12
Source File: PcepInterLayerObjectVer1.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public int write(ChannelBuffer cb) throws PcepParseException {

    //write Object header
    int objStartIndex = cb.writerIndex();

    int objLenIndex = interLayerObjHeader.write(cb);

    if (objLenIndex <= 0) {
        throw new PcepParseException(" ObjectLength Index is " + objLenIndex);
    }

    int iTemp = 0;

    if (bIFlag) {
        iTemp = iTemp | (byte) IFLAG_SHIFT_VALUE;
    }
    if (bNFlag) {
        iTemp = iTemp | (byte) NFLAG_SHIFT_VALUE;
    }

    cb.writeInt(iTemp);

    //Update object length now
    int length = cb.writerIndex() - objStartIndex;
    //will be helpful during print().
    interLayerObjHeader.setObjLen((short) length);
    cb.setShort(objLenIndex, (short) length);

    objLenIndex = cb.writerIndex();
    return objLenIndex;
}
 
Example 13
Source File: PcepIroObjectVer1.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public int write(ChannelBuffer cb) throws PcepParseException {
    //write Object header
    int objStartIndex = cb.writerIndex();

    int objLenIndex = iroObjHeader.write(cb);

    if (objLenIndex <= 0) {
        throw new PcepParseException(" ObjectLength is " + objLenIndex);
    }

    ListIterator<PcepValueType> listIterator = llSubObjects.listIterator();
    while (listIterator.hasNext()) {
        listIterator.next().write(cb);
    }

    //Update object length now
    int length = cb.writerIndex() - objStartIndex;
    //will be helpful during print().
    iroObjHeader.setObjLen((short) length);
    // As per RFC the length of object should be
    // multiples of 4
    int pad = length % 4;
    if (pad != 0) {
        pad = 4 - pad;
        for (int i = 0; i < pad; i++) {
            cb.writeByte((byte) 0);
        }
        length = length + pad;
    }
    cb.setShort(objLenIndex, (short) length);
    objLenIndex = cb.writerIndex();
    return objLenIndex;
}
 
Example 14
Source File: PcepFecObjectIPv6Ver1.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public int write(ChannelBuffer cb) throws PcepParseException {

    int objStartIndex = cb.writerIndex();

    //write common header
    int objLenIndex = fecObjHeader.write(cb);
    cb.writeBytes(nodeID);

    //now write FEC IPv4 Object Length
    cb.setShort(objLenIndex, (short) (cb.writerIndex() - objStartIndex));
    return cb.writerIndex();
}
 
Example 15
Source File: PcepRsvpIpv6ErrorSpec.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public int write(ChannelBuffer cb) {
    int objLenIndex = objHeader.write(cb);
    cb.writeBytes(ipv6Addr);
    cb.writeByte(flags);
    cb.writeByte(errCode);
    cb.writeShort(errValue);
    short objLen = (short) (cb.writerIndex() - objLenIndex);
    cb.setShort(objLenIndex, objLen);
    return objLen;
}
 
Example 16
Source File: BgpUpdateMsgVer4.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void write(ChannelBuffer cb, BgpUpdateMsgVer4 message) throws BgpParseException {

    int startIndex = cb.writerIndex();
    short afi = 0;
    byte safi = 0;

    // write common header and get msg length index
    int msgLenIndex = message.bgpHeader.write(cb);

    if (msgLenIndex <= 0) {
        throw new BgpParseException("Unable to write message header.");
    }
    List<BgpValueType> pathAttr = message.bgpPathAttributes.pathAttributes();
    if (pathAttr != null) {
        Iterator<BgpValueType> listIterator = pathAttr.iterator();

        while (listIterator.hasNext()) {
            BgpValueType attr = listIterator.next();
            if (attr instanceof MpReachNlri) {
                MpReachNlri mpReach = (MpReachNlri) attr;
                afi = mpReach.afi();
                safi = mpReach.safi();
            } else if (attr instanceof MpUnReachNlri) {
                MpUnReachNlri mpUnReach = (MpUnReachNlri) attr;
                afi = mpUnReach.afi();
                safi = mpUnReach.safi();
            }
        }

        if ((afi == Constants.AFI_FLOWSPEC_VALUE)
                || (afi == Constants.AFI_VALUE)) {
            //unfeasible route length
            cb.writeShort(0);
        }

        if ((afi == Constants.AFI_EVPN_VALUE)
                && (safi == Constants.SAFI_EVPN_VALUE)) {
            cb.writeShort(0);
        }

    }

    if (message.bgpPathAttributes != null) {
        message.bgpPathAttributes.write(cb);
    }

    // write UPDATE Object Length
    int length = cb.writerIndex() - startIndex;
    cb.setShort(msgLenIndex, (short) length);
    message.bgpHeader.setLength((short) length);
}
 
Example 17
Source File: MpUnReachNlri.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public int write(ChannelBuffer cb) {
    int iLenStartIndex = cb.writerIndex();
    if ((afi == Constants.AFI_FLOWSPEC_VALUE) && ((safi == Constants.SAFI_FLOWSPEC_VALUE) ||
        (safi == Constants.VPN_SAFI_FLOWSPEC_VALUE))) {
        List<BgpValueType> flowSpec = bgpFlowSpecNlri.flowSpecComponents();
        ListIterator<BgpValueType> listIterator = flowSpec.listIterator();
        boolean isAllFlowTypesIdentical = true;

        cb.writeByte(FLAGS);
        cb.writeByte(MPUNREACHNLRI_TYPE);

        int mpUnReachIndx = cb.writerIndex();
        cb.writeShort(0);

        cb.writeShort(afi);
        cb.writeByte(safi);

        if (bgpFlowSpecNlri.routeDistinguisher() != null) {
            cb.writeLong(bgpFlowSpecNlri.routeDistinguisher().getRouteDistinguisher());
        }

        BgpValueType tlv1 = null;
        if (listIterator.hasNext()) {
            tlv1 = listIterator.next();
        }
        while (tlv1 != null && listIterator.hasNext()) {
            BgpValueType tlv = listIterator.next();
            if (tlv.getType() != tlv1.getType()) {
                isAllFlowTypesIdentical = false;
                break;
            }
        }

        if (isAllFlowTypesIdentical) {
            BgpFlowSpecNlri.updateBufferIdenticalFlowTypes(cb, bgpFlowSpecNlri());
        } else {
            BgpFlowSpecNlri.updateBufferNonIdenticalFlowTypes(cb, bgpFlowSpecNlri());
        }
        int fsNlriLen = cb.writerIndex() - mpUnReachIndx;
        cb.setShort(mpUnReachIndx, (short) (fsNlriLen - 2));
    } else if ((afi == Constants.AFI_EVPN_VALUE)
            && (safi == Constants.SAFI_EVPN_VALUE)) {

        cb.writeByte(FLAGS);
        cb.writeByte(MPUNREACHNLRI_TYPE);

        int mpUnReachDataIndex = cb.writerIndex();
        cb.writeShort(0);
        cb.writeShort(afi);
        cb.writeByte(safi);

        for (BgpEvpnNlri element : evpnNlri) {
            short routeType = element.getType();
            switch (routeType) {
                case Constants.BGP_EVPN_MAC_IP_ADVERTISEMENT:
                    cb.writeByte(element.getType());
                    int iSpecStartIndex = cb.writerIndex();
                    cb.writeByte(0);
                    BgpEvpnRouteType2Nlri macIpAdvNlri =
                            (BgpEvpnRouteType2Nlri) element
                                    .getNlri();

                    macIpAdvNlri.write(cb);
                    cb.setByte(iSpecStartIndex, (short) (cb.writerIndex()
                            - iSpecStartIndex - 1));
                    break;
                case Constants.BGP_EVPN_ETHERNET_AUTO_DISCOVERY:
                    break;
                case Constants.BGP_EVPN_INCLUSIVE_MULTICASE_ETHERNET:
                    break;
                case Constants.BGP_EVPN_ETHERNET_SEGMENT:
                    break;
                default:
                    break;
            }
        }

        int evpnNlriLen = cb.writerIndex() - mpUnReachDataIndex;
        cb.setShort(mpUnReachDataIndex, (short) (evpnNlriLen - 2));
    }

    return cb.writerIndex() - iLenStartIndex;
}
 
Example 18
Source File: BgpPathAttributes.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Write path attributes to channelBuffer.
 *
 * @param cb channelBuffer
 * @return object of BgpPathAttributes
 * @throws BgpParseException while parsing BGP path attributes
 */
public int write(ChannelBuffer cb)
        throws BgpParseException {

    if (pathAttribute == null) {
        return 0;
    }
    int iLenStartIndex = cb.writerIndex();

    ListIterator<BgpValueType> iterator = pathAttribute.listIterator();

    int pathAttributeIndx = cb.writerIndex();
    cb.writeShort(0);

    while (iterator.hasNext()) {

        BgpValueType attr = iterator.next();

        switch (attr.getType()) {
        case Origin.ORIGIN_TYPE:
            Origin origin = (Origin) attr;
            origin.write(cb);
            break;
        case AsPath.ASPATH_TYPE:
            AsPath asPath = (AsPath) attr;
            asPath.write(cb);
            break;
        case As4Path.AS4PATH_TYPE:
            As4Path as4Path = (As4Path) attr;
            as4Path.write(cb);
            break;
        case NextHop.NEXTHOP_TYPE:
            NextHop nextHop = (NextHop) attr;
            nextHop.write(cb);
            break;
        case Med.MED_TYPE:
            Med med = (Med) attr;
            med.write(cb);
            break;
        case LocalPref.LOCAL_PREF_TYPE:
            LocalPref localPref = (LocalPref) attr;
            localPref.write(cb);
            break;
        case Constants.BGP_EXTENDED_COMMUNITY:
            BgpExtendedCommunity extendedCommunity = (BgpExtendedCommunity) attr;
            extendedCommunity.write(cb);
            break;
        case WideCommunity.TYPE:
            WideCommunity wideCommunity = (WideCommunity) attr;
            wideCommunity.write(cb);
            break;
        case MpReachNlri.MPREACHNLRI_TYPE:
            MpReachNlri mpReach = (MpReachNlri) attr;
            mpReach.write(cb);
            break;
        case MpUnReachNlri.MPUNREACHNLRI_TYPE:
            MpUnReachNlri mpUnReach = (MpUnReachNlri) attr;
            mpUnReach.write(cb);
            break;
        case LINK_STATE_ATTRIBUTE_TYPE:
            LinkStateAttributes linkState = (LinkStateAttributes) attr;
            linkState.write(cb);
            break;
        default:
            return cb.writerIndex() - iLenStartIndex;
        }
    }

    int pathAttrLen = cb.writerIndex() - pathAttributeIndx;
    cb.setShort(pathAttributeIndx, (short) (pathAttrLen - 2));
    return cb.writerIndex() - iLenStartIndex;
}
 
Example 19
Source File: GameServerPacketEncoder.java    From aion-germany with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Object encode(ChannelHandlerContext ctx, Channel arg1, Object arg2) throws Exception {
    ChannelBuffer message = (ChannelBuffer) arg2;
    message.setShort(0, (short) message.readableBytes());
    return message;
}
 
Example 20
Source File: PcepLspaObjectVer1.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public int write(ChannelBuffer cb) throws PcepParseException {

    //write Object header
    int objStartIndex = cb.writerIndex();

    int objLenIndex = lspaObjHeader.write(cb);

    if (objLenIndex <= 0) {
        throw new PcepParseException("Failed to write lspa object header. Index " + objLenIndex);
    }

    cb.writeInt(iExcludeAny);
    cb.writeInt(iIncludeAny);
    cb.writeInt(iIncludeAll);

    int iTemp = cSetupPriority << SETUP_PRIORITY_SHIFT_VALUE;
    iTemp = iTemp | (cHoldPriority << HOLD_PRIORITY_SHIFT_VALUE);
    byte bFlag;
    bFlag = (bLFlag) ? (byte) LFLAG_SET : LFLAG_RESET;
    iTemp = iTemp | (bFlag << BFLAG_SHIFT_VALUE);
    cb.writeInt(iTemp);

    // Add optional TLV
    if (!packOptionalTlv(cb)) {
        throw new PcepParseException("Faild to write lspa objects tlv to channel buffer");
    }

    short length = (short) (cb.writerIndex() - objStartIndex);

    lspaObjHeader.setObjLen(length); //will be helpful during print().

    //As per RFC the length of object should be multiples of 4
    short pad = (short) (length % 4);

    if (pad != 0) {
        pad = (short) (4 - pad);
        for (int i = 0; i < pad; i++) {
            cb.writeByte((byte) 0);
        }
        length = (short) (length + pad);
    }
    cb.setShort(objLenIndex, length);
    return cb.writerIndex();
}