Package io.netty.buffer

Examples of io.netty.buffer.ByteBuf


    HelixIPCCallback wrappedCallback = new HelixIPCCallback() {
      @Override
      public void onMessage(HelixMessageScope scope, UUID messageId, ByteBuf message) {
        if (pendingMessages.replace(messageId, true, false)) {
          pendingMessages.remove(messageId);
          ByteBuf originalMessage = messageBuffers.remove(messageId);
          if (originalMessage != null) {
            originalMessage.release();
          }
          retriesLeft.remove(messageId);
          callback.onMessage(scope, messageId, message);
        }
      }
View Full Code Here


              + getLength(destination.getScope().getState()) + getLength(config.getInstanceName())
              + getLength(destination.getInstanceName());
      int messageLength = message == null ? 0 : message.readableBytes();

      // Build message header
      ByteBuf headerBuf = channel.alloc().buffer(headerLength);
      headerBuf.writeInt(MESSAGE_VERSION).writeInt(messageType)
          .writeLong(messageId.getMostSignificantBits())
          .writeLong(messageId.getLeastSignificantBits());
      writeStringWithLength(headerBuf, destination.getScope().getCluster());
      writeStringWithLength(headerBuf, destination.getScope().getResource());
      writeStringWithLength(headerBuf, destination.getScope().getPartition());
      writeStringWithLength(headerBuf, destination.getScope().getState());
      writeStringWithLength(headerBuf, config.getInstanceName());
      writeStringWithLength(headerBuf, destination.getInstanceName());

      // Compose message header and payload
      headerBuf.writeInt(messageLength);
      CompositeByteBuf fullByteBuf = channel.alloc().compositeBuffer(2);
      fullByteBuf.addComponent(headerBuf);
      fullByteBuf.writerIndex(headerBuf.readableBytes());
      if (message != null) {
        fullByteBuf.addComponent(message);
        fullByteBuf.writerIndex(fullByteBuf.writerIndex() + message.readableBytes());
      }
View Full Code Here

            new HelixMessageScope.Builder().cluster(firstNode.getClusterName())
                .resource(RESOURCE_NAME).partition(partitionName).state("ONLINE").build();

        Set<HelixAddress> destinations = firstResolver.getDestinations(scope);
        for (HelixAddress destination : destinations) {
          ByteBuf message = Unpooled.wrappedBuffer(("Hello" + i).getBytes());
          firstIPC.send(destination, messageType, UUID.randomUUID(), message);
        }
      }
    }

    // Loopback
    for (String partitionName : secondPartitions) {
      for (int i = 0; i < numMessages; i++) {
        HelixMessageScope scope =
            new HelixMessageScope.Builder().cluster(secondNode.getClusterName())
                .resource(RESOURCE_NAME).partition(partitionName).state("ONLINE").build();

        Set<HelixAddress> destinations = secondResolver.getDestinations(scope);
        for (HelixAddress destination : destinations) {
          ByteBuf message = Unpooled.wrappedBuffer(("Hello" + i).getBytes());
          secondIPC.send(destination, messageType, UUID.randomUUID(), message);
        }
      }
    }
View Full Code Here

        HelixMessageScope scope =
            new HelixMessageScope.Builder().cluster(firstNode.getClusterName())
                .resource(RESOURCE_NAME).partition(partitionName).state("ONLINE").build();
        Set<HelixAddress> destinations = firstResolver.getDestinations(scope);
        for (HelixAddress destination : destinations) {
          ByteBuf message = Unpooled.wrappedBuffer(("Hello" + i).getBytes());
          messageManager.send(destination, messageType, UUID.randomUUID(), message);
        }
      }
    }
