-
Notifications
You must be signed in to change notification settings - Fork 24
/
mqtt.go
293 lines (258 loc) · 7.24 KB
/
mqtt.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package mqtt
import (
"fmt"
"sync"
"net"
"io"
log "github.com/cihub/seelog"
)
/* Glabal status */
var G_clients map[string]*ClientRep = make(map[string]*ClientRep)
var G_clients_lock *sync.Mutex = new(sync.Mutex)
// Map topic => sub
// sub is implemented as map, key is client_id, value is qos
var G_subs map[string]map[string]uint8 = make(map[string]map[string]uint8)
var G_subs_lock *sync.Mutex = new(sync.Mutex)
var G_redis_client *RedisClient = StartRedisClient()
// This function should be called upon starting up. It will
// try to recover global status(G_subs, etc) from Redis.
func RecoverFromRedis() {
// Reconstruct the subscription map from redis records
client_id_keys := G_redis_client.GetSubsClients()
log.Info("Recovering subscription info from Redis")
for _, client_id_key := range(client_id_keys) {
sub_map := make(map[string]uint8)
G_redis_client.Fetch(client_id_key, &sub_map)
var client_id string
fmt.Sscanf(client_id_key, "gossipd.client-subs.%s", &client_id)
for topic, qos := range(sub_map) {
// lock won't be needed since this is at the startup phase
subs := G_subs[topic]
if subs == nil {
log.Debug("current subscription is the first client to topic:", topic)
subs = make(map[string]uint8)
G_subs[topic] = subs
}
subs[client_id] = qos
}
log.Debugf("client(%s) subscription info recovered", client_id)
}
}
func (mqtt *Mqtt)Show() {
if mqtt.FixedHeader != nil {
mqtt.FixedHeader.Show()
} else {
log.Debug("Fixed header is nil")
}
if mqtt.ConnectFlags != nil {
mqtt.ConnectFlags.Show()
} else {
log.Debug("ConnectFlags is nil")
}
fmt.Println("ProtocolName:", mqtt.ProtocolName)
fmt.Println("Version:", mqtt.ProtocolVersion)
fmt.Println("TopicName:", mqtt.TopicName)
fmt.Println("ClientId:", mqtt.ClientId)
fmt.Println("WillTopic:", mqtt.WillTopic)
fmt.Println("WillMessage:", mqtt.WillMessage)
fmt.Println("Username:", mqtt.Username)
fmt.Println("Password:", mqtt.Password)
fmt.Println("KeepAliveTimer:", mqtt.KeepAliveTimer)
fmt.Println("MessageId:", mqtt.MessageId)
fmt.Println("Data:", mqtt.Data)
fmt.Println("Topics:", len(mqtt.Topics))
for i := 0; i < len(mqtt.Topics); i++ {
fmt.Printf("(%s) (qos=%d)\n", mqtt.Topics[i], mqtt.Topics_qos[i])
}
fmt.Println("ReturnCode:", mqtt.ReturnCode)
}
func (header *FixedHeader)Show() {
fmt.Println("header detail:")
fmt.Println("message type: ", MessageTypeStr(header.MessageType))
fmt.Println("DupFlag: ", header.DupFlag)
fmt.Println("Retain: ", header.Retain)
fmt.Println("QOS: ", header.QosLevel)
fmt.Println("length: ", header.Length)
fmt.Println("\n=====================\n")
}
func (flags *ConnectFlags)Show() {
fmt.Println("connect flags detail:")
fmt.Println("UsernameFlag:", flags.UsernameFlag)
fmt.Println("PasswordFlag:", flags.PasswordFlag)
fmt.Println("WillRetain:", flags.WillRetain)
fmt.Println("WillFlag:", flags.WillFlag)
fmt.Println("WillQos:", flags.WillQos)
fmt.Println("CleanSession:", flags.CleanSession)
fmt.Println("\n=====================\n")
}
func ReadFixedHeader(conn *net.Conn) *FixedHeader {
var buf = make([]byte, 2)
n, _ := io.ReadFull(*conn, buf)
if n != len(buf) {
log.Debug("read header failed")
return nil
}
byte1 := buf[0]
header := new(FixedHeader)
header.MessageType = uint8(byte1 & 0xF0 >> 4)
header.DupFlag = byte1 & 0x08 > 0
header.QosLevel = uint8(byte1 & 0x06 >> 1)
header.Retain = byte1 & 0x01 > 0
byte2 := buf[1]
header.Length = decodeVarLength(byte2, conn)
return header
}
func ReadCompleteCommand(conn *net.Conn) (*FixedHeader, []byte) {
fixed_header := ReadFixedHeader(conn)
if fixed_header == nil {
log.Debug("failed to read fixed header")
return nil, make([]byte, 0)
}
length := fixed_header.Length
buf := make([]byte, length)
n, _ := io.ReadFull(*conn, buf)
if uint32(n) != length {
panic(fmt.Sprintf("failed to read %d bytes specified in fixed header, only %d read", length, n))
}
log.Debugf("Complete command(%s) read into buffer", MessageTypeStr(fixed_header.MessageType))
return fixed_header, buf
}
// CONNECT parse
func parseConnectInfo(buf []byte) *ConnectInfo {
var info = new(ConnectInfo)
info.Protocol, buf = parseUTF8(buf)
info.Version, buf = parseUint8(buf)
flagByte := buf[0]
log.Debug("parsing connect flag:", flagByte)
info.UsernameFlag = (flagByte & 0x80) > 0
info.PasswordFlag = (flagByte & 0x40) > 0
info.WillRetain = (flagByte & 0x20) > 0
info.WillQos = uint8(flagByte & 0x18 >> 3)
info.WillFlag = (flagByte & 0x04) > 0
info.CleanSession = (flagByte & 0x02) > 0
buf = buf[1:]
info.Keepalive, _ = parseUint16(buf)
return info
}
func (con_info *ConnectInfo)Show() {
fmt.Println("connect info detail:")
fmt.Println("Protocol:", con_info.Protocol)
fmt.Println("Version:", con_info.Version)
fmt.Println("Username Flag:", con_info.UsernameFlag)
fmt.Println("Password Flag:", con_info.PasswordFlag)
fmt.Println("WillRetain:", con_info.WillRetain)
fmt.Println("WillQos:", con_info.WillQos)
fmt.Println("WillFlag:", con_info.WillFlag)
fmt.Println("CleanSession:", con_info.CleanSession)
fmt.Println("Keepalive:", con_info.Keepalive)
}
// Fixed header parse
func decodeVarLength(cur byte, conn *net.Conn) uint32 {
length := uint32(0)
multi := uint32(1)
for {
length += multi * uint32(cur & 0x7f)
if cur & 0x80 == 0 {
break
}
buf := make([]byte, 1)
n, _ := io.ReadFull(*conn, buf)
if n != 1 {
panic("failed to read variable length in MQTT header")
}
cur = buf[0]
multi *= 128
}
return length
}
func parseUint16(buf []byte) (uint16, []byte) {
return uint16(buf[0] << 8) + uint16(buf[1]), buf[2:]
}
func parseUint8(buf []byte) (uint8, []byte) {
return uint8(buf[0]), buf[1:]
}
func parseUTF8(buf []byte) (string, []byte) {
length, buf := parseUint16(buf)
str := buf [: length]
return string(str), buf[length:]
}
func MessageTypeStr(mt uint8) string {
var strArray = []string {
"reserved",
"CONNECT",
"CONNACK",
"PUBLISH",
"PUBACK",
"PUBREC",
"PUBREL",
"PUBCOMP",
"SUBSCRIBE",
"SUBACK",
"UNSUBSCRIBE",
"UNSUBACK",
"PINGREQ",
"PINGRESP",
"DISCONNEC"}
if mt > uint8(len(strArray)) {
return "Undefined MessageType"
}
return strArray[mt]
}
// Types
const(
CONNECT = uint8(iota + 1)
CONNACK
PUBLISH
PUBACK
PUBREC
PUBREL
PUBCOMP
SUBSCRIBE
SUBACK
UNSUBSCRIBE
UNSUBACK
PINGREQ
PINGRESP
DISCONNECT
)
const(
ACCEPTED = uint8(iota)
UNACCEPTABLE_PROTOCOL_VERSION
IDENTIFIER_REJECTED
SERVER_UNAVAILABLE
BAD_USERNAME_OR_PASSWORD
NOT_AUTHORIZED
)
type Mqtt struct{
FixedHeader *FixedHeader
ProtocolName, TopicName, ClientId, WillTopic, WillMessage, Username, Password string
ProtocolVersion uint8
ConnectFlags *ConnectFlags
KeepAliveTimer, MessageId uint16
Data []byte
Topics []string
Topics_qos []uint8
ReturnCode uint8
}
type ConnectFlags struct{
UsernameFlag, PasswordFlag, WillRetain, WillFlag, CleanSession bool
WillQos uint8
}
type ConnectInfo struct {
Protocol string // Must be 'MQIsdp' for now
Version uint8
UsernameFlag bool
PasswordFlag bool
WillRetain bool
WillQos uint8
WillFlag bool
CleanSession bool
Keepalive uint16
}
type FixedHeader struct {
MessageType uint8
DupFlag bool
Retain bool
QosLevel uint8
Length uint32
}