This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopics.go
270 lines (228 loc) · 6.25 KB
/
topics.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
package libwimark
import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strconv"
)
const MQTT_ANY_WILDCARD = "+"
const MQTT_MULTILEVEL_WILDCARD = "#"
const (
TOPIC_STATUS_FORMAT = `B/%s/%s`
TOPIC_STATUS_REGEXP = `B/(.*)/(.*)`
TOPIC_LOG_FORMAT = `LOG/%s/%s`
TOPIC_LOG_REGEXP = `LOG/(.*)/(.*)`
TOPIC_EVENT_FORMAT = `EVENT/%s/%s/%s`
TOPIC_EVENT_REGEXP = `EVENT/(.*)/(.*)/(.*)`
TOPIC_REQ_FORMAT = `REQ/%s/%s/%s/%s/%s/%s/%s`
TOPIC_REQ_REGEXP = `REQ/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)`
TOPIC_RSP_FORMAT = `RSP/%s/%s/%s/%s/%s`
TOPIC_RSP_REGEXP = `RSP/(.*)/(.*)/(.*)/(.*)/(.*)`
)
var errTopicParse = errors.New("failed to parse topic")
type Topic interface {
TopicPath() string
}
type BroadcastTopic struct {
SenderModule Module
SenderID string
}
func ParseBroadcastTopic(s string, format string) (BroadcastTopic, error) {
var v BroadcastTopic
var r = regexp.MustCompile(format)
var ds = r.FindAllStringSubmatch(s, -1)
if len(ds) == 1 {
var data = ds[0]
if data != nil && len(data) == 2+1 {
var smodule Module
var smodule_err = json.Unmarshal([]byte(strconv.Quote(data[1])), &smodule)
if smodule_err == nil {
v.SenderModule = smodule
v.SenderID = data[2]
return v, nil
}
}
}
return v, errTopicParse
}
func (topic BroadcastTopic) TopicPathGeneric(format string) string {
var sm, _ = topic.SenderModule.MarshalJSON()
var sm_s, _ = strconv.Unquote(string(sm))
return fmt.Sprintf(format, sm_s, topic.SenderID)
}
type StatusTopic BroadcastTopic
func ParseStatusTopic(s string) (StatusTopic, error) {
var v, err = ParseBroadcastTopic(s, TOPIC_STATUS_REGEXP)
return StatusTopic(v), err
}
func (topic StatusTopic) TopicPath() string {
return (BroadcastTopic)(topic).TopicPathGeneric(TOPIC_STATUS_FORMAT)
}
type LogTopic BroadcastTopic
func ParseLogTopic(s string) (LogTopic, error) {
var v, err = ParseBroadcastTopic(s, TOPIC_LOG_REGEXP)
return LogTopic(v), err
}
func (topic LogTopic) TopicPath() string {
return (BroadcastTopic)(topic).TopicPathGeneric(TOPIC_LOG_FORMAT)
}
type EventTopic struct {
SenderModule Module
SenderID string
Type SystemEventType
}
func ParseEventTopic(s string) (EventTopic, error) {
var v EventTopic
var r = regexp.MustCompile(TOPIC_EVENT_REGEXP)
var ds = r.FindAllStringSubmatch(s, -1)
if len(ds) == 1 {
var data = ds[0]
if data != nil && len(data) == 3+1 {
var smodule Module
var event_type SystemEventType
var smodule_err = json.Unmarshal([]byte(strconv.Quote(data[1])), &smodule)
var event_type_err = json.Unmarshal([]byte(strconv.Quote(data[3])), &event_type)
if smodule_err == nil && event_type_err == nil {
v.SenderModule = smodule
v.SenderID = data[2]
v.Type = event_type
return v, nil
}
}
}
return v, errTopicParse
}
func (topic EventTopic) TopicPath() string {
var sm, _ = topic.SenderModule.MarshalJSON()
var sm_s, _ = strconv.Unquote(string(sm))
var t, _ = topic.Type.MarshalJSON()
var t_s, _ = strconv.Unquote(string(t))
return fmt.Sprintf(TOPIC_EVENT_FORMAT, sm_s, topic.SenderID, t_s)
}
type RequestTopic struct {
SenderModule Module
SenderID string
ReceiverModule Module
ReceiverID string
RequestID string
Operation Operation
Tag string
}
func (topic RequestTopic) TopicPath() string {
var u = strconv.Unquote
var sm, _ = topic.SenderModule.MarshalJSON()
var sm_s, _ = u(string(sm))
var rm, _ = topic.ReceiverModule.MarshalJSON()
var rm_s, _ = u(string(rm))
var op, _ = topic.Operation.MarshalJSON()
var op_s, _ = u(string(op))
return fmt.Sprintf(TOPIC_REQ_FORMAT, sm_s, topic.SenderID, rm_s, topic.ReceiverID, topic.RequestID, op_s, topic.Tag)
}
func (topic RequestTopic) ToResponse() ResponseTopic {
return ResponseTopic{
SenderModule: topic.ReceiverModule,
SenderID: topic.ReceiverID,
ReceiverModule: topic.SenderModule,
ReceiverID: topic.SenderID,
RequestID: topic.RequestID,
}
}
func ParseRequestTopic(s string) (RequestTopic, error) {
var v RequestTopic
var r = regexp.MustCompile(TOPIC_REQ_REGEXP)
var ds = r.FindAllStringSubmatch(s, -1)
if len(ds) == 1 {
var data = ds[0]
if data != nil && len(data) == 7+1 {
var smodule Module
var rmodule Module
var op Operation
var smodule_err = json.Unmarshal([]byte(strconv.Quote(data[1])), &smodule)
var rmodule_err = json.Unmarshal([]byte(strconv.Quote(data[3])), &rmodule)
var op_err = json.Unmarshal([]byte(strconv.Quote(data[6])), &op)
if smodule_err == nil && rmodule_err == nil && op_err == nil {
v.SenderModule = smodule
v.SenderID = data[2]
v.ReceiverModule = rmodule
v.ReceiverID = data[4]
v.RequestID = data[5]
v.Operation = op
v.Tag = data[7]
return v, nil
}
}
}
return v, errTopicParse
}
type ResponseTopic struct {
SenderModule Module
SenderID string
ReceiverModule Module
ReceiverID string
RequestID string
}
func (topic ResponseTopic) TopicPath() string {
var u = strconv.Unquote
var sm, _ = topic.SenderModule.MarshalJSON()
var rm, _ = topic.ReceiverModule.MarshalJSON()
var sm_s, _ = u(string(sm))
var rm_s, _ = u(string(rm))
return fmt.Sprintf(TOPIC_RSP_FORMAT, sm_s, topic.SenderID, rm_s, topic.ReceiverID, topic.RequestID)
}
func ParseResponseTopic(s string) (ResponseTopic, error) {
var v ResponseTopic
var r = regexp.MustCompile(TOPIC_RSP_REGEXP)
var ds = r.FindAllStringSubmatch(s, -1)
if len(ds) == 1 {
var data = ds[0]
if data != nil && len(data) == 5+1 {
var smodule Module
var rmodule Module
var smodule_err = json.Unmarshal([]byte(strconv.Quote(data[1])), &smodule)
var rmodule_err = json.Unmarshal([]byte(strconv.Quote(data[3])), &rmodule)
if smodule_err == nil && rmodule_err == nil {
v.SenderModule = smodule
v.SenderID = data[2]
v.ReceiverModule = rmodule
v.ReceiverID = data[4]
v.RequestID = data[5]
return v, nil
}
}
}
return v, errTopicParse
}
func ParseTopicPath(s string) Topic {
{
var v, err = ParseStatusTopic(s)
if err == nil {
return v
}
}
{
var v, err = ParseLogTopic(s)
if err == nil {
return v
}
}
{
var v, err = ParseEventTopic(s)
if err == nil {
return v
}
}
{
var v, err = ParseRequestTopic(s)
if err == nil {
return v
}
}
{
var v, err = ParseResponseTopic(s)
if err == nil {
return v
}
}
return nil
}