Merge pull request #1712 from koo-taejin/#1711

Added UdpDataSender using NIO #1711
This commit is contained in:
Woonduk Kang
2016-04-25 11:45:18 +09:00
6 changed files with 163 additions and 0 deletions
@@ -0,0 +1,18 @@
package com.navercorp.pinpoint.rpc.buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* @Author Taejin Koo
*/
public interface ByteBufferFactory {
public static final ByteOrder DEFAULT_BYTE_ORDER = ByteOrder.BIG_ENDIAN;
public static final ByteOrder NATIVE_BYTE_ORDER = ByteOrder.nativeOrder();
ByteBuffer getBuffer(int capacity);
ByteBuffer getBuffer(ByteOrder endianness, int capacity);
}
@@ -0,0 +1,29 @@
package com.navercorp.pinpoint.rpc.buffer;
import java.util.EnumMap;
import java.util.Map;
/**
* @Author Taejin Koo
*/
public final class ByteBufferFactoryLocator {
private static final Map<ByteBufferType, ByteBufferFactory> FACTORY_REPOSITORY = new EnumMap<ByteBufferType, ByteBufferFactory>(ByteBufferType.class);
static {
FACTORY_REPOSITORY.put(ByteBufferType.DIRECT, new DirectByteBufferFactory());
FACTORY_REPOSITORY.put(ByteBufferType.HEAP, new HeapByteBufferFactory());
}
public static ByteBufferFactory getFactory(String name) {
ByteBufferType byteBufferType = ByteBufferType.getValue(name);
if (byteBufferType == null) {
throw new IllegalArgumentException("Unknown ByteBufferType:" + name);
}
return getFactory(byteBufferType);
}
public static ByteBufferFactory getFactory(ByteBufferType byteBufferType) {
return FACTORY_REPOSITORY.get(byteBufferType);
}
}
@@ -0,0 +1,25 @@
package com.navercorp.pinpoint.rpc.buffer;
/**
* @Author Taejin Koo
*/
public enum ByteBufferType {
HEAP,
DIRECT;
public static ByteBufferType getValue(String name) {
if (name == null) {
throw new NullPointerException("name");
}
for (ByteBufferType byteBufferType : ByteBufferType.values()) {
if (name.equalsIgnoreCase(byteBufferType.name())) {
return byteBufferType;
}
}
return null;
}
}
@@ -0,0 +1,21 @@
package com.navercorp.pinpoint.rpc.buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* @Author Taejin Koo
*/
public class DirectByteBufferFactory implements ByteBufferFactory {
@Override
public ByteBuffer getBuffer(int capacity) {
return getBuffer(DEFAULT_BYTE_ORDER, capacity);
}
@Override
public ByteBuffer getBuffer(ByteOrder endianness, int capacity) {
return ByteBuffer.allocateDirect(capacity).order(endianness);
}
}
@@ -0,0 +1,21 @@
package com.navercorp.pinpoint.rpc.buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* @Author Taejin Koo
*/
public class HeapByteBufferFactory implements ByteBufferFactory {
@Override
public ByteBuffer getBuffer(int capacity) {
return getBuffer(DEFAULT_BYTE_ORDER, capacity);
}
@Override
public ByteBuffer getBuffer(ByteOrder endianness, int capacity) {
return ByteBuffer.allocate(capacity).order(endianness);
}
}
@@ -0,0 +1,49 @@
package com.navercorp.pinpoint.rpc.buffer;
import org.junit.Test;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* @Author Taejin Koo
*/
public class ByteBufferFactoryTest {
@Test
public void directByteBufferFactoryTest() throws Exception {
ByteBufferFactory byteBufferFactory = ByteBufferFactoryLocator.getFactory("direct");
ByteBuffer buffer = byteBufferFactory.getBuffer(20);
assertBufferOrder(buffer, ByteBufferFactory.DEFAULT_BYTE_ORDER);
assertBufferType(buffer, true);
}
@Test
public void heapByteBufferFactoryTest() throws Exception {
ByteBufferFactory byteBufferFactory = ByteBufferFactoryLocator.getFactory("heap");
ByteBuffer buffer = byteBufferFactory.getBuffer(20);
assertBufferOrder(buffer, ByteBufferFactory.DEFAULT_BYTE_ORDER);
assertBufferType(buffer, false);
}
@Test(expected = IllegalArgumentException.class)
public void unknownByteBufferFactoryTest() throws Exception {
ByteBufferFactory byteBufferFactory = ByteBufferFactoryLocator.getFactory("unknown");
}
private void assertBufferOrder(ByteBuffer byteBuffer, ByteOrder order) {
if (byteBuffer.order() != order) {
throw new IllegalArgumentException("");
}
}
private void assertBufferType(ByteBuffer byteBuffer, boolean isDirect) {
if (byteBuffer.isDirect() != isDirect) {
throw new IllegalArgumentException("");
}
}
}