-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
242 lines (224 loc) · 5.92 KB
/
client.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
package magina
import (
"log"
"net"
"strings"
"time"
"github.com/ruizeng/magina/packets"
"github.com/streadway/amqp"
)
// Client a client servces an MQTT remote client connection
type Client struct {
Conn net.Conn
Broker *Broker
Identifier string
LastHeartbeat time.Time
KeepAliveInterval int
needDisconnect bool
Channel *amqp.Channel
Exchangers map[string]Exchanger
SubscribeTopics map[string]string
}
func (c *Client) initRabbit() error {
var err error
if c.Channel == nil {
c.Channel, err = c.Broker.RabbitConnection.Channel()
if err != nil {
return err
}
err = c.initExchangers()
if err != nil {
return err
}
if c.SubscribeTopics == nil {
c.SubscribeTopics = make(map[string]string)
}
for topic := range c.SubscribeTopics {
err := c.subscribe(topic)
if err != nil {
return err
}
}
}
return nil
}
func (c *Client) initExchangers() error {
c.Exchangers = make(map[string]Exchanger)
c.Exchangers[""] = NewPubSubExchanger(c.Channel)
c.Exchangers["rpc"] = NewPRCExchanger(c.Channel)
for exc := range c.Exchangers {
err := c.Exchangers[exc].Init()
if err != nil {
return err
}
}
return nil
}
func (c *Client) getExchanger(topic string) Exchanger {
if strings.HasPrefix(topic, "rpc://") {
return c.Exchangers["rpc"]
}
return c.Exchangers[""]
}
func (c *Client) trySendPacket(packet packets.ControlPacket) error {
log.Printf("try send packet******** \n%v\n**********\n", packet)
return packet.Write(c.Conn)
}
func (c *Client) handlePublish(pub *packets.PublishPacket) error {
exchanger := c.getExchanger(pub.TopicName)
err := exchanger.Publish(ExchangeMessage{pub.TopicName, pub.Payload})
if pub.Qos == 1 {
puback := packets.NewControlPacket(packets.Puback).(*packets.PubackPacket)
puback.MessageID = pub.MessageID
puback.Qos = pub.Qos
c.trySendPacket(puback)
}
return err
}
func (c *Client) subscribe(topic string) error {
exchanger := c.getExchanger(topic)
msgs, err := exchanger.Subscribe(topic)
if err != nil {
return err
}
go func() {
for d := range msgs {
pub := packets.NewControlPacket(packets.Publish).(*packets.PublishPacket)
pub.Payload = d.Payload
pub.TopicName = d.Topic
c.trySendPacket(pub)
}
}()
return nil
}
func (c *Client) handleSubscribe(sub *packets.SubscribePacket) error {
topic := sub.Topics[0]
c.SubscribeTopics[topic] = topic
err := c.subscribe(topic)
if err != nil {
return err
}
suback := packets.NewControlPacket(packets.Suback).(*packets.SubackPacket)
suback.MessageID = sub.MessageID
suback.Qos = sub.Qos
c.trySendPacket(suback)
return nil
}
func (c *Client) handleUnsubscribe(unsub *packets.UnsubscribePacket) error {
topic := unsub.Topics[0] // only suport one topic a time now
exchanger := c.getExchanger(topic)
err := exchanger.Unsubscribe(topic)
if err != nil {
return err
}
unsuback := packets.NewControlPacket(packets.Unsuback).(*packets.UnsubackPacket)
unsuback.MessageID = unsub.MessageID
unsuback.Qos = unsub.Qos
return c.trySendPacket(unsuback)
}
func (c *Client) checkHeartbeat() {
for {
if c.needDisconnect {
break
}
checkInterval := c.KeepAliveInterval
if checkInterval == 0 {
checkInterval = 60
}
log.Printf("timer, check interval: %v", checkInterval)
time.Sleep(time.Second * time.Duration(checkInterval))
// no ping for 2 times
if time.Since(c.LastHeartbeat) > time.Second*time.Duration(2*checkInterval) {
log.Printf("client lost: %v", c.Identifier)
if c.Broker.OnClientOffline != nil {
c.Broker.OnClientOffline(c)
}
c.needDisconnect = true
}
}
}
// Serve serves a mqtt client.
func (c *Client) Serve() {
defer func() {
if c.Channel != nil {
c.Channel.Close()
c.Channel = nil
}
if c.Conn != nil {
c.Conn.Close()
c.Conn = nil
}
}()
for {
if c.needDisconnect {
break
}
packet, err := packets.ReadPacket(c.Conn)
if err != nil {
// log.Printf("reading packets from connection error: %v", err)
c.needDisconnect = true
break
}
log.Printf("packet received =========\n%v\n===============\n", packet)
switch packet.(type) {
case *packets.ConnectPacket:
conn := packet.(*packets.ConnectPacket)
ca := packets.NewControlPacket(packets.Connack).(*packets.ConnackPacket)
ca.ReturnCode = conn.Validate()
c.Identifier = conn.ClientIdentifier
c.KeepAliveInterval = int(conn.KeepaliveTimer)
if c.Broker.Authenticate != nil {
if !c.Broker.Authenticate(c, string(conn.Username), string(conn.Password)) {
ca.ReturnCode = packets.ErrRefusedBadUsernameOrPassword
}
}
err = c.initRabbit()
if err != nil {
log.Printf("init rabbitmq for client failed: %v\n", err)
ca.ReturnCode = packets.ErrRefusedServerUnavailable
}
if ca.ReturnCode != packets.Accepted {
c.needDisconnect = true
} else {
if c.Broker.OnClientOnline != nil {
go func() { c.Broker.OnClientOnline(c) }()
}
go c.checkHeartbeat()
}
err = c.trySendPacket(ca)
case *packets.DisconnectPacket:
log.Println("disconnecting client...")
c.needDisconnect = true
case *packets.PingreqPacket:
pres := packets.NewControlPacket(packets.Pingresp)
log.Println("ping back to client...")
err = c.trySendPacket(pres)
case *packets.PublishPacket:
pub := packet.(*packets.PublishPacket)
err = c.handlePublish(pub)
case *packets.SubscribePacket:
sub := packet.(*packets.SubscribePacket)
err = c.handleSubscribe(sub)
case *packets.UnsubscribePacket:
unsub := packet.(*packets.UnsubscribePacket)
err = c.handleUnsubscribe(unsub)
default:
log.Println("unknown packet: ", packet)
}
if err != nil {
log.Println("handle packat error: ", err)
if err == amqp.ErrClosed {
c.Channel = nil
err := c.initRabbit()
if err != nil {
log.Println("wanning: reinit rabbit error: ", err)
continue
}
}
}
c.LastHeartbeat = time.Now()
if c.Broker.OnClientHeartbeat != nil {
go func() { c.Broker.OnClientHeartbeat(c) }()
}
}
}