suback.go 1.4 KB

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