View Full Code Here

      System.out.println("Starting " + trafficThreads.length + " threads to generate traffic");
      for (int i = 0; i < trafficThreads.length; i++) {
        Thread t = new Thread() {
          @Override
          public void run() {
            ByteBuf m = ByteBufAllocator.DEFAULT.buffer(messageBytes.length);
            m.writeBytes(messageBytes);
            while (!isShutdown.get()) {
              for (int i = 0; i < numPartitions; i++) {
                HelixMessageScope scope =
                    new HelixMessageScope.Builder().cluster("CLUSTER").resource("RESOURCE")
                        .partition("PARTITION_" + i).sourceInstance(localhost + "_" + port).build();

                Set<HelixAddress> destinations =
                    ImmutableSet.of(new HelixAddress(scope, remoteHost + "_" + remotePort,
                        new InetSocketAddress(remoteHost, remotePort)));

                UUID uuid = UUID.randomUUID();

                try {
                  for (HelixAddress destination : destinations) {
                    m.retain();
                    ipcService.send(destination, MESSAGE_TYPE, uuid, m);
                  }
                } catch (Exception e) {
                  e.printStackTrace();
                }
View Full Code Here

        throws IOException
        {
            int headerLength = frame.header.version >= Server.VERSION_3
                             ? Header.MODERN_LENGTH
                             : Header.LEGACY_LENGTH;
            ByteBuf header = CBUtil.allocator.buffer(headerLength);

            Message.Type type = frame.header.type;
            header.writeByte(type.direction.addToVersion(frame.header.version));
            header.writeByte(Header.Flag.serialize(frame.header.flags));

            if (frame.header.version >= Server.VERSION_3)
                header.writeShort(frame.header.streamId);
            else
                header.writeByte(frame.header.streamId);

            header.writeByte(type.opcode);
            header.writeInt(frame.body.readableBytes());

            results.add(header);
            results.add(frame.body);
        }
View Full Code Here

            Codec<Message> codec = (Codec<Message>)message.type.codec;
            try
            {
                int messageSize = codec.encodedSize(message, version);
                ByteBuf body;
                if (message instanceof Response)
                {
                    UUID tracingId = ((Response)message).getTracingId();
                    if (tracingId != null)
                    {
                        body = CBUtil.allocator.buffer(CBUtil.sizeOfUUID(tracingId) + messageSize);
                        CBUtil.writeUUID(tracingId, body);
                        flags.add(Frame.Header.Flag.TRACING);
                    }
                    else
                    {
                        body = CBUtil.allocator.buffer(messageSize);
                    }
                }
                else
                {
                    assert message instanceof Request;
                    body = CBUtil.allocator.buffer(messageSize);
                    if (((Request)message).isTracingRequested())
                        flags.add(Frame.Header.Flag.TRACING);
                }

                try
                {
                    codec.encode(message, body, version);
                }
                catch (Throwable e)
                {
                    body.release();
                    throw e;
                }

                results.add(Frame.create(message.type, message.getStreamId(), version, flags, body));
            }
View Full Code Here

            if (buffer.readableBytes() < frameLength)
                return;

            // extract body
            ByteBuf body = buffer.slice(idx, (int) bodyLength);
            body.retain();
           
            idx += bodyLength;
            buffer.readerIndex(idx);

            Connection connection = ctx.channel().attr(Connection.attributeKey).get();
View Full Code Here

            EnumSet<Frame.Header.Flag> flags = EnumSet.noneOf(Frame.Header.Flag.class);

            Codec<Message> codec = (Codec<Message>)message.type.codec;
            int messageSize = codec.encodedSize(message, version);
            ByteBuf body;
            if (message instanceof Response)
            {
                UUID tracingId = ((Response)message).getTracingId();
                if (tracingId != null)
                {
                    body = CBUtil.allocator.buffer(CBUtil.sizeOfUUID(tracingId) + messageSize);
                    CBUtil.writeUUID(tracingId, body);
                    flags.add(Frame.Header.Flag.TRACING);
                }
                else
                {
                    body = CBUtil.allocator.buffer(messageSize);
                }
            }
            else
            {
                assert message instanceof Request;
                body = CBUtil.allocator.buffer(messageSize);
                if (((Request)message).isTracingRequested())
                    flags.add(Frame.Header.Flag.TRACING);
            }

            try
            {
                codec.encode(message, body, version);
            }
            catch (Throwable e)
            {
                body.release();
                throw ErrorMessage.wrap(e, message.getStreamId());
            }

            results.add(Frame.create(message.type, message.getStreamId(), version, flags, body));
        }
View Full Code Here

            if (buffer.readableBytes() < frameLength)
                return;

            // extract body
            ByteBuf body = buffer.slice(idx, (int) bodyLength);
            body.retain();
           
            idx += bodyLength;
            buffer.readerIndex(idx);

            Connection connection = ctx.channel().attr(Connection.attributeKey).get();
View Full Code Here

TOP

Related Classes of io.netty.buffer.ByteBuf

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.