forked from IOT-DSA/sdk-dslink-go
/
link.go
264 lines (223 loc) · 5.38 KB
/
link.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
package conn
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/butlermatt/dslink"
"github.com/butlermatt/dslink/log"
"github.com/butlermatt/dslink/nodes"
)
const dslinkJson = "dslink.json"
// Optional configuration functions which can be passed to NewLink
// IsRequester is an option for NewLink. It specifies that the link
// should also include requester functionality. By default requester
// is disabled.
func IsRequester(c *config) {
c.isRequester = true
}
// IsNotResponder is an option for NewLink. It will disable the
// Responder functionality of the link. By default responder
// is always enabled.
func IsNotResponder(c *config) {
c.isResponder = false
}
// AutoInit is an option for NewLink. This will indicate that
// the link should automatically try to call the Init method
// during the NewLink call. By default you must manually call
// the Init method on the Link.
func AutoInit(c *config) {
c.autoInit = true
}
// LogLevel is an option for NewLink. It accepts a Level from
// log. This indicates what level logging should be enabled.
// By default LogLevel is set to DisabledLevel
func LogLevel(l log.Level) func(c *config) {
return func(c *config) {
c.logLevel = l
}
}
// OnConnected is an option for NewLink. It accepts a callback
// with the signature of ConnectedCB. If supplied this will be
// called once the Link has successfully connected to an upstream
// broker.
func OnConnected(oc ConnectedCB) func(c *config) {
return func(c *config) {
c.oc = oc
}
}
type config struct {
isResponder bool
isRequester bool
autoInit bool
broker string
name string
home string
token string
rootPath string
keyPath string
logFile string
logLevel log.Level
oc ConnectedCB
}
// NewLink will create a new Link. The prefix is a require string which
// identifies this link with the upstream broker. The prefix should end
// with a hyphen (-) character. You may specify a variable number of
// configuration methods to help configure the link
func NewLink(prefix string, options ...func(*config)) *Link {
var l Link
// Set default options
l.conf.isResponder = true
l.conf.logLevel = log.DisabledLevel
// Handle Options passed
for _, option := range options {
option(&l.conf)
}
l.conf.name = prefix
// Handle Flags
parseFlags(&l.conf)
if l.conf.logLevel == log.DisabledLevel {
log.SetLevel(log.DisabledLevel)
log.SetOutput(nil)
} else {
log.SetLevel(l.conf.logLevel)
}
if l.conf.logFile != "" {
//TODO: Deal with log files right.
}
if l.conf.autoInit {
l.Init()
}
return &l
}
type ConnectedCB func(*Link)
type Link struct {
conf config
cl *httpClient
pr *nodes.Provider
msgs chan *dslink.Message
resp chan *dslink.Response
reqs chan *dslink.Request
salt string
reqer *nodes.Requester
init bool
}
type dsJson struct {
Config map[string]map[string]string `json:"configs"`
}
func (l *Link) Init() {
if l.conf.name[len(l.conf.name)-1] != '-' {
l.conf.name += "-"
}
if l.conf.isResponder {
l.resp = make(chan *dslink.Response)
l.pr = nodes.NewProvider(l.resp)
}
if l.conf.isRequester {
l.reqs = make(chan *dslink.Request)
l.reqer = nodes.NewRequester(l.reqs)
}
// TODO:
// Load dslink.json
l.loadDsJson()
// load nodes.json
}
func (l *Link) Start() {
var err error
l.msgs = make(chan *dslink.Message)
l.cl, err = dial(&l.conf, l.msgs)
if err != nil {
panic(err)
}
for {
select {
case im := <-l.msgs:
go l.handleMessage(im)
case oresp := <-l.resp:
m := &dslink.Message{}
if oresp != nil {
m.Resp = append(m.Resp, oresp)
l.cl.out <- m
}
case oreq := <-l.reqs:
m := &dslink.Message{}
if oreq != nil {
m.Reqs = append(m.Reqs, oreq)
l.cl.out <- m
}
}
}
}
func (l *Link) Stop() {
// TODO: This isn't stopping the link though
l.cl.Close()
}
func (l *Link) GetProvider() *nodes.Provider {
return l.pr
}
func (l *Link) GetRequester() *nodes.Requester {
return l.reqer
}
func (l *Link) handleMessage(m *dslink.Message) {
var ackM *dslink.Message
if len(m.Reqs) == 0 && len(m.Resp) == 0 && m.Salt == "" {
// Ignore message.
return
}
if l.reqer != nil {
for _, resp := range m.Resp {
l.reqer.HandleResponse(resp)
}
} else if len(m.Resp) > 0 {
log.Debug.Println("Received responses when no requester active.")
}
ackM = &dslink.Message{Ack: m.Msg}
if m.Salt != "" {
l.salt = m.Salt
// TODO: Handle actually using the reconnect salt (if the websocket ever drops)
if !l.init {
l.init = true
if l.conf.oc != nil {
go l.conf.oc(l)
}
}
}
for _, req := range m.Reqs {
res := l.pr.HandleRequest(req)
if res != nil {
ackM.Resp = append(ackM.Resp, res)
}
}
if ackM != nil {
l.cl.out<- ackM
}
}
func (l *Link) loadDsJson() {
if l.conf.rootPath != "" {
err := os.Chdir(l.conf.rootPath)
if err != nil {
log.Warn.Printf("Unable to load %s, cannot find root path: %s\n", dslinkJson, l.conf.rootPath)
return
}
}
d, err := ioutil.ReadFile(dslinkJson)
if err != nil && os.IsNotExist(err) {
log.Warn.Printf("Unable to find file: %q", dslinkJson)
return
} else if err != nil {
log.Error.Printf("Unexpected error: %v", err)
return
}
ds := &dsJson{}
err = json.Unmarshal(d, ds)
if err != nil {
log.Error.Printf("Unable to Unmarshal data: %s\nError:%v\n", d, err)
return
}
key := ds.Config["key"]
if key!= nil {
keyPath, ok := key["value"]
if ok {
l.conf.keyPath = keyPath
}
}
}