This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
273 lines (219 loc) · 7.12 KB
/
server.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
//--------------------------------------------------------------------------
// Copyright 2018 infinimesh
// www.infinimesh.io
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//--------------------------------------------------------------------------
package shadow
import (
"bytes"
"context"
"encoding/json"
"errors"
"github.com/Shopify/sarama"
"github.com/cskr/pubsub"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/ptypes"
structpb "github.com/golang/protobuf/ptypes/struct"
"go.uber.org/zap"
"github.com/slntopp/infinimesh/pkg/shadow/shadowpb"
)
//Server is a data strcuture for shadow server
type Server struct {
shadowpb.UnimplementedShadowsServer
Repo Repo
Producer sarama.SyncProducer // Sync producer, we want to guarantee execution
ProduceTopic string
Log *zap.Logger
PubSub *pubsub.PubSub
}
//Get is a method to get a device state
func (s *Server) Get(context context.Context, request *shadowpb.GetRequest) (response *shadowpb.GetResponse, err error) {
log := s.Log.Named("Get State Controller")
log.Debug("Function Invoked", zap.String("Device", request.Id))
response = &shadowpb.GetResponse{
Shadow: &shadowpb.Shadow{},
}
// TODO fetch device from registry, 404 if not found
reportedState, err := s.Repo.GetReported(request.Id)
if err != nil {
reportedState.ID = request.Id
reportedState.State = DeviceStateMessage{
Version: uint64(0),
State: json.RawMessage([]byte("{}")),
}
}
desiredState, err := s.Repo.GetDesired(request.Id)
if err != nil {
desiredState.ID = request.Id
desiredState.State = DeviceStateMessage{
Version: uint64(0),
State: json.RawMessage([]byte("{}")),
}
}
u := &jsonpb.Unmarshaler{}
var reportedValue structpb.Value
if err := u.Unmarshal(bytes.NewReader(reportedState.State.State), &reportedValue); err != nil {
log.Error("Failed to unmarshal reported JSON from database", zap.Error(err))
} else {
ts, err := ptypes.TimestampProto(reportedState.State.Timestamp)
if err != nil {
return nil, err
}
response.Shadow.Reported = &shadowpb.VersionedValue{
Version: uint64(reportedState.State.Version),
Data: &reportedValue,
Timestamp: ts,
}
}
var desiredValue structpb.Value
if err := u.Unmarshal(bytes.NewReader(desiredState.State.State), &desiredValue); err != nil {
log.Error("Failed to unmarshal desired JSON from database", zap.Error(err))
} else {
ts, err := ptypes.TimestampProto(desiredState.State.Timestamp)
if err != nil {
return nil, err
}
response.Shadow.Desired = &shadowpb.VersionedValue{
Version: uint64(desiredState.State.Version),
Data: &desiredValue,
Timestamp: ts,
}
}
return response, nil
}
//PatchDesiredState is a method to patch a message to a device state
func (s *Server) PatchDesiredState(context context.Context, request *shadowpb.PatchDesiredStateRequest) (response *shadowpb.PatchDesiredStateResponse, err error) {
log := s.Log.Named("Patch Desired State Controller")
log.Debug("Function Invoked", zap.String("Device", request.Id))
// TODO sanity-check request
var marshaler jsonpb.Marshaler
var b bytes.Buffer
err = marshaler.Marshal(&b, request.GetData())
if err != nil {
return nil, err
}
_, _, err = s.Producer.SendMessage(&sarama.ProducerMessage{
Topic: s.ProduceTopic,
Key: sarama.StringEncoder(request.GetId()),
Value: sarama.ByteEncoder(b.Bytes()),
})
if err != nil {
return nil, err
}
return &shadowpb.PatchDesiredStateResponse{}, nil
}
//StreamReportedStateChanges is a method to start streaming of data from a device
func (s *Server) StreamReportedStateChanges(request *shadowpb.StreamReportedStateChangesRequest, srv shadowpb.Shadows_StreamReportedStateChangesServer) (err error) {
log := s.Log.Named("Stream State Controller")
log.Debug("Function Invoked", zap.String("Device", request.Id), zap.Bool("Delta Flag", request.OnlyDelta))
// TODO validate request/Id
var subPathReported string
if request.OnlyDelta {
subPathReported = "/reported/delta"
} else {
subPathReported = "/reported/full"
}
topicEvents := request.Id + subPathReported
events := s.PubSub.Sub(topicEvents)
log.Debug("Reported Streaming Details", zap.String("Topic Events", topicEvents), zap.Any("Events", events))
defer func() {
go func() {
s.PubSub.Unsub(events)
}()
// Drain
for range events {
}
log.Info("Drained Reported Channel")
}()
var subPathDesired string
if request.OnlyDelta {
subPathDesired = "/desired/delta"
} else {
subPathDesired = "/desired/full"
}
topicEventsDesired := request.Id + subPathDesired
eventsDesired := s.PubSub.Sub(topicEventsDesired)
log.Debug("Desired Streaming Details", zap.String("Topic Events", topicEventsDesired), zap.Any("Events", eventsDesired))
defer func() {
go func() {
s.PubSub.Unsub(eventsDesired)
}()
// Drain
for range eventsDesired {
}
log.Debug("Drained Desired Channel")
}()
outer:
for {
log.Debug("Looping through Stream")
select {
case reportedEvent := <-events:
log.Debug("Inside reported event reading")
value, err := toProto(reportedEvent, log)
if err != nil {
log.Error("Unable to Unmarshal data", zap.Error(err))
break outer
}
log.Debug("Reported", zap.Any("Reported Value", value))
err = srv.Send(&shadowpb.StreamReportedStateChangesResponse{
ReportedState: value,
})
if err != nil {
log.Error("Unable to send Reported data", zap.Error(err))
break outer
}
case desiredEvent := <-eventsDesired:
log.Debug("Inside desired event reading")
value, err := toProto(desiredEvent, log)
if err != nil {
log.Error("Unable to Unmarshal data", zap.Error(err))
break outer
}
log.Debug("Desired", zap.Any("Desired Value", value))
err = srv.Send(&shadowpb.StreamReportedStateChangesResponse{
DesiredState: value,
})
if err != nil {
log.Error("Unable to send Desired data", zap.Error(err))
break outer
}
case <-srv.Context().Done():
log.Debug("Streaming is Done")
break outer
}
}
return nil
}
func toProto(event interface{}, log *zap.Logger) (result *shadowpb.VersionedValue, err error) {
var value structpb.Value
if raw, ok := event.(*DeviceStateMessage); ok {
var u jsonpb.Unmarshaler
err = u.Unmarshal(bytes.NewReader(raw.State), &value)
if err != nil {
log.Error("Failed to unmarshal jsonpb", zap.Error(err))
return nil, err
}
ts, err := ptypes.TimestampProto(raw.Timestamp)
if err != nil {
log.Error("Invalid timestamp", zap.Error(err))
return nil, err
}
return &shadowpb.VersionedValue{
Version: raw.Version,
Data: &value,
Timestamp: ts, // TODO
}, nil
}
return nil, errors.New("Failed type assertion")
}