-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: wei <[email protected]>
- Loading branch information
Showing
32 changed files
with
505 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...mon/src/main/java/io/github/protocol/mdtp/common/codec/DeviceDiscoveryRequestDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
@Override | ||
public AbstractMessageBody handle(ByteBuf in) { | ||
return DeviceDiscoveryRequest.fromByteBuf(in); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MdtpDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.readByteBuf(in); | ||
|
||
MessageBodyHeader messageBodyHeader = MessageBodyHeader.readByteBuf(in); | ||
MessageBodyDecoder messageDecode = MessageDecoderFactory.getDecoder(messageBodyHeader); | ||
AbstractMessageBody messageBody = messageDecode.handle(in); | ||
messageBody.setMessageBodyHeader(messageBodyHeader); | ||
|
||
mdtpPacket.setHeader(header); | ||
mdtpPacket.setBody(messageBody); | ||
|
||
out.add(mdtpPacket); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MessageBodyDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
19 changes: 19 additions & 0 deletions
19
mdtp-common/src/main/java/io/github/protocol/mdtp/common/codec/MessageDecoderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); | ||
|
||
static { | ||
decoders.put(MessageBodyHeader.DEVICE_DISCOVERY_REQUEST.toShort(), new DeviceDiscoveryRequestDecoder()); | ||
} | ||
|
||
public static MessageBodyDecoder getDecoder(MessageBodyHeader header) { | ||
return decoders.get(header.toShort()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
mdtp-common/src/main/java/io/github/protocol/mdtp/common/model/MessageBodyHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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 void writeByteBuf(ByteBuf buffer) { | ||
buffer.writeShort(toShort()); | ||
} | ||
|
||
public static MessageBodyHeader readByteBuf(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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.