-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.go
151 lines (127 loc) · 3.22 KB
/
messages.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
package idefixgo
import (
"bytes"
"compress/gzip"
"encoding/json"
"io"
"strings"
mqtt "github.com/eclipse/paho.mqtt.golang"
e "github.com/nayarsystems/idefix-go/errors"
m "github.com/nayarsystems/idefix-go/messages"
"github.com/nayarsystems/idefix-go/normalize"
"github.com/vmihailenco/msgpack/v5"
)
func (c *Client) sendMessage(tm *m.Message) (err error) {
var flags string
var marshaled bool
var marshalErr error
var data []byte
if strings.Contains(c.opts.Encoding, "j") && !marshaled {
marshaled = true
flags += "j"
if v, ok := tm.Data.(map[string]interface{}); ok {
opts := normalize.EncodeTypesOpts{BytesToB64: true}
marshalErr = normalize.EncodeTypes(v, &opts)
}
if marshalErr == nil {
data, marshalErr = json.Marshal(tm)
}
}
if strings.Contains(c.opts.Encoding, "m") && !marshaled {
marshaled = true
flags += "m"
if v, ok := tm.Data.(map[string]interface{}); ok {
opts := normalize.EncodeTypesOpts{}
marshalErr = normalize.EncodeTypes(v, &opts)
}
if marshalErr == nil {
data, marshalErr = msgpack.Marshal(tm)
}
}
if marshalErr != nil {
return e.ErrMarshal
}
if !marshaled {
return e.ErrMarshal.With("unsupported encoding")
}
var compressed bool
if strings.Contains(c.opts.Encoding, "g") && !compressed { // TODO: Compression threshold?
buf := new(bytes.Buffer)
wr := gzip.NewWriter(buf)
n, err := wr.Write(data)
wr.Close()
if err == nil && n == len(data) {
comp, err := io.ReadAll(buf)
if err == nil && len(comp) < len(data) {
compressed = true
flags += "g"
data = comp
}
}
}
msg := c.client.Publish(c.publishTopic(flags), 1, false, data)
msg.Wait()
if msg.Error() != nil {
return e.ErrInternal.With(msg.Error().Error())
}
return nil
}
func (c *Client) receiveMessage(client mqtt.Client, msg mqtt.Message) {
if !strings.HasPrefix(msg.Topic(), c.responseTopic()) {
return
}
topicChuncks := strings.Split(msg.Topic(), "/")
if len(topicChuncks) != 4 {
return
}
flags := topicChuncks[3]
payload := msg.Payload()
var tm m.Message
var unmarshalErr error
var unmarshaled bool
if strings.Contains(flags, "g") {
r := bytes.NewReader(payload)
gzr, err := gzip.NewReader(r)
if err == nil {
payload, err = io.ReadAll(gzr)
gzr.Close()
}
if err != nil {
// fmt.Printf("can't decompress gzip: %v\n", err)
return
}
}
if strings.Contains(flags, "j") && !unmarshaled {
unmarshalErr = json.Unmarshal(payload, &tm)
unmarshaled = true
}
if strings.Contains(flags, "m") && !unmarshaled {
unmarshalErr = msgpack.Unmarshal(payload, &tm)
unmarshaled = true
}
if unmarshalErr != nil {
// fmt.Printf("unmarshal error decoding message: %v\n", unmarshalErr)
return
}
if !unmarshaled {
// fmt.Println("unmarshal error: codec not found ")
return
}
if strings.HasPrefix(tm.To, c.opts.Address+".") {
return
}
tm.To = strings.TrimPrefix(tm.To, c.opts.Address+".")
if tm.To == "" {
return
}
if msiData, ok := tm.Data.(map[string]interface{}); ok {
err := normalize.DecodeTypes(msiData)
if err != nil {
// fmt.Println("error decoding mqtt message", err, msiData)
return
}
}
if n := c.ps.Publish(tm.To, &tm); n == 0 {
// fmt.Printf("mqtt message published but there is no receivers: %#v\n", tm)
}
}