Example usage for io.netty.buffer Unpooled copyInt

List of usage examples for io.netty.buffer Unpooled copyInt

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled copyInt.

Prototype

public static ByteBuf copyInt(int... values) 

Source Link

Document

Create a big-endian buffer that holds a sequence of the specified 32-bit integers.

Usage

From source file:ch.ethz.globis.distindex.middleware.net.MiddlewareChannelHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;/*  ww  w . java2 s  .  c o m*/
    String clientHost = ctx.channel().remoteAddress().toString();
    ByteBuffer response = ioHandler.handle(clientHost, buf.nioBuffer());
    ByteBuf nettyBuf = Unpooled.wrappedBuffer(response);
    ByteBuf sizeBuf = Unpooled.copyInt(nettyBuf.readableBytes());

    ctx.write(sizeBuf);
    ctx.write(nettyBuf);
    ctx.flush();
    buf.release();
}

From source file:groovyx.gpars.remote.netty.discovery.DiscoveryResponseWithRecipientEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, DiscoveryResponseWithRecipient msg, List<Object> out)
        throws Exception {
    DiscoveryResponse response = msg.getResponse();
    ByteBuf portBuf = Unpooled.copyInt(response.getServerSocketAddress().getPort());
    ByteBuf urlBuf = Unpooled.copiedBuffer(response.getActorUrl(), CharsetUtil.UTF_8);

    DatagramPacket packet = new DatagramPacket(Unpooled.wrappedBuffer(portBuf, urlBuf), msg.getRecipient());
    out.add(packet);/* w w  w .j  a  va  2s .c  om*/
}

From source file:org.ensembl.gti.seqstore.server.RequestResponseServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    log.trace("Reading message");
    ByteBuf in = (ByteBuf) msg;//from  w ww. j  a  v  a2s  .  c  o m
    int l = in.readInt();
    log.trace("Reading message of length " + l);
    ByteBuf x = in.readBytes(l);
    Response response = null;
    try {
        Request request = Request.parseFrom(x.array());
        log.debug("Received " + request.getOperation());
        String responseMsg;
        switch (request.getOperation()) {
        case START_SESSION:
            log.debug("Creating new session for " + request.getClientId());
            Long id = seqStore.startSession(request.getClientId());
            responseMsg = id.toString();
            break;
        case ABORT_SESSION:
            log.debug("Aborting session " + request.getSessionId() + " for " + request.getClientId());
            seqStore.clearSession(request.getSessionId());
            responseMsg = "Aborted session " + request.getSessionId();
            break;
        case STORE_GENOME:
            log.debug("Storing " + request.getGeneCount() + " genes");
            long genomeId = seqStore.storeGenome(request.getSessionId(),
                    new GenomeDelegate(request.getGenome()));
            responseMsg = String.valueOf(genomeId);
            break;
        case STORE_GENOME_SEQUENCE:
            log.info("Storing " + request.getGenomeSequenceCount() + " genome sequences");
            int n = 0;
            for (GenomeSequence g : request.getGenomeSequenceList()) {
                seqStore.storeGenomeSequence(request.getSessionId(), new GenomeSequenceDelegate(g));
                n++;
            }
            responseMsg = "Stored " + n + " genome sequences";
            break;
        case STORE_GENE:
            log.debug("Storing " + request.getGeneCount() + " genes");
            n = 0;
            for (Gene g : request.getGeneList()) {
                seqStore.storeGene(request.getSessionId(), new GeneDelegate(g));
                n++;
            }
            responseMsg = "Stored " + n + " genes";
            break;
        default:
            responseMsg = "Unknown operation " + request.getOperation();
            response = Response.newBuilder().setStatus(Status.DATA_ERROR).setMessage(responseMsg).build();
            throw new UnsupportedOperationException(responseMsg);
        }
        response = Response.newBuilder().setStatus(Status.OK).setMessage(responseMsg).build();
    } catch (SeqStoreException e) {
        log.error("Error processing request", e);
        response = Response.newBuilder().setStatus(Status.DATA_ERROR).setMessage(e.getMessage()).build();
    } catch (Throwable e) {
        log.error("Error processing request", e);
        response = Response.newBuilder().setStatus(Status.SERVER_ERROR).setMessage(e.getMessage()).build();
    } finally {
        byte[] ra = response.toByteArray();
        ctx.write(Unpooled.copyInt(ra.length));
        ctx.write(Unpooled.copiedBuffer(ra));
        ctx.flush();
        if (x != null)
            x.release();
        if (in != null)
            in.release();

    }
}

