forked from liangdas/mqant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gate_handler.go
282 lines (256 loc) · 7.71 KB
/
gate_handler.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
// Copyright 2014 mqant Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package basegate
import (
"fmt"
"github.com/liangdas/mqant/gate"
"github.com/liangdas/mqant/log"
"github.com/liangdas/mqant/utils"
"strings"
)
type handler struct {
//gate.AgentLearner
//gate.GateHandler
gate gate.Gate
sessions *utils.BeeMap //连接列表
}
func NewGateHandler(gate gate.Gate) *handler {
handler := &handler{
gate: gate,
sessions: utils.NewBeeMap(),
}
return handler
}
//当连接建立 并且MQTT协议握手成功
func (h *handler) Connect(a gate.Agent) {
if a.GetSession() != nil {
h.sessions.Set(a.GetSession().GetSessionId(), a)
}
if h.gate.GetSessionLearner() != nil {
h.gate.GetSessionLearner().Connect(a.GetSession())
}
}
//当连接关闭 或者客户端主动发送MQTT DisConnect命令
func (h *handler) DisConnect(a gate.Agent) {
if a.GetSession() != nil {
h.sessions.Delete(a.GetSession().GetSessionId())
}
if h.gate.GetSessionLearner() != nil {
h.gate.GetSessionLearner().DisConnect(a.GetSession())
}
}
func (h *handler) OnDestroy() {
for _, v := range h.sessions.Items() {
v.(gate.Agent).Close()
}
h.sessions.DeleteAll()
}
/**
*更新整个Session 通常是其他模块拉取最新数据
*/
func (h *handler) Update(span log.TraceSpan, Sessionid string) (result gate.Session, err string) {
agent := h.sessions.Get(Sessionid)
if agent == nil {
err = "No Sesssion found"
return
}
result = agent.(gate.Agent).GetSession()
return
}
/**
*Bind the session with the the Userid.
*/
func (h *handler) Bind(span log.TraceSpan, Sessionid string, Userid string) (result gate.Session, err string) {
agent := h.sessions.Get(Sessionid)
if agent == nil {
err = "No Sesssion found"
return
}
agent.(gate.Agent).GetSession().SetUserId(Userid)
if h.gate.GetStorageHandler() != nil && agent.(gate.Agent).GetSession().GetUserId() != "" {
//可以持久化
data, err := h.gate.GetStorageHandler().Query(Userid)
if err == nil && data != nil {
//有已持久化的数据,可能是上一次连接保存的
impSession, err := h.gate.NewSession(data)
if err == nil {
if agent.(gate.Agent).GetSession().GetSettings() == nil {
agent.(gate.Agent).GetSession().SetSettings(impSession.GetSettings())
} else {
//合并两个map 并且以 agent.(Agent).GetSession().Settings 已有的优先
settings := impSession.GetSettings()
if settings != nil {
for k, v := range settings {
if _, ok := agent.(gate.Agent).GetSession().GetSettings()[k]; ok {
//不用替换
} else {
agent.(gate.Agent).GetSession().GetSettings()[k] = v
}
}
}
//数据持久化
h.gate.GetStorageHandler().Storage(Userid, agent.(gate.Agent).GetSession())
}
} else {
//解析持久化数据失败
log.Warning("Sesssion Resolve fail %s", err.Error())
}
}
}
result = agent.(gate.Agent).GetSession()
return
}
/**
*查询某一个userId是否连接中,这里只是查询这一个网关里面是否有userId客户端连接,如果有多个网关就需要遍历了
*/
func (h *handler) IsConnect(span log.TraceSpan, Sessionid string, Userid string) (bool, string) {
for _, agent := range h.sessions.Items() {
if agent.(gate.Agent).GetSession().GetUserId() == Userid {
return !agent.(gate.Agent).IsClosed(), ""
}
}
return false, fmt.Sprintf("The gateway did not find the corresponding userId 【%s】", Userid)
}
/**
*UnBind the session with the the Userid.
*/
func (h *handler) UnBind(span log.TraceSpan, Sessionid string) (result gate.Session, err string) {
agent := h.sessions.Get(Sessionid)
if agent == nil {
err = "No Sesssion found"
return
}
agent.(gate.Agent).GetSession().SetUserId("")
result = agent.(gate.Agent).GetSession()
return
}
/**
*Push the session with the the Userid.
*/
func (h *handler) Push(span log.TraceSpan, Sessionid string, Settings map[string]string) (result gate.Session, err string) {
agent := h.sessions.Get(Sessionid)
if agent == nil {
err = "No Sesssion found"
return
}
agent.(gate.Agent).GetSession().SetSettings(Settings)
result = agent.(gate.Agent).GetSession()
if h.gate.GetStorageHandler() != nil && agent.(gate.Agent).GetSession().GetUserId() != "" {
err := h.gate.GetStorageHandler().Storage(agent.(gate.Agent).GetSession().GetUserId(), agent.(gate.Agent).GetSession())
if err != nil {
log.Warning("gate session storage failure : %s", err.Error())
}
}
return
}
/**
*Set values (one or many) for the session.
*/
func (h *handler) Set(span log.TraceSpan, Sessionid string, key string, value string) (result gate.Session, err string) {
agent := h.sessions.Get(Sessionid)
if agent == nil {
err = "No Sesssion found"
return
}
agent.(gate.Agent).GetSession().GetSettings()[key] = value
result = agent.(gate.Agent).GetSession()
if h.gate.GetStorageHandler() != nil && agent.(gate.Agent).GetSession().GetUserId() != "" {
err := h.gate.GetStorageHandler().Storage(agent.(gate.Agent).GetSession().GetUserId(), agent.(gate.Agent).GetSession())
if err != nil {
log.Error("gate session storage failure : %s", err.Error())
}
}
return
}
/**
*Remove value from the session.
*/
func (h *handler) Remove(span log.TraceSpan, Sessionid string, key string) (result interface{}, err string) {
agent := h.sessions.Get(Sessionid)
if agent == nil {
err = "No Sesssion found"
return
}
delete(agent.(gate.Agent).GetSession().GetSettings(), key)
result = agent.(gate.Agent).GetSession()
if h.gate.GetStorageHandler() != nil && agent.(gate.Agent).GetSession().GetUserId() != "" {
err := h.gate.GetStorageHandler().Storage(agent.(gate.Agent).GetSession().GetUserId(), agent.(gate.Agent).GetSession())
if err != nil {
log.Error("gate session storage failure :%s", err.Error())
}
}
return
}
/**
*Send message to the session.
*/
func (h *handler) Send(span log.TraceSpan, Sessionid string, topic string, body []byte) (result interface{}, err string) {
agent := h.sessions.Get(Sessionid)
if agent == nil {
err = "No Sesssion found"
return
}
e := agent.(gate.Agent).WriteMsg(topic, body)
if e != nil {
err = e.Error()
} else {
result = "success"
}
return
}
/**
*批量发送消息,sessionid之间用,分割
*/
func (h *handler) SendBatch(span log.TraceSpan, SessionidStr string, topic string, body []byte) (int64, string) {
sessionids := strings.Split(SessionidStr, ",")
var count int64 = 0
for _, sessionid := range sessionids {
agent := h.sessions.Get(sessionid)
if agent == nil {
//log.Warning("No Sesssion found")
continue
}
e := agent.(gate.Agent).WriteMsg(topic, body)
if e != nil {
log.Warning("WriteMsg error:", e.Error())
} else {
count++
}
}
return count, ""
}
func (h *handler) BroadCast(span log.TraceSpan, topic string, body []byte) (int64, string) {
var count int64 = 0
for _, agent := range h.sessions.Items() {
e := agent.(gate.Agent).WriteMsg(topic, body)
if e != nil {
log.Warning("WriteMsg error:", e.Error())
} else {
count++
}
}
return count, ""
}
/**
*主动关闭连接
*/
func (h *handler) Close(span log.TraceSpan, Sessionid string) (result interface{}, err string) {
agent := h.sessions.Get(Sessionid)
if agent == nil {
err = "No Sesssion found"
return
}
agent.(gate.Agent).Close()
return
}