List of usage examples for io.netty.buffer ByteBuf nioBuffer
public abstract ByteBuffer nioBuffer();
From source file:org.apache.carbondata.spark.dictionary.server.SecureDictionaryServerHandler.java
License:Apache License
@Override public void receive(TransportClient transportClient, ByteBuffer byteBuffer, RpcResponseCallback rpcResponseCallback) { try {//from www. j a v a2 s .co m ByteBuf data = Unpooled.wrappedBuffer(byteBuffer); DictionaryMessage key = new DictionaryMessage(); key.readFullLength(data); data.release(); int outPut = processMessage(key); key.setDictionaryValue(outPut); // Send back the response ByteBuf buff = ByteBufAllocator.DEFAULT.buffer(); key.writeData(buff); rpcResponseCallback.onSuccess(buff.nioBuffer()); } catch (Exception e) { LOGGER.error(e); } }
From source file:org.apache.distributedlog.protocol.util.ProtocolUtils.java
License:Apache License
/** * Generate crc32 for WriteOp./*from w w w . j a v a 2s .c o m*/ */ public static Long writeOpCRC32(String stream, ByteBuf data) { CRC32 crc = requestCRC.get(); try { crc.update(stream.getBytes(UTF_8)); crc.update(data.nioBuffer()); return crc.getValue(); } finally { crc.reset(); } }
From source file:org.apache.flink.runtime.io.network.netty.NettyMessageSerializationTest.java
License:Apache License
@Test public void testEncodeDecode() { {/*from www .j a va 2 s. co m*/ Buffer buffer = spy( new Buffer(MemorySegmentFactory.allocateUnpooledSegment(1024), mock(BufferRecycler.class))); ByteBuffer nioBuffer = buffer.getNioBuffer(); for (int i = 0; i < 1024; i += 4) { nioBuffer.putInt(i); } NettyMessage.BufferResponse expected = new NettyMessage.BufferResponse(buffer, random.nextInt(), new InputChannelID()); NettyMessage.BufferResponse actual = encodeAndDecode(expected); // Verify recycle has been called on buffer instance verify(buffer, times(1)).recycle(); final ByteBuf retainedSlice = actual.getNettyBuffer(); // Ensure not recycled and same size as original buffer assertEquals(1, retainedSlice.refCnt()); assertEquals(1024, retainedSlice.readableBytes()); nioBuffer = retainedSlice.nioBuffer(); for (int i = 0; i < 1024; i += 4) { assertEquals(i, nioBuffer.getInt()); } // Release the retained slice actual.releaseBuffer(); assertEquals(0, retainedSlice.refCnt()); assertEquals(expected.sequenceNumber, actual.sequenceNumber); assertEquals(expected.receiverId, actual.receiverId); } { { IllegalStateException expectedError = new IllegalStateException(); InputChannelID receiverId = new InputChannelID(); NettyMessage.ErrorResponse expected = new NettyMessage.ErrorResponse(expectedError, receiverId); NettyMessage.ErrorResponse actual = encodeAndDecode(expected); assertEquals(expected.cause.getClass(), actual.cause.getClass()); assertEquals(expected.cause.getMessage(), actual.cause.getMessage()); assertEquals(receiverId, actual.receiverId); } { IllegalStateException expectedError = new IllegalStateException("Illegal illegal illegal"); InputChannelID receiverId = new InputChannelID(); NettyMessage.ErrorResponse expected = new NettyMessage.ErrorResponse(expectedError, receiverId); NettyMessage.ErrorResponse actual = encodeAndDecode(expected); assertEquals(expected.cause.getClass(), actual.cause.getClass()); assertEquals(expected.cause.getMessage(), actual.cause.getMessage()); assertEquals(receiverId, actual.receiverId); } { IllegalStateException expectedError = new IllegalStateException("Illegal illegal illegal"); NettyMessage.ErrorResponse expected = new NettyMessage.ErrorResponse(expectedError); NettyMessage.ErrorResponse actual = encodeAndDecode(expected); assertEquals(expected.cause.getClass(), actual.cause.getClass()); assertEquals(expected.cause.getMessage(), actual.cause.getMessage()); assertNull(actual.receiverId); assertTrue(actual.isFatalError()); } } { NettyMessage.PartitionRequest expected = new NettyMessage.PartitionRequest( new ResultPartitionID(new IntermediateResultPartitionID(), new ExecutionAttemptID()), random.nextInt(), new InputChannelID()); NettyMessage.PartitionRequest actual = encodeAndDecode(expected); assertEquals(expected.partitionId, actual.partitionId); assertEquals(expected.queueIndex, actual.queueIndex); assertEquals(expected.receiverId, actual.receiverId); } { NettyMessage.TaskEventRequest expected = new NettyMessage.TaskEventRequest( new IntegerTaskEvent(random.nextInt()), new ResultPartitionID(new IntermediateResultPartitionID(), new ExecutionAttemptID()), new InputChannelID()); NettyMessage.TaskEventRequest actual = encodeAndDecode(expected); assertEquals(expected.event, actual.event); assertEquals(expected.partitionId, actual.partitionId); assertEquals(expected.receiverId, actual.receiverId); } { NettyMessage.CancelPartitionRequest expected = new NettyMessage.CancelPartitionRequest( new InputChannelID()); NettyMessage.CancelPartitionRequest actual = encodeAndDecode(expected); assertEquals(expected.receiverId, actual.receiverId); } { NettyMessage.CloseRequest expected = new NettyMessage.CloseRequest(); NettyMessage.CloseRequest actual = encodeAndDecode(expected); assertEquals(expected.getClass(), actual.getClass()); } }
From source file:org.apache.qpid.jms.provider.amqp.AmqpProvider.java
License:Apache License
@Override public void onData(final ByteBuf input) { // We need to retain until the serializer gets around to processing it. ReferenceCountUtil.retain(input);//from ww w .ja v a 2s . c o m serializer.execute(new Runnable() { @Override public void run() { try { if (isTraceBytes()) { TRACE_BYTES.info("Received: {}", ByteBufUtil.hexDump(input)); } ByteBuffer source = input.nioBuffer(); do { ByteBuffer buffer = protonTransport.getInputBuffer(); int limit = Math.min(buffer.remaining(), source.remaining()); ByteBuffer duplicate = source.duplicate(); duplicate.limit(source.position() + limit); buffer.put(duplicate); protonTransport.processInput().checkIsOk(); source.position(source.position() + limit); } while (source.hasRemaining()); ReferenceCountUtil.release(input); // Process the state changes from the latest data and then answer back // any pending updates to the Broker. processUpdates(); pumpToProtonTransport(); } catch (Throwable t) { LOG.warn("Caught problem during data processing: {}", t.getMessage(), t); fireProviderException(t); } } }); }
From source file:org.apache.qpid.jms.provider.amqp.message.AmqpCodec.java
License:Apache License
/** * Given an encoded AMQP Section, decode the value previously written there. * * @param encoded//from ww w . j av a 2 s .co m * the AMQP Section value to decode. * * @return a Section object read from its encoded form. */ public static Section decode(ByteBuf encoded) { if (encoded == null || !encoded.isReadable()) { return null; } DecoderImpl decoder = TLS_CODEC.get().decoder; decoder.setByteBuffer(encoded.nioBuffer()); Section result = (Section) decoder.readObject(); decoder.setByteBuffer(null); encoded.resetReaderIndex(); return result; }
From source file:org.apache.qpid.jms.provider.amqp.message.AmqpCodec.java
License:Apache License
/** * Create a new JmsMessage and underlying JmsMessageFacade that represents the proper * message type for the incoming AMQP message. * * @param consumer// w w w . j a va 2 s. c om * The AmqpConsumer instance that will be linked to the decoded message. * @param messageBytes * The the raw bytes that compose the incoming message. (Read-Only) * * @return a AmqpJmsMessageFacade instance decoded from the message bytes. * * @throws IOException if an error occurs while creating the message objects. */ public static AmqpJmsMessageFacade decodeMessage(AmqpConsumer consumer, ByteBuf messageBytes) throws IOException { DecoderImpl decoder = getDecoder(); ByteBuffer buffer = messageBytes.nioBuffer(); decoder.setByteBuffer(buffer); Header header = null; DeliveryAnnotations deliveryAnnotations = null; MessageAnnotations messageAnnotations = null; Properties properties = null; ApplicationProperties applicationProperties = null; Section body = null; Footer footer = null; Section section = null; if (buffer.hasRemaining()) { section = (Section) decoder.readObject(); } if (section instanceof Header) { header = (Header) section; if (buffer.hasRemaining()) { section = (Section) decoder.readObject(); } else { section = null; } } if (section instanceof DeliveryAnnotations) { deliveryAnnotations = (DeliveryAnnotations) section; if (buffer.hasRemaining()) { section = (Section) decoder.readObject(); } else { section = null; } } if (section instanceof MessageAnnotations) { messageAnnotations = (MessageAnnotations) section; if (buffer.hasRemaining()) { section = (Section) decoder.readObject(); } else { section = null; } } if (section instanceof Properties) { properties = (Properties) section; if (buffer.hasRemaining()) { section = (Section) decoder.readObject(); } else { section = null; } } if (section instanceof ApplicationProperties) { applicationProperties = (ApplicationProperties) section; if (buffer.hasRemaining()) { section = (Section) decoder.readObject(); } else { section = null; } } if (section != null && !(section instanceof Footer)) { body = section; if (buffer.hasRemaining()) { section = (Section) decoder.readObject(); } else { section = null; } } if (section instanceof Footer) { footer = (Footer) section; } decoder.setByteBuffer(null); messageBytes.resetReaderIndex(); // First we try the easy way, if the annotation is there we don't have to work hard. AmqpJmsMessageFacade result = createFromMsgAnnotation(messageAnnotations); if (result == null) { // Next, match specific section structures and content types result = createWithoutAnnotation(body, properties); } if (result != null) { result.setHeader(header); result.setDeliveryAnnotations(deliveryAnnotations); result.setMessageAnnotations(messageAnnotations); result.setProperties(properties); result.setApplicationProperties(applicationProperties); result.setBody(body); result.setFooter(footer); result.initialize(consumer); return result; } throw new IOException("Could not create a JMS message from incoming message"); }
From source file:org.apache.spark.network.crypto.AuthClientBootstrap.java
License:Apache License
private void doSparkAuth(TransportClient client, Channel channel) throws GeneralSecurityException, IOException { String secretKey = secretKeyHolder.getSecretKey(appId); try (AuthEngine engine = new AuthEngine(appId, secretKey, conf)) { ClientChallenge challenge = engine.challenge(); ByteBuf challengeData = Unpooled.buffer(challenge.encodedLength()); challenge.encode(challengeData); ByteBuffer responseData = client.sendRpcSync(challengeData.nioBuffer(), conf.authRTTimeoutMs()); ServerResponse response = ServerResponse.decodeMessage(responseData); engine.validate(response);// w w w. j ava2 s . c o m engine.sessionCipher().addToChannel(channel); } }
From source file:org.apache.spark.network.crypto.AuthMessagesSuite.java
License:Apache License
private ByteBuffer encode(Encodable msg) { ByteBuf buf = Unpooled.buffer(); msg.encode(buf); return buf.nioBuffer(); }
From source file:org.apache.spark.network.crypto.AuthRpcHandler.java
License:Apache License
@Override public void receive(TransportClient client, ByteBuffer message, RpcResponseCallback callback) { if (doDelegate) { delegate.receive(client, message, callback); return;/*w w w . j av a 2s .c om*/ } int position = message.position(); int limit = message.limit(); ClientChallenge challenge; try { challenge = ClientChallenge.decodeMessage(message); LOG.debug("Received new auth challenge for client {}.", channel.remoteAddress()); } catch (RuntimeException e) { if (conf.saslFallback()) { LOG.warn("Failed to parse new auth challenge, reverting to SASL for client {}.", channel.remoteAddress()); delegate = new SaslRpcHandler(conf, channel, delegate, secretKeyHolder); message.position(position); message.limit(limit); delegate.receive(client, message, callback); doDelegate = true; } else { LOG.debug("Unexpected challenge message from client {}, closing channel.", channel.remoteAddress()); callback.onFailure(new IllegalArgumentException("Unknown challenge message.")); channel.close(); } return; } // Here we have the client challenge, so perform the new auth protocol and set up the channel. AuthEngine engine = null; try { String secret = secretKeyHolder.getSecretKey(challenge.appId); Preconditions.checkState(secret != null, "Trying to authenticate non-registered app %s.", challenge.appId); LOG.debug("Authenticating challenge for app {}.", challenge.appId); engine = new AuthEngine(challenge.appId, secret, conf); ServerResponse response = engine.respond(challenge); ByteBuf responseData = Unpooled.buffer(response.encodedLength()); response.encode(responseData); callback.onSuccess(responseData.nioBuffer()); engine.sessionCipher().addToChannel(channel); } catch (Exception e) { // This is a fatal error: authentication has failed. Close the channel explicitly. LOG.debug("Authentication failed for client {}, closing channel.", channel.remoteAddress()); callback.onFailure(new IllegalArgumentException("Authentication failed.")); channel.close(); return; } finally { if (engine != null) { try { engine.close(); } catch (Exception e) { throw Throwables.propagate(e); } } } LOG.debug("Authorization successful for client {}.", channel.remoteAddress()); doDelegate = true; }
From source file:org.apache.spark.network.protocol.MessageWithHeader.java
License:Apache License
private int copyByteBuf(ByteBuf buf, WritableByteChannel target) throws IOException { ByteBuffer buffer = buf.nioBuffer(); int written = (buffer.remaining() <= NIO_BUFFER_LIMIT) ? target.write(buffer) : writeNioBuffer(target, buffer); buf.skipBytes(written);//from w w w . j a v a 2s . c om return written; }