-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoteinstance.go
More file actions
405 lines (324 loc) · 11.7 KB
/
remoteinstance.go
File metadata and controls
405 lines (324 loc) · 11.7 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/spf13/viper"
"io"
"time"
)
type RemoteInstance struct {
UUID string
DisplayName string
RemoteAddress string
SensorUUIDs []string
sensors []*Sensor
tlsConn *tls.Conn
connected bool
nextRequests chan *Request
enc *json.Encoder
dec *json.Decoder
}
func (r *RemoteInstance) HandleIncomingRequests() {
logger.Printf("Info: RemoteInstance: Connected to %s. Now handling requests\n", r.UUID)
defer r.Disconnect()
for {
var req Request
if err := r.dec.Decode(&req); err != nil {
if err == io.EOF {
logger.Printf("Info: RemoteInstance: %s closed connection. No longer connected.", r.UUID)
return
}
logger.Println("Error decoding request:", err)
return
}
// Connection is already established and acknowledged, i.e.
// no RequestTypeConnectionAttempt and no RequestTypeConnectionACK
RequestDestinction:
switch req.RequestType {
case RequestTypeGetSensorList:
logger.Printf("Info: RemoteInstance: %s asks for the sensor list", req.OriginUUID)
entries := make([]SensorListEntry, 0)
for _, s := range local.sensors {
entries = append(entries, SensorListEntry{
UUID: s.UUID,
DisplayName: s.DisplayName,
NextMeasurement: s.NextMeasurement,
})
}
// Encode the entries manually
enc, err := json.Marshal(entries)
if err != nil {
logger.Printf("Error: RemoteInstance: Could not collect sensor data requested by %s", req.OriginUUID)
break RequestDestinction
}
r.nextRequests <- &Request{
RequestType: RequestTypeAnswerSensorList,
OriginUUID: local.UUID,
Data: map[string]string{
"entries": string(enc),
},
}
break
case RequestTypeAnswerSensorList:
logger.Printf("Info: RemoteInstance: %s answered with its sensors", req.OriginUUID)
entries := make([]SensorListEntry, 0)
err := json.Unmarshal([]byte(req.Data["entries"]), &entries)
if err != nil {
logger.Println("Error: RemoteInstance: Could not decode sensor list")
break RequestDestinction
}
requiresUpdate := make([]SensorUpdateRequestEntry, 0)
for _, entry := range entries {
// Check if we already know the sensor
requireAllMeasurements := true
for _, sensor := range r.sensors {
if sensor.UUID == entry.UUID {
requireAllMeasurements = false
logger.Printf("Sensor:", sensor.UUID)
logger.Printf("Local DB: next measurement: %d Remote entry: next measurement: %d", sensor.NextMeasurement, entry.NextMeasurement)
// Check if we are up to date
if sensor.NextMeasurement == entry.NextMeasurement {
break
}
// Not up-to-date, but we don't require the all measurements
requiresUpdate = append(requiresUpdate, SensorUpdateRequestEntry{
UUID: sensor.UUID,
StartingAtMeasurement: sensor.NextMeasurement,
})
break
}
}
if requireAllMeasurements {
requiresUpdate = append(requiresUpdate, SensorUpdateRequestEntry{
UUID: entry.UUID,
StartingAtMeasurement: 0,
})
}
}
logger.Printf("Received %d sensors of which %d require an update or are unknown.", len(entries), len(requiresUpdate))
logger.Printf("Request: %v", requiresUpdate)
// Encode the entries manually
enc, err := json.Marshal(requiresUpdate)
if err != nil {
logger.Println("Error: RemoteInstance: Could not encode required updates")
break RequestDestinction
}
// Request update for all sensors in requiresUpdate
r.nextRequests <- &Request{
RequestType: RequestTypeGetSensorMeasurements,
OriginUUID: local.UUID,
Data: map[string]string{
"entries": string(enc),
},
}
break
case RequestTypeGetSensorMeasurements:
requestedMeasurements := make([]SensorUpdateRequestEntry, 0)
if err := json.Unmarshal([]byte(req.Data["entries"]), &requestedMeasurements); err != nil {
logger.Println("Error: RemoteInstance: Could not decode requested sensor measurements")
break RequestDestinction
}
logger.Printf("Info: RemoteInstance: Remote %s asked for %d sensor updates", req.OriginUUID, len(requestedMeasurements))
// Init sensor updates collection
collectedUpdates := SensorUpdateList{
SensorMetadata: map[string]Sensor{},
SensorMeasurements: map[string][]SensorMeasurement{},
}
for _, requestedPair := range requestedMeasurements {
// Collect requested updates
index := local.GetSensorIndex(requestedPair.UUID)
if index < 0 || index >= len(local.sensors) {
logger.Println("Error: RemoteInstance: Invalid request for sensor", requestedPair.UUID)
break
}
if requestedPair.StartingAtMeasurement < 0 ||
requestedPair.StartingAtMeasurement >= local.sensors[index].NextMeasurement {
logger.Printf("Error: RemoteInstance: Invalid request for sensor %s measurement %d",
requestedPair.UUID,
requestedPair.StartingAtMeasurement)
break RequestDestinction
}
logger.Printf("Info: RemoteInstance: Collected %d measurements from sensor %s starting from %d up to %d",
len(local.sensors[index].Measurements)-requestedPair.StartingAtMeasurement,
requestedPair.UUID,
requestedPair.StartingAtMeasurement,
len(local.sensors[index].Measurements))
sensorMetadata := *local.sensors[index]
sensorMetadata.Measurements = make([]SensorMeasurement, 0)
collectedUpdates.SensorMetadata[requestedPair.UUID] = sensorMetadata
collectedUpdates.SensorMeasurements[requestedPair.UUID] = local.sensors[index].Measurements[requestedPair.StartingAtMeasurement:]
}
encUpdates, err := json.Marshal(collectedUpdates)
if err != nil {
logger.Println("Error: RemoteInstance: Could not encode collected updates")
break
}
r.nextRequests <- &Request{
RequestType: RequestTypeAnswerSensorMeasurements,
OriginUUID: local.UUID,
Data: map[string]string{
"collectedUpdates": string(encUpdates),
},
}
break
case RequestTypeAnswerSensorMeasurements:
collectedUpdates := SensorUpdateList{}
if err := json.Unmarshal([]byte(req.Data["collectedUpdates"]), &collectedUpdates); err != nil {
logger.Println("Error: RemoteInstance: Could not decode collected updates")
break RequestDestinction
}
SensorIndexInAnswer := 0
for UUID, measurements := range collectedUpdates.SensorMeasurements {
// Create the sensor if it appears to be new
index := r.GetSensorIndex(UUID)
if index < 0 {
logger.Printf("Info: RemoteInstance: Learned about sensor %s from remote %s", UUID, r.UUID)
sensorFile := viper.New()
sensorFile.SetConfigFile(local.DataDir + UUID + ".json")
r.AddSensor(&Sensor{
UUID: UUID,
DisplayName: collectedUpdates.SensorMetadata[UUID].DisplayName,
Type: collectedUpdates.SensorMetadata[UUID].Type,
NextMeasurement: 0,
Settings: collectedUpdates.SensorMetadata[UUID].Settings,
Measurements: []SensorMeasurement{},
sensorFile: sensorFile,
})
index = r.GetSensorIndex(UUID)
}
// Update DisplayName and settings if required and if possible (i.e. metadata is available)
if collectedUpdates.SensorMetadata != nil {
r.sensors[index].DisplayName = collectedUpdates.SensorMetadata[UUID].DisplayName
r.sensors[index].Settings = collectedUpdates.SensorMetadata[UUID].Settings
}
// Then start adding the measurements (don't log single live updates)
if len(measurements) > 1 {
logger.Printf("Info: RemoteInstance: Received update from sensor %s with %d measurements, starting at %d",
UUID,
len(measurements),
measurements[0].MeasurementId)
}
for _, m := range measurements {
if m.MeasurementId != r.sensors[index].NextMeasurement {
logger.Println("Error: RemoteInstance: Collected updates are not in correct order")
break RequestDestinction
}
r.sensors[index].addMeasurement(&m, true)
}
// "Commit" bulk transaction
r.sensors[index].addMeasurement(nil, true)
SensorIndexInAnswer++
}
// Notify UI
WebAPIBroadcastSensors()
break
default:
logger.Println("Error: RemoteInstance: Unexpected request type:", req.RequestType)
logger.Println("Error: RemoteInstance: Received request:", req)
}
}
}
func (r *RemoteInstance) Connect() bool {
logger.Println("Info: Connect(): Trying to connect to", r.UUID)
tlsConfig := &tls.Config{InsecureSkipVerify: true, Certificates: []tls.Certificate{local.keyPair}, ClientAuth: tls.RequireAnyClientCert}
tlsConn, err := tls.Dial("tcp", r.RemoteAddress, tlsConfig)
if err != nil {
logger.Println("Info: Could not connect to", r.UUID, ":", err)
return false
}
tlsConn.SetDeadline(time.Time{})
r.tlsConn = tlsConn
r.enc = json.NewEncoder(r.tlsConn)
r.dec = json.NewDecoder(r.tlsConn)
// Verify identity
sha256Sum := SHA256FromTLSCert(r.tlsConn.ConnectionState().PeerCertificates[0])
if !matchesAuthorizedKey(r.UUID, sha256Sum) {
return false
}
// Send ConnectionAttempt
r.SendRequest(&Request{
RequestType: RequestTypeConnectionAttempt,
OriginUUID: local.UUID,
Data: map[string]string{
"DisplayName": local.DisplayName,
},
})
// Wait for ACK
var ack Request
r.dec.Decode(&ack)
if ack.RequestType != RequestTypeConnectionACK {
logger.Printf("Did not receive acknowledgement from host %s (Received type %d)", ack.OriginUUID, ack.RequestType)
return false
}
logger.Println("Connected to", ack.OriginUUID)
r.connected = true
// Notify UI
WebAPIBroadcastRemoteInstances()
return true
}
func (r *RemoteInstance) Disconnect() {
r.tlsConn.Close()
r.connected = false
// Notify UI
WebAPIBroadcastRemoteInstances()
logger.Println("Info: RemoteInstance: Disconnected from", r.UUID)
}
func (r *RemoteInstance) SendRequest(req *Request) {
if err := r.enc.Encode(req); err != nil {
fmt.Println("Error encoding request:", err)
}
}
func (r *RemoteInstance) GeneratePeriodicRequests() {
// todo implement
}
func (r *RemoteInstance) MultiplexRequests() {
// Sleep until connection is ready and standard handshake collected some sync data
time.Sleep(500 * time.Millisecond)
for nextReq := range r.nextRequests {
r.SendRequest(nextReq)
}
}
func (r *RemoteInstance) AddSensor(sensor *Sensor) {
// todo: mutex
// Create measurements file
sensor.sensorFile = viper.New()
sensor.sensorFile.SetConfigFile(local.DataDir + sensor.UUID + ".json")
sensor.sensorFile.Set("Sensor", sensor)
sensor.sensorFile.WriteConfig()
// Add sensor to remote instances and save it
r.SensorUUIDs = append(r.SensorUUIDs, sensor.UUID)
r.sensors = append(r.sensors, sensor)
local.config.Set("RemoteInstances", local.RemoteInstances)
local.config.WriteConfig()
}
func connectToRemoteInstances() {
logger.Println("Info: Trying to connect to remote instances")
// todo: mutex
for i, _ := range local.RemoteInstances {
currentRemote := &local.RemoteInstances[i]
// Prepare multiplexing
currentRemote.nextRequests = make(chan *Request, 2048)
if currentRemote.Connect() {
go currentRemote.HandleIncomingRequests() // Handle incoming requests
go currentRemote.MultiplexRequests() // Enable outgoing message multiplexing
go currentRemote.GeneratePeriodicRequests() // Activate periodic polling (heartbeats, etc.)
// First thing to do once we're connected is to ask for the remote instance's sensors
currentRemote.nextRequests <- &Request{
RequestType: RequestTypeGetSensorList,
OriginUUID: local.UUID,
Data: map[string]string{},
}
} else {
logger.Println("Error: Could not connect to", local.RemoteInstances[i].UUID)
}
}
}
func (r *RemoteInstance) GetSensorIndex(UUID string) int {
for i, s := range r.SensorUUIDs {
if s == UUID {
return i
}
}
return -1
}