From source file:org.nd4j.linalg.api.buffer.BaseDataBuffer.java

License:Apache License

/**
 *
 * @param data//from ww  w  .  ja  v a 2  s  . c om
 * @param copy
 */
public BaseDataBuffer(int[] data, boolean copy) {
    allocationMode = Nd4j.alloc;
    if (allocationMode == AllocationMode.HEAP) {
        if (copy)
            intData = ArrayUtil.copy(data);

        else
            this.intData = data;

    } else
        dataBuffer = Unpooled.copyInt(data).order(ByteOrder.nativeOrder());

    length = data.length;
}

From source file:org.opendaylight.protocol.bgp.linkstate.attribute.LinkAttributesParser.java

License:Open Source License

static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
    final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
    LOG.trace("Started serializing Link Attributes");
    if (linkAttributes.getLocalIpv4RouterId() != null) {
        TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID,
                Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
    }//from  w ww  .j a v  a 2  s. c  o m
    if (linkAttributes.getLocalIpv6RouterId() != null) {
        TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID,
                Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
    }
    if (linkAttributes.getRemoteIpv4RouterId() != null) {
        TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID,
                Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
    }
    if (linkAttributes.getRemoteIpv6RouterId() != null) {
        TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID,
                Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
    }
    if (linkAttributes.getAdminGroup() != null) {
        TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()),
                byteAggregator);
    }
    if (linkAttributes.getMaxLinkBandwidth() != null) {
        TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()),
                byteAggregator);
    }
    if (linkAttributes.getMaxReservableBandwidth() != null) {
        TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH,
                Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
    }
    serializeUnreservedBw(linkAttributes.getUnreservedBandwidth(), byteAggregator);
    if (linkAttributes.getTeMetric() != null) {
        TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()),
                byteAggregator);
    }
    if (linkAttributes.getLinkProtection() != null) {
        TlvUtil.writeTLV(LINK_PROTECTION_TYPE,
                Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
    }
    serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
    if (linkAttributes.getMetric() != null) {
        // size of metric can be 1,2 or 3 depending on the protocol
        TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()),
                byteAggregator);
    }
    serializeSrlg(linkAttributes.getSharedRiskLinkGroups(), byteAggregator);
    if (linkAttributes.getLinkName() != null) {
        TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())),
                byteAggregator);
    }
    if (linkAttributes.getSrAdjIds() != null) {
        SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers(linkAttributes.getSrAdjIds(), SR_ADJ_ID,
                byteAggregator);
    }
    if (linkAttributes.getSrLanAdjIds() != null) {
        SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifiers(linkAttributes.getSrLanAdjIds(),
                SR_LAN_ADJ_ID, byteAggregator);
    }
    if (linkAttributes.getPeerNodeSid() != null) {
        TlvUtil.writeTLV(PEER_NODE_SID_CODE,
                SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerNodeSid()),
                byteAggregator);
    }
    if (linkAttributes.getPeerAdjSid() != null) {
        TlvUtil.writeTLV(PEER_ADJ_SID_CODE,
                SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerAdjSid()),
                byteAggregator);
    }
    if (linkAttributes.getPeerSetSids() != null) {
        SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers(linkAttributes.getPeerSetSids(),
                PEER_SET_SID_CODE, byteAggregator);
    }
    LOG.trace("Finished serializing Link Attributes");
}

From source file:org.opendaylight.protocol.bgp.linkstate.attribute.PrefixAttributesParser.java

