message.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2013 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution, and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Seth Hoenig
  11. * Allan Stockdill-Mander
  12. * Mike Robertson
  13. */
  14. package mqtt
  15. import (
  16. "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git/packets"
  17. )
  18. // Message defines the externals that a message implementation must support
  19. // these are received messages that are passed to the callbacks, not internal
  20. // messages
  21. type Message interface {
  22. Duplicate() bool
  23. Qos() byte
  24. Retained() bool
  25. Topic() string
  26. MessageID() uint16
  27. Payload() []byte
  28. }
  29. type message struct {
  30. duplicate bool
  31. qos byte
  32. retained bool
  33. topic string
  34. messageID uint16
  35. payload []byte
  36. }
  37. func (m *message) Duplicate() bool {
  38. return m.duplicate
  39. }
  40. func (m *message) Qos() byte {
  41. return m.qos
  42. }
  43. func (m *message) Retained() bool {
  44. return m.retained
  45. }
  46. func (m *message) Topic() string {
  47. return m.topic
  48. }
  49. func (m *message) MessageID() uint16 {
  50. return m.messageID
  51. }
  52. func (m *message) Payload() []byte {
  53. return m.payload
  54. }
  55. func messageFromPublish(p *packets.PublishPacket) Message {
  56. return &message{
  57. duplicate: p.Dup,
  58. qos: p.Qos,
  59. retained: p.Retain,
  60. topic: p.TopicName,
  61. messageID: p.MessageID,
  62. payload: p.Payload,
  63. }
  64. }
  65. func newConnectMsgFromOptions(options *ClientOptions) *packets.ConnectPacket {
  66. m := packets.NewControlPacket(packets.Connect).(*packets.ConnectPacket)
  67. m.CleanSession = options.CleanSession
  68. m.WillFlag = options.WillEnabled
  69. m.WillRetain = options.WillRetained
  70. m.ClientIdentifier = options.ClientID
  71. if options.WillEnabled {
  72. m.WillQos = options.WillQos
  73. m.WillTopic = options.WillTopic
  74. m.WillMessage = options.WillPayload
  75. }
  76. if options.Username != "" {
  77. m.UsernameFlag = true
  78. m.Username = options.Username
  79. //mustn't have password without user as well
  80. if options.Password != "" {
  81. m.PasswordFlag = true
  82. m.Password = []byte(options.Password)
  83. }
  84. }
  85. m.KeepaliveTimer = uint16(options.KeepAlive)
  86. return m
  87. }