forked from VolantMQ/volantmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
59 lines (49 loc) · 1.67 KB
/
server.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
package systree
import (
"encoding/json"
"github.com/VolantMQ/vlapi/mqttp"
"github.com/VolantMQ/volantmq/types"
)
type server struct {
version string
upTime *dynamicValueUpTime
currTime *dynamicValueCurrentTime
capabilities struct {
SupportedVersions []mqttp.ProtocolVersion
MaxQoS string
MaxConnections uint64
MaximumPacketSize uint32
ServerKeepAlive uint16
ReceiveMaximum uint16
RetainAvailable bool
WildcardSubscriptionAvailable bool
SubscriptionIDAvailable bool
SharedSubscriptionAvailable bool
}
}
func newServer(topicPrefix string, dynRetains, staticRetains *[]types.RetainObject) server {
b := server{
upTime: newDynamicValueUpTime(topicPrefix + "/uptime"),
currTime: newDynamicValueCurrentTime(topicPrefix + "/datetime"),
version: "1.0.0",
}
m, _ := mqttp.New(mqttp.ProtocolV311, mqttp.PUBLISH)
msg, _ := m.(*mqttp.Publish)
msg.SetQoS(mqttp.QoS0) // nolint: errcheck
msg.SetTopic(topicPrefix + "/version") // nolint: errcheck
msg.SetPayload([]byte(b.version))
*dynRetains = append(*dynRetains, b.upTime)
*dynRetains = append(*dynRetains, b.currTime)
*staticRetains = append(*staticRetains, msg)
m, _ = mqttp.New(mqttp.ProtocolV311, mqttp.PUBLISH)
msg, _ = m.(*mqttp.Publish)
msg.SetQoS(mqttp.QoS0) // nolint: errcheck
msg.SetTopic(topicPrefix + "/capabilities") // nolint: errcheck
if data, err := json.Marshal(&b.capabilities); err == nil {
msg.SetPayload(data)
} else {
msg.SetPayload([]byte(err.Error()))
}
*staticRetains = append(*staticRetains, msg)
return b
}