puback.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package packets
  2. import (
  3. "fmt"
  4. "github.com/pborman/uuid"
  5. "io"
  6. )
  7. //PubackPacket is an internal representation of the fields of the
  8. //Puback MQTT packet
  9. type PubackPacket struct {
  10. FixedHeader
  11. MessageID uint16
  12. uuid uuid.UUID
  13. }
  14. func (pa *PubackPacket) String() string {
  15. str := fmt.Sprintf("%s\n", pa.FixedHeader)
  16. str += fmt.Sprintf("messageID: %d", pa.MessageID)
  17. return str
  18. }
  19. func (pa *PubackPacket) Write(w io.Writer) error {
  20. var err error
  21. pa.FixedHeader.RemainingLength = 2
  22. packet := pa.FixedHeader.pack()
  23. packet.Write(encodeUint16(pa.MessageID))
  24. _, err = packet.WriteTo(w)
  25. return err
  26. }
  27. //Unpack decodes the details of a ControlPacket after the fixed
  28. //header has been read
  29. func (pa *PubackPacket) Unpack(b io.Reader) {
  30. pa.MessageID = decodeUint16(b)
  31. }
  32. //Details returns a Details struct containing the Qos and
  33. //MessageID of this ControlPacket
  34. func (pa *PubackPacket) Details() Details {
  35. return Details{Qos: pa.Qos, MessageID: pa.MessageID}
  36. }
  37. //UUID returns the unique ID assigned to the ControlPacket when
  38. //it was originally received. Note: this is not related to the
  39. //MessageID field for MQTT packets
  40. func (pa *PubackPacket) UUID() uuid.UUID {
  41. return pa.uuid
  42. }