-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevent.go
299 lines (216 loc) · 4.82 KB
/
event.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
291
292
293
294
295
296
297
298
299
package mqtt
import "net"
////////////////////Interface//////////////////////////////
type EventType int
const (
EVENT_CONNECT EventType = iota
EVENT_PUBLISH
EVENT_SUBSCRIBE
EVENT_UNSUBSCRIBE
EVENT_TIMEOUT
EVENT_IOEXCEPTION
EVENT_SESSION_TERMINATED
)
type TimeoutType int
const (
TIMEOUT_RETRANSMIT TimeoutType = iota
TIMEOUT_SESSION
)
type Event interface {
GetEventType() EventType
GetSession() Session
}
type EventConnect interface {
Event
GetConnectFlags() byte
GetKeepAlive() uint16
GetClientId() string
GetWillTopic() string
GetWillMessage() string
GetUserName() string
GetPassword() []byte
}
type EventPublish interface {
Event
GetMessage() Message
}
type EventSubscribe interface {
Event
GetPacketId() uint16
GetSubscribeTopics() []string
GetQoSs() []QOS
}
type EventUnsubscribe interface {
Event
GetUnsubscribeTopics() []string
}
type EventTimeout interface {
Event
GetTimeoutType() TimeoutType
}
type EventIOException interface {
Event
GetRemoteAddr() net.Addr
}
type EventSessionTerminated interface {
Event
GetReason() string
GetWillMessage() Message
}
////////////////////Implementation////////////////////////
type event struct {
eventType EventType
session Session
}
func newEvent(e EventType, s Session) *event {
return &event{eventType: e, session: s}
}
func (this *event) GetEventType() EventType {
return this.eventType
}
func (this *event) GetSession() Session {
return this.session
}
type event_connect struct {
event
connectFlags byte
keepAlive uint16
clientId string
willTopic string
willMessage string
userName string
password []byte
}
func newEventConnect(s Session, p PacketConnect) *event_connect {
this := &event_connect{}
this.eventType = EVENT_CONNECT
this.session = s
this.connectFlags = p.GetConnectFlags()
this.keepAlive = p.GetKeepAlive()
this.clientId = p.GetClientId()
this.willTopic = p.GetWillTopic()
this.willMessage = p.GetWillMessage()
this.userName = p.GetUserName()
this.password = p.GetPassword()
return this
}
func (this *event_connect) GetConnectFlags() byte {
return this.connectFlags
}
func (this *event_connect) GetKeepAlive() uint16 {
return this.keepAlive
}
func (this *event_connect) GetClientId() string {
return this.clientId
}
func (this *event_connect) GetWillTopic() string {
return this.willTopic
}
func (this *event_connect) GetWillMessage() string {
return this.willMessage
}
func (this *event_connect) GetUserName() string {
return this.userName
}
func (this *event_connect) GetPassword() []byte {
return this.password
}
type event_publish struct {
event
msg Message
}
func newEventPublish(s Session, m Message) *event_publish {
this := &event_publish{}
this.eventType = EVENT_PUBLISH
this.session = s
this.msg = m
return this
}
func (this *event_publish) GetMessage() Message {
return this.msg
}
type event_subscribe struct {
event
packetid uint16
topics []string
qos []QOS
}
func newEventSubscribe(s Session, p uint16, t []string, q []QOS) *event_subscribe {
this := &event_subscribe{}
this.eventType = EVENT_SUBSCRIBE
this.session = s
this.packetid = p
this.topics = t
this.qos = q
return this
}
func (this *event_subscribe) GetPacketId() uint16 {
return this.packetid
}
func (this *event_subscribe) GetSubscribeTopics() []string {
return this.topics
}
func (this *event_subscribe) GetQoSs() []QOS {
return this.qos
}
type event_unsubscribe struct {
event
topics []string
}
func newEventUnsubscribe(s Session, t []string) *event_unsubscribe {
this := &event_unsubscribe{}
this.eventType = EVENT_UNSUBSCRIBE
this.session = s
this.topics = t
return this
}
func (this *event_unsubscribe) GetUnsubscribeTopics() []string {
return this.topics
}
type event_timeout struct {
event
timeoutType TimeoutType
}
func newEventTimeout(s Session, t TimeoutType) *event_timeout {
this := &event_timeout{}
this.eventType = EVENT_TIMEOUT
this.session = s
this.timeoutType = t
return this
}
func (this *event_timeout) GetTimeoutType() TimeoutType {
return this.timeoutType
}
type event_ioexception struct {
event
addr net.Addr
}
func newEventIOException(s Session, addr net.Addr) *event_ioexception {
this := &event_ioexception{}
this.eventType = EVENT_IOEXCEPTION
this.session = s
this.addr = addr
return this
}
func (this *event_ioexception) GetRemoteAddr() net.Addr {
return this.addr
}
type event_session_terminated struct {
event
reason string
will Message
}
func newEventSessionTerminated(s Session, r string, w Message) *event_session_terminated {
this := &event_session_terminated{}
this.eventType = EVENT_SESSION_TERMINATED
this.session = s
this.reason = r
this.will = w
return this
}
func (this *event_session_terminated) GetReason() string {
return this.reason
}
func (this *event_session_terminated) GetWillMessage() Message {
return this.will
}