-
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,12 @@ | ||
package io.github.protocol.mdtp.common.codec; | ||
|
||
import io.github.protocol.mdtp.common.model.AbstractMessageBody; | ||
import io.github.protocol.mdtp.common.model.DeviceDiscoveryRequest; | ||
import io.netty.buffer.ByteBuf; | ||
|
||
public class DeviceDiscoveryRequestDecoder implements MessageBodyDecoder { | ||
Check warning on line 7 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/DeviceDiscoveryRequestDecoder.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/DeviceDiscoveryRequestDecoder.java#L7
|
||
@Override | ||
public AbstractMessageBody handle(ByteBuf in) { | ||
return DeviceDiscoveryRequest.fromByteBuf(in); | ||
Check warning on line 10 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/DeviceDiscoveryRequestDecoder.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/DeviceDiscoveryRequestDecoder.java#L10
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.github.protocol.mdtp.common.codec; | ||
|
||
|
||
import io.github.protocol.mdtp.common.model.AbstractMessageBody; | ||
import io.github.protocol.mdtp.common.model.CDATHeader; | ||
import io.github.protocol.mdtp.common.model.MdtpPacket; | ||
import io.github.protocol.mdtp.common.model.MessageBodyHeader; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.ByteToMessageDecoder; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
public class MdtpDecoder extends ByteToMessageDecoder { | ||
|
||
@Override | ||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { | ||
|
||
MdtpPacket mdtpPacket = new MdtpPacket(); | ||
CDATHeader header = CDATHeader.fromByteBuf(in); | ||
Check warning on line 22 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MdtpDecoder.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MdtpDecoder.java#L21-L22
|
||
|
||
MessageBodyHeader messageBodyHeader = MessageBodyHeader.fromByteBuf(in); | ||
MessageBodyDecoder messageDecode = MessageDecoderFactory.getDecoder(messageBodyHeader); | ||
AbstractMessageBody messageBody = messageDecode.handle(in); | ||
messageBody.setMessageBodyHeader(messageBodyHeader); | ||
Check warning on line 27 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MdtpDecoder.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MdtpDecoder.java#L24-L27
|
||
|
||
mdtpPacket.setHeader(header); | ||
mdtpPacket.setBody(messageBody); | ||
Check warning on line 30 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MdtpDecoder.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MdtpDecoder.java#L29-L30
|
||
|
||
out.add(mdtpPacket); | ||
} | ||
Check warning on line 33 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MdtpDecoder.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MdtpDecoder.java#L32-L33
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.github.protocol.mdtp.common.codec; | ||
|
||
import io.github.protocol.mdtp.common.model.AbstractMessageBody; | ||
import io.netty.buffer.ByteBuf; | ||
|
||
public interface MessageBodyDecoder { | ||
AbstractMessageBody handle(ByteBuf in); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.github.protocol.mdtp.common.codec; | ||
|
||
import io.github.protocol.mdtp.common.model.MessageBodyHeader; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class MessageDecoderFactory { | ||
Check warning on line 8 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MessageDecoderFactory.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MessageDecoderFactory.java#L8
|
||
|
||
private static final Map<Short, MessageBodyDecoder> decoders = new HashMap<>(); | ||
Check warning on line 10 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MessageDecoderFactory.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MessageDecoderFactory.java#L10
|
||
|
||
static { | ||
decoders.put(MessageBodyHeader.DEVICE_DISCOVERY_REQUEST.toShort(), new DeviceDiscoveryRequestDecoder()); | ||
} | ||
Check warning on line 14 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MessageDecoderFactory.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MessageDecoderFactory.java#L13-L14
|
||
|
||
public static MessageBodyDecoder getDecoder(MessageBodyHeader header) { | ||
return decoders.get(header.toShort()); | ||
Check warning on line 17 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MessageDecoderFactory.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MessageDecoderFactory.java#L17
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.github.protocol.mdtp.common.model; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Slf4j | ||
public class MessageBodyHeader { | ||
private MessageType messageType; | ||
|
||
private ServiceGroup serviceGroup; | ||
|
||
private DiscoveryServiceCode serviceCode; | ||
|
||
public static final MessageBodyHeader DEVICE_DISCOVERY_REQUEST = | ||
new MessageBodyHeader(MessageType.REQUEST, ServiceGroup.DISCOVERY_SERVICE, DiscoveryServiceCode.DEVICE_DISCOVERY); | ||
|
||
public static final MessageBodyHeader DEVICE_DISCOVERY_RESPONSE = | ||
new MessageBodyHeader(MessageType.RESPONSE, ServiceGroup.DISCOVERY_SERVICE, DiscoveryServiceCode.DEVICE_DISCOVERY); | ||
|
||
public short toShort() { | ||
short messageBodyHeader = 0; | ||
messageBodyHeader |= (short) (this.messageType.getCode() & 0b111); | ||
messageBodyHeader |= (short) ((this.serviceGroup.getCode() & 0b1111111) << 3); | ||
messageBodyHeader |= (short) ((this.serviceCode.getCode() & 0b111111) << 10); | ||
return messageBodyHeader; | ||
} | ||
|
||
public ByteBuf toByteBuf(ByteBuf buffer) { | ||
buffer.writeShort(toShort()); | ||
return buffer; | ||
} | ||
|
||
public static MessageBodyHeader fromByteBuf(ByteBuf buffer) { | ||
short messageBodyHeader = buffer.readShort(); | ||
MessageType messageType = MessageType.fromCode((short) (messageBodyHeader & 0b111)); | ||
ServiceGroup serviceGroup = ServiceGroup.fromCode((short) ((messageBodyHeader >> 3) & 0b1111111)); | ||
DiscoveryServiceCode serviceCode = DiscoveryServiceCode.fromCode((short) ((messageBodyHeader >> 10) & 0b111111)); | ||
return new MessageBodyHeader(messageType, serviceGroup, serviceCode); | ||
Check warning on line 45 in mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageBodyHeader.java Codecov / codecov/patchmdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageBodyHeader.java#L41-L45
|
||
} | ||
} |