-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package packet | ||
|
||
// puback represents a PUBACK Packet. | ||
type pubrel struct { | ||
base | ||
// packetID is the Packet Identifier of the variable header. | ||
packetID uint16 | ||
} | ||
|
||
// PacketID returns the Packet Identifier of the Packet. | ||
func (p *pubrel) PacketID() uint16 { | ||
return p.packetID | ||
} | ||
|
||
// setFixedHeader sets the fixed header to the Packet. | ||
func (p *pubrel) setFixedHeader() { | ||
// Append the first byte to the fixed header. | ||
p.fixedHeader = append(p.fixedHeader, TypePUBREL<<4|0x02) | ||
|
||
// Append the Remaining Length to the fixed header. | ||
p.appendRemainingLength() | ||
} | ||
|
||
// setVariableHeader sets the variable header to the Packet. | ||
func (p *pubrel) setVariableHeader() { | ||
// Append the Packet Identifier to the variable header. | ||
p.variableHeader = append(p.variableHeader, encodeUint16(p.packetID)...) | ||
} | ||
|
||
// NewPUBREL creates and returns a PUBREL Packet. | ||
func NewPUBREL(opts *PUBRELOptions) Packet { | ||
// Initialize the options. | ||
if opts == nil { | ||
opts = &PUBRELOptions{} | ||
} | ||
|
||
// Create a PUBREL Packet. | ||
p := &pubrel{ | ||
packetID: opts.PacketID, | ||
} | ||
|
||
// Set the variable header to the Packet. | ||
p.setVariableHeader() | ||
|
||
// Set the Fixed header to the Packet. | ||
p.setFixedHeader() | ||
|
||
// Return the Packet. | ||
return p | ||
} |
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,7 @@ | ||
package packet | ||
|
||
// PUBRELOptions represents options for a PUBREL Packet. | ||
type PUBRELOptions struct { | ||
// PacketID is the Packet Identifier of the variable header. | ||
PacketID uint16 | ||
} |
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,62 @@ | ||
package packet | ||
|
||
import "testing" | ||
|
||
func Test_pubrel_PacketID(t *testing.T) { | ||
want := uint16(1) | ||
|
||
p := &pubrel{ | ||
packetID: want, | ||
} | ||
|
||
if got := p.PacketID(); got != want { | ||
t.Errorf("got => %d, want => %d", got, want) | ||
} | ||
} | ||
|
||
func Test_pubrel_setFixedHeader(t *testing.T) { | ||
p := &pubrel{ | ||
packetID: 1, | ||
} | ||
|
||
p.variableHeader = []byte{0x00, 0x01} | ||
|
||
p.setFixedHeader() | ||
|
||
want := []byte{ | ||
TypePUBREL<<4 | 0x02, | ||
byte(len(p.variableHeader)), | ||
} | ||
|
||
if len(p.fixedHeader) != len(want) || p.fixedHeader[0] != want[0] || p.fixedHeader[1] != want[1] { | ||
t.Errorf("p.fixedHeader => %v, want => %v", p.fixedHeader, want) | ||
} | ||
} | ||
|
||
func Test_pubrel_setVariableHeader(t *testing.T) { | ||
p := &pubrel{ | ||
packetID: 65534, | ||
} | ||
|
||
p.setVariableHeader() | ||
|
||
want := []byte{0xFF, 0xFE} | ||
|
||
if len(p.variableHeader) != len(want) || p.variableHeader[0] != want[0] || p.variableHeader[1] != want[1] { | ||
t.Errorf("p.variableHeader => %v, want => %v", p.variableHeader, want) | ||
} | ||
} | ||
|
||
func TestNewPUBREL(t *testing.T) { | ||
p := NewPUBREL(nil) | ||
|
||
ptype, err := p.Type() | ||
if err != nil { | ||
nilErrorExpected(t, err) | ||
return | ||
} | ||
|
||
if ptype != TypePUBREL { | ||
t.Errorf("ptype => %X, want => %X", ptype, TypePUBREL) | ||
} | ||
} |