Skip to content

Commit

Permalink
Add mqtt/packet/pubrel.go
Browse files Browse the repository at this point in the history
  • Loading branch information
yosssi committed Dec 25, 2014
1 parent 16abbc0 commit 85d2264
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
50 changes: 50 additions & 0 deletions mqtt/packet/pubrel.go
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
}
7 changes: 7 additions & 0 deletions mqtt/packet/pubrel_options.go
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
}
62 changes: 62 additions & 0 deletions mqtt/packet/pubrel_test.go
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)
}
}

0 comments on commit 85d2264

Please sign in to comment.