/
activity.go
331 lines (277 loc) · 8.29 KB
/
activity.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package mqttcoupler
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/Joker666/AsyncGoDemo/async"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data/metadata"
"github.com/project-flogo/core/support/log"
"github.com/project-flogo/core/support/ssl"
)
var clients = make(map[string]mqtt.Client)
var clientMapMutex = &sync.Mutex{}
var gotMessage = make(chan bool, 1)
func getClient(logger log.Logger, connectionID string, opts *mqtt.ClientOptions) (client mqtt.Client, err error) {
clientMapMutex.Lock()
defer clientMapMutex.Unlock()
client = clients[connectionID]
if client != nil {
logger.Debug("[mqtt.activity.getClient] Mqtt Publish is reusing an existing connection...")
return client, nil
}
client = mqtt.NewClient(opts)
logger.Debug("[mqtt.activity.getClient] Mqtt Publish is establishing a connection, client id : ", connectionID)
if token := client.Connect(); token.Wait() && token.Error() != nil {
return nil, errors.New(fmt.Sprintf("Connection to mqtt broker failed %v", token.Error()))
}
clients[connectionID] = client
return client, nil
}
var activityMd = activity.ToMetadata(&Settings{}, &Input{}, &Output{})
func init() {
_ = activity.Register(&Activity{}, New)
}
// TokenType is a type of token
type TokenType int
const (
// Literal is a literal token type
Literal TokenType = iota
// Substitution is a parameter substitution
Substitution
)
// Token is a MQTT topic token
type Token struct {
TokenType TokenType
Token string
}
// Topic is a parsed topic
type Topic []Token
// ParseTopic parses the topic
func ParseTopic(topic string) Topic {
var parsed Topic
parts, index := strings.Split(topic, "/"), 0
for _, part := range parts {
if strings.HasPrefix(part, ":") {
token := strings.TrimPrefix(part, ":")
if token == "" {
token = strconv.Itoa(index)
index++
}
parsed = append(parsed, Token{
TokenType: Substitution,
Token: token,
})
} else {
parsed = append(parsed, Token{
TokenType: Literal,
Token: part,
})
}
}
return parsed
}
// String generates a string for the topic with params
func (t Topic) String(params map[string]string) string {
output := strings.Builder{}
for i, token := range t {
if i > 0 {
output.WriteString("/")
}
switch token.TokenType {
case Literal:
output.WriteString(token.Token)
case Substitution:
if value, ok := params[token.Token]; ok {
output.WriteString(value)
} else {
output.WriteString(":")
output.WriteString(token.Token)
}
}
}
return output.String()
}
func New(ctx activity.InitContext) (activity.Activity, error) {
settings := &Settings{}
err := metadata.MapToStruct(ctx.Settings(), settings, true)
if err != nil {
return nil, err
}
ctx.Logger().Debug("(mqttcoupler.New) Entering with connection id : ", settings.Id)
defer ctx.Logger().Debug("(mqttcoupler.New) Exit for connection id : ", settings.Id)
options := initClientOption(ctx.Logger(), settings)
if strings.HasPrefix(settings.Broker, "ssl") {
cfg := &ssl.Config{}
if len(settings.SSLConfig) != 0 {
err := cfg.FromMap(settings.SSLConfig)
if err != nil {
return nil, err
}
if _, set := settings.SSLConfig["skipVerify"]; !set {
cfg.SkipVerify = true
}
if _, set := settings.SSLConfig["useSystemCert"]; !set {
cfg.UseSystemCert = true
}
} else {
//using ssl but not configured, use defaults
cfg.SkipVerify = true
cfg.UseSystemCert = true
}
tlsConfig, err := ssl.NewClientTLSConfig(cfg)
if err != nil {
return nil, err
}
options.SetTLSConfig(tlsConfig)
}
mqttClient, err := getClient(ctx.Logger(), settings.Id, options)
if nil != err {
return nil, err
}
act := &Activity{
client: mqttClient,
settings: settings,
topic: ParseTopic(settings.Topic),
topicMessages: NewTopicMessages(),
}
return act, nil
}
type Activity struct {
settings *Settings
client mqtt.Client
topic Topic
topicMessages *TopicMessages
c *sync.Cond
}
func (a *Activity) Metadata() *activity.Metadata {
return activityMd
}
func (a *Activity) Eval(ctx activity.Context) (done bool, err error) {
input := &Input{}
err = ctx.GetInputObject(input)
if err != nil {
return true, err
}
responseTimeout := a.settings.ResponseTimeout
ctx.Logger().Infof("MQTT response timeout : %d", responseTimeout)
topic := a.settings.Topic
if params := input.TopicParams; len(params) > 0 {
topic = a.topic.String(params)
}
uuid, _ := uuid.NewUUID()
respTopic := fmt.Sprintf("%s/%s", topic, uuid)
ctx.Logger().Infof("MQTT client sunscribe topic, client id : %s, topic : %s", a.settings.Id, respTopic)
if token := a.client.Subscribe(respTopic, 0, a.topicMessages.receiveMessage); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
return true, token.Error()
}
future := async.Exec(func() interface{} {
return a.WaitMessage(ctx.Logger(), respTopic)
})
ctx.Logger().Infof("MQTT client publishing, client id : %s, msg : %s", a.settings.Id, input.Message)
messageEnvelope := map[string]interface{}{
"Sender": "Project Air",
"ResponseTopic": respTopic,
"Payload": input.Message,
}
messageEnvelopebytes, err := json.Marshal(messageEnvelope)
if nil != err {
return false, err
}
if token := a.client.Publish(topic, byte(a.settings.Qos), a.settings.Retain, string(messageEnvelopebytes)); token.Wait() && token.Error() != nil {
ctx.Logger().Errorf("Error in publishing: %v", err)
return true, token.Error()
}
a.topicMessages.startTimer(ctx.Logger(), respTopic, responseTimeout)
ctx.Logger().Debugf("Request Message: %v", input.Message)
msg := future.Await()
ctx.Logger().Infof("Response Message: %v", msg)
if nil == msg {
return false, errors.New("Timeout !!!")
}
output := &Output{}
output.Data = string(msg.(mqtt.Message).Payload())
err = ctx.SetOutputObject(output)
if err != nil {
return false, err
}
return true, nil
}
func (a *Activity) WaitMessage(logger log.Logger, topic string) mqtt.Message {
logger.Debugf("Awaiting message on : %s", topic)
<-gotMessage
a.client.Unsubscribe(topic)
logger.Debugf("Unsubscribe topic : %s", topic)
msg := a.topicMessages.removeMessage(topic)
logger.Debugf("Got signal! here is the message for the topic : %v", msg)
return msg
}
func NewTopicMessages() *TopicMessages {
topicMessages := &TopicMessages{
messages: make(map[string]interface{}),
waitingTopics: make(map[string]bool),
}
return topicMessages
}
type TopicMessages struct {
activity *Activity
messages map[string]interface{}
waitingTopics map[string]bool
}
func (t *TopicMessages) startTimer(logger log.Logger, topic string, timeout int) {
go func() {
t.waitingTopics[topic] = true
time.Sleep(time.Duration(timeout) * time.Second)
logger.Debugf("Wake up from waiting on topic %s for %s seconds!", topic, timeout)
if true == t.waitingTopics[topic] {
logger.Debugf("Send timeout signal for topic %s ", topic)
gotMessage <- true
}
logger.Debugf("Ignore timeout signal on topic %s ", topic)
}()
}
func (t *TopicMessages) receiveMessage(client mqtt.Client, msg mqtt.Message) {
t.messages[msg.Topic()] = msg
delete(t.waitingTopics, msg.Topic())
fmt.Println("xxxxxx receiveMessage xxxxxxx Topic = ", msg.Topic())
gotMessage <- true
}
func (t *TopicMessages) removeMessage(topic string) mqtt.Message {
msg := t.messages[topic]
if nil != msg {
delete(t.messages, topic)
return msg.(mqtt.Message)
}
return nil
}
func initClientOption(logger log.Logger, settings *Settings) *mqtt.ClientOptions {
opts := mqtt.NewClientOptions()
opts.AddBroker(settings.Broker)
opts.SetClientID(settings.Id)
opts.SetUsername(settings.Username)
password := settings.Password
if strings.HasPrefix(password, "SECRET:") {
pwdBytes, _ := base64.StdEncoding.DecodeString(password[7:])
password = string(pwdBytes)
}
opts.SetPassword(password)
opts.SetCleanSession(settings.CleanSession)
if settings.KeepAlive != 0 {
opts.SetKeepAlive(time.Duration(settings.KeepAlive) * time.Second)
} else {
opts.SetKeepAlive(2 * time.Second)
}
if settings.Store != "" && settings.Store != ":memory:" {
logger.Debugf("Using file store: %s", settings.Store)
opts.SetStore(mqtt.NewFileStore(settings.Store))
}
return opts
}