提交部分代码。可能导致编译问题。

This commit is contained in:
wusheng 2016-07-04 11:34:54 +08:00
parent 195f4b3ec9
commit 34fd22c794
10 changed files with 148 additions and 16 deletions

View File

@ -21,7 +21,7 @@ import java.net.InetSocketAddress;
import com.ai.cloud.skywalking.selfexamination.HeathReading;
import com.ai.cloud.skywalking.selfexamination.SDKHealthCollector;
import com.ai.cloud.skywalking.util.ProtocolPackager;
import com.ai.cloud.skywalking.util.TransportPackager;
public class DataSender implements IDataSender {
private EventLoopGroup group;
@ -76,7 +76,7 @@ public class DataSender implements IDataSender {
try {
if (channel != null && channel.isActive()) {
byte[] dataPackage = ProtocolPackager.pack(data.getBytes());
byte[] dataPackage = TransportPackager.pack(data.getBytes());
channel.writeAndFlush(dataPackage);
SDKHealthCollector.getCurrentHeathReading("sender").updateData(HeathReading.INFO, "DataSender[" + socketAddress + "] send data successfully.");

View File

@ -0,0 +1,11 @@
package com.ai.cloud.skywalking.exception;
/**
* Created by wusheng on 16/7/4.
*/
public class SerializableDataTypeRegisterException extends RuntimeException {
public SerializableDataTypeRegisterException(String message) {
super(message);
}
}

View File

@ -0,0 +1,42 @@
package com.ai.cloud.skywalking.protocol;
import com.ai.cloud.skywalking.util.IntegerAssist;
import java.util.HashSet;
import java.util.Set;
/**
* Created by wusheng on 16/7/4.
*/
public abstract class AbstractDataSerializable implements ISerializable, NullableClass{
private static Set<Integer> DATA_TYPE_SCOPE = new HashSet<Integer>();
public AbstractDataSerializable(){
SerializableDataTypeRegister.init(getDataType(), this.getClass());
}
public abstract int getDataType();
public abstract byte[] getData();
@Override
public byte[] convert2Bytes() {
byte[] type = IntegerAssist.intToBytes(SerializableDataTypeRegister.getType(this.getClass()));
//TODO:类型+ data = 消息包
return getData();
}
@Override
public Object convert2Object(byte[] data) {
// TODO:data的前4位转成type;
int dataType = 1;
if(!SerializableDataTypeRegister.isTypeAndClassMatch(dataType, this.getClass())){
return new NullClass();
}
// TODO: 反序列化
return null;
}
}

View File

@ -0,0 +1,11 @@
package com.ai.cloud.skywalking.protocol;
/**
* Created by wusheng on 16/7/4.
*/
public interface ISerializable {
byte[] convert2Bytes();
Object convert2Object(byte[] data);
}

View File

@ -0,0 +1,11 @@
package com.ai.cloud.skywalking.protocol;
/**
* Created by wusheng on 16/7/4.
*/
public class NullClass implements NullableClass {
@Override
public boolean isNull() {
return true;
}
}

View File

@ -0,0 +1,9 @@
package com.ai.cloud.skywalking.protocol;
/**
* Created by wusheng on 16/7/4.
*/
public interface NullableClass {
boolean isNull();
}

View File

@ -0,0 +1,43 @@
package com.ai.cloud.skywalking.protocol;
import com.ai.cloud.skywalking.exception.SerializableDataTypeRegisterException;
import java.util.HashMap;
import java.util.Map;
/**
* Created by wusheng on 16/7/4.
*/
public class SerializableDataTypeRegister {
private static Map<Integer, Class<?>> DATA_TYPE_MAPPING_CLASS = new HashMap<Integer, Class<?>>();
private static Map<Class<?>, Integer> CLASS_MAPPING_DATA_TYPE = new HashMap<Class<?>, Integer>();
public static void init(Integer dataType, Class<?> clazz) {
if (DATA_TYPE_MAPPING_CLASS.containsKey(dataType)) {
if (!DATA_TYPE_MAPPING_CLASS.get(dataType).equals(clazz)) {
throw new SerializableDataTypeRegisterException("dataType=" + dataType + " has been registered to " + DATA_TYPE_MAPPING_CLASS.get(dataType));
} else {
return;
}
}
DATA_TYPE_MAPPING_CLASS.put(dataType, clazz);
CLASS_MAPPING_DATA_TYPE.put(clazz, dataType);
}
public static boolean isTypeAndClassMatch(Integer dataType, Class<?> clazz) {
if (DATA_TYPE_MAPPING_CLASS.containsKey(dataType)) {
return true;
} else {
return false;
}
}
public static Integer getType(Class<?> clazz) {
if (CLASS_MAPPING_DATA_TYPE.containsKey(clazz)) {
return CLASS_MAPPING_DATA_TYPE.get(clazz);
} else {
throw new SerializableDataTypeRegisterException("class " + clazz + " not found in SerializableDataTypeRegister.");
}
}
}

View File

@ -0,0 +1,15 @@
package com.ai.cloud.skywalking.util;
/**
* Created by wusheng on 16/7/4.
*/
public class IntegerAssist {
public static byte[] intToBytes(int value) {
byte[] src = new byte[4];
src[0] = (byte) ((value >> 24) & 0xFF);
src[1] = (byte) ((value >> 16) & 0xFF);
src[2] = (byte) ((value >> 8) & 0xFF);
src[3] = (byte) (value & 0xFF);
return src;
}
}

View File

@ -2,7 +2,7 @@ package com.ai.cloud.skywalking.util;
import java.util.Arrays;
public class ProtocolPackager {
public class TransportPackager {
public static byte[] pack(byte[] data) {
// 对协议格式进行修改
// | check sum(4 byte) | data
@ -58,16 +58,6 @@ public class ProtocolPackager {
result ^= data[i];
}
return intToBytes(result);
return IntegerAssist.intToBytes(result);
}
private static byte[] intToBytes(int value) {
byte[] src = new byte[4];
src[0] = (byte) ((value >> 24) & 0xFF);
src[1] = (byte) ((value >> 16) & 0xFF);
src[2] = (byte) ((value >> 8) & 0xFF);
src[3] = (byte) (value & 0xFF);
return src;
}
}

View File

@ -3,7 +3,7 @@ package com.ai.cloud.skywalking.reciever.handler;
import com.ai.cloud.skywalking.reciever.buffer.DataBufferThreadContainer;
import com.ai.cloud.skywalking.reciever.conf.Config;
import com.ai.cloud.skywalking.reciever.util.RedisConnector;
import com.ai.cloud.skywalking.util.ProtocolPackager;
import com.ai.cloud.skywalking.util.TransportPackager;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import redis.clients.jedis.Jedis;
@ -18,7 +18,7 @@ public class CollectionServerDataHandler extends SimpleChannelInboundHandler<byt
// 当接受到这条消息的是空则忽略
if (msg != null && msg.length >= 0 && msg.length < Config.DataPackage.MAX_DATA_PACKAGE) {
byte[] data = ProtocolPackager.unpack(msg);
byte[] data = TransportPackager.unpack(msg);
if (data != null) {
DataBufferThreadContainer.getDataBufferThread().saveTemporarily(data);