-
Notifications
You must be signed in to change notification settings - Fork 2
/
message_mon_subscribe.go
87 lines (74 loc) · 1.93 KB
/
message_mon_subscribe.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
// message_mon_subscribe.go - define the mon subscribe message
package gorados
import (
"bytes"
"encoding/binary"
)
const (
SUBSCRIBE_MONMAP = "monmap"
SUBSCRIBE_MGRMAP = "mgrmap"
SUBSCRIBE_OSDMAP = "osdmap"
SUBSCRIBE_CONFIG = "config"
)
type subscribeItem struct {
StartTime uint64
Flags uint8
}
// MessageMonSubscribe is used to subscribe client to the rados cluster monitor. No need to
// handle subscribtion return message. Decode implements the `config` returned message.
type MessageMonSubscribe struct {
message
Hostname string
What map[string]subscribeItem
}
func NewMessageMonSubscribe(hn string) *MessageMonSubscribe {
result := &MessageMonSubscribe{Hostname: hn}
result.Header.Type = MSG_MON_SUBSCRIBE
return result
}
func (m *MessageMonSubscribe) Name() string { return "MonSubscribe" }
func (m *MessageMonSubscribe) Add(name string, content *subscribeItem) {
if m.What == nil {
m.What = make(map[string]subscribeItem)
}
m.What[name] = *content
}
func (m *MessageMonSubscribe) Encode() error {
var b bytes.Buffer
if err := binary.Write(&b, binary.LittleEndian, uint32(len(m.What))); err != nil {
return err
}
for k := range m.What {
nameBytes, err := encodeSizeValue(len(k), []byte(k))
if err != nil {
return err
}
if _, err := b.Write(nameBytes); err != nil {
return err
}
if err := binary.Write(&b, binary.LittleEndian, m.What[k]); err != nil {
return err
}
}
hostnameBytes, err := encodeSizeValue(len(m.Hostname), []byte(m.Hostname))
if err != nil {
return err
}
if _, err := b.Write(hostnameBytes); err != nil {
return err
}
// Put the payload to the `Payload` field.
if m.Payload == nil {
m.Payload = make([]byte, 0)
}
m.Payload = append(m.Payload, b.Bytes()...)
return nil
}
func (m *MessageMonSubscribe) Decode(input []byte) (int, error) {
size, data, err := decodeSizeValue(input, 1)
if err != nil {
return 0, err
}
m.Payload = data
return size + SIZE_BYTES, nil
}