forked from eclipse/paho.golang
/
cp_publish.go
103 lines (93 loc) · 2.96 KB
/
cp_publish.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package paho
import (
"bytes"
"fmt"
"github.com/netdata/paho.golang/packets"
)
type (
// Publish is a reporesentation of the MQTT Publish packet
Publish struct {
QoS byte
Retain bool
Topic string
Properties *PublishProperties
Payload []byte
}
// PublishProperties is a struct of the properties that can be set
// for a Publish packet
PublishProperties struct {
CorrelationData []byte
ContentType string
ResponseTopic string
PayloadFormat *byte
MessageExpiry *uint32
SubscriptionIdentifier *uint32
TopicAlias *uint16
User map[string]string
}
)
// InitProperties is a function that takes a lower level
// Properties struct and completes the properties of the Publish on
// which it is called
func (p *Publish) InitProperties(prop *packets.Properties) {
p.Properties = &PublishProperties{
PayloadFormat: prop.PayloadFormat,
MessageExpiry: prop.MessageExpiry,
ContentType: prop.ContentType,
ResponseTopic: prop.ResponseTopic,
CorrelationData: prop.CorrelationData,
TopicAlias: prop.TopicAlias,
SubscriptionIdentifier: prop.SubscriptionIdentifier,
User: prop.User,
}
}
// Packet returns a packets library Publish from the paho Publish
// on which it is called
func (p *Publish) Packet() *packets.Publish {
v := &packets.Publish{
QoS: p.QoS,
Retain: p.Retain,
Topic: p.Topic,
Payload: p.Payload,
}
if p.Properties != nil {
v.Properties = &packets.Properties{
PayloadFormat: p.Properties.PayloadFormat,
MessageExpiry: p.Properties.MessageExpiry,
ContentType: p.Properties.ContentType,
ResponseTopic: p.Properties.ResponseTopic,
CorrelationData: p.Properties.CorrelationData,
TopicAlias: p.Properties.TopicAlias,
SubscriptionIdentifier: p.Properties.SubscriptionIdentifier,
User: p.Properties.User,
}
}
return v
}
func (p *Publish) String() string {
var b bytes.Buffer
fmt.Fprintf(&b, "topic: %s qos: %d retain: %t\n", p.Topic, p.QoS, p.Retain)
if p.Properties.PayloadFormat != nil {
fmt.Fprintf(&b, "PayloadFormat: %v\n", p.Properties.PayloadFormat)
}
if p.Properties.MessageExpiry != nil {
fmt.Fprintf(&b, "MessageExpiry: %v\n", p.Properties.MessageExpiry)
}
if p.Properties.ContentType != "" {
fmt.Fprintf(&b, "ContentType: %v\n", p.Properties.ContentType)
}
if p.Properties.ResponseTopic != "" {
fmt.Fprintf(&b, "ResponseTopic: %v\n", p.Properties.ResponseTopic)
}
if p.Properties.CorrelationData != nil {
fmt.Fprintf(&b, "CorrelationData: %v\n", p.Properties.CorrelationData)
}
if p.Properties.SubscriptionIdentifier != nil {
fmt.Fprintf(&b, "SubscriptionIdentifier: %v\n", p.Properties.SubscriptionIdentifier)
}
for k, v := range p.Properties.User {
fmt.Fprintf(&b, "User: %s : %s\n", k, v)
}
b.WriteString(string(p.Payload))
return b.String()
}