mirror of
https://github.com/deavmi/doap
synced 2024-07-04 15:50:15 +02:00
Packet
- Added `setType(MessageType)` which sets the messagr type - The `type` field is no longer a `ubyte` but rather a `MessageType` Packet (unit tests) - Test the encoding of the type field Types - Added new module - Added `MessageType` enum
This commit is contained in:
parent
8e8d1d2ba1
commit
d3ffdd46db
|
@ -1,5 +1,6 @@
|
||||||
module doap.packet;
|
module doap.packet;
|
||||||
|
|
||||||
|
import doap.types : MessageType;
|
||||||
import doap.codes : Code;
|
import doap.codes : Code;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,7 +8,8 @@ import doap.codes : Code;
|
||||||
*/
|
*/
|
||||||
public class CoapPacket
|
public class CoapPacket
|
||||||
{
|
{
|
||||||
private ubyte ver, type;
|
private ubyte ver;
|
||||||
|
private MessageType type;
|
||||||
private ubyte tokenLen;
|
private ubyte tokenLen;
|
||||||
private Code code;
|
private Code code;
|
||||||
private ushort mid;
|
private ushort mid;
|
||||||
|
@ -40,6 +42,11 @@ public class CoapPacket
|
||||||
return encoded;
|
return encoded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setType(MessageType type)
|
||||||
|
{
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
// public ubyte getVersion()
|
// public ubyte getVersion()
|
||||||
// {
|
// {
|
||||||
|
|
||||||
|
@ -47,6 +54,11 @@ public class CoapPacket
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
version(unittest)
|
||||||
|
{
|
||||||
|
import std.stdio;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encoding tests
|
* Encoding tests
|
||||||
*
|
*
|
||||||
|
@ -58,6 +70,8 @@ public class CoapPacket
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
CoapPacket packet = new CoapPacket();
|
CoapPacket packet = new CoapPacket();
|
||||||
|
packet.setType(MessageType.RESET);
|
||||||
|
|
||||||
ubyte[] encoded = packet.getBytes();
|
ubyte[] encoded = packet.getBytes();
|
||||||
|
|
||||||
ubyte firstByte = encoded[0];
|
ubyte firstByte = encoded[0];
|
||||||
|
@ -66,5 +80,11 @@ unittest
|
||||||
ubyte versionField = cast(ubyte)(firstByte & 192) >> 6;
|
ubyte versionField = cast(ubyte)(firstByte & 192) >> 6;
|
||||||
assert(versionField == 1);
|
assert(versionField == 1);
|
||||||
|
|
||||||
|
// Ensure the type is 3/RESET
|
||||||
|
writeln(firstByte);
|
||||||
|
ubyte typeField = cast(ubyte)(firstByte & 48) >> 4;
|
||||||
|
writeln(typeField);
|
||||||
|
writeln(cast(ubyte)MessageType.RESET);
|
||||||
|
assert(typeField == MessageType.RESET);
|
||||||
|
|
||||||
}
|
}
|
12
source/doap/types.d
Normal file
12
source/doap/types.d
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
module doap.types;
|
||||||
|
|
||||||
|
public enum MessageType : ubyte
|
||||||
|
{
|
||||||
|
// Request
|
||||||
|
CONFIRMABLE = 0,
|
||||||
|
NON_CONFIRMABLE = 1,
|
||||||
|
|
||||||
|
// Response
|
||||||
|
ACKNOWLEDGEMENT = 2,
|
||||||
|
RESET = 3
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user