-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
Signed-off-by: wei <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.github.protocol.mdtp.common.codec; | ||
|
||
import io.github.protocol.mdtp.common.model.AbstractMessageBody; | ||
import io.github.protocol.mdtp.common.model.DeviceDiscoveryResponse; | ||
import io.netty.buffer.ByteBuf; | ||
|
||
public class DeviceDiscoveryReponseDecoder implements MessageBodyDecoder { | ||
@Override | ||
public AbstractMessageBody handle(ByteBuf in) { | ||
return DeviceDiscoveryResponse.readFromBuffer(in); | ||
Check warning on line 10 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/DeviceDiscoveryReponseDecoder.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/DeviceDiscoveryReponseDecoder.java#L10
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.github.protocol.mdtp.common.handler; | ||
|
||
import io.github.protocol.mdtp.common.model.Attributes; | ||
import io.github.protocol.mdtp.common.model.CDATHeader; | ||
import io.github.protocol.mdtp.common.model.CDATHeaderFactory; | ||
import io.github.protocol.mdtp.common.model.Device; | ||
import io.github.protocol.mdtp.common.model.DeviceDiscoveryRequest; | ||
import io.github.protocol.mdtp.common.model.DeviceDiscoveryResponse; | ||
import io.github.protocol.mdtp.common.model.MdtpPacket; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class DeviceDiscoveryRequestHandler implements MessageBodyHandler { | ||
|
||
@Override | ||
public void handle (ChannelHandlerContext ctx, MdtpPacket requestPacket) { | ||
log.info("start to send device discovery response."); | ||
DeviceDiscoveryRequest deviceDiscoveryRequest = (DeviceDiscoveryRequest) requestPacket.getBody(); | ||
DeviceDiscoveryResponse deviceDiscoveryResponse = new DeviceDiscoveryResponse(); | ||
deviceDiscoveryResponse.setRequestId(deviceDiscoveryRequest.getRequestId()); | ||
deviceDiscoveryResponse.setResponseId(deviceDiscoveryResponse.generateId()); | ||
Device device = ctx.channel().attr(Attributes.DEVICE_KEY).get(); | ||
deviceDiscoveryResponse.setDevice(device); | ||
|
||
CDATHeader cdatHeader = CDATHeaderFactory.createDeviceDiscoveryCDATHeader(); | ||
|
||
MdtpPacket packet = new MdtpPacket(); | ||
packet.setHeader(cdatHeader); | ||
packet.setSecurityHeader(null); | ||
packet.setBody(deviceDiscoveryResponse); | ||
packet.setSignature(null); | ||
ctx.channel().writeAndFlush(packet.toByteBuf()); | ||
log.info("send device discovery response success."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.github.protocol.mdtp.common.handler; | ||
|
||
import io.github.protocol.mdtp.common.model.MdtpPacket; | ||
import io.netty.channel.ChannelHandlerContext; | ||
|
||
public interface MessageBodyHandler { | ||
|
||
void handle (ChannelHandlerContext ctx, MdtpPacket mdtpPacket); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.github.protocol.mdtp.common.handler; | ||
|
||
|
||
import io.github.protocol.mdtp.common.model.MessageBodyHeader; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class MessageHandlerFactory { | ||
Check warning on line 9 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/handler/MessageHandlerFactory.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/handler/MessageHandlerFactory.java#L9
|
||
private static final Map<Short, MessageBodyHandler> handlers = new HashMap<>(); | ||
|
||
static { | ||
handlers.put(MessageBodyHeader.DEVICE_DISCOVERY_REQUEST.toShort(), new DeviceDiscoveryRequestHandler()); | ||
} | ||
|
||
public static MessageBodyHandler getHandler(MessageBodyHeader header) { | ||
return handlers.get(header.toShort()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.github.protocol.mdtp.common.model; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
|
||
public class Address { | ||
public static final byte IPV4_TYPE = 4; | ||
public static final byte IPV6_TYPE = 6; | ||
|
||
private final byte type; | ||
private final byte[] value; | ||
|
||
public Address(byte type, byte[] value) { | ||
this.type = type; | ||
this.value = value; | ||
} | ||
|
||
public String getIpString() throws UnknownHostException { | ||
return InetAddress.getByAddress(value).getHostAddress(); | ||
} | ||
|
||
public void writeByteBuf(ByteBuf buffer) { | ||
buffer.writeByte(type); | ||
buffer.writeBytes(value); | ||
} | ||
|
||
public static Address readByteBuf(ByteBuf buffer) { | ||
byte type = buffer.readByte(); | ||
int length = (type == IPV4_TYPE) ? 4 : 16; | ||
byte[] value = new byte[length]; | ||
buffer.readBytes(value); | ||
return new Address(type, value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.github.protocol.mdtp.common.model; | ||
|
||
import io.netty.util.AttributeKey; | ||
|
||
public class Attributes { | ||
public static final AttributeKey<Device> DEVICE_KEY = AttributeKey.valueOf("device"); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.github.protocol.mdtp.common.model; | ||
|
||
public class CDATHeaderFactory { | ||
Check warning on line 3 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/CDATHeaderFactory.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/CDATHeaderFactory.java#L3
|
||
public static CDATHeader createMessageTransferCDATHeader() { | ||
return initializeDefault(new CDATHeader(), (byte) 0x00); | ||
Check warning on line 5 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/CDATHeaderFactory.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/CDATHeaderFactory.java#L5
|
||
} | ||
|
||
public static CDATHeader createDeviceDiscoveryCDATHeader() { | ||
return initializeDefault(new CDATHeader(), (byte) 0x02); | ||
} | ||
|
||
private static CDATHeader initializeDefault(CDATHeader header, byte formatType) { | ||
header.setFormatType(formatType); | ||
header.setProtocolVersion((byte) 1); | ||
header.setMessageLength((short) 0); | ||
header.setTimestamp(System.currentTimeMillis()); | ||
header.setFlags((byte) 0b01100000); | ||
|
||
if (formatType == 0x00) { | ||
header.setSequenceNumber(0); | ||
header.setLogicalChannelId(0); | ||
Check warning on line 21 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/CDATHeaderFactory.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/CDATHeaderFactory.java#L20-L21
|
||
} else { | ||
header.setSequenceNumber(null); | ||
header.setLogicalChannelId(null); | ||
} | ||
|
||
return header; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package io.github.protocol.mdtp.common.model; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class Device { | ||
private byte mask; | ||
|
||
private byte deviceStatus; | ||
|
||
private byte addressCount; | ||
|
||
private List<Address> addresses; | ||
|
||
private short port; | ||
|
||
private int deviceType; | ||
|
||
private byte[] uniqueId; | ||
|
||
private String deviceName; | ||
|
||
public void writeByteBuf(ByteBuf buffer) { | ||
buffer.writeByte(mask); | ||
buffer.writeByte(deviceStatus); | ||
buffer.writeByte(addressCount); | ||
|
||
for (Address address : addresses) { | ||
address.writeByteBuf(buffer); | ||
} | ||
|
||
buffer.writeShort(port); | ||
buffer.writeInt(deviceType); | ||
|
||
if (uniqueId != null) { | ||
buffer.writeShort(uniqueId.length); | ||
buffer.writeBytes(uniqueId); | ||
} else { | ||
buffer.writeShort(0); | ||
} | ||
|
||
if (deviceName != null) { | ||
byte[] nameBytes = deviceName.getBytes(StandardCharsets.UTF_8); | ||
buffer.writeShort(nameBytes.length); | ||
buffer.writeBytes(nameBytes); | ||
} else { | ||
buffer.writeShort(0); | ||
} | ||
} | ||
|
||
public static Device readByteBuf(ByteBuf buffer) { | ||
Device device = new Device(); | ||
|
||
device.mask = buffer.readByte(); | ||
device.deviceStatus = buffer.readByte(); | ||
device.addressCount = buffer.readByte(); | ||
|
||
device.addresses = new ArrayList<>(); | ||
for (int i = 0; i < device.addressCount; i++) { | ||
device.addresses.add(Address.readByteBuf(buffer)); | ||
} | ||
|
||
device.port = buffer.readShort(); | ||
device.deviceType = buffer.readInt(); | ||
|
||
int uniqueIdLength = buffer.readShort(); | ||
if (uniqueIdLength > 0) { | ||
device.uniqueId = new byte[uniqueIdLength]; | ||
buffer.readBytes(device.uniqueId); | ||
} else { | ||
device.uniqueId = null; | ||
} | ||
|
||
int deviceNameLength = buffer.readShort(); | ||
if (deviceNameLength > 0) { | ||
byte[] nameBytes = new byte[deviceNameLength]; | ||
buffer.readBytes(nameBytes); | ||
device.deviceName = new String(nameBytes, StandardCharsets.UTF_8); | ||
} else { | ||
device.deviceName = null; | ||
} | ||
|
||
return device; | ||
} | ||
} |