This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 279
/
schedule.go
287 lines (260 loc) · 7.57 KB
/
schedule.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package gateway
import (
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/TheThingsNetwork/api/logfields"
pb_lorawan "github.com/TheThingsNetwork/api/protocol/lorawan"
router_pb "github.com/TheThingsNetwork/api/router"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/TheThingsNetwork/ttn/utils/random"
"github.com/TheThingsNetwork/ttn/utils/toa"
)
// Schedule is used to schedule downlink transmissions
type Schedule interface {
fmt.GoStringer
// Synchronize the schedule with the gateway timestamp (in microseconds)
Sync(timestamp uint32)
// Get an "option" on a transmission slot at timestamp for the maximum duration of length (both in microseconds)
GetOption(timestamp uint32, length uint32) (id string, score uint)
// Schedule a transmission on a slot
Schedule(id string, downlink *router_pb.DownlinkMessage) error
// Subscribe to downlink messages
Subscribe(subscriptionID string) <-chan *router_pb.DownlinkMessage
// Whether the gateway has active downlink
IsActive() bool
// Stop the subscription
Stop(subscriptionID string)
}
// NewSchedule creates a new Schedule
func NewSchedule(ctx ttnlog.Interface) Schedule {
s := &schedule{
ctx: ctx,
items: make(map[string]*scheduledItem),
downlinkSubscriptions: make(map[string]chan *router_pb.DownlinkMessage),
}
go func() {
for {
<-time.After(10 * time.Second)
s.RLock()
numItems := len(s.items)
s.RUnlock()
if numItems > 0 {
s.Lock()
for id, item := range s.items {
// Delete the item if we are more than 2 seconds after the deadline
if time.Now().After(item.deadlineAt.Add(2 * time.Second)) {
delete(s.items, id)
}
}
s.Unlock()
}
}
}()
return s
}
type scheduledItem struct {
id string
deadlineAt time.Time
timestamp uint32
length uint32
score uint
payload *router_pb.DownlinkMessage
}
type schedule struct {
offset int64 // should be on top to ensure memory alignment needed for sync/atomic
sync.RWMutex
ctx ttnlog.Interface
items map[string]*scheduledItem
downlink chan *router_pb.DownlinkMessage
downlinkSubscriptionsLock sync.RWMutex
downlinkSubscriptions map[string]chan *router_pb.DownlinkMessage
gateway *Gateway
}
func (s *schedule) GoString() (str string) {
s.RLock()
defer s.RUnlock()
for _, item := range s.items {
str += fmt.Sprintf("%s at %s\n", item.id, item.deadlineAt)
}
return
}
// Deadline for sending a downlink back to the gateway
// TODO: Make configurable
var Deadline = 1000 * time.Millisecond
const uintmax = 1 << 32
// getConflicts walks over the schedule and returns the number of conflicts.
// Both timestamp and length are in microseconds
func (s *schedule) getConflicts(timestamp uint32, length uint32) (conflicts uint) {
s.RLock()
defer s.RUnlock()
for _, item := range s.items {
scheduledFrom := uint64(item.timestamp) % uintmax
scheduledTo := scheduledFrom + uint64(item.length)
from := uint64(timestamp)
to := from + uint64(length)
if scheduledTo > uintmax || to > uintmax {
if scheduledTo-uintmax <= from || scheduledFrom >= to-uintmax {
continue
}
} else if scheduledTo <= from || scheduledFrom >= to {
continue
}
if item.payload == nil {
conflicts++
} else {
conflicts += 100
}
}
return
}
// realtime gets the synchronized time for a timestamp (in microseconds). Time
// should first be syncronized using func Sync()
func (s *schedule) realtime(timestamp uint32) (t time.Time) {
offset := atomic.LoadInt64(&s.offset)
t = time.Unix(0, 0)
t = t.Add(time.Duration(int64(timestamp)*1000 + offset))
if t.Before(time.Now()) {
t = t.Add(time.Duration(int64(1<<32) * 1000))
}
return
}
// see interface
func (s *schedule) Sync(timestamp uint32) {
atomic.StoreInt64(&s.offset, time.Now().UnixNano()-int64(timestamp)*1000)
}
// see interface
func (s *schedule) GetOption(timestamp uint32, length uint32) (id string, score uint) {
id = random.String(32)
score = s.getConflicts(timestamp, length)
item := &scheduledItem{
id: id,
deadlineAt: s.realtime(timestamp).Add(-1 * Deadline),
timestamp: timestamp,
length: length,
score: score,
}
s.Lock()
defer s.Unlock()
s.items[id] = item
return id, score
}
// see interface
func (s *schedule) Schedule(id string, downlink *router_pb.DownlinkMessage) error {
ctx := s.ctx.WithFields(logfields.ForMessage(downlink)).WithFields(ttnlog.Fields{
"Identifier": id,
"TxPower": downlink.GatewayConfiguration.Power,
})
s.Lock()
defer s.Unlock()
if item, ok := s.items[id]; ok {
item.payload = downlink
conf := downlink.GetProtocolConfiguration()
if lorawan := conf.GetLoRaWAN(); lorawan != nil {
var time time.Duration
if lorawan.Modulation == pb_lorawan.Modulation_LORA {
// Calculate max ToA
time, _ = toa.ComputeLoRa(
uint(len(downlink.Payload)),
lorawan.DataRate,
lorawan.CodingRate,
)
}
if lorawan.Modulation == pb_lorawan.Modulation_FSK {
// Calculate max ToA
time, _ = toa.ComputeFSK(
uint(len(downlink.Payload)),
int(lorawan.BitRate),
)
}
item.length = uint32(time / 1000)
}
if time.Now().Before(item.deadlineAt) {
// Schedule transmission before the Deadline
go func() {
waitTime := item.deadlineAt.Sub(time.Now())
ctx.WithField("Remaining", waitTime).Debug("Scheduled downlink")
downlink.Trace = downlink.Trace.WithEvent("schedule", "duration", waitTime)
<-time.After(waitTime)
s.RLock()
defer s.RUnlock()
if s.downlink != nil {
ctx.Info("Send Downlink")
s.downlink <- item.payload
}
}()
} else {
go func() {
s.RLock()
defer s.RUnlock()
if s.downlink != nil {
overdue := time.Now().Sub(item.deadlineAt)
if overdue < Deadline {
ctx.Info("Send Downlink")
s.downlink <- item.payload
} else {
ctx.WithField("Overdue", overdue).Warn("Discard Late Downlink")
}
} else {
ctx.Warn("Unable to send Downlink")
}
}()
}
return nil
}
return errors.NewErrNotFound(id)
}
func (s *schedule) Stop(subscriptionID string) {
s.downlinkSubscriptionsLock.Lock()
defer s.downlinkSubscriptionsLock.Unlock()
if sub, ok := s.downlinkSubscriptions[subscriptionID]; ok {
close(sub)
delete(s.downlinkSubscriptions, subscriptionID)
}
if len(s.downlinkSubscriptions) == 0 {
s.Lock()
defer s.Unlock()
close(s.downlink)
s.downlink = nil
}
}
func (s *schedule) Subscribe(subscriptionID string) <-chan *router_pb.DownlinkMessage {
s.Lock()
if s.downlink == nil {
s.downlink = make(chan *router_pb.DownlinkMessage)
go func() {
for downlink := range s.downlink {
if s.gateway != nil && s.gateway.Utilization != nil {
s.gateway.Utilization.AddTx(downlink) // FIXME: Issue #420
}
s.downlinkSubscriptionsLock.RLock()
for _, ch := range s.downlinkSubscriptions {
select {
case ch <- downlink:
default:
s.ctx.WithField("SubscriptionID", subscriptionID).Warn("Could not send downlink message")
}
}
s.downlinkSubscriptionsLock.RUnlock()
}
}()
}
s.Unlock()
s.downlinkSubscriptionsLock.Lock()
if _, ok := s.downlinkSubscriptions[subscriptionID]; ok {
return nil
}
sub := make(chan *router_pb.DownlinkMessage)
s.downlinkSubscriptions[subscriptionID] = sub
s.downlinkSubscriptionsLock.Unlock()
return sub
}
func (s *schedule) IsActive() bool {
s.RLock()
defer s.RUnlock()
return s.downlink != nil
}