License:Open Source License

static void serializePrefixAttributes(final PrefixAttributesCase prefixAttributesCase,
        final ByteBuf byteAggregator) {
    final PrefixAttributes prefixAtrributes = prefixAttributesCase.getPrefixAttributes();
    if (prefixAtrributes.getIgpBits() != null) {
        final BitArray igpBit = new BitArray(FLAGS_SIZE);
        igpBit.set(UP_DOWN_BIT, prefixAtrributes.getIgpBits().getUpDown().isUpDown());
        TlvUtil.writeTLV(IGP_FLAGS, Unpooled.wrappedBuffer(igpBit.array()), byteAggregator);
    }//from  w w  w .ja v  a 2 s . c  o  m
    serializeRouteTags(prefixAtrributes.getRouteTags(), byteAggregator);
    serializeExtendedRouteTags(prefixAtrributes.getExtendedTags(), byteAggregator);
    if (prefixAtrributes.getPrefixMetric() != null) {
        TlvUtil.writeTLV(PREFIX_METRIC,
                Unpooled.copyInt(prefixAtrributes.getPrefixMetric().getValue().intValue()), byteAggregator);
    }
    serializeForwardingAddress(prefixAtrributes.getOspfForwardingAddress(), byteAggregator);
    if (prefixAtrributes.getSrPrefix() != null) {
        final ByteBuf buffer = Unpooled.buffer();
        SrPrefixAttributesParser.serializeSrPrefix(prefixAtrributes.getSrPrefix(), buffer);
        TlvUtil.writeTLV(PREFIX_SID, buffer, byteAggregator);
    }
    if (prefixAtrributes.getSrRange() != null) {
        final ByteBuf sidBuffer = Unpooled.buffer();
        RangeTlvParser.serializeSrRange(prefixAtrributes.getSrRange(), sidBuffer);
        TlvUtil.writeTLV(RANGE, sidBuffer, byteAggregator);
    }
    if (prefixAtrributes.getSrBindingSidLabels() != null) {
        BindingSidLabelParser.serializeBindingSidLabels(prefixAtrributes.getSrBindingSidLabels(),
                byteAggregator, BINDING_SID);
    }
}

From source file:org.opendaylight.protocol.bgp.linkstate.attribute.sr.SidLabelIndexParser.java

License:Open Source License

static ByteBuf serializeSidValue(final SidLabelIndex tlv) {
    if (tlv instanceof Ipv6AddressCase) {
        return Ipv6Util.byteBufForAddress(((Ipv6AddressCase) tlv).getIpv6Address());
    } else if (tlv instanceof LocalLabelCase) {
        return Unpooled.copyMedium(((LocalLabelCase) tlv).getLocalLabel().getValue().intValue() & LABEL_MASK);
    } else if (tlv instanceof SidCase) {
        return Unpooled.copyInt(((SidCase) tlv).getSid().intValue());
    }// w w w  .ja  v  a  2  s.c  o  m
    return null;
}

From source file:org.opendaylight.protocol.bgp.linkstate.impl.attribute.LinkAttributesParser.java

License:Open Source License

static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf output) {
    final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
    LOG.trace("Started serializing Link Attributes");
    ifPresentApply(linkAttributes.getLocalIpv4RouterId(), value -> TlvUtil
            .writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress((Ipv4Address) value), output));
    ifPresentApply(linkAttributes.getLocalIpv6RouterId(), value -> TlvUtil
            .writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress((Ipv6Address) value), output));
    ifPresentApply(linkAttributes.getRemoteIpv4RouterId(), value -> TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID,
            Ipv4Util.byteBufForAddress((Ipv4Address) value), output));
    ifPresentApply(linkAttributes.getRemoteIpv6RouterId(), value -> TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID,
            Ipv6Util.byteBufForAddress((Ipv6Address) value), output));
    ifPresentApply(linkAttributes.getAdminGroup(), value -> TlvUtil.writeTLV(ADMIN_GROUP,
            Unpooled.copyInt((((AdministrativeGroup) value).getValue()).intValue()), output));
    ifPresentApply(linkAttributes.getMaxLinkBandwidth(), value -> TlvUtil.writeTLV(MAX_BANDWIDTH,
            Unpooled.wrappedBuffer(((Bandwidth) value).getValue()), output));
    ifPresentApply(linkAttributes.getMaxReservableBandwidth(),
            value -> TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH,
                    Unpooled.wrappedBuffer(((Bandwidth) value).getValue()), output));
    serializeUnreservedBw(linkAttributes.getUnreservedBandwidth(), output);
    ifPresentApply(linkAttributes.getTeMetric(),
            value -> TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(((TeMetric) value).getValue()), output));
    ifPresentApply(linkAttributes.getLinkProtection(), value -> TlvUtil.writeTLV(LINK_PROTECTION_TYPE,
            Unpooled.copyShort(((LinkProtectionType) value).getIntValue()), output));
    serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), output);
    ifPresentApply(linkAttributes.getMetric(), value -> TlvUtil.writeTLV(METRIC,
            Unpooled.copyMedium(((Metric) value).getValue().intValue()), output));
    serializeSrlg(linkAttributes.getSharedRiskLinkGroups(), output);
    ifPresentApply(linkAttributes.getLinkName(), value -> TlvUtil.writeTLV(LINK_NAME,
            Unpooled.wrappedBuffer(StandardCharsets.UTF_8.encode((String) value)), output));
    ifPresentApply(linkAttributes.getSrAdjIds(), value -> SrLinkAttributesParser
            .serializeAdjacencySegmentIdentifiers((List<SrAdjIds>) value, SR_ADJ_ID, output));
    ifPresentApply(linkAttributes.getSrLanAdjIds(), value -> SrLinkAttributesParser
            .serializeLanAdjacencySegmentIdentifiers((List<SrLanAdjIds>) value, output));
    ifPresentApply(linkAttributes.getPeerNodeSid(), value -> TlvUtil.writeTLV(PEER_NODE_SID_CODE,
            SrLinkAttributesParser.serializeAdjacencySegmentIdentifier((PeerNodeSid) value), output));
    ifPresentApply(linkAttributes.getPeerAdjSid(), value -> TlvUtil.writeTLV(PEER_ADJ_SID_CODE,
            SrLinkAttributesParser.serializeAdjacencySegmentIdentifier((PeerAdjSid) value), output));
    ifPresentApply(linkAttributes.getPeerSetSids(), value -> SrLinkAttributesParser
            .serializeAdjacencySegmentIdentifiers((List<PeerSetSids>) value, PEER_SET_SID_CODE, output));
    LOG.trace("Finished serializing Link Attributes");
}

From source file:org.opendaylight.protocol.bgp.linkstate.impl.attribute.PrefixAttributesParser.java

License:Open Source License

private static void serializePrefixMetric(final IgpMetric prefixMetric, final ByteBuf byteAggregator) {
    if (prefixMetric != null) {
        TlvUtil.writeTLV(PREFIX_METRIC, Unpooled.copyInt(prefixMetric.getValue().intValue()), byteAggregator);
    }//  w w  w  . jav a 2  s  .  c  o m
}

From source file:org.opendaylight.protocol.bgp.linkstate.impl.attribute.sr.SidLabelIndexParser.java

License:Open Source License

public static ByteBuf serializeSidValue(final SidLabelIndex tlv) {
    if (tlv instanceof Ipv6AddressCase) {
        return Ipv6Util.byteBufForAddress(((Ipv6AddressCase) tlv).getIpv6Address());
    } else if (tlv instanceof LocalLabelCase) {
        return Unpooled.copyMedium(((LocalLabelCase) tlv).getLocalLabel().getValue().intValue() & LABEL_MASK);
    } else if (tlv instanceof SidCase) {
        return Unpooled.copyInt(((SidCase) tlv).getSid().intValue());
    }//w  w  w . j a v a2  s . c  om
    return null